Class: Synqa::LocalContentLocation

Inherits:
ContentLocation show all
Defined in:
lib/synqa.rb

Overview

A directory of files on a local system. The corresponding content tree can be calculated directly using Ruby library functions.

Instance Attribute Summary collapse

Attributes inherited from ContentLocation

#cachedContentFile

Instance Method Summary collapse

Methods inherited from ContentLocation

#clearCachedContentFile, #getCachedContentTree, #getCachedContentTreeMapOfHashes, #getExistingCachedContentTreeFile

Constructor Details

#initialize(baseDirectory, hashClass, cachedContentFile = nil) ⇒ LocalContentLocation

Returns a new instance of LocalContentLocation.



674
675
676
677
678
# File 'lib/synqa.rb', line 674

def initialize(baseDirectory, hashClass, cachedContentFile = nil)
  super(cachedContentFile)
  @baseDirectory = baseDirectory
  @hashClass = hashClass
end

Instance Attribute Details

#baseDirectoryObject (readonly)

the base directory, for example of type Based::BaseDirectory. Methods invoked are: allFiles, subDirs and fullPath. For file and dir objects returned by allFiles & subDirs, methods invoked are: relativePath and fullPath



670
671
672
# File 'lib/synqa.rb', line 670

def baseDirectory
  @baseDirectory
end

#hashClassObject (readonly)

the ruby class that generates the hash, e.g. Digest::SHA256



672
673
674
# File 'lib/synqa.rb', line 672

def hashClass
  @hashClass
end

Instance Method Details

#getContentTreeObject

get the content tree for this base directory by iterating over all sub-directories and files within the base directory (and excluding the excluded files) and calculating file hashes using the specified Ruby hash class If there is an existing cached content file, use that to get the hash values of files whose modification time is earlier than the time value for the cached content tree. Also, if a cached content file is specified, write the final content tree back out to the cached content file.



696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/synqa.rb', line 696

def getContentTree
  cachedTimeAndMapOfHashes = getCachedContentTreeMapOfHashes
  cachedTime = cachedTimeAndMapOfHashes[0]
  cachedMapOfHashes = cachedTimeAndMapOfHashes[1]
  contentTree = ContentTree.new()
  contentTree.time = Time.now.utc
  for subDir in @baseDirectory.subDirs
    contentTree.addDir(subDir.relativePath)
  end
  for file in @baseDirectory.allFiles
    cachedDigest = cachedMapOfHashes[file.relativePath]
    if cachedTime and cachedDigest and File.stat(file.fullPath).mtime < cachedTime
      digest = cachedDigest
    else
      digest = hashClass.file(file.fullPath).hexdigest
    end
    contentTree.addFile(file.relativePath, digest)
  end
  contentTree.sort!
  if cachedContentFile != nil
    contentTree.writeToFile(cachedContentFile)
  end
  return contentTree
end

#getFullPath(relativePath) ⇒ Object

get the full path of a relative path (i.e. of a file/directory within the base directory)



686
687
688
# File 'lib/synqa.rb', line 686

def getFullPath(relativePath)
  return @baseDirectory.fullPath + relativePath
end

#getScpPath(relativePath) ⇒ Object

get the path as required for an SCP command



681
682
683
# File 'lib/synqa.rb', line 681

def getScpPath(relativePath)
  return getFullPath(relativePath)
end