Class: Mast::Manifest

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

Overview

Manifest stores a list of package files, and optionally checksums.

The class can be used to create and compare package manifests and digests.

Note that the #diff method currently shells out. Eventually this will be internalized.

Constant Summary collapse

OverwriteError =

Manifest file overwrite error.

Class.new(Exception)
NoManifestError =

No Manifest File Error.

Class.new(LoadError) do
  def message; "ERROR: no manifest file"; end
end
DEFAULT_EXCLUDE =

By default mast will exclude any pathname matching ‘CVS’, ‘_darcs’, ‘.git*’ or ‘.config’.

%w{CVS _darcs .git* .config}
DEFAULT_IGNORE =

By default, mast will ignore any file with a name matching ‘.svn’ or ‘*~’, ie. ending with a tilde.

%w{*~ .svn}
DEFAULT_FILE =
'{manifest,digest}{,.txt,.list}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manifest

New Manifest object.



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

def initialize(options={})
  @include   = ['*']
  @exclude   = []
  @ignore    = []
  @format    = 'csf'
  @all       = false
  @dir       = false
  @bang      = false
  @digest    = nil
  @directory = Dir.pwd
  @io        = $stdout

  change_options(options)

  #if @file
  #  read(@file)
  #else
    #if file = Dir.glob(self.class.filename)[0]
    #  @file = file
    #else
  #    @file = DEFAULT_FILE
    #end
  #end
end

Instance Attribute Details

#allObject Also known as: all?

Do not exclude standard exclusions.



74
75
76
# File 'lib/mast/manifest.rb', line 74

def all
  @all
end

#bangObject Also known as: bang?

Show as if another manifest (i.e. use file’s bang options).



80
81
82
# File 'lib/mast/manifest.rb', line 80

def bang
  @bang
end

#digestObject

Encryption type



71
72
73
# File 'lib/mast/manifest.rb', line 71

def digest
  @digest
end

#dirObject Also known as: dir?

Include directories.



77
78
79
# File 'lib/mast/manifest.rb', line 77

def dir
  @dir
end

#directoryObject

Directory of manifest.



65
66
67
# File 'lib/mast/manifest.rb', line 65

def directory
  @directory
end

#excludeObject

What files to exclude.



92
93
94
# File 'lib/mast/manifest.rb', line 92

def exclude
  @exclude
end

#fileObject

File used to store manifest/digest file.



68
69
70
# File 'lib/mast/manifest.rb', line 68

def file
  @file
end

#formatObject

Layout of digest – ‘csf’ or ‘sfv’. Default is ‘csf’.



98
99
100
# File 'lib/mast/manifest.rb', line 98

def format
  @format
end

#headlessObject Also known as: headless?

Omit mast header from manifest output.



83
84
85
# File 'lib/mast/manifest.rb', line 83

def headless
  @headless
end

#ignoreObject

Special files to ignore.



95
96
97
# File 'lib/mast/manifest.rb', line 95

def ignore
  @ignore
end

#includeObject

What files to include. Defaults to [‘*’]. Note that Mast automatically recurses through directory entries, so using ‘*/’ would simply be a waste of of processing cycles.



89
90
91
# File 'lib/mast/manifest.rb', line 89

def include
  @include
end

#ioObject

An IO object to output manifest. Default is ‘$stdout`.



104
105
106
# File 'lib/mast/manifest.rb', line 104

def io
  @io
end

Class Method Details

.open(file = nil, options = {}) ⇒ Object

Possible file name (was for Fileable?). def self.filename

DEFAULT_FILE

end



55
56
57
58
59
60
61
62
# File 'lib/mast/manifest.rb', line 55

def self.open(file=nil, options={})
  unless file
    file = Dir.glob(filename, File::FNM_CASEFOLD).first
    raise NoManifestError, "Manifest file is required." unless file
  end
  options[:file] = file
  new(options)
end

Instance Method Details

#change_options(opts) ⇒ Object

Set options.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mast/manifest.rb', line 147

def change_options(opts)
  opts.each do |k,v|
    k = k.to_s.downcase
    send("#{k}=",v||send(k))
  end
  #@file       = options[:file]    || @file
  #@digest     = options[:digest]  || @digest
  #@all        = options[:all]     || @all
  #@exclude    = options[:exclude] || options[:ignore] || @exclude
  #@exclude    = [@exclude].flatten.compact
end

#changed?Boolean

Is the current mainfest in need of updating?

Returns:

  • (Boolean)

Raises:



174
175
176
177
178
179
180
# File 'lib/mast/manifest.rb', line 174

def changed?
  raise NoManifestError unless file and FileTest.file?(file)
  txt = File.read(file)
  out = StringIO.new #('', 'w')
  generate(out)
  out.string != txt
end

#chartObject

Chart of current files (name => checksum).



268
269
270
# File 'lib/mast/manifest.rb', line 268

def chart
  @chart ||= parse_directory
end

#cleanObject

Clean non-manifest files.



252
253
254
255
256
257
258
259
260
# File 'lib/mast/manifest.rb', line 252

def clean
  cfiles, cdirs = cleanlist.partition{ |f| !File.directory?(f) }
  if cfiles.empty? && cdirs.empty?
    $stderr < "No difference between list and actual.\n"
  else
    FileUtils.rm(cfiles)
    FileUtils.rmdir(cdirs)
  end
end

#cleanlistObject



283
284
285
# File 'lib/mast/manifest.rb', line 283

def cleanlist
  showlist - filelist
end

#diffObject

Diff file against actual files.

TODO: Do not shell out for diff.

Raises:



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/mast/manifest.rb', line 219

def diff
  raise NoManifestError unless file and FileTest.file?(file)
  parse_topline # parse_file unless read?
  manifest = create_temporary_manifest
  begin
    result = `diff -du #{file} #{manifest.file}`
  ensure
    FileUtils.rm(manifest.file)
  end
  # pass = result.empty?
  return result
end

#filechartObject

Chart of files as given in MANIFEST file (name => checksum).



278
279
280
# File 'lib/mast/manifest.rb', line 278

def filechart
  @filechart ||= parse_file
end

#filelistObject

List of files as given in MANIFEST file.



273
274
275
# File 'lib/mast/manifest.rb', line 273

def filelist
  @filelist ||= filechart.keys.sort
end

#filenameObject

File’s basename.



317
318
319
# File 'lib/mast/manifest.rb', line 317

def filename
  File.basename(file)
end

#generate(out = nil) ⇒ Object

Generate manifest.



192
193
194
195
196
197
# File 'lib/mast/manifest.rb', line 192

def generate(out=nil)
  out ||= self.io
  parse_topline unless read? if bang?
  out << topline_string unless headless?
  output(out)
end

#listObject

List of current files.



263
264
265
# File 'lib/mast/manifest.rb', line 263

def list
  @list ||= chart.keys.sort
end

#list_without_foldersObject

List of files in file system, but omit folders.



420
421
422
# File 'lib/mast/manifest.rb', line 420

def list_without_folders
  list.select{ |f| !File.directory?(f) }
end

#listingObject

Produce textual listing less the manifest file.



426
427
428
429
430
# File 'lib/mast/manifest.rb', line 426

def listing
  str = ''
  output(str)
  str
end

#read?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/mast/manifest.rb', line 169

def read?
  @read
end

#saveObject

Save as file.



207
208
209
210
211
212
213
# File 'lib/mast/manifest.rb', line 207

def save
  File.open(file, 'w') do |file|
    file << topline_string
    output(file)
  end
  return file
end

#showlistObject



297
298
299
300
# File 'lib/mast/manifest.rb', line 297

def showlist
  parse_topline unless read?
  list
end

#to_sObject



433
434
435
# File 'lib/mast/manifest.rb', line 433

def to_s
  topline_string + listing
end

#unlistedObject

Files not listed in manifest.



288
289
290
291
292
293
294
# File 'lib/mast/manifest.rb', line 288

def unlisted
  list = []
  Dir.chdir(directory) do
    list = Dir.glob('**/*')
  end
  list - filelist
end

#updateObject

Update file.

Raises:



200
201
202
203
204
# File 'lib/mast/manifest.rb', line 200

def update
  raise NoManifestError unless file and FileTest.file?(file)
  parse_topline
  save
end

#verifyObject



246
247
248
249
# File 'lib/mast/manifest.rb', line 246

def verify
  parse_file unless read?
  chart == filechart
end

#whatsnewObject

Files found in file system, but not listed in the manifest file.



240
241
242
243
# File 'lib/mast/manifest.rb', line 240

def whatsnew
  parse_file unless read?
  list - (filelist + [filename])
end

#whatsoldObject

Files listed in the manifest file, but not found in file system.



234
235
236
237
# File 'lib/mast/manifest.rb', line 234

def whatsold
  parse_file unless read?
  filelist - list
end