Class: Synqa::DirContentHost

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

Overview

Base class for an object representing a remote system where the contents of a directory on the system are enumerated by one command to list all sub-directories and another command to list all files in the directory and their hash values.

Direct Known Subclasses

SshContentHost

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hashCommand, pathPrefix = "") ⇒ DirContentHost

Returns a new instance of DirContentHost.



121
122
123
124
# File 'lib/synqa.rb', line 121

def initialize(hashCommand, pathPrefix = "")
  @hashCommand = hashCommand
  @pathPrefix = pathPrefix
end

Instance Attribute Details

#hashCommandObject (readonly)

The HashCommand object used to calculate and parse hash values of files



116
117
118
# File 'lib/synqa.rb', line 116

def hashCommand
  @hashCommand
end

#pathPrefixObject (readonly)

Prefix required for find command (usually nothing, since it should be on the system path)



119
120
121
# File 'lib/synqa.rb', line 119

def pathPrefix
  @pathPrefix
end

Instance Method Details

#findDirectoriesCommand(baseDir) ⇒ Object

Generate the find command which will list all the sub-directories of the base directory



127
128
129
# File 'lib/synqa.rb', line 127

def findDirectoriesCommand(baseDir)
  return ["#{@pathPrefix}find", baseDir, "-type", "d", "-print"]
end

#findFilesCommand(baseDir) ⇒ Object

Generate the find command which will list all the files within the base directory



154
155
156
# File 'lib/synqa.rb', line 154

def findFilesCommand(baseDir)
  return ["#{@pathPrefix}find", baseDir, "-type", "f", "-print"]
end

#getContentTree(baseDir) ⇒ Object

Construct the ContentTree for the given base directory



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/synqa.rb', line 173

def getContentTree(baseDir)
  contentTree = ContentTree.new()
  contentTree.time = Time.now.utc
  for dir in listDirectories(baseDir)
    contentTree.addDir(dir)
  end
  for fileHash in listFileHashes(baseDir)
    contentTree.addFile(fileHash.relativePath, fileHash.hash)
  end
  return contentTree
end

#listDirectories(baseDir) ⇒ Object

Return the list of sub-directories relative to the base directory



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/synqa.rb', line 132

def listDirectories(baseDir)
  baseDir = normalisedDir(baseDir)
  command = findDirectoriesCommand(baseDir)
  output = getCommandOutput(command)
  directories = []
  baseDirLen = baseDir.length
  puts "Listing directories ..."
  while (line = output.gets)
    line = line.chomp
    puts " #{line}"
    if line.start_with?(baseDir)
      directories << line[baseDirLen..-1]
    else
      raise "Directory #{line} is not a sub-directory of base directory #{baseDir}"
    end
  end
  output.close()
  checkProcessStatus(command)
  return directories
end

#listFileHashes(baseDir) ⇒ Object

List file hashes by executing the command to hash each file on the output of the find command which lists all files, and parse the output.



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/synqa.rb', line 160

def listFileHashes(baseDir)
  baseDir = normalisedDir(baseDir)
  fileHashes = []
  listFileHashLines(baseDir) do |fileHashLine|
    fileHash = self.hashCommand.parseFileHashLine(baseDir, fileHashLine)
    if fileHash != nil
      fileHashes << fileHash
    end
  end
  return fileHashes
end