Module: Obfusk::Util
- Defined in:
- lib/obfusk/util/fs.rb,
lib/obfusk/util/os.rb,
lib/obfusk/util/sh.rb,
lib/obfusk/util/cmd.rb,
lib/obfusk/util/die.rb,
lib/obfusk/util/opt.rb,
lib/obfusk/util/run.rb,
lib/obfusk/util/data.rb,
lib/obfusk/util/misc.rb,
lib/obfusk/util/spec.rb,
lib/obfusk/util/term.rb,
lib/obfusk/util/valid.rb,
lib/obfusk/util/module.rb,
lib/obfusk/util/struct.rb,
lib/obfusk/util/message.rb,
lib/obfusk/util/process.rb,
lib/obfusk/util/version.rb
Defined Under Namespace
Modules: BetterStruct, Cmd, FS, OS, Opt, Process, ShBase, Term, Valid Classes: RunError, Sh, Shc
Constant Summary collapse
- VERSION =
'0.5.1'
- DATE =
'2014-03-03'
Class Method Summary collapse
-
._die_msgs(msgs) ⇒ Object
helper; modifies msgs -> OK b/c comes from *msgs.
-
._enoent_to_run(what, args, &b) ⇒ Object
helper.
-
._owhat(what) ⇒ Object
(helper for onow).
-
._pipeline_args(*args) ⇒ Object
helper.
-
._sh(cmd, args) ⇒ Object
helper.
-
._spawn_args(cmd, *args) ⇒ Object
helper.
-
._spawn_rm_opts(args) ⇒ Object
helper.
-
.assoc(x, h = {}) ⇒ Object
assoc key(s) w/ value(s); array keys represent nested keys; will autovivivy missing (if false/nil) nested objects as hashes.
-
.capture2(*args) ⇒ Object
better Open3.capture2; see capture3.
-
.capture2e(*args) ⇒ Object
better Open3.capture2e; see capture3.
-
.capture3(*args) ⇒ Object
better Open3.capture3; see popen3.
-
.capture_stderr(isatty = false, &b) ⇒ Object
use StringIO to capture $stderr from block.
-
.capture_stdout(isatty = false, &b) ⇒ Object
use StringIO to capture $stdout from block.
-
.chk_exit(args, &b) ⇒ Object
run block w/ args, check
.exitstatus
. - .chk_exitstatus(args, c) ⇒ Object
-
.deep_merge(h1, h2, &b) ⇒ Object
merge hashes recursively.
-
.deep_stringify_keys(h) ⇒ Object
convert nested hash keys to strings.
-
.deep_symbolize_keys(h, always = true) ⇒ Object
convert nested hash keys to symbols.
-
.deep_transform_keys(h, &b) ⇒ Object
convert nested hash keys using block.
-
.deepdup(obj) ⇒ Object
deep copy using Marshal.
-
.die!(*msgs) ⇒ Object
print msgs to stderr and exit 1; pass
{ status: code }
as last argument to use other statuscode. -
.empty_as_nil(x) ⇒ Object
nil if x is
.empty?
, x otherwise. -
.exec(*args) ⇒ Object
better Kernel.exec; should never return (if successful); see spawn, spawn_w, system.
- .exit_non_zero!(args, c) ⇒ Object
-
.get_in(x, *ks, &b) ⇒ Object
get the value in a nested associative structure; returns the value (if found), the result of the block (if passed), or nil.
-
.h_to_struct(h = {}) ⇒ Object
hash to struct.
-
.link_mod_method(from, from_meth, to, to_meth = from_meth) ⇒ Object
create module method to.to_meth that calls from.from_meth.
-
.odie!(msg, opts = {}) ⇒ Object
onoe, exit; requires
obfusk/util/message
. -
.ohai(msg) ⇒ Object
info message w/ colours:
"==> <msg>"
. -
.onoe(msg, opts = {}) ⇒ Object
error message w/ colours:
"<label>: <msg>"
. -
.onow(msg, *what) ⇒ Object
info message w/ colours:
"==> <msg>: <a>[, <b>, ...]"
. -
.opoo(msg, opts = {}) ⇒ Object
warning message (onoe w/ label 'Warning').
-
.osh(*args) ⇒ Object
onow + sh; requires
obfusk/util/message
. -
.osh!(*args) ⇒ Object
onow + sh!; requires
obfusk/util/message
. -
.osh?(*args) ⇒ Boolean
onow + sh?; requires
obfusk/util/message
. -
.oshc(*args) ⇒ Object
onow + shc; requires
obfusk/util/message
. -
.oshc!(*args) ⇒ Object
onow + shc!; requires
obfusk/util/message
. -
.oshc?(*args) ⇒ Boolean
onow + shc?; requires
obfusk/util/message
. -
.ospawn(*args) ⇒ Object
ohai + spawn; requires
obfusk/util/message
. -
.ospawn_w(*args) ⇒ Object
ohai + spawn_w; requires
obfusk/util/message
. -
.pipeline(*args) ⇒ Object
better Open3.pipeline; see exec, popen3.
-
.pipeline_r(*args, &b) ⇒ Object
better Open3.pipeline_r; see pipeline_rw.
-
.pipeline_rw(*args, &b) ⇒ Object
better Open3.pipeline_rw; see popen3.
-
.pipeline_start(*args, &b) ⇒ Object
better Open3.pipeline_start; see popen3.
-
.pipeline_w(*args, &b) ⇒ Object
better Open3.pipeline_w; see pipeline_rw.
-
.popen2(*args, &b) ⇒ Object
better Open3.popen2; see popen3.
-
.popen2e(*args, &b) ⇒ Object
better Open3.popen2e; see popen3.
-
.popen3(*args, &b) ⇒ Object
better Open3.popen3; see exec, spawn, spawn_w, system.
-
.provide_stdin(str, isatty = false, &b) ⇒ Object
use StringIO to provide $stdin to block.
-
.require_all(dir) ⇒ Hash
load
dir/*
(by searching fordir/*.rb
in$LOAD_PATH
). -
.sh(cmd, *args) ⇒ Sh
run a command using bash (w/ arguments); see also Util.shc; uses Util.spawn_w.
-
.sh!(cmd, *args) ⇒ Object
sh(...).ok!
. -
.sh?(cmd, *args) ⇒ Boolean
sh(...).ok?
. -
.shc(cmd, *args) ⇒ Shc
run a command using bash (w/ arguments) and capture its stdout and stderr; accepts the same arguments as Util.sh; uses
capture{2e,3}
. -
.shc!(cmd, *args) ⇒ Object
shc(...).ok!
. -
.shc?(cmd, *args) ⇒ Boolean
shc(...).ok?
. -
.spawn(*args) ⇒ Object
better Kernel.spawn; options can be passed as last arg like w/ Kernel.spawn, but instead of env as optional first arg, options takes an :env key as well; also, no shell is ever used; returns PID; see exec, spawn_w, system.
-
.spawn_w(*args) ⇒ Object
better system: spawn + wait; returns $?; see exec, spawn, system.
-
.stringify_keys(h) ⇒ Object
convert hash keys to strings.
-
.struct(*fields, &b) ⇒ Object
better struct; see examples in README.md.
-
.submodules(mod) ⇒ Object
get submodules as hash.
-
.symbolize_keys(h, always = true) ⇒ Object
convert hash keys to symbols.
-
.system(*args) ⇒ Object
better Kernel.system; returns true/false; see exec, spawn, spawn_w.
-
.transform_keys(h, &b) ⇒ Object
convert hash keys using block.
-
.udie!(usage, *msgs) ⇒ Object
print msgs to stderr, show usage, exit.
Class Method Details
._die_msgs(msgs) ⇒ Object
helper; modifies msgs -> OK b/c comes from *msgs
37 38 39 40 41 |
# File 'lib/obfusk/util/die.rb', line 37 def self._die_msgs(msgs) code = (msgs.last.is_a?(Hash) && msgs.pop[:status]) || 1 msgs.each { |m| $stderr.puts "Error: #{m}" } code end |
._enoent_to_run(what, args, &b) ⇒ Object
helper
181 182 183 184 185 186 187 188 |
# File 'lib/obfusk/util/run.rb', line 181 def self._enoent_to_run(what, args, &b) # {{{1 begin b[args] rescue Errno::ENOENT => e raise RunError, "failed to #{what} command #{args}: #{e.message}" end end |
._owhat(what) ⇒ Object
(helper for onow)
58 59 60 |
# File 'lib/obfusk/util/message.rb', line 58 def self._owhat(what) ': ' + what.map { |x| _tcol(:lgn) + x + _tcol(:non) } *', ' end |
._pipeline_args(*args) ⇒ Object
helper
202 203 204 205 206 207 208 |
# File 'lib/obfusk/util/run.rb', line 202 def self._pipeline_args(*args) # {{{1 if args.last && (l = args.last.dup).is_a?(Hash) args[0..-2].map { |c| _spawn_args(*c) } + [l] else args.map { |c| _spawn_args(*c) } end end |
._sh(cmd, args) ⇒ Object
helper
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/obfusk/util/sh.rb', line 148 def self._sh(cmd, args) # {{{1 a, o = args.last && (l = args.last.dup).is_a?(Hash) ? [args[0..-2],l] : [args,{}] shell = o.delete(:shell) || 'bash' print = o.delete :print exit = o.delete :exit merge = o.delete :merge env = o.delete(:env) || {} syms = o.select { |k,v| Symbol === k } strs = o.select { |k,v| String === k } opts = syms.merge env: env.merge(strs) c = [shell] + (print ? %w{-x} : []) + (exit ? %w{-e} : []) + ['-c', cmd, shell] + a { cmd: c, opts: opts, merge: merge } end |
._spawn_args(cmd, *args) ⇒ Object
helper
191 192 193 194 195 196 197 198 199 |
# File 'lib/obfusk/util/run.rb', line 191 def self._spawn_args(cmd, *args) # {{{1 c = [cmd, cmd] if args.last && (l = args.last.dup).is_a?(Hash) \ && e = l.delete(:env) [e, c] + args[0..-2] + [l] else [c] + args end end |
._spawn_rm_opts(args) ⇒ Object
helper
211 212 213 |
# File 'lib/obfusk/util/run.rb', line 211 def self._spawn_rm_opts(args) args.last.is_a?(Hash) ? args[0..-2] : args end |
.assoc(x, h = {}) ⇒ Object
assoc key(s) w/ value(s); array keys represent nested keys; will autovivivy missing (if false/nil) nested objects as hashes
x = { x: { y: 0 } }; assoc(x, [:x,:y] => 1); x[:x][:y] == 1
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/obfusk/util/data.rb', line 21 def self.assoc(x, h = {}) # {{{1 h.each do |k,v| if k.is_a? Array case k.length when 0; raise ArgumentError, 'empty array key' when 1; x[k.first] = v else h, *t = k; assoc(x[h] ||= {}, t => v) end else x[k] = v end end x end |
.capture2(*args) ⇒ Object
better Open3.capture2; see capture3
58 59 60 61 62 |
# File 'lib/obfusk/util/run.rb', line 58 def self.capture2(*args) _enoent_to_run('capture2', args) do |a| Open3.capture2(*_spawn_args(*a)) end end |
.capture2e(*args) ⇒ Object
better Open3.capture2e; see capture3
66 67 68 69 70 |
# File 'lib/obfusk/util/run.rb', line 66 def self.capture2e(*args) _enoent_to_run('capture2e', args) do |a| Open3.capture2e(*_spawn_args(*a)) end end |
.capture3(*args) ⇒ Object
better Open3.capture3; see popen3
74 75 76 77 78 |
# File 'lib/obfusk/util/run.rb', line 74 def self.capture3(*args) _enoent_to_run('capture3', args) do |a| Open3.capture3(*_spawn_args(*a)) end end |
.capture_stderr(isatty = false, &b) ⇒ Object
use StringIO to capture $stderr from block
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/obfusk/util/spec.rb', line 42 def self.capture_stderr(isatty = false, &b) # {{{1 old = $stderr begin StringIO.open('', 'w') do |s| def s.isatty; true; end if isatty; $stderr = s; b[]; s.string end ensure $stderr = old end end |
.capture_stdout(isatty = false, &b) ⇒ Object
use StringIO to capture $stdout from block
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/obfusk/util/spec.rb', line 30 def self.capture_stdout(isatty = false, &b) # {{{1 old = $stdout begin StringIO.open('', 'w') do |s| def s.isatty; true; end if isatty; $stdout = s; b[]; s.string end ensure $stdout = old end end |
.chk_exit(args, &b) ⇒ Object
run block w/ args, check .exitstatus
164 165 166 |
# File 'lib/obfusk/util/run.rb', line 164 def self.chk_exit(args, &b) chk_exitstatus args, b[args].exitstatus end |
.chk_exitstatus(args, c) ⇒ Object
169 170 171 |
# File 'lib/obfusk/util/run.rb', line 169 def self.chk_exitstatus(args, c) exit_non_zero! args, c if c != 0 end |
.deep_merge(h1, h2, &b) ⇒ Object
merge hashes recursively
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/obfusk/util/data.rb', line 91 def self.deep_merge(h1, h2, &b) # {{{1 h1.merge(Hash[h2.map do |k,v2| if h1.has_key?(k) if (v1 = h1[k]).is_a?(Hash) && v2.is_a?(Hash) [k, deep_merge(v1, v2, &b)] else [k, b ? b[k,h1[k],v2] : v2] end else [k, v2] end end]) end |
.deep_stringify_keys(h) ⇒ Object
convert nested hash keys to strings
73 74 75 |
# File 'lib/obfusk/util/data.rb', line 73 def self.deep_stringify_keys(h) deep_transform_keys(h, &:to_s) end |
.deep_symbolize_keys(h, always = true) ⇒ Object
convert nested hash keys to symbols
79 80 81 |
# File 'lib/obfusk/util/data.rb', line 79 def self.deep_symbolize_keys(h, always = true) deep_transform_keys(h, &(always ? :to_sym : -> k { k.to_sym rescue k })) end |
.deep_transform_keys(h, &b) ⇒ Object
convert nested hash keys using block
84 85 86 |
# File 'lib/obfusk/util/data.rb', line 84 def self.deep_transform_keys(h, &b) Hash[h.map { |k,v| [b[k], v.is_a?(Hash) ? deep_transform_keys(v,&b) : v] }] end |
.deepdup(obj) ⇒ Object
deep copy using Marshal
108 109 110 |
# File 'lib/obfusk/util/data.rb', line 108 def self.deepdup(obj) Marshal.load Marshal.dump obj end |
.die!(*msgs) ⇒ Object
print msgs to stderr and exit 1;
pass { status: code }
as last argument to use other statuscode
17 18 19 |
# File 'lib/obfusk/util/die.rb', line 17 def self.die!(*msgs) code = _die_msgs msgs; exit code end |
.empty_as_nil(x) ⇒ Object
nil if x is .empty?
, x otherwise
113 114 115 |
# File 'lib/obfusk/util/data.rb', line 113 def self.empty_as_nil(x) x && x.empty? ? nil : x end |
.exec(*args) ⇒ Object
better Kernel.exec; should never return (if successful); see spawn, spawn_w, system
24 25 26 27 28 |
# File 'lib/obfusk/util/run.rb', line 24 def self.exec(*args) _enoent_to_run('exec', args) do |a| Kernel.exec(*_spawn_args(*a)) end end |
.exit_non_zero!(args, c) ⇒ Object
174 175 176 |
# File 'lib/obfusk/util/run.rb', line 174 def self.exit_non_zero!(args, c) raise RunError, "command returned non-zero: #{args} -> #{c}" end |
.get_in(x, *ks, &b) ⇒ Object
get the value in a nested associative structure; returns the value (if found), the result of the block (if passed), or nil.
get_in({ x: { y: 1 } }, :x, :y)
# => 1
43 44 45 46 47 48 49 50 |
# File 'lib/obfusk/util/data.rb', line 43 def self.get_in(x, *ks, &b) # {{{1 if ks.length == 0 x else v = x.fetch(ks.first) { return b && b[] } get_in v, *ks.drop(1), &b end end |
.h_to_struct(h = {}) ⇒ Object
hash to struct
118 119 120 |
# File 'lib/obfusk/util/data.rb', line 118 def self.h_to_struct(h = {}) Struct.new(*h.keys).new(*h.values) end |
.link_mod_method(from, from_meth, to, to_meth = from_meth) ⇒ Object
create module method to.to_meth that calls from.from_meth
16 17 18 19 20 21 |
# File 'lib/obfusk/util/module.rb', line 16 def self.link_mod_method(from, from_meth, to, to_meth = from_meth) to.module_exec(from, from_meth, to_meth) do |f,fm,tm| (class << self; self; end).send(:define_method, tm) \ { |*a,&b| f.send(fm, *a, &b) } end end |
.odie!(msg, opts = {}) ⇒ Object
onoe, exit; requires obfusk/util/message
29 30 31 32 |
# File 'lib/obfusk/util/die.rb', line 29 def self.odie!(msg, opts = {}) o = opts.dup; c = o.delete(:status) || 1 ::Obfusk::Util.onoe msg, o; exit c end |
.ohai(msg) ⇒ Object
info message w/ colours:
"==> <msg>"
22 23 24 |
# File 'lib/obfusk/util/message.rb', line 22 def self.ohai(msg) puts _tcol(:lbl) + '==> ' + _tcol(:whi) + msg + _tcol(:non) end |
.onoe(msg, opts = {}) ⇒ Object
error message w/ colours:
"<label>: <msg>"
opts[:label]
defaults to 'Error';
set opts[:log]
to a lambda to pass message on to a logger
44 45 46 47 48 |
# File 'lib/obfusk/util/message.rb', line 44 def self.onoe(msg, opts = {}) l = opts[:label] || 'Error' $stderr.puts _tcole(:lrd) + l + _tcole(:non) + ': ' + msg opts[:log]["#{l}: #{msg}"] if opts[:log] end |
.onow(msg, *what) ⇒ Object
info message w/ colours:
"==> <msg>: <a>[, <b>, ...]"
30 31 32 33 |
# File 'lib/obfusk/util/message.rb', line 30 def self.onow(msg, *what) puts _tcol(:lgn) + '==> ' + _tcol(:whi) + msg + _tcol(:non) + (what.empty? ? '' : _owhat(what)) end |
.opoo(msg, opts = {}) ⇒ Object
warning message (onoe w/ label 'Warning')
51 52 53 |
# File 'lib/obfusk/util/message.rb', line 51 def self.opoo(msg, opts = {}) onoe msg, opts.merge(label: 'Warning') end |
.osh(*args) ⇒ Object
onow + sh; requires obfusk/util/message
114 115 116 |
# File 'lib/obfusk/util/sh.rb', line 114 def self.osh(*args) ::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); sh(*args) end |
.osh!(*args) ⇒ Object
onow + sh!; requires obfusk/util/message
124 125 126 |
# File 'lib/obfusk/util/sh.rb', line 124 def self.osh!(*args) ::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); sh!(*args) end |
.osh?(*args) ⇒ Boolean
onow + sh?; requires obfusk/util/message
119 120 121 |
# File 'lib/obfusk/util/sh.rb', line 119 def self.osh?(*args) ::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); sh?(*args) end |
.oshc(*args) ⇒ Object
onow + shc; requires obfusk/util/message
131 132 133 |
# File 'lib/obfusk/util/sh.rb', line 131 def self.oshc(*args) ::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); shc(*args) end |
.oshc!(*args) ⇒ Object
onow + shc!; requires obfusk/util/message
141 142 143 |
# File 'lib/obfusk/util/sh.rb', line 141 def self.oshc!(*args) ::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); shc!(*args) end |
.oshc?(*args) ⇒ Boolean
onow + shc?; requires obfusk/util/message
136 137 138 |
# File 'lib/obfusk/util/sh.rb', line 136 def self.oshc?(*args) ::Obfusk::Util.onow 'sh', *_spawn_rm_opts(args); shc?(*args) end |
.ospawn(*args) ⇒ Object
ohai + spawn; requires obfusk/util/message
151 152 153 |
# File 'lib/obfusk/util/run.rb', line 151 def self.ospawn(*args) ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; spawn(*args) end |
.ospawn_w(*args) ⇒ Object
ohai + spawn_w; requires obfusk/util/message
156 157 158 |
# File 'lib/obfusk/util/run.rb', line 156 def self.ospawn_w(*args) ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; spawn_w(*args) end |
.pipeline(*args) ⇒ Object
better Open3.pipeline; see exec, popen3
84 85 86 87 88 |
# File 'lib/obfusk/util/run.rb', line 84 def self.pipeline(*args) _enoent_to_run('pipeline', args) do |a| Open3.pipeline(*_pipeline_args(*a)) end end |
.pipeline_r(*args, &b) ⇒ Object
better Open3.pipeline_r; see pipeline_rw
92 93 94 95 96 |
# File 'lib/obfusk/util/run.rb', line 92 def self.pipeline_r(*args, &b) _enoent_to_run('pipeline_r', args) do |a| Open3.pipeline_r(*_pipeline_args(*a), &b) end end |
.pipeline_rw(*args, &b) ⇒ Object
better Open3.pipeline_rw; see popen3
100 101 102 103 104 |
# File 'lib/obfusk/util/run.rb', line 100 def self.pipeline_rw(*args, &b) _enoent_to_run('pipeline_rw', args) do |a| Open3.pipeline_rw(*_pipeline_args(*a), &b) end end |
.pipeline_start(*args, &b) ⇒ Object
better Open3.pipeline_start; see popen3
108 109 110 111 112 |
# File 'lib/obfusk/util/run.rb', line 108 def self.pipeline_start(*args, &b) _enoent_to_run('pipeline_start', args) do |a| Open3.pipeline_start(*_pipeline_args(*a), &b) end end |
.pipeline_w(*args, &b) ⇒ Object
better Open3.pipeline_w; see pipeline_rw
116 117 118 119 120 |
# File 'lib/obfusk/util/run.rb', line 116 def self.pipeline_w(*args, &b) _enoent_to_run('pipeline_w', args) do |a| Open3.pipeline_w(*_pipeline_args(*a), &b) end end |
.popen2(*args, &b) ⇒ Object
better Open3.popen2; see popen3
126 127 128 129 130 |
# File 'lib/obfusk/util/run.rb', line 126 def self.popen2(*args, &b) _enoent_to_run('popen2', args) do |a| Open3.popen2(*_spawn_args(*a), &b) end end |
.popen2e(*args, &b) ⇒ Object
better Open3.popen2e; see popen3
134 135 136 137 138 |
# File 'lib/obfusk/util/run.rb', line 134 def self.popen2e(*args, &b) _enoent_to_run('popen2e', args) do |a| Open3.popen2e(*_spawn_args(*a), &b) end end |
.popen3(*args, &b) ⇒ Object
better Open3.popen3; see exec, spawn, spawn_w, system
142 143 144 145 146 |
# File 'lib/obfusk/util/run.rb', line 142 def self.popen3(*args, &b) _enoent_to_run('popen3', args) do |a| Open3.popen3(*_spawn_args(*a), &b) end end |
.provide_stdin(str, isatty = false, &b) ⇒ Object
use StringIO to provide $stdin to block
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/obfusk/util/spec.rb', line 18 def self.provide_stdin(str, isatty = false, &b) # {{{1 old = $stdin begin StringIO.open(str, 'r') do |s| def s.isatty; true; end if isatty; $stdin = s; b[] end ensure $stdin = old end end |
.require_all(dir) ⇒ Hash
load dir/*
(by searching for dir/*.rb
in $LOAD_PATH
)
require_all('napp/types') ~> require 'napp/types/*'
30 31 32 33 34 |
# File 'lib/obfusk/util/module.rb', line 30 def self.require_all(dir) Hash[ $LOAD_PATH.map { |x| Dir["#{x}/#{dir}/*.rb"] } .flatten \ .map { |x| "#{dir}/" + File.basename(x, '.rb') } .uniq \ .map { |x| y = require x; [x,y] } ] end |
.sh(cmd, *args) ⇒ Sh
64 65 66 67 68 |
# File 'lib/obfusk/util/sh.rb', line 64 def self.sh(cmd, *args) c = _sh cmd, args; o = c[:opts] o_ = c[:merge] ? o.merge(:err => o[:out] ? [:child, :out] : :out) : o Sh.new c[:cmd], spawn_w(*c[:cmd], o_) end |
.sh!(cmd, *args) ⇒ Object
sh(...).ok!
76 77 78 |
# File 'lib/obfusk/util/sh.rb', line 76 def self.sh!(cmd, *args) sh(cmd, *args).ok! end |
.sh?(cmd, *args) ⇒ Boolean
sh(...).ok?
71 72 73 |
# File 'lib/obfusk/util/sh.rb', line 71 def self.sh?(cmd, *args) sh(cmd, *args).ok? end |
.shc(cmd, *args) ⇒ Shc
run a command using bash (w/ arguments) and capture its stdout and
stderr; accepts the same arguments as sh; uses capture{2e,3}
shc('echo "$0" ">>$1<<" ">>$FOO<<"', '"one"', 'FOO' => 'foo').stdout
# => %Q{bash >>"one"<< >>foo<<}
91 92 93 94 95 96 97 98 99 |
# File 'lib/obfusk/util/sh.rb', line 91 def self.shc(cmd, *args) # {{{1 c = _sh cmd, args if c[:merge] stderr = nil; stdout, status = capture2e(*c[:cmd], c[:opts]) else stdout, stderr, status = capture3(*c[:cmd], c[:opts]) end Shc.new c[:cmd], status, stdout, stderr end |
.shc!(cmd, *args) ⇒ Object
shc(...).ok!
107 108 109 |
# File 'lib/obfusk/util/sh.rb', line 107 def self.shc!(cmd, *args) shc(cmd, *args).ok! end |
.shc?(cmd, *args) ⇒ Boolean
shc(...).ok?
102 103 104 |
# File 'lib/obfusk/util/sh.rb', line 102 def self.shc?(cmd, *args) shc(cmd, *args).ok? end |
.spawn(*args) ⇒ Object
better Kernel.spawn; options can be passed as last arg like w/ Kernel.spawn, but instead of env as optional first arg, options takes an :env key as well; also, no shell is ever used; returns PID; see exec, spawn_w, system
35 36 37 38 39 |
# File 'lib/obfusk/util/run.rb', line 35 def self.spawn(*args) _enoent_to_run('spawn', args) do |a| Kernel.spawn(*_spawn_args(*a)) end end |
.spawn_w(*args) ⇒ Object
better system: spawn + wait; returns $?; see exec, spawn, system
42 43 44 |
# File 'lib/obfusk/util/run.rb', line 42 def self.spawn_w(*args) pid = spawn(*args); ::Process.wait pid; $? end |
.stringify_keys(h) ⇒ Object
convert hash keys to strings
55 56 57 |
# File 'lib/obfusk/util/data.rb', line 55 def self.stringify_keys(h) transform_keys(h, &:to_s) end |
.struct(*fields, &b) ⇒ Object
better struct; see examples in README.md
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/obfusk/util/struct.rb', line 67 def self.struct(*fields, &b) # {{{1 Class.new(Struct.new(*fields.map(&:to_sym))) do include BetterStruct # init w/ hash def initialize(h = {}) h.each { |k,v| self[k] = v } end self.class_eval(&b) if b end end |
.submodules(mod) ⇒ Object
get submodules as hash
submodules(Foo) -> { 'bar' => Foo::Bar, ... }
41 42 43 44 45 |
# File 'lib/obfusk/util/module.rb', line 41 def self.submodules(mod) Hash[ mod.constants \ .map { |x| [x.downcase.to_s, mod.const_get(x)] } \ .select { |k,v| v.class == Module } ] end |
.symbolize_keys(h, always = true) ⇒ Object
convert hash keys to symbols
61 62 63 |
# File 'lib/obfusk/util/data.rb', line 61 def self.symbolize_keys(h, always = true) transform_keys(h, &(always ? :to_sym : -> k { k.to_sym rescue k })) end |
.system(*args) ⇒ Object
better Kernel.system; returns true/false; see exec, spawn, spawn_w
48 49 50 51 52 |
# File 'lib/obfusk/util/run.rb', line 48 def self.system(*args) r = Kernel.system(*_spawn_args(*args)) raise RunError, "failed to run command #{args} (#$?)" if r.nil? r end |
.transform_keys(h, &b) ⇒ Object
convert hash keys using block
66 67 68 |
# File 'lib/obfusk/util/data.rb', line 66 def self.transform_keys(h, &b) Hash[h.map { |k,v| [b[k],v] }] end |
.udie!(usage, *msgs) ⇒ Object
print msgs to stderr, show usage, exit
22 23 24 |
# File 'lib/obfusk/util/die.rb', line 22 def self.udie!(usage, *msgs) code = _die_msgs msgs; $stderr.puts "Usage: #{usage}"; exit code end |