Module: Reap::Utilities::FileUtils

Included in:
Manager, Subversion
Defined in:
lib/reap/utilities/fileutils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.directory!Object

Assert that a given path is a directory.



147
148
149
150
151
# File 'lib/reap/utilities/fileutils.rb', line 147

def dir!(*paths)
  paths.each do |path|
    abort "Directory not found: '#{path}'." unless  dir?(path)
  end
end

.directory?Boolean

Is a given path a directory? If path is a glob checks to see if all matches are directories.

Returns:

  • (Boolean)


138
139
140
141
# File 'lib/reap/utilities/fileutils.rb', line 138

def dir?(path)
  paths = Dir.glob(path)
  paths.not_empty? && paths.all?{ |f| FileTest.directory?(f) }
end

.exist!Object

Assert that a path exists.



114
115
116
# File 'lib/reap/utilities/fileutils.rb', line 114

def exists!(*paths)
  abort "path not found #{path}" unless paths.any?{|path| exists?(path)}
end

.exist?Boolean

Assert that a path exists.

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/reap/utilities/fileutils.rb', line 106

def exists?(path)
  paths = Dir.glob(path)
  paths.not_empty?
end

.path!Object

Assert that a path exists.



115
116
117
# File 'lib/reap/utilities/fileutils.rb', line 115

def exists!(*paths)
  abort "path not found #{path}" unless paths.any?{|path| exists?(path)}
end

.path?Boolean

Assert that a path exists.

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/reap/utilities/fileutils.rb', line 107

def exists?(path)
  paths = Dir.glob(path)
  paths.not_empty?
end

Instance Method Details

#bin?(fname) ⇒ Boolean

Is a file a command executable?

TODO: Make more robust. Probably needs to be fixed for Windows.

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
# File 'lib/reap/utilities/fileutils.rb', line 174

def bin?(fname)
  #@command_paths ||= ENV['PATH'].split(/[:;]/)
  is_bin = command_paths.any? do |f|
    FileTest.exist?(File.join(f, fname))
  end
  #is_bin ? File.basename(fname) : false
  is_bin ? fname : false
end

#cd(*a, &b) ⇒ Object

Bonus FileUtils features.



79
80
81
82
# File 'lib/reap/utilities/fileutils.rb', line 79

def cd(*a,&b)
  puts "cd #{a}" if dryrun? or trace?
  fileutils.chdir(*a,&b)
end

#command_pathsObject

Return a cached list of the PATH environment variable. This is a support method used by #bin?



166
167
168
# File 'lib/reap/utilities/fileutils.rb', line 166

def command_paths
  @command_paths ||= ENV['PATH'].split(/[:;]/)
end

#dir!(*paths) ⇒ Object Also known as: directory!

Assert that a given path is a directory.



142
143
144
145
146
# File 'lib/reap/utilities/fileutils.rb', line 142

def dir!(*paths)
  paths.each do |path|
    abort "Directory not found: '#{path}'." unless  dir?(path)
  end
end

#dir?(path) ⇒ Boolean Also known as: directory?

Is a given path a directory? If path is a glob checks to see if all matches are directories.

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/reap/utilities/fileutils.rb', line 134

def dir?(path)
  paths = Dir.glob(path)
  paths.not_empty? && paths.all?{ |f| FileTest.directory?(f) }
end

#exists!(*paths) ⇒ Object Also known as: exist!, path!

Assert that a path exists.



111
112
113
# File 'lib/reap/utilities/fileutils.rb', line 111

def exists!(*paths)
  abort "path not found #{path}" unless paths.any?{|path| exists?(path)}
end

#exists?(path) ⇒ Boolean Also known as: exist?, path?

Assert that a path exists.

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/reap/utilities/fileutils.rb', line 102

def exists?(path)
  paths = Dir.glob(path)
  paths.not_empty?
end

#file!(*paths) ⇒ Object

Assert that a given path is a file.



127
128
129
# File 'lib/reap/utilities/fileutils.rb', line 127

def file!(*paths)
  abort "file not found #{path}" unless paths.any?{|path| file?(path)}
end

#file?(path) ⇒ Boolean

Is a given path a regular file? If path is a glob then checks to see if all matches are refular files.

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/reap/utilities/fileutils.rb', line 120

def file?(path)
  paths = Dir.glob(path)
  paths.not_empty? && paths.all?{ |f| FileTest.file?(f) }
end

#file_read(path) ⇒ Object

Read file.



86
87
88
# File 'lib/reap/utilities/fileutils.rb', line 86

def file_read(path)
  File.read(path)
end

#file_write(path, text) ⇒ Object

Write file.



92
93
94
95
96
97
98
# File 'lib/reap/utilities/fileutils.rb', line 92

def file_write(path, text)
  if dryrun?
    puts "write #{path}"
  else
    File.open(path, 'w'){ |f| f << text }
  end
end

#fileutilsObject

Delegate access to FileUtils.



41
42
43
# File 'lib/reap/utilities/fileutils.rb', line 41

def fileutils
  dryrun? ? ::FileUtils::DryRun : ::FileUtils
end

#glob(*args, &blk) ⇒ Object

Glob files.



212
213
214
# File 'lib/reap/utilities/fileutils.rb', line 212

def glob(*args, &blk)
  Dir.glob(*args, &blk)
end

#multiglob(*args, &blk) ⇒ Object



216
217
218
# File 'lib/reap/utilities/fileutils.rb', line 216

def multiglob(*args, &blk)
  Dir.multiglob(*args, &blk)
end

#multiglob_r(*args, &blk) ⇒ Object



220
221
222
# File 'lib/reap/utilities/fileutils.rb', line 220

def multiglob_r(*args, &blk)
  Dir.multiglob_r(*args, &blk)
end

#out_of_date?(path, *sources) ⇒ Boolean

Does a path need updating, based on given sources? This compares mtimes of give paths. Returns false if the path needs to be updated.

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
207
208
# File 'lib/reap/utilities/fileutils.rb', line 199

def out_of_date?(path, *sources)
  return true unless File.exist?(path)

  sources = sources.collect{ |source| Dir.glob(source) }.flatten
  mtimes  = sources.collect{ |file| File.mtime(file) }

  return true if mtimes.empty?  # TODO: This the way to go here?

  File.mtime(path) < mtimes.max
end

#rm_r(*a) ⇒ Object

Specific.



69
70
71
72
73
74
75
# File 'lib/reap/utilities/fileutils.rb', line 69

def rm_r(*a)
  if dryrun?
    puts "rm_r #{a.join(' ')}"
  else
    fileutils.rm_r(*a)
  end
end

#safe?(path) ⇒ Boolean

Is a path considered reasonably “safe”?

TODO: Make more robust.

Returns:

  • (Boolean)


187
188
189
190
191
192
193
# File 'lib/reap/utilities/fileutils.rb', line 187

def safe?(path)
  case path
  when *[ '/', '/*', '/**/*' ]
    return false
  end
  true
end

#stage(stage_directory, files) ⇒ Object

Stage package by hard linking included files to a stage directory. Stage files in a directory.

stage_directory       Stage directory.
files                 Files to link to stage.


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/reap/utilities/fileutils.rb', line 230

def stage(stage_directory, files)
  return stage_directory if dryrun?           # Don't link to stage if dryrun.

  if File.directory?(stage_directory)         # Ensure existance of staging area.
    #raise(???Error, stage_directory) unless force?
    rm_r(stage_directory)
  end

  mkdir_p(stage_directory)                    #dir = File.expand_path(stage)

  #files = package.filelist  #+ [package.manifest_file]

  # TODO Dryrun test here or before folder creation?
  files.each do |f|                 # Link files into staging area.
    file = File.join(stage_directory, f)
    if File.directory?(f)
      mkdir_p(file)
    else
      unless File.exist?(file) and File.mtime(file) >= File.mtime(f)
        ln(f, file) #safe_ln ?
      end
    end
  end

  # stage meanifest ?

  return stage_directory
end

#stage_manifest(directory) ⇒ Object

Create manifest for a directory.

TODO: Do this programatically rather then via shell.



263
264
265
266
267
# File 'lib/reap/utilities/fileutils.rb', line 263

def stage_manifest(directory)
  cd(directory) do
    sh 'manifest up'
  end
end

#tar_bzip(folder, file = nil, options = {}) ⇒ Object

BZip and tarball folder into file.



283
284
285
# File 'lib/reap/utilities/fileutils.rb', line 283

def tar_bzip(folder, file=nil, options={})
  ziputils.tar_bzip(folder, file, options)
end

#tgz(folder, file = nil, options = {}) ⇒ Object

GZip and tarball folder into file. Shortcut for ziputils.tgz.



289
290
291
# File 'lib/reap/utilities/fileutils.rb', line 289

def tgz(folder, file=nil, options={})
  ziputils.tgz(folder, file, options)
end

#zip(folder, file = nil, options = {}) ⇒ Object

Zip folder into file.



277
278
279
# File 'lib/reap/utilities/fileutils.rb', line 277

def zip(folder, file=nil, options={})
  ziputils.zip(folder, file, options)
end

#ziputilsObject

Delegate access to ZipUtils.



271
272
273
# File 'lib/reap/utilities/fileutils.rb', line 271

def ziputils
  dryrun? ? ::ZipUtils::DryRun : ::ZipUtils
end