obfusk-util 0.0.1.SNAPSHOT.20130725000153 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/run.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -23,7 +23,7 @@ module Obfusk; module Util
23
23
  # @raise RunError on ENOENT
24
24
  def self.exec(*args)
25
25
  _enoent_to_run('exec', args) do |a|
26
- Kernel.exec *_spawn_args(*a)
26
+ Kernel.exec(*_spawn_args(*a))
27
27
  end
28
28
  end
29
29
 
@@ -34,7 +34,7 @@ module Obfusk; module Util
34
34
  # @raise RunError on ENOENT
35
35
  def self.spawn(*args)
36
36
  _enoent_to_run('spawn', args) do |a|
37
- Kernel.spawn *_spawn_args(*a)
37
+ Kernel.spawn(*_spawn_args(*a))
38
38
  end
39
39
  end
40
40
 
@@ -46,18 +46,102 @@ module Obfusk; module Util
46
46
  # better Kernel.system; returns true/false; see exec, spawn, spawn_w
47
47
  # @raise RunError on failure (Kernel.system -> nil)
48
48
  def self.system(*args)
49
- r = Kernel.system *_spawn_args(*args)
49
+ r = Kernel.system(*_spawn_args(*args))
50
50
  raise RunError, "failed to run command #{args} (#$?)" if r.nil?
51
51
  r
52
52
  end
53
53
 
54
54
  # --
55
55
 
56
+ # better Open3.capture2; see capture3
57
+ # @raise RunError on ENOENT
58
+ def self.capture2(*args)
59
+ _enoent_to_run('capture2', args) do |a|
60
+ Open3.capture2(*_spawn_args(*a))
61
+ end
62
+ end
63
+
64
+ # better Open3.capture2e; see capture3
65
+ # @raise RunError on ENOENT
66
+ def self.capture2e(*args)
67
+ _enoent_to_run('capture2e', args) do |a|
68
+ Open3.capture2e(*_spawn_args(*a))
69
+ end
70
+ end
71
+
72
+ # better Open3.capture3; see popen3
73
+ # @raise RunError on ENOENT
74
+ def self.capture3(*args)
75
+ _enoent_to_run('capture3', args) do |a|
76
+ Open3.capture3(*_spawn_args(*a))
77
+ end
78
+ end
79
+
80
+ # --
81
+
82
+ # better Open3.pipeline; see exec, popen3
83
+ # @raise RunError on ENOENT
84
+ def self.pipeline(*args)
85
+ _enoent_to_run('pipeline', args) do |a|
86
+ Open3.pipeline(*_pipeline_args(*a))
87
+ end
88
+ end
89
+
90
+ # better Open3.pipeline_r; see pipeline_rw
91
+ # @raise RunError on ENOENT
92
+ def self.pipeline_r(*args, &b)
93
+ _enoent_to_run('pipeline_r', args) do |a|
94
+ Open3.pipeline_r(*_pipeline_args(*a), &b)
95
+ end
96
+ end
97
+
98
+ # better Open3.pipeline_rw; see popen3
99
+ # @raise RunError on ENOENT
100
+ def self.pipeline_rw(*args, &b)
101
+ _enoent_to_run('pipeline_rw', args) do |a|
102
+ Open3.pipeline_rw(*_pipeline_args(*a), &b)
103
+ end
104
+ end
105
+
106
+ # better Open3.pipeline_start; see popen3
107
+ # @raise RunError on ENOENT
108
+ def self.pipeline_start(*args, &b)
109
+ _enoent_to_run('pipeline_start', args) do |a|
110
+ Open3.pipeline_start(*_pipeline_args(*a), &b)
111
+ end
112
+ end
113
+
114
+ # better Open3.pipeline_w; see pipeline_rw
115
+ # @raise RunError on ENOENT
116
+ def self.pipeline_w(*args, &b)
117
+ _enoent_to_run('pipeline_w', args) do |a|
118
+ Open3.pipeline_w(*_pipeline_args(*a), &b)
119
+ end
120
+ end
121
+
122
+ # --
123
+
124
+ # better Open3.popen2; see popen3
125
+ # @raise RunError on ENOENT
126
+ def self.popen2(*args, &b)
127
+ _enoent_to_run('popen2', args) do |a|
128
+ Open3.popen2(*_spawn_args(*a), &b)
129
+ end
130
+ end
131
+
132
+ # better Open3.popen2e; see popen3
133
+ # @raise RunError on ENOENT
134
+ def self.popen2e(*args, &b)
135
+ _enoent_to_run('popen2e', args) do |a|
136
+ Open3.popen2e(*_spawn_args(*a), &b)
137
+ end
138
+ end
139
+
56
140
  # better Open3.popen3; see exec, spawn, spawn_w, system
57
141
  # @raise RunError on ENOENT
58
142
  def self.popen3(*args, &b)
59
143
  _enoent_to_run('popen3', args) do |a|
60
- Open3.popen3 *_spawn_args(*a), &b
144
+ Open3.popen3(*_spawn_args(*a), &b)
61
145
  end
62
146
  end
63
147
 
@@ -65,12 +149,12 @@ module Obfusk; module Util
65
149
 
66
150
  # ohai + spawn; requires `obfusk/util/message`
67
151
  def self.ospawn(*args)
68
- ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; spawn *args
152
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; spawn(*args)
69
153
  end
70
154
 
71
155
  # ohai + spawn_w; requires `obfusk/util/message`
72
156
  def self.ospawn_w(*args)
73
- ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; spawn_w *args
157
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; spawn_w(*args)
74
158
  end
75
159
 
76
160
  # --
@@ -114,6 +198,15 @@ module Obfusk; module Util
114
198
  end
115
199
  end # }}}1
116
200
 
201
+ # helper
202
+ def self._pipeline_args(*args) # {{{1
203
+ if args.last && (l = args.last.dup).is_a?(Hash)
204
+ args[0..-2].map { |c| _spawn_args(*c) } + [l]
205
+ else
206
+ args.map { |c| _spawn_args(*c) }
207
+ end
208
+ end # }}}1
209
+
117
210
  # helper
118
211
  def self._spawn_rm_opts(args)
119
212
  args.last.is_a?(Hash) ? args[0..-2] : args
@@ -0,0 +1,102 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/sh.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2014-02-19
6
+ #
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/run'
13
+
14
+ # my namespace
15
+ module Obfusk; module Util
16
+
17
+ # shell command result
18
+ class Sh < Struct.new(:cmd, :status, :stdout, :stderr) # {{{1
19
+
20
+ # was the exitstatus zero?
21
+ def ok?
22
+ status.exitstatus == 0
23
+ end
24
+
25
+ # @raise RunError when exitstatus non-zero
26
+ def ok!
27
+ ::Obfusk::Util.chk_exitstatus cmd, status.exitstatus
28
+ end
29
+
30
+ # ... TODO ...
31
+
32
+ end # }}}1
33
+
34
+ # --
35
+
36
+ # run a command using bash (w/ arguments)
37
+ #
38
+ # ```
39
+ # sh 'echo "$0" ">>$1<<" ">>$FOO<<"', '"one"', 'FOO' => 'foo'
40
+ # # => bash >>"one"<< >>foo<<
41
+ # ```
42
+ #
43
+ # @param [Hash] args.last
44
+ # * `:shell` the shell to use (default: `'bash'`)
45
+ # * `:print` whether to pass `-x` to the shell (default: `false`)
46
+ # * `:exit` whether to pass `-e` to the shell (default: `false`)
47
+ # * `:merge` whether to merge stdout and stderr (default: `false`)
48
+ # * `:env` the environment
49
+ # * any other `String` key is added to the `env`
50
+ # * any other `Symbol` key is passed as an option to `capture3`
51
+ # @return [Sh]
52
+ def self.sh(cmd, *args) # {{{1
53
+ a, o = args.last && (l = args.last.dup).is_a?(Hash) ?
54
+ [args[0..-2],l] : [args,{}]
55
+ shell = o.delete(:shell) || 'bash'
56
+ print = o.delete :print
57
+ exit = o.delete :exit
58
+ merge = o.delete :merge
59
+ env = o.delete(:env) || {}
60
+ syms = o.select { |k,v| Symbol === k }
61
+ strs = o.select { |k,v| String === k }
62
+ opts = syms.merge env: env.merge(strs)
63
+ c = [shell] + (print ? %w{-x} : []) + (exit ? %w{-e} : []) +
64
+ ['-c', cmd, shell] + a
65
+ if merge
66
+ stderr = nil; stdout, status = capture2e *c, opts
67
+ else
68
+ stdout, stderr, status = capture3 *c, opts
69
+ end
70
+ Sh.new c, status, stdout, stderr
71
+ end # }}}1
72
+
73
+ # `sh(...).ok?`
74
+ def self.sh?(cmd, *args)
75
+ sh(cmd, *args).ok?
76
+ end
77
+
78
+ # `sh(...).ok!`
79
+ def self.sh!(cmd, *args)
80
+ sh(cmd, *args).ok!
81
+ end
82
+
83
+ # --
84
+
85
+ # ohai + sh; requires `obfusk/util/message`
86
+ def self.osh(*args)
87
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sh(*args)
88
+ end
89
+
90
+ # ohai + sh?; requires `obfusk/util/message`
91
+ def self.osh?(*args)
92
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sh?(*args)
93
+ end
94
+
95
+ # ohai + sh!; requires `obfusk/util/message`
96
+ def self.osh!(*args)
97
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sh!(*args)
98
+ end
99
+
100
+ end; end
101
+
102
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/struct.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -24,6 +24,7 @@ module Obfusk; module Util
24
24
  base.extend ClassMethods
25
25
  end
26
26
 
27
+ # class methods
27
28
  module ClassMethods # {{{2
28
29
 
29
30
  # new, block, freeze, return
@@ -73,7 +74,7 @@ module Obfusk; module Util
73
74
  h.each { |k,v| self[k] = v }
74
75
  end
75
76
 
76
- self.class_eval &b if b
77
+ self.class_eval(&b) if b
77
78
 
78
79
  end
79
80
  end # }}}1
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/term.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/valid.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -35,7 +35,7 @@ module Obfusk; module Util; module Valid
35
35
  # validate value against regex
36
36
  # @raise ValidationError on no match
37
37
  def self.validate!(x, rx, name)
38
- x.to_s.match /^(#{rx})$/ or invalid! "invalid #{name}"
38
+ x.to_s.match(/^(#{rx})$/) or invalid! "invalid #{name}"
39
39
  end
40
40
 
41
41
  end; end; end
@@ -1,5 +1,5 @@
1
1
  # my namespace
2
2
  module Obfusk; module Util
3
- VERSION = '0.0.1.SNAPSHOT.20130725000153'
4
- DATE = '2013-07-24'
3
+ VERSION = '0.4.2'
4
+ DATE = '2014-02-19'
5
5
  end; end
data/obfusk-util.gemspec CHANGED
@@ -7,8 +7,6 @@ Gem::Specification.new do |s|
7
7
 
8
8
  s.description = <<-END.gsub(/^ {4}/, '')
9
9
  miscellaneous utility library for ruby
10
-
11
- ...
12
10
  END
13
11
 
14
12
  s.version = Obfusk::Util::VERSION
@@ -17,7 +15,7 @@ Gem::Specification.new do |s|
17
15
  s.authors = [ 'Felix C. Stegerman' ]
18
16
  s.email = %w{ flx@obfusk.net }
19
17
 
20
- s.license = 'GPLv2'
18
+ s.licenses = %w{ LGPLv3+ }
21
19
 
22
20
  s.files = %w{ .yardopts README.md Rakefile } \
23
21
  + %w{ obfusk-util.gemspec } \
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/cmd_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/data_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/die_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/fs_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/message_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/module_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -23,6 +23,22 @@ ms = ou::Module__Spec
23
23
 
24
24
  describe 'obfusk/util/module' do
25
25
 
26
+ context 'link_mod_method' do # {{{1
27
+ it 'link_mod_method' do
28
+ x = Module.new; y = Module.new
29
+ x.module_eval do
30
+ def self.foo; 42; end
31
+ def self.bar; 37; end
32
+ end
33
+ y.module_eval do
34
+ ou.link_mod_method x, :foo, self
35
+ ou.link_mod_method x, :bar, self, :baz
36
+ end
37
+ y.foo.should == 42
38
+ y.baz.should == 37
39
+ end
40
+ end # }}}1
41
+
26
42
  context 'require_all' do # {{{1
27
43
  it 'require_all' do
28
44
  Dir.mktmpdir do |dir|
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/opt_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/os_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -32,7 +32,7 @@ describe 'obfusk/util/os' do
32
32
 
33
33
  context 'now' do # {{{1
34
34
  it 'now' do
35
- os.now.should match /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/
35
+ os.now.should match(/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/)
36
36
  end
37
37
  end # }}}1
38
38
 
@@ -2,10 +2,10 @@
2
2
  #
3
3
  # File : obfusk/util/process_spec.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2013-07-24
5
+ # Date : 2014-02-19
6
6
  #
7
- # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
- # Licence : GPLv2
7
+ # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
+ # Licence : LGPLv3+
9
9
  #
10
10
  # -- ; }}}1
11
11
 
@@ -19,7 +19,7 @@ describe 'obfusk/util/process' do
19
19
  it 'age' do
20
20
  pid = spawn 'sleep 100'
21
21
  begin
22
- pr.age(pid).should match /^00:0\d$/ # ????
22
+ pr.age(pid).should match(/^00:0\d$/) # ????
23
23
  ensure
24
24
  Process.kill 'TERM', pid; Process.wait pid
25
25
  end