coulis 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/coulis.rb +133 -130
- data/test/coulis_test.rb +157 -151
- metadata +3 -3
data/lib/coulis.rb
CHANGED
@@ -1,136 +1,139 @@
|
|
1
1
|
require "timeout"
|
2
2
|
|
3
3
|
class Coulis
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
@args << [ definition || "#{"-" if m[0..0] != "-"}#{m}" , "#{q}#{args}#{q}" ]
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
attr_accessor :args
|
48
|
-
|
49
|
-
def initialize(&block)
|
50
|
-
self.class.instance_eval(&block) if block_given?
|
51
|
-
@args = self.class.args
|
52
|
-
self.class.args = []
|
53
|
-
self
|
54
|
-
end
|
55
|
-
|
56
|
-
def options(&block)
|
57
|
-
self.class.args = @args
|
58
|
-
self.class.new(&block)
|
59
|
-
end
|
60
|
-
|
61
|
-
def remove(*args)
|
62
|
-
new_args = []
|
63
|
-
args.each do |a|
|
64
|
-
@args.select {|b| b[0] == (self.class._definitions[a] || "-#{a}")}.each do |b|
|
65
|
-
@args.delete(b)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
self.class.args = @args
|
69
|
-
self
|
70
|
-
end
|
71
|
-
|
72
|
-
def reset
|
73
|
-
@args = []
|
74
|
-
self.class.args = []
|
75
|
-
end
|
76
|
-
|
77
|
-
def build_args
|
78
|
-
return if @args.nil? or @args.empty?
|
79
|
-
@args.flatten.join(" ")
|
80
|
-
end
|
81
|
-
|
82
|
-
def command
|
83
|
-
"#{self.class._bin || self.class.to_s.downcase} #{build_args}".strip
|
84
|
-
end
|
85
|
-
|
86
|
-
def fire_command(&block)
|
87
|
-
puts command + " (timeout: #{self.class.timeout || -1}) + #{block_given?}" if $DEBUG
|
88
|
-
res = "" unless block_given?
|
89
|
-
IO.popen(command + " 3>&2 2>&1") do |pipe|
|
90
|
-
pipe.each("\r") do |line|
|
91
|
-
if block_given?
|
92
|
-
yield parse_output(line)
|
4
|
+
class << self
|
5
|
+
attr_accessor :args, :_definitions, :_bin, :timeout
|
6
|
+
|
7
|
+
def exec(*args, &block)
|
8
|
+
self.new.exec *args, &block
|
9
|
+
end
|
10
|
+
|
11
|
+
def options(&block)
|
12
|
+
self.new(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def bin(p)
|
16
|
+
@_bin = p.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def _timeout(t)
|
20
|
+
@timeout = t
|
21
|
+
end
|
22
|
+
|
23
|
+
def adef(name, option=nil, &block)
|
24
|
+
(@_definitions||={})[name.to_sym] = (option || Proc.new { self.instance_eval(&block).flatten })
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_missing(m, args=nil)
|
28
|
+
m = m.to_s.gsub("=", "")
|
29
|
+
@args ||= []
|
30
|
+
definition = @_definitions[m.to_sym] rescue nil
|
31
|
+
|
32
|
+
#puts "m: #{m}, def: #{definition.inspect} | args:#{args}"
|
33
|
+
if definition.is_a?(Proc)
|
34
|
+
definition.call
|
35
|
+
else
|
36
|
+
arg_name = "#{"-" if m[0..0] != "-"}#{m}"
|
37
|
+
arg_name = "-" + arg_name if arg_name.size > 2
|
38
|
+
|
39
|
+
if args.to_s.empty?
|
40
|
+
@args << [ definition || arg_name ]
|
93
41
|
else
|
94
|
-
|
42
|
+
q = ""
|
43
|
+
q = "'" if args.to_s[0..0] != "'"
|
44
|
+
@args << [ definition || arg_name , "#{q}#{args}#{q}" ]
|
95
45
|
end
|
96
46
|
end
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
attr_accessor :args
|
51
|
+
|
52
|
+
def initialize(&block)
|
53
|
+
self.class.instance_eval(&block) if block_given?
|
54
|
+
@args = self.class.args
|
55
|
+
self.class.args = []
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def options(&block)
|
60
|
+
self.class.args = @args
|
61
|
+
self.class.new(&block)
|
62
|
+
end
|
63
|
+
|
64
|
+
def remove(*args)
|
65
|
+
new_args = []
|
66
|
+
args.each do |a|
|
67
|
+
@args.select {|b| b[0] == (self.class._definitions[a] || "-#{a}")}.each do |b|
|
68
|
+
@args.delete(b)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
self.class.args = @args
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
def reset
|
76
|
+
@args = []
|
77
|
+
self.class.args = []
|
78
|
+
end
|
79
|
+
|
80
|
+
def build_args
|
81
|
+
return if @args.nil? or @args.empty?
|
82
|
+
@args.flatten.join(" ")
|
83
|
+
end
|
84
|
+
|
85
|
+
def command
|
86
|
+
"#{self.class._bin || self.class.to_s.downcase} #{build_args}".strip
|
87
|
+
end
|
88
|
+
|
89
|
+
def fire_command(&block)
|
90
|
+
puts command + " (timeout: #{self.class.timeout || -1}) + #{block_given?}" if $DEBUG
|
91
|
+
res = "" unless block_given?
|
92
|
+
IO.popen(command + " 3>&2 2>&1") do |pipe|
|
93
|
+
pipe.each("\r") do |line|
|
94
|
+
if block_given?
|
95
|
+
yield parse_output(line)
|
96
|
+
else
|
97
|
+
res << line
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
return (block_given? ? $? : parse_output(res))
|
102
|
+
end
|
103
|
+
|
104
|
+
def parse_output(output)
|
105
|
+
output
|
106
|
+
end
|
107
|
+
|
108
|
+
def method_missing(m, args=nil)
|
109
|
+
self.class.args = @args
|
110
|
+
self.class.method_missing(m, args)
|
111
|
+
@args = self.class.args
|
112
|
+
self
|
113
|
+
end
|
114
|
+
|
115
|
+
def inspect
|
116
|
+
"#<#{self.class.to_s} command=#{command} timeout=#{self.class.timeout || -1}>"
|
117
|
+
end
|
118
|
+
|
119
|
+
def exec(*args, &block)
|
120
|
+
if args.size > 0
|
121
|
+
i = 0
|
122
|
+
(args.size / 2).times do
|
123
|
+
self.class.send(args[i], args[i+1])
|
124
|
+
i+=2
|
125
|
+
end
|
126
|
+
|
127
|
+
@args = self.class.args
|
128
|
+
self.class.args = []
|
129
|
+
end
|
130
|
+
|
131
|
+
if self.class.timeout
|
132
|
+
Timeout::timeout(self.class.timeout) do
|
133
|
+
fire_command(&block)
|
134
|
+
end
|
135
|
+
else
|
136
|
+
fire_command(&block)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
data/test/coulis_test.rb
CHANGED
@@ -2,164 +2,170 @@ require "test/unit"
|
|
2
2
|
require "coulis"
|
3
3
|
|
4
4
|
class Ls < Coulis
|
5
|
-
|
6
|
-
|
5
|
+
adef :all, "-a"
|
6
|
+
adef :human, "-h"
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
adef :full do
|
9
|
+
all; human
|
10
|
+
end
|
11
11
|
end
|
12
12
|
|
13
13
|
class Ping < Coulis
|
14
|
-
|
15
|
-
|
14
|
+
bin `whereis ping`.strip
|
15
|
+
adef :count, "-c"
|
16
16
|
end
|
17
17
|
|
18
18
|
class NSLookup < Coulis
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
def parse_output(output)
|
20
|
+
output.split("\n").
|
21
|
+
map{|x| x.match(/Address: ([0-9\.]+)/)[1] rescue nil}.
|
22
|
+
compact
|
23
|
+
end
|
24
24
|
end
|
25
25
|
|
26
26
|
class SimpleCliTest < Test::Unit::TestCase
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
27
|
+
def teardown
|
28
|
+
Ls.new.reset
|
29
|
+
Ping.new.reset
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_default_bin
|
33
|
+
assert_equal Ls.new.command, "ls"
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_defined_bin
|
37
|
+
assert_equal Ping.new.command, `whereis ping`.strip
|
38
|
+
assert_equal Ping.new.command, Ping._bin
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_adef
|
42
|
+
assert_equal Ls._definitions[:all], "-a"
|
43
|
+
assert_equal Ls._definitions[:human], "-h"
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_adef_with_block
|
47
|
+
assert_instance_of Proc, Ls._definitions[:full]
|
48
|
+
assert_equal Ls._definitions[:full].call, ["-a", "-h"]
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_argument_added
|
52
|
+
ls = Ls.options { all }
|
53
|
+
assert true, ls.args[0] == "-a"
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_all_arguments_from_adef_added
|
57
|
+
ls = Ls.options { full }
|
58
|
+
assert_equal ls.args.size, 2
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_not_defined_short_argument
|
62
|
+
ls = Ls.options { s }
|
63
|
+
assert_equal ls.args.size, 1
|
64
|
+
assert_equal ls.args.to_s, "-s"
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_not_defined_long_argument
|
68
|
+
ls = Ls.options { color }
|
69
|
+
assert_equal ls.args.size, 1
|
70
|
+
assert_equal ls.args.to_s, "--color"
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_command
|
74
|
+
assert_equal Ls.options { full; s }.command, "ls -a -h -s"
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_add_options
|
78
|
+
ls = Ls.options { a }
|
79
|
+
assert_equal ls.args.flatten.size, 1
|
80
|
+
ls.options { l; h }
|
81
|
+
assert_equal ls.args.flatten.size, 3
|
82
|
+
|
83
|
+
assert_equal ls.command, "ls -a -l -h"
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_add_option
|
87
|
+
ls = Ls.new
|
88
|
+
ls.all
|
89
|
+
assert_equal ls.args.flatten.size, 1
|
90
|
+
assert_equal ls.command, "ls -a"
|
91
|
+
|
92
|
+
ls.l
|
93
|
+
assert_equal ls.args.flatten.size, 2
|
94
|
+
assert_equal ls.command, "ls -a -l"
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_add_option_with_args
|
98
|
+
ping = Ping.options {
|
99
|
+
@args << ["-c", 2] << ["google.com"]
|
100
|
+
}
|
101
|
+
|
102
|
+
assert_equal ping.command, "#{Ping._bin} -c 2 google.com"
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_remove_options
|
106
|
+
ls = Ls.options { a; l; h }
|
107
|
+
ls.remove :a, :h
|
108
|
+
assert_equal ls.command, "ls -l"
|
109
|
+
|
110
|
+
ls.reset
|
111
|
+
ls.all
|
112
|
+
ls.remove :all
|
113
|
+
assert_equal ls.command, "ls"
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_reset
|
117
|
+
ls = Ls.options { a; l; h }
|
118
|
+
assert_equal ls.command, "ls -a -l -h"
|
119
|
+
ls.reset
|
120
|
+
assert_equal ls.command, "ls"
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_exec
|
124
|
+
ls = Ls.options { full }.exec
|
125
|
+
|
126
|
+
assert_instance_of String, ls
|
127
|
+
assert true, ls.size > 0
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_exec_with_block
|
131
|
+
ls = Ls.options { full }
|
132
|
+
process = ls.exec do |out|
|
133
|
+
assert_instance_of String, out
|
134
|
+
assert true, out.size > 0
|
135
|
+
end
|
136
|
+
|
137
|
+
assert_instance_of Process::Status, process
|
138
|
+
assert_equal process.exitstatus, 0
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_timeout
|
142
|
+
assert_raise Timeout::Error do
|
143
|
+
Ping.options {
|
144
|
+
@args << ["google.com"]
|
145
|
+
_timeout 2
|
146
|
+
}.exec
|
147
|
+
end
|
148
|
+
assert_equal Ping.timeout, 2
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_stdout
|
152
|
+
res = ""
|
153
|
+
Ping.options {
|
154
|
+
count 2
|
155
|
+
@args << ["google.com"]
|
156
|
+
}.exec do |out|
|
157
|
+
res << out
|
158
|
+
end
|
159
|
+
|
160
|
+
assert true, res.size > 0
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_parse_output
|
164
|
+
NSLookup.options {
|
165
|
+
@args = ["google.com"]
|
166
|
+
}.exec do |ips|
|
167
|
+
assert_instance_of Array, ips
|
168
|
+
assert true, ips.size > 1
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Bruno Celeste
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-07-
|
17
|
+
date: 2011-07-23 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|