dotanuki 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/dotanuki.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency "popen4"
22
+ s.add_dependency "posix-spawn"
23
23
 
24
24
  s.add_development_dependency "rspec"
25
25
  s.add_development_dependency "metric_fu"
data/lib/dotanuki.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'popen4'
1
+ require 'posix/spawn'
2
2
 
3
3
  # Module intented to be included into classes which execute system commands
4
4
  # @author Martin Englund
@@ -90,6 +90,7 @@ module Dotanuki
90
90
  # execute "ls /does/not/exist"
91
91
  # end
92
92
  # @note this method isn't thread safe
93
+ # @todo pass an environment too
93
94
  def guard(options={}, &block)
94
95
  opts = @defaults.merge(options)
95
96
  validate_options(opts)
@@ -108,48 +109,41 @@ module Dotanuki
108
109
  # @param [String, Array] commands string or array containing the command to be executed
109
110
  # @param [Hash] options (see #guard)
110
111
  # @return [ExecResult]
112
+ # @todo pass an environment too
111
113
  def execute(commands, options={})
112
114
  validate_options(options)
113
115
 
114
116
  result = ExecResult.new
115
117
 
116
118
  [commands].flatten.each do |command|
117
- stdout, stderr, exit_status = _execute(command)
118
- result.add(stdout, stderr, exit_status)
119
- if options[:on_error] == :exception || @guard
120
- if exit_status.nil?
119
+ begin
120
+ child = POSIX::Spawn::Child.new(command)
121
+ stdout = child.out.strip
122
+ stderr = child.err.strip
123
+ result.add(stdout, stderr, child.status.exitstatus)
124
+
125
+ if child.status.exitstatus != 0
126
+ if options[:on_error] == :exception || @guard
127
+ @guard << result if @guard
128
+ raise ExecError, stderr
129
+ end
130
+ break
131
+ end
132
+ rescue Errno::ENOENT
133
+ result.add(nil, nil, nil)
134
+ if options[:on_error] == :exception
121
135
  @guard << result if @guard
122
136
  raise ExecError, "#{command}: command not found"
123
- elsif exit_status != 0
124
- @guard << result if @guard
125
- raise ExecError, stderr
126
137
  end
127
- elsif exit_status.nil? || exit_status != 0
128
138
  break
129
139
  end
130
140
  end
141
+
131
142
  @guard << result if @guard
132
143
 
133
144
  return result
134
145
  end
135
146
 
136
- # Execute a single command
137
- #
138
- # @param [String] command string containing the command to be executed
139
- # @return [String, String, Fixnum] standard out, standard error and exit
140
- # status of the command
141
- def _execute(command)
142
- stdout = stderr = ""
143
-
144
- status =
145
- POpen4::popen4(command) do |out, err, stdin, pid|
146
- stdout = out.read.chomp
147
- stderr = err.read.chomp
148
- end
149
-
150
- return stdout, stderr, status ? status.exitstatus : status
151
- end
152
-
153
147
  # Validates options for Dotanuki#execute or Dotanuki#guard
154
148
  #
155
149
  # @raise [ArgumentError] if an unknown option is given
@@ -1,4 +1,4 @@
1
1
  module Dotanuki
2
2
  # gem version
3
- VERSION = "0.0.4"
3
+ VERSION = "0.1.0"
4
4
  end
@@ -110,10 +110,6 @@ describe Dotanuki do
110
110
  r.stderr.should == [""]
111
111
  end
112
112
 
113
- it "should execute a single command" do
114
- _execute("echo 'foo'").should == ["foo", "", 0]
115
- end
116
-
117
113
  describe "with exception option should throw an exception" do
118
114
  it "on missing command" do
119
115
  lambda { execute(COMMAND_NOT_FOUND, {:on_error => :exception}) }.should raise_error Dotanuki::ExecError, "#{NON_EXISTING_PATH}: command not found"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotanuki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-02 00:00:00.000000000 -07:00
12
+ date: 2011-06-06 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: popen4
17
- requirement: &2161180600 !ruby/object:Gem::Requirement
16
+ name: posix-spawn
17
+ requirement: &2152082320 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2161180600
25
+ version_requirements: *2152082320
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &2161180020 !ruby/object:Gem::Requirement
28
+ requirement: &2151846680 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2161180020
36
+ version_requirements: *2151846680
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: metric_fu
39
- requirement: &2161179480 !ruby/object:Gem::Requirement
39
+ requirement: &2151810360 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,7 +44,7 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2161179480
47
+ version_requirements: *2151810360
48
48
  description: A command executioner which doesn't blindly stumble on when a command
49
49
  fails'
50
50
  email: