module Util
Public Instance Methods
absolute_path_for(*args)
click to toggle source
# File lib/fbomb/util.rb, line 68 def absolute_path_for(*args) ('/' + paths_for(*args).join('/')).squeeze('/') end
absolute_prefix_for(*args)
click to toggle source
# File lib/fbomb/util.rb, line 72 def absolute_prefix_for(*args) absolute_path_for(*args) + '/' end
columnize(buf, opts = {})
click to toggle source
# File lib/fbomb/util.rb, line 130 def columnize(buf, opts = {}) width = Util.getopt 'width', opts, 80 indent = Util.getopt 'indent', opts indent = Fixnum === indent ? (' ' * indent) : "#{ indent }" column = [] words = buf.split %r/\s+/o row = "#{ indent }" while((word = words.shift)) if((row.size + word.size) < (width - 1)) row << word else column << row row = "#{ indent }" row << word end row << ' ' unless row.size == (width - 1) end column << row unless row.strip.empty? column.join "\n" end
getopt(opt, hash, default = nil)
click to toggle source
# File lib/fbomb/util.rb, line 151 def getopt(opt, hash, default = nil) keys = opt.respond_to?('each') ? opt : [opt] keys.each do |key| return hash[key] if hash.has_key? key key = "#{ key }" return hash[key] if hash.has_key? key key = key.intern return hash[key] if hash.has_key? key end return default end
hostname()
click to toggle source
# File lib/fbomb/util.rb, line 10 def hostname @hostname ||= (Socket.gethostname rescue 'localhost') end
indent(chunk, n = 2)
click to toggle source
# File lib/fbomb/util.rb, line 104 def indent(chunk, n = 2) lines = chunk.split %r/\n/ re = nil s = ' ' * n lines.map! do |line| unless re margin = line[%r/^\s*/] re = %r/^#{ margin }/ end line.gsub re, s end.join("\n") end
normalize_path(arg, *args)
click to toggle source
# File lib/fbomb/util.rb, line 84 def normalize_path(arg, *args) absolute_path_for(arg, *args) end
path_for(*args)
click to toggle source
# File lib/fbomb/util.rb, line 76 def path_for(*args) paths_for(*args).join('/').squeeze('/') end
paths_for(*args)
click to toggle source
# File lib/fbomb/util.rb, line 59 def paths_for(*args) path = args.flatten.compact.join('/') path.gsub!(%r|[.]+/|, '/') path.squeeze!('/') path.sub!(%r|^/|, '') path.sub!(%r|/$|, '') paths = path.split('/') end
pid()
click to toggle source
# File lib/fbomb/util.rb, line 14 def pid @pid ||= Process.pid end
ppid()
click to toggle source
# File lib/fbomb/util.rb, line 18 def ppid @ppid ||= Process.ppid end
prefix_for(*args)
click to toggle source
# File lib/fbomb/util.rb, line 80 def prefix_for(*args) path_for(*args) + '/' end
thread_id()
click to toggle source
# File lib/fbomb/util.rb, line 22 def thread_id Thread.current.object_id.abs end
tmpdir(*args, &block)
click to toggle source
# File lib/fbomb/util.rb, line 26 def tmpdir(*args, &block) options = Hash === args.last ? args.pop : {} dirname = Dir.tmpdir return dirname unless block turd = options['turd'] || options[:turd] basename = [ hostname, ppid, pid, thread_id, rand ].join('-') made = false 42.times do |n| pathname = File.join(dirname, "#{ basename }-n=#{ n }") begin FileUtils.mkdir_p(pathname) made = true return Dir.chdir(pathname, &block) rescue Object sleep(rand) :retry ensure unless turd FileUtils.rm_rf(pathname) if made end end end raise "failed to make tmpdir in #{ dirname.inspect }" end
unindent(chunk)
click to toggle source
# File lib/fbomb/util.rb, line 117 def unindent(chunk) lines = chunk.split %r/\n/ indent = nil re = %r/^/ lines.map! do |line| unless indent indent = line[%r/^\s*/] re = %r/^#{ indent }/ end line.gsub re, '' end.join("\n") end
which_ruby()
click to toggle source
# File lib/fbomb/util.rb, line 2 def which_ruby c = RbConfig::CONFIG bindir = c["bindir"] || c['BINDIR'] ruby_install_name = c['ruby_install_name'] || c['RUBY_INSTALL_NAME'] || 'ruby' ruby_ext = c['EXEEXT'] || '' ruby = File.join(bindir, (ruby_install_name + ruby_ext)) end