em-systemcommand 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.add_development_dependency 'rspec'
19
19
  gem.add_development_dependency 'guard'
20
20
  gem.add_development_dependency 'guard-rspec'
21
+ gem.add_development_dependency 'rake'
21
22
 
23
+ gem.add_dependency 'escape'
22
24
  gem.add_dependency 'eventmachine'
23
25
  end
@@ -3,6 +3,7 @@ require 'open3'
3
3
  require "em-systemcommand/version"
4
4
  require "em-systemcommand/pipe"
5
5
  require "em-systemcommand/pipe_handler"
6
+ require "em-systemcommand/builder"
6
7
 
7
8
  module EventMachine
8
9
  class SystemCommand
@@ -17,14 +18,36 @@ module EventMachine
17
18
 
18
19
  def initialize *args, &block
19
20
  @pipes = {}
21
+ @command = EM::SystemCommand::Builder.new *args
20
22
 
21
- stdin, stdout, stderr, @wait_thr = Open3.popen3 *args
23
+ @execution_proc = block
24
+ end
25
+
26
+ def self.execute *args, &block
27
+ sys_cmd = EM::SystemCommand.new *args, &block
28
+ sys_cmd.execute
29
+ end
30
+
31
+ # Executes the command
32
+ def execute &block
33
+ raise 'Previous process still exists' unless pipes.empty?
34
+
35
+ # clear callbacks
36
+ @callbacks = []
37
+ @errbacks = []
38
+
39
+ stdin, stdout, stderr, @wait_thr = Open3.popen3 @command.to_s
22
40
 
23
41
  @stdin = attach_pipe_handler :stdin, stdin
24
42
  @stdout = attach_pipe_handler :stdout, stdout
25
43
  @stderr = attach_pipe_handler :stderr, stderr
26
44
 
27
- yield self if block_given?
45
+ if block
46
+ block.call self
47
+ elsif @execution_proc
48
+ @execution_proc.call self
49
+ end
50
+ self
28
51
  end
29
52
 
30
53
  def pid
@@ -0,0 +1,65 @@
1
+ require 'escape'
2
+
3
+ module EventMachine
4
+ class SystemCommand
5
+ class Builder
6
+
7
+ def initialize *args
8
+ unless args.length > 0
9
+ raise "You have to provide at least one argument as command"
10
+ end
11
+ @arr = args
12
+ end
13
+
14
+ def add opt, val = nil
15
+ if opt.is_a?(Array)
16
+ opt.each do |element|
17
+ add *element
18
+ end
19
+ else
20
+ if val
21
+ @arr << [ opt, val ]
22
+ else
23
+ @arr << opt
24
+ end
25
+ end
26
+ self
27
+ end
28
+ alias :<< :add
29
+
30
+ ##
31
+ # Returns the command string
32
+ def to_s
33
+ cmd = @arr.shift
34
+ @arr.each do |arg|
35
+ if arg.is_a?(Array)
36
+ param = arg.shift
37
+ param = param.to_s if param.is_a?(Symbol)
38
+ if param =~ /^\-{1,2}(.*)/
39
+ param = $1
40
+ end
41
+ value = arg.shift
42
+ if param.length == 1
43
+ cmd << ' ' << "-#{param} #{Escape.shell_single_word(value)}"
44
+ else
45
+ cmd << ' ' << "--#{param}=#{Escape.shell_single_word(value)}"
46
+ end
47
+ elsif arg.is_a?(Symbol)
48
+ arg = arg.to_s
49
+ if arg.length == 1
50
+ cmd << ' ' << "-#{arg}"
51
+ else
52
+ cmd << ' ' << "--#{arg}"
53
+ end
54
+ elsif arg.strip =~ /^\-/
55
+ cmd << ' ' << arg
56
+ else
57
+ cmd << ' ' << Escape.shell_single_word(arg)
58
+ end
59
+ end
60
+ cmd
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -14,7 +14,6 @@ module EventMachine
14
14
  end
15
15
 
16
16
  module ClassMethods
17
-
18
17
  def pipe_handler name, klass
19
18
  pipe_handlers[name] = klass
20
19
  end
@@ -30,7 +29,6 @@ module EventMachine
30
29
  stderr: EM::SystemCommand::Pipe
31
30
  }
32
31
  end
33
-
34
32
  end
35
33
  end
36
34
  end
@@ -1,5 +1,5 @@
1
1
  module Em
2
2
  module Systemcommand
3
- VERSION = "0.0.5"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,91 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe EM::SystemCommand::Builder do
5
+ before :all do
6
+ @cmd = EM::SystemCommand::Builder.new 'ffmpeg'
7
+ end
8
+
9
+ describe '.new' do
10
+ it 'should take multiple arguments' do
11
+ b = EM::SystemCommand::Builder.new 'echo', 'Irgendwas'
12
+ b.to_s.should == 'echo Irgendwas'
13
+ end
14
+ end
15
+
16
+
17
+ describe '#add' do
18
+ before :each do
19
+ @cmd = EM::SystemCommand::Builder.new 'echo'
20
+ end
21
+
22
+ it 'should add options' do
23
+ @cmd << :n
24
+ @cmd.to_s.should == 'echo -n'
25
+ end
26
+
27
+ it 'should add parameters' do
28
+ @cmd.add :creative, 'not'
29
+ @cmd.add :a, 'b'
30
+ @cmd.add '--something', 'weird$'
31
+ @cmd.to_s.should == "echo --creative=not -a b --something='weird$'"
32
+ end
33
+
34
+ it 'should add arguments' do
35
+ @cmd << "A long text. It´s got some $pecial Characters like \" or '. Yeah, so be it"
36
+ @cmd.to_s.should == "echo 'A long text. It´s got some $pecial Characters like \" or '\\''. Yeah, so be it'"
37
+ end
38
+
39
+ it 'should add an array' do
40
+ @cmd << [
41
+ [:foo, 'bar'],
42
+ '-a',
43
+ :b,
44
+ 'Something'
45
+ ]
46
+ @cmd.to_s.should == 'echo --foo=bar -a -b Something'
47
+ end
48
+
49
+ it 'should be chainable' do
50
+ @cmd << :p << 'Something else'
51
+ @cmd.to_s.should == "echo -p 'Something else'"
52
+ end
53
+ end
54
+
55
+ describe '#to_s' do
56
+ it 'should escape argument strings' do
57
+ EM::SystemCommand::Builder.new('echo', 'Irgendwas"$').to_s.
58
+ should == "echo 'Irgendwas\"$'"
59
+ end
60
+
61
+ it 'should not escape symbol options' do
62
+ EM::SystemCommand::Builder.new('echo', :n, 'Irgendwas"$').to_s.
63
+ should == "echo -n 'Irgendwas\"$'"
64
+ end
65
+
66
+ it 'should not escape symbol long parameter names' do
67
+ EM::SystemCommand::Builder.new('echo', [:creative, 'not$'], 'Irgendwas"$').to_s.
68
+ should == "echo --creative='not$' 'Irgendwas\"$'"
69
+ end
70
+
71
+ it 'should not escape symbol short parameter names' do
72
+ EM::SystemCommand::Builder.new('echo', [:a, 'not$'], 'Irgendwas"$').to_s.
73
+ should == "echo -a 'not$' 'Irgendwas\"$'"
74
+ end
75
+
76
+ it 'should not escape long parameters' do
77
+ EM::SystemCommand::Builder.new('echo', ['--creative', 'not$'], 'Irgendwas"$').to_s.
78
+ should == "echo --creative='not$' 'Irgendwas\"$'"
79
+ end
80
+
81
+ it 'should not escape short parameters' do
82
+ EM::SystemCommand::Builder.new('echo', ['-a', 'not$'], 'Irgendwas"$').to_s.
83
+ should == "echo -a 'not$' 'Irgendwas\"$'"
84
+ end
85
+
86
+ it 'should not escape arguments' do
87
+ EM::SystemCommand::Builder.new('echo', '-n', 'Irgendwas"$').to_s.
88
+ should == "echo -n 'Irgendwas\"$'"
89
+ end
90
+ end
91
+ end
@@ -5,7 +5,7 @@ describe 'Pipe' do
5
5
  context '#receive_data' do
6
6
  it 'should correctly do a carriage return in the output buffer' do
7
7
  EM.run do
8
- EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "12345\r"; puts "321"; print "123"; print "\r3210"; exit 0;'} do |on|
8
+ EM::SystemCommand.execute %q{ruby -e '$stdout.sync = true; print "12345\r"; puts "321"; print "123"; print "\r3210"; exit 0;'} do |on|
9
9
  on.success do |process|
10
10
  EM.stop_event_loop
11
11
  process.stdout.output.should == "32145\n3210"
@@ -20,7 +20,7 @@ describe 'Pipe' do
20
20
  it 'should be called on data' do
21
21
  received = []
22
22
  EM.run do
23
- EM::SystemCommand.new %q{echo -n "123123";} do |on|
23
+ EM::SystemCommand.execute %q{echo -n "123123";} do |on|
24
24
  on.stdout.data do |data|
25
25
  received << data
26
26
  end
@@ -36,7 +36,7 @@ describe 'Pipe' do
36
36
  it 'should be called once even when there is a linebreak' do
37
37
  received = []
38
38
  EM.run do
39
- EM::SystemCommand.new %Q{echo "123\n456"} do |on|
39
+ EM::SystemCommand.execute %Q{echo "123\n456"} do |on|
40
40
  on.stdout.data do |data|
41
41
  received << data
42
42
  end
@@ -55,7 +55,7 @@ describe 'Pipe' do
55
55
  it 'should be called on readline' do
56
56
  received = []
57
57
  EM.run do
58
- EM::SystemCommand.new 'echo "123"; echo "456"' do |on|
58
+ EM::SystemCommand.execute 'echo "123"; echo "456"' do |on|
59
59
  on.stdout.line do |data|
60
60
  received << data
61
61
  end
@@ -71,7 +71,7 @@ describe 'Pipe' do
71
71
  it 'should be called on carriage return' do
72
72
  received = []
73
73
  EM.run do
74
- EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "123\r"; print "456\r"; exit 0;'} do |on|
74
+ EM::SystemCommand.execute %q{ruby -e '$stdout.sync = true; print "123\r"; print "456\r"; exit 0;'} do |on|
75
75
  on.stdout.line do |data|
76
76
  received << data
77
77
  end
@@ -91,7 +91,7 @@ describe 'Pipe' do
91
91
  it 'should be called on receive data' do
92
92
  received = []
93
93
  EM.run do
94
- EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; puts "123"; puts "456"; exit 0'} do |on|
94
+ EM::SystemCommand.execute %q{ruby -e '$stdout.sync = true; puts "123"; puts "456"; exit 0'} do |on|
95
95
  on.stdout.update do |data|
96
96
  received << data
97
97
  end
@@ -107,7 +107,7 @@ describe 'Pipe' do
107
107
  it 'should be called on carriage return'do
108
108
  received = []
109
109
  EM.run do
110
- EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "123\r"; sleep 0.1; print "456\r"; exit 0;'} do |on|
110
+ EM::SystemCommand.execute %q{ruby -e '$stdout.sync = true; print "123\r"; sleep 0.1; print "456\r"; exit 0;'} do |on|
111
111
  on.stdout.update do |data|
112
112
  received << data
113
113
  end
@@ -127,7 +127,7 @@ describe 'Pipe' do
127
127
  it 'should match in lines on receive_line' do
128
128
  received = []
129
129
  EM.run do
130
- EM::SystemCommand.new 'echo "-123-"; echo "-456-"' do |on|
130
+ EM::SystemCommand.execute 'echo "-123-"; echo "-456-"' do |on|
131
131
  on.stdout.match /-([0-9]+)-/, in: :line do |match, number|
132
132
  received << number
133
133
  end
@@ -143,7 +143,7 @@ describe 'Pipe' do
143
143
  it 'should match in output buffer on receive_update' do
144
144
  received = []
145
145
  EM.run do
146
- EM::SystemCommand.new %q{ruby -e '$stdout.sync = true; print "-123-\r"; sleep 0.1; print "-456-\r"; exit 0'} do |on|
146
+ EM::SystemCommand.execute %q{ruby -e '$stdout.sync = true; print "-123-\r"; sleep 0.1; print "-456-\r"; exit 0'} do |on|
147
147
  on.stdout.match /-([0-9]+)-/, in: :output do |match, number|
148
148
  received << number
149
149
  end
@@ -2,10 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe EM::SystemCommand do
4
4
 
5
- it 'should call a success callback when process succeeds' do
5
+ it 'should call a success callback when process succeeds' do
6
6
  called = false
7
7
  EM.run do
8
- EM::SystemCommand.new 'exit 0;' do |on|
8
+ EM::SystemCommand.execute 'exit 0;' do |on|
9
9
  on.success do |ps|
10
10
  called = true
11
11
  end
@@ -19,11 +19,15 @@ describe EM::SystemCommand do
19
19
 
20
20
  it 'should take a success callback with process as parameter' do
21
21
  EM.run do
22
- EM::SystemCommand.new 'exit 0;' do |on|
22
+ EM::SystemCommand.execute 'exit 0;' do |on|
23
23
  on.success do |ps|
24
24
  EM.stop_event_loop
25
25
  ps.should be_a EM::SystemCommand
26
26
  end
27
+
28
+ on.failure do |ps|
29
+ EM.stop_event_loop
30
+ end
27
31
  end
28
32
  end
29
33
  end
@@ -31,7 +35,7 @@ describe EM::SystemCommand do
31
35
  it 'should call a failure callback when process fails' do
32
36
  called = false
33
37
  EM.run do
34
- EM::SystemCommand.new 'echo "123"; exit 1;' do |on|
38
+ EM::SystemCommand.execute 'echo "123"; exit 1;' do |on|
35
39
  on.failure do |ps|
36
40
  called = true
37
41
  end
@@ -45,7 +49,7 @@ describe EM::SystemCommand do
45
49
 
46
50
  it 'should take a failure callback with process as parameter' do
47
51
  EM.run do
48
- EM::SystemCommand.new 'exit 1;' do |on|
52
+ EM::SystemCommand.execute 'exit 1;' do |on|
49
53
  on.failure do |ps|
50
54
  EM.stop_event_loop
51
55
  ps.should be_a EM::SystemCommand
@@ -56,7 +60,7 @@ describe EM::SystemCommand do
56
60
 
57
61
  it 'should have stdin pipe' do
58
62
  EM.run do
59
- ps = EM::SystemCommand.new 'echo "123"; exit 1;'
63
+ ps = EM::SystemCommand.execute 'echo "123"; exit 1;'
60
64
  ps.stdin.should be_a EM::SystemCommand::Pipe
61
65
  EM.stop_event_loop
62
66
  end
@@ -64,7 +68,7 @@ describe EM::SystemCommand do
64
68
 
65
69
  it 'should have stdout pipe' do
66
70
  EM.run do
67
- ps = EM::SystemCommand.new 'echo "123"; exit 1;'
71
+ ps = EM::SystemCommand.execute 'echo "123"; exit 1;'
68
72
  ps.stdout.should be_a EM::SystemCommand::Pipe
69
73
  EM.stop_event_loop
70
74
  end
@@ -72,7 +76,7 @@ describe EM::SystemCommand do
72
76
 
73
77
  it 'should have stderr pipe' do
74
78
  EM.run do
75
- ps = EM::SystemCommand.new 'echo "123"; exit 1;'
79
+ ps = EM::SystemCommand.execute 'echo "123"; exit 1;'
76
80
  ps.stderr.should be_a EM::SystemCommand::Pipe
77
81
  EM.stop_event_loop
78
82
  end
@@ -90,7 +94,5 @@ describe EM::SystemCommand do
90
94
  stderr: EM::SystemCommand::Pipe
91
95
  }
92
96
  end
93
-
94
97
  end
95
-
96
98
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-systemcommand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-14 00:00:00.000000000 Z
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &18462820 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *18462820
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: guard
27
- requirement: &18461760 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,31 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *18461760
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: guard-rspec
38
- requirement: &18460700 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
39
65
  none: false
40
66
  requirements:
41
67
  - - ! '>='
@@ -43,10 +69,31 @@ dependencies:
43
69
  version: '0'
44
70
  type: :development
45
71
  prerelease: false
46
- version_requirements: *18460700
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: escape
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
47
94
  - !ruby/object:Gem::Dependency
48
95
  name: eventmachine
49
- requirement: &18477240 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
50
97
  none: false
51
98
  requirements:
52
99
  - - ! '>='
@@ -54,7 +101,12 @@ dependencies:
54
101
  version: '0'
55
102
  type: :runtime
56
103
  prerelease: false
57
- version_requirements: *18477240
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
58
110
  description: ! '`em-systemcommand` is a simple abstraction for invoking system commands
59
111
  and handling their output easily with eventmachine.'
60
112
  email:
@@ -73,9 +125,11 @@ files:
73
125
  - Rakefile
74
126
  - em-systemcommand.gemspec
75
127
  - lib/em-systemcommand.rb
128
+ - lib/em-systemcommand/builder.rb
76
129
  - lib/em-systemcommand/pipe.rb
77
130
  - lib/em-systemcommand/pipe_handler.rb
78
131
  - lib/em-systemcommand/version.rb
132
+ - spec/em-systemcommand/builder_spec.rb
79
133
  - spec/em-systemcommand/pipe_spec.rb
80
134
  - spec/em-systmcommand_spec.rb
81
135
  - spec/spec_helper.rb
@@ -91,19 +145,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
145
  - - ! '>='
92
146
  - !ruby/object:Gem::Version
93
147
  version: '0'
148
+ segments:
149
+ - 0
150
+ hash: 1884745858443868072
94
151
  required_rubygems_version: !ruby/object:Gem::Requirement
95
152
  none: false
96
153
  requirements:
97
154
  - - ! '>='
98
155
  - !ruby/object:Gem::Version
99
156
  version: '0'
157
+ segments:
158
+ - 0
159
+ hash: 1884745858443868072
100
160
  requirements: []
101
161
  rubyforge_project:
102
- rubygems_version: 1.8.17
162
+ rubygems_version: 1.8.24
103
163
  signing_key:
104
164
  specification_version: 3
105
165
  summary: Sweeter popen3 for EventMachine.
106
166
  test_files:
167
+ - spec/em-systemcommand/builder_spec.rb
107
168
  - spec/em-systemcommand/pipe_spec.rb
108
169
  - spec/em-systmcommand_spec.rb
109
170
  - spec/spec_helper.rb