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.



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

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



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

def hashCommand
  @hashCommand
end

#pathPrefixObject (readonly)

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



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

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



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

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



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

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

#getContentTree(baseDir) ⇒ Object

Construct the ContentTree for the given base directory



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

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



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

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.



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

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