Module: Bwkfanboy::Utils

Defined in:
lib/bwkfanboy/utils.rb

Class Method Summary collapse

Class Method Details

.cl_parse(arr, banner, o = nil, simple = false) ⇒ Object

Parses command line options. arr is an array of options (usually ARGV). banner is a help string that describes what your program does.

If o is non nil function parses arr immediately, otherwise it only creates OptionParser object and return it (if simple is false). See bwkfanboy script for examples.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bwkfanboy/utils.rb', line 111

def self.cl_parse(arr, banner, o = nil, simple = false)
  if ! o then 
    o = OptionParser.new
    o.banner = banner
    o.on('-v', 'Be more verbose.') { |i| Bwkfanboy::Utils.cfg[:verbose] += 1 }
    o.on('-V', 'Show version & exit.') { |i|
      puts Bwkfanboy::Meta::VERSION
      exit 0
    }
    return o if ! simple
  end

  begin
    o.parse!(arr)
  rescue
    Bwkfanboy::Utils.errx(1, $!.to_s)
  end
end

.cmd_run(cmd) ⇒ Object

used in CGI and WEBrick examples



131
132
133
134
135
136
137
138
# File 'lib/bwkfanboy/utils.rb', line 131

def self.cmd_run(cmd)
  so = sr = ''
  status = Open4::popen4(cmd) { |pid, stdin, stdout, stderr|
    so = stdout.read
    sr = stderr.read
  }
  [status.exitstatus, sr, so]
end

.dir_tmp_createObject

Logs and pidfiles the other temporal stuff sits here



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bwkfanboy/utils.rb', line 53

def self.dir_tmp_create()
  if ! File.writable?(Meta::DIR_TMP) then
    begin
      t = '/'
      Meta::DIR_TMP.split('/')[1..-1].each {|i|
        t += i + '/'
        Dir.mkdir(t) if ! Dir.exists?(t)
      }
    rescue
      warnx("cannot create/open directory #{Meta::DIR_TMP} for writing")
    end
  end
end

.errx(ec, t) ⇒ Object



33
34
35
36
37
38
# File 'lib/bwkfanboy/utils.rb', line 33

def self.errx(ec, t)
  m = File.basename($0) +" error: "+ t + "\n"
  $stderr.print(m);
  log.error(m.chomp) if log
  exit(ec)
end

.gem_dir_systemObject



140
141
142
143
144
145
# File 'lib/bwkfanboy/utils.rb', line 140

def self.gem_dir_system
  t = ["#{File.dirname(File.expand_path($0))}/../lib/#{Meta::NAME}",
       "#{Gem.dir}/gems/#{Meta::NAME}-#{Meta::VERSION}/lib/#{Meta::NAME}"]
  t.each {|i| return i if File.readable?(i) }
  raise "both paths are invalid: #{t}"
end

.log_startObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bwkfanboy/utils.rb', line 67

def self.log_start()
  dir_tmp_create()
  begin
    Dir.mkdir(Meta::DIR_LOG) if ! File.writable?(Meta::DIR_LOG)
    log = Logger.new(cfg[:log], 2, Meta::LOG_MAXSIZE)
  rescue
    warnx("cannot open log #{cfg[:log]}");
    return nil
  end
  log.level = Logger::DEBUG
  log.datetime_format = "%H:%M:%S"
  log.info("#{$0} starting")
  log
end

.plugin_load(path, class_name) ⇒ Object

Loads (via require()) a Ruby code from path (the full path to the file). class_name is the name of the class to check for existence after successful plugin loading.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bwkfanboy/utils.rb', line 86

def self.plugin_load(path, class_name)
  begin
    require(path)
    # TODO get rid of eval()
    fail "class #{class_name} isn't defined" if (! eval("defined?#{class_name}") || ! eval(class_name).is_a?(Class) )
  rescue LoadError
    errx(1, "cannot load plugin '#{path}' #{$!}");
  rescue Exception
    errx(1, "plugin '#{path}' has errors: #{$!}\n\nBacktrace:\n\n#{$!.backtrace.join("\n")}")
  end
end

.plugin_opts(a) ⇒ Object

Get possible options for the parser.



99
100
101
# File 'lib/bwkfanboy/utils.rb', line 99

def self.plugin_opts(a)
  opt = a.size >= 2 ? a[1..-1] : ''
end

.veputs(level, t) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/bwkfanboy/utils.rb', line 40

def self.veputs(level, t)
  if cfg[:verbose] >= level then
#        p log
    log.info(t.chomp) if log
    print(t)
  end
end

.vewarnx(level, t) ⇒ Object



48
49
50
# File 'lib/bwkfanboy/utils.rb', line 48

def self.vewarnx(level, t)
  warnx(t) if cfg[:verbose] >= level
end

.warnx(t) ⇒ Object



27
28
29
30
31
# File 'lib/bwkfanboy/utils.rb', line 27

def self.warnx(t)
  m = File.basename($0) +" warning: "+ t + "\n";
  $stderr.print(m);
  log.warn(m.chomp) if log
end