Module: Reap::Utilities
- Included in:
- Project, Systems::System, Tool
- Defined in:
- lib/reap/utilities.rb
Overview
Utilities
Gerenal methods useful to project and tools.
Class Method Summary collapse
-
.directory! ⇒ Object
Assert that a given path is a directory.
-
.directory? ⇒ Boolean
Is a given path a directory? If
path
is a glob checks to see if all matches are directories. -
.exist! ⇒ Object
Assert that a path exists.
-
.exist? ⇒ Boolean
Assert that a path exists.
-
.path! ⇒ Object
Assert that a path exists.
-
.path? ⇒ Boolean
Assert that a path exists.
Instance Method Summary collapse
-
#ask(question, answers = nil) ⇒ Object
Convenient method to get simple console reply.
-
#bin?(fname) ⇒ Boolean
Is a file a command executable?.
-
#cd(*a, &b) ⇒ Object
Bonus FileUtils features.
-
#command_paths ⇒ Object
Return a cached list of the PATH environment variable.
- #compress(format, folder, file = nil, options = {}) ⇒ Object
-
#dir!(*paths) ⇒ Object
(also: #directory!)
Assert that a given path is a directory.
-
#dir?(path) ⇒ Boolean
(also: #directory?)
Is a given path a directory? If
path
is a glob checks to see if all matches are directories. -
#email(message, options) ⇒ Object
# Zip folder into file.
-
#exists!(*paths) ⇒ Object
(also: #exist!, #path!)
Assert that a path exists.
-
#exists?(path) ⇒ Boolean
(also: #exist?, #path?)
Assert that a path exists.
-
#file!(*paths) ⇒ Object
Assert that a given path is a file.
-
#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. -
#fileutils ⇒ Object
Delegate access to FileUtils.
-
#glob(*args, &blk) ⇒ Object
Glob files.
-
#list_option(option) ⇒ Object
Helper method for cleaning list options.
- #multiglob(*args, &blk) ⇒ Object
- #multiglob_r(*args, &blk) ⇒ Object
-
#out_of_date?(path, *sources) ⇒ Boolean
Does a path need updating, based on given
sources
? This compares mtimes of give paths. -
#password(prompt = nil) ⇒ Object
Ask for a password.
-
#read(path) ⇒ Object
Read file.
-
#rm_r(*a) ⇒ Object
Specific.
-
#safe?(path) ⇒ Boolean
Is a path considered reasonably “safe”?.
-
#sh(cmd) ⇒ Object
Shell runner.
-
#stage(stage_directory, files) ⇒ Object
Stage package by hard linking included files to a stage directory.
-
#stage_manifest(directory) ⇒ Object
Create manifest for a directory.
-
#status(message) ⇒ Object
Internal status report.
-
#write(path, text) ⇒ Object
Write file.
-
#ziputils ⇒ Object
Delegate access to ZipUtils.
Class Method Details
.directory! ⇒ Object
Assert that a given path is a directory.
193 194 195 196 197 |
# File 'lib/reap/utilities.rb', line 193 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.
184 185 186 187 |
# File 'lib/reap/utilities.rb', line 184 def dir?(path) paths = Dir.glob(path) paths.not_empty? && paths.all?{ |f| FileTest.directory?(f) } end |
.exist! ⇒ Object
Assert that a path exists.
160 161 162 |
# File 'lib/reap/utilities.rb', line 160 def exists!(*paths) abort "path not found #{path}" unless paths.any?{|path| exists?(path)} end |
.exist? ⇒ Boolean
Assert that a path exists.
152 153 154 155 |
# File 'lib/reap/utilities.rb', line 152 def exists?(path) paths = Dir.glob(path) paths.not_empty? end |
.path! ⇒ Object
Assert that a path exists.
161 162 163 |
# File 'lib/reap/utilities.rb', line 161 def exists!(*paths) abort "path not found #{path}" unless paths.any?{|path| exists?(path)} end |
.path? ⇒ Boolean
Assert that a path exists.
153 154 155 156 |
# File 'lib/reap/utilities.rb', line 153 def exists?(path) paths = Dir.glob(path) paths.not_empty? end |
Instance Method Details
#ask(question, answers = nil) ⇒ Object
Convenient method to get simple console reply.
57 58 59 60 61 62 |
# File 'lib/reap/utilities.rb', line 57 def ask(question, answers=nil) print "#{question}" print " [#{answers}] " if answers until inp = $stdin.gets ; sleep 1 ; end inp.strip end |
#bin?(fname) ⇒ Boolean
Is a file a command executable?
TODO: Make more robust. Probably needs to be fixed for Windows.
206 207 208 209 210 211 212 213 |
# File 'lib/reap/utilities.rb', line 206 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.
125 126 127 128 |
# File 'lib/reap/utilities.rb', line 125 def cd(*a,&b) puts "cd #{a}" if dryrun? or trace? fileutils.chdir(*a,&b) end |
#command_paths ⇒ Object
Return a cached list of the PATH environment variable. This is a support method used by #bin?
198 199 200 |
# File 'lib/reap/utilities.rb', line 198 def command_paths @command_paths ||= ENV['PATH'].split(/[:;]/) end |
#compress(format, folder, file = nil, options = {}) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/reap/utilities.rb', line 309 def compress(format, folder, file=nil, ={}) case format.to_s.downcase when 'zip' ziputils.zip(folder, file, ) when 'tgz' ziputils.tgz(folder, file, ) when 'tbz', 'bzip' ziputils.tar_bzip(folder, file, ) else raise ArguementError, "unsupported compression format -- #{format}" end end |
#dir!(*paths) ⇒ Object Also known as: directory!
Assert that a given path is a directory.
188 189 190 191 192 |
# File 'lib/reap/utilities.rb', line 188 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.
180 181 182 183 |
# File 'lib/reap/utilities.rb', line 180 def dir?(path) paths = Dir.glob(path) paths.not_empty? && paths.all?{ |f| FileTest.directory?(f) } end |
#email(message, options) ⇒ Object
# Zip folder into file.
def zip(folder, file=nil, ={})
ziputils.zip(folder, file, )
end
# BZip and tarball folder into file.
def tar_bzip(folder, file=nil, ={})
ziputils.tar_bzip(folder, file, )
end
# GZip and tarball folder into file. Shortcut for ziputils.tgz.
def tgz(folder, file=nil, ={})
ziputils.tgz(folder, file, )
end
342 343 344 |
# File 'lib/reap/utilities.rb', line 342 def email(, ) Emailer.new().email() end |
#exists!(*paths) ⇒ Object Also known as: exist!, path!
Assert that a path exists.
157 158 159 |
# File 'lib/reap/utilities.rb', line 157 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.
148 149 150 151 |
# File 'lib/reap/utilities.rb', line 148 def exists?(path) paths = Dir.glob(path) paths.not_empty? end |
#file!(*paths) ⇒ Object
Assert that a given path is a file.
173 174 175 |
# File 'lib/reap/utilities.rb', line 173 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.
166 167 168 169 |
# File 'lib/reap/utilities.rb', line 166 def file?(path) paths = Dir.glob(path) paths.not_empty? && paths.all?{ |f| FileTest.file?(f) } end |
#fileutils ⇒ Object
Delegate access to FileUtils.
87 88 89 |
# File 'lib/reap/utilities.rb', line 87 def fileutils dryrun? ? ::FileUtils::DryRun : ::FileUtils end |
#glob(*args, &blk) ⇒ Object
Glob files.
244 245 246 |
# File 'lib/reap/utilities.rb', line 244 def glob(*args, &blk) Dir.glob(*args, &blk) end |
#list_option(option) ⇒ Object
Helper method for cleaning list options. This will split the option on ‘:’ or ‘;’ if it is a string, rather than an array. And it will make sure there are no nil elements.
29 30 31 32 |
# File 'lib/reap/utilities.rb', line 29 def list_option(option) option = option.to_s.split(/[:;,]/) unless Array===option [option].compact.flatten end |
#multiglob(*args, &blk) ⇒ Object
248 249 250 |
# File 'lib/reap/utilities.rb', line 248 def multiglob(*args, &blk) Dir.multiglob(*args, &blk) end |
#multiglob_r(*args, &blk) ⇒ Object
252 253 254 |
# File 'lib/reap/utilities.rb', line 252 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.
231 232 233 234 235 236 237 238 239 240 |
# File 'lib/reap/utilities.rb', line 231 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 |
#password(prompt = nil) ⇒ Object
Ask for a password. (FIXME: only for unix so far)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/reap/utilities.rb', line 66 def password(prompt=nil) msg ||= "Enter Password: " inp = '' print "#{prompt} " begin #system "stty -echo" #inp = gets.chomp until inp = $stdin.gets sleep 1 end ensure #system "stty echo" end return inp.chomp end |
#read(path) ⇒ Object
Read file.
132 133 134 |
# File 'lib/reap/utilities.rb', line 132 def read(path) File.read(path) end |
#rm_r(*a) ⇒ Object
Specific.
115 116 117 118 119 120 121 |
# File 'lib/reap/utilities.rb', line 115 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.
219 220 221 222 223 224 225 |
# File 'lib/reap/utilities.rb', line 219 def safe?(path) case path when *[ '/', '/*', '/**/*' ] return false end true end |
#sh(cmd) ⇒ Object
Shell runner.
45 46 47 48 49 50 51 52 53 |
# File 'lib/reap/utilities.rb', line 45 def sh(cmd) if dryrun? puts cmd true else puts "--> system call: #{cmd}" if trace? system(cmd) end 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.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/reap/utilities.rb', line 262 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 manifest ? return stage_directory end |
#stage_manifest(directory) ⇒ Object
Create manifest for a directory.
293 294 295 296 297 298 299 |
# File 'lib/reap/utilities.rb', line 293 def stage_manifest(directory) cd(directory) do #sh 'manifest up' files = Dir['**/*'] File.open('MANIFEST', 'w'){|f| f << file.join("\n") } end end |
#status(message) ⇒ Object
Internal status report. Only output if dryrun or trace mode.
37 38 39 40 41 |
# File 'lib/reap/utilities.rb', line 37 def status() if dryrun? or trace? puts end end |
#write(path, text) ⇒ Object
Write file.
138 139 140 141 142 143 144 |
# File 'lib/reap/utilities.rb', line 138 def write(path, text) if dryrun? puts "write #{path}" else File.open(path, 'w'){ |f| f << text } end end |
#ziputils ⇒ Object
Delegate access to ZipUtils.
303 304 305 |
# File 'lib/reap/utilities.rb', line 303 def ziputils dryrun? ? ::ZipUtils::DryRun : ::ZipUtils end |