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(baseDir, hashClass, cachedContentFile = nil, options = {}) ⇒ LocalContentLocation

Returns a new instance of LocalContentLocation.



662
663
664
665
666
667
668
# File 'lib/synqa.rb', line 662

def initialize(baseDir, hashClass, cachedContentFile = nil, options = {})
  super(cachedContentFile)
  @baseDir = normalisedDir(baseDir)
  @baseDirLen = @baseDir.length
  @hashClass = hashClass
  @excludeGlobs = options.fetch(:excludes, [])
end

Instance Attribute Details

#baseDirObject (readonly)

the base directory



658
659
660
# File 'lib/synqa.rb', line 658

def baseDir
  @baseDir
end

#hashClassObject (readonly)

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



660
661
662
# File 'lib/synqa.rb', line 660

def hashClass
  @hashClass
end

Instance Method Details

#fileIsExcluded?(relativeFile) ⇒ Boolean

is the relative path name excluded by one of the specified exclusion globs?

Returns:

  • (Boolean)


690
691
692
693
694
695
696
697
698
# File 'lib/synqa.rb', line 690

def fileIsExcluded?(relativeFile)
  for excludeGlob in @excludeGlobs
    if File.fnmatch(excludeGlob, relativeFile)
      puts "   file #{relativeFile} excluded by glob #{excludeGlob}"
      return true
    end
  end
  return false
end

#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.



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/synqa.rb', line 706

def getContentTree
  cachedTimeAndMapOfHashes = getCachedContentTreeMapOfHashes
  cachedTime = cachedTimeAndMapOfHashes[0]
  cachedMapOfHashes = cachedTimeAndMapOfHashes[1]
  contentTree = ContentTree.new()
  contentTree.time = Time.now.utc
  #puts "LocalContentLocation.getContentTree for baseDir #{baseDir} ..."

  for fileOrDir in Dir.glob(baseDir + "**/*", File::FNM_DOTMATCH)
    if not (fileOrDir.end_with?("/.") or fileOrDir.end_with?("/.."))
      relativePath = getRelativePath(fileOrDir)
      #puts " #{relativePath}"

      if File.directory? fileOrDir
        contentTree.addDir(relativePath)
      else
        if not fileIsExcluded?(relativePath)
          cachedDigest = cachedMapOfHashes[relativePath]
          if cachedTime and cachedDigest and File.stat(fileOrDir).mtime < cachedTime
            digest = cachedDigest
          else
            digest = hashClass.file(fileOrDir).hexdigest
          end
          contentTree.addFile(relativePath, digest)
        end
      end
    end
  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)



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

def getFullPath(relativePath)
  return @baseDir + relativePath
end

#getRelativePath(fileName) ⇒ Object

get the path of a file name relative to the base directory



671
672
673
674
675
676
677
# File 'lib/synqa.rb', line 671

def getRelativePath(fileName)
  if fileName.start_with? @baseDir
    return fileName[@baseDirLen..-1]
  else
    raise "File name #{fileName} does not start with #{baseDir}"
  end
end

#getScpPath(relativePath) ⇒ Object

get the path as required for an SCP command



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

def getScpPath(relativePath)
  return getFullPath(relativePath)
end