Class: Synqa::SyncOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/synqa.rb

Overview

The operation of synchronising files on the remote directory with files on the local directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sourceLocation, destinationLocation) ⇒ SyncOperation

Returns a new instance of SyncOperation.



935
936
937
938
# File 'lib/synqa.rb', line 935

def initialize(sourceLocation, destinationLocation)
  @sourceLocation = sourceLocation
  @destinationLocation = destinationLocation
end

Instance Attribute Details

#destinationLocationObject (readonly)

The destination location (presumed to be remote)



933
934
935
# File 'lib/synqa.rb', line 933

def destinationLocation
  @destinationLocation
end

#sourceLocationObject (readonly)

The source location (presumed to be local)



930
931
932
# File 'lib/synqa.rb', line 930

def sourceLocation
  @sourceLocation
end

Instance Method Details

#clearCachedContentFilesObject

Delete the local and remote cached content files (which will force a full recalculation of both content trees next time)



962
963
964
965
# File 'lib/synqa.rb', line 962

def clearCachedContentFiles
  @sourceLocation.clearCachedContentFile()
  @destinationLocation.clearCachedContentFile()
end

#closeConnectionsObject



1048
1049
1050
# File 'lib/synqa.rb', line 1048

def closeConnections
  destinationLocation.closeConnections()
end

#doAllCopyOperations(dryRun) ⇒ Object

Do all the copy operations, copying local directories or files which are missing from the remote location



989
990
991
# File 'lib/synqa.rb', line 989

def doAllCopyOperations(dryRun)
  doCopyOperations(@sourceContent, @destinationContent, dryRun)
end

#doAllDeleteOperations(dryRun) ⇒ Object

Do all delete operations, deleting remote directories or files which do not exist at the local location



994
995
996
# File 'lib/synqa.rb', line 994

def doAllDeleteOperations(dryRun)
  doDeleteOperations(@destinationContent, dryRun)
end

#doCopyOperations(sourceContent, destinationContent, dryRun) ⇒ Object

Recursively perform all marked copy operations from the source content tree to the destination content tree, or if dryRun, just pretend to perform them.



1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
# File 'lib/synqa.rb', line 1010

def doCopyOperations(sourceContent, destinationContent, dryRun)
  for dir in sourceContent.dirs
    if dir.copyDestination != nil
      sourcePath = sourceLocation.getFullPath(dir.relativePath)
      destinationPath = destinationLocation.getFullPath(dir.copyDestination.relativePath)
      destinationLocation.contentHost.copyLocalToRemoteDirectory(sourcePath, destinationPath, dryRun)
    else
      doCopyOperations(dir, destinationContent.getDir(dir.name), dryRun)
    end
  end
  for file in sourceContent.files
    if file.copyDestination != nil
      sourcePath = sourceLocation.getFullPath(file.relativePath)
      destinationPath = destinationLocation.getFullPath(file.copyDestination.relativePath)
      destinationLocation.contentHost.copyLocalFileToRemoteDirectory(sourcePath, destinationPath, dryRun)
    end
  end
end

#doDeleteOperations(destinationContent, dryRun) ⇒ Object

Recursively perform all marked delete operations on the destination content tree, or if dryRun, just pretend to perform them.



1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/synqa.rb', line 1031

def doDeleteOperations(destinationContent, dryRun)
  for dir in destinationContent.dirs
    if dir.toBeDeleted
      dirPath = destinationLocation.getFullPath(dir.relativePath)
      destinationLocation.contentHost.deleteDirectory(dirPath, dryRun)
    else
      doDeleteOperations(dir, dryRun)
    end
  end
  for file in destinationContent.files
    if file.toBeDeleted
      filePath = destinationLocation.getFullPath(file.relativePath)
      destinationLocation.contentHost.deleteFile(filePath, dryRun)
    end
  end
end

#doSync(options = {}) ⇒ Object

Do the sync. Options: :full = true means clear the cached content files first, :dryRun means don’t do the actual copies and deletes, but just show what they would be.



969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
# File 'lib/synqa.rb', line 969

def doSync(options = {})
  if options[:full]
    clearCachedContentFiles()
  end
  getContentTrees()
  markSyncOperations()
  dryRun = options[:dryRun]
  if not dryRun
    @destinationLocation.clearCachedContentFile()
  end
  doAllCopyOperations(dryRun)
  doAllDeleteOperations(dryRun)
  if (not dryRun and @destinationLocation.cachedContentFile and @sourceLocation.cachedContentFile and
      File.exists?(@sourceLocation.cachedContentFile))
    FileUtils::Verbose.cp(@sourceLocation.cachedContentFile, @destinationLocation.cachedContentFile)
  end
  closeConnections()
end

#executeCommand(command, dryRun) ⇒ Object

Execute a (local) command, or, if dryRun, just pretend to execute it. Raise an exception if the process exit status is not 0.



1000
1001
1002
1003
1004
1005
1006
# File 'lib/synqa.rb', line 1000

def executeCommand(command, dryRun)
  puts "EXECUTE: #{command}"
  if not dryRun
    system(command)
    checkProcessStatus(command)
  end
end

#getContentTreesObject

Get the local and remote content trees



941
942
943
944
# File 'lib/synqa.rb', line 941

def getContentTrees
  @sourceContent = @sourceLocation.getContentTree()
  @destinationContent = @destinationLocation.getContentTree()
end

#markSyncOperationsObject

On the local and remote content trees, mark the copy and delete operations required to sync the remote location to the local location.



948
949
950
951
952
953
954
955
956
957
958
# File 'lib/synqa.rb', line 948

def markSyncOperations
  @sourceContent.markSyncOperationsForDestination(@destinationContent)
  puts " ================================================ "
  puts "After marking for sync --"
  puts ""
  puts "Local:"
  @sourceContent.showIndented()
  puts ""
  puts "Remote:"
  @destinationContent.showIndented()
end