Module: Reap::Utilities

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dryrunObject

Returns the value of attribute dryrun.



9
10
11
# File 'lib/reap/utilities.rb', line 9

def dryrun
  @dryrun
end

#forceObject

Returns the value of attribute force.



11
12
13
# File 'lib/reap/utilities.rb', line 11

def force
  @force
end

#traceObject

Returns the value of attribute trace.



10
11
12
# File 'lib/reap/utilities.rb', line 10

def trace
  @trace
end

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/reap/utilities.rb', line 12

def verbose
  @verbose
end

Class Method Details

.directory!Object

Assert that a given path is a directory.



178
179
180
181
182
# File 'lib/reap/utilities.rb', line 178

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)


169
170
171
172
# File 'lib/reap/utilities.rb', line 169

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

.exist!Object

Assert that a path exists.



145
146
147
# File 'lib/reap/utilities.rb', line 145

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

.exist?Boolean

Assert that a path exists.

Returns:

  • (Boolean)


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

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

.path!Object

Assert that a path exists.



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

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

.path?Boolean

Assert that a path exists.

Returns:

  • (Boolean)


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

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.



42
43
44
45
46
47
# File 'lib/reap/utilities.rb', line 42

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.

Returns:

  • (Boolean)


191
192
193
194
195
196
197
198
# File 'lib/reap/utilities.rb', line 191

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.



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

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?



183
184
185
# File 'lib/reap/utilities.rb', line 183

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

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

Assert that a given path is a directory.



173
174
175
176
177
# File 'lib/reap/utilities.rb', line 173

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)


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

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

#dryrun?Boolean

Returns:

  • (Boolean)


14
# File 'lib/reap/utilities.rb', line 14

def dryrun?  ; @dryrun  ; end

#email(message, settings) ⇒ Object

Email function to easily send out an email.

Settings:

subject      Subject of email message.
from         Message FROM address [email].
to           Email address to send announcemnt.
server       Email server to route message.
port         Email server's port.
port_secure  Email server's port.
domain       Email server's domain name.
account      Email account name if needed.
password     Password for login..
login        Login type: plain, cram_md5 or login [plain].
secure       Uses TLS security, true or false? [false]
message      Mesage to send -or-
file         File that contains message.

Raises:

  • (ArgumentError)


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/reap/utilities.rb', line 329

def email(message, settings)
  settings ||= {}
  settings.rekey

  server    = settings[:server]
     = settings[:account]  || ENV['EMAIL_ACCOUNT']
  passwd    = settings[:password] || ENV['EMAIL_PASSWORD']
       = settings[:login]
  subject   = settings[:subject]
  mail_to   = settings[:to]     || settings[:mail_to]
  mail_from = settings[:from]   || settings[:mail_from]
  secure    = settings[:secure]
  domain    = settings[:domain] || server

  port      = (settings[:port_secure] || 465) if secure
  port      = (settings[:port] || 25) unless secure

   ||= mail_from
     ||= :plain

   = .to_sym

  #mail_to = nil if mail_to.empty?

  raise ArgumentError, "missing email field -- server"  unless server
  raise ArgumentError, "missing email field -- account" unless 
  raise ArgumentError, "missing email field -- subject" unless subject
  raise ArgumentError, "missing email field -- to"      unless mail_to
  raise ArgumentError, "missing email field -- from"    unless mail_from

  passwd ||= password("#{} password:")

  mail_to = [mail_to].flatten.compact

  msg = ""
  msg << "From: #{mail_from}\n"
  msg << "To: #{mail_to.join(';')}\n"
  msg << "Subject: #{subject}\n"
  msg << ""
  msg << message

  if secure
    Net::SMTP.send(:include, Net::SMTP::TLS)
    Net::SMTP.enable_tls #if secure #if Net::SMTP.respond_to?(:enable_tls) and secure
  end

  begin
    Net::SMTP.start(server, port, domain, , passwd, ) do |smtp|
      smtp.send_message(msg, mail_from, mail_to)
    end
    puts "Email sent successfully to #{mail_to.join(';')}."
    return true
  rescue => e
    if trace?
      raise e
    else
      abort "Email delivery failed."
    end
  end
end

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

Assert that a path exists.



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

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)


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

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

#file!(*paths) ⇒ Object

Assert that a given path is a file.



158
159
160
# File 'lib/reap/utilities.rb', line 158

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)


151
152
153
154
# File 'lib/reap/utilities.rb', line 151

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

#fileutilsObject

Delegate access to FileUtils.



72
73
74
# File 'lib/reap/utilities.rb', line 72

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

#force?Boolean

Returns:

  • (Boolean)


16
# File 'lib/reap/utilities.rb', line 16

def force?   ; @force   ; end

#glob(*args, &blk) ⇒ Object

Glob files.



229
230
231
# File 'lib/reap/utilities.rb', line 229

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

#multiglob(*args, &blk) ⇒ Object



233
234
235
# File 'lib/reap/utilities.rb', line 233

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

#multiglob_r(*args, &blk) ⇒ Object



237
238
239
# File 'lib/reap/utilities.rb', line 237

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)


216
217
218
219
220
221
222
223
224
225
# File 'lib/reap/utilities.rb', line 216

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)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/reap/utilities.rb', line 51

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.



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

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

#rm_r(*a) ⇒ Object

Specific.



100
101
102
103
104
105
106
# File 'lib/reap/utilities.rb', line 100

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)


204
205
206
207
208
209
210
# File 'lib/reap/utilities.rb', line 204

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

#sh(cmd) ⇒ Object

Shell runner.



30
31
32
33
34
35
36
37
38
# File 'lib/reap/utilities.rb', line 30

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.


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/reap/utilities.rb', line 247

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.



278
279
280
281
282
283
284
# File 'lib/reap/utilities.rb', line 278

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.



22
23
24
25
26
# File 'lib/reap/utilities.rb', line 22

def status(message)
  if dryrun? or trace?
    puts message
  end
end

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

BZip and tarball folder into file.



300
301
302
# File 'lib/reap/utilities.rb', line 300

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.



306
307
308
# File 'lib/reap/utilities.rb', line 306

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

#trace?Boolean

Returns:

  • (Boolean)


15
# File 'lib/reap/utilities.rb', line 15

def trace?   ; @trace   ; end

#verbose?Boolean

Returns:

  • (Boolean)


17
# File 'lib/reap/utilities.rb', line 17

def verbose? ; @verbose ; end

#write(path, text) ⇒ Object

Write file.



123
124
125
126
127
128
129
# File 'lib/reap/utilities.rb', line 123

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

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

Zip folder into file.



294
295
296
# File 'lib/reap/utilities.rb', line 294

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

#ziputilsObject

Delegate access to ZipUtils.



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

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