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.



113
114
115
116
# File 'lib/synqa.rb', line 113

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



108
109
110
# File 'lib/synqa.rb', line 108

def hashCommand
  @hashCommand
end

#pathPrefixObject (readonly)

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



111
112
113
# File 'lib/synqa.rb', line 111

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



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

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



146
147
148
# File 'lib/synqa.rb', line 146

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

#getCommandOutput(command) ⇒ Object

Return the enumerated lines of the command’s output



165
166
167
168
# File 'lib/synqa.rb', line 165

def getCommandOutput(command)
  puts "#{command.inspect} ..."
  return IO.popen(command)
end

#getContentTree(baseDir) ⇒ Object

Construct the ContentTree for the given base directory



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

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/synqa.rb', line 124

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.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/synqa.rb', line 152

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