right_popen 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +8 -3
- data/lib/linux/right_popen.rb +22 -9
- data/right_popen.gemspec +14 -8
- data/spec/print_env.rb +2 -1
- data/spec/right_popen_spec.rb +24 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -41,8 +41,13 @@ to report issues.
|
|
41
41
|
|
42
42
|
EM.run do
|
43
43
|
EM.next_tick do
|
44
|
-
|
45
|
-
RightScale.popen3(
|
44
|
+
command = "ruby -e \"puts 'some stdout text'; $stderr.puts 'some stderr text'\; exit 99\""
|
45
|
+
RightScale.popen3(:command => command,
|
46
|
+
:target => self,
|
47
|
+
:environment => nil,
|
48
|
+
:stdout_handler => :on_read_stdout,
|
49
|
+
:stderr_handler => :on_read_stderr,
|
50
|
+
:exit_handler => :on_exit)
|
46
51
|
end
|
47
52
|
timer = EM::PeriodicTimer.new(0.1) do
|
48
53
|
if @exit_status
|
@@ -90,7 +95,7 @@ The build can be tested using the RSpec gem. Create a link to the installed
|
|
90
95
|
under Windows) and run the following command from the gem directory to execute
|
91
96
|
the RightPopen tests:
|
92
97
|
|
93
|
-
|
98
|
+
rake spec
|
94
99
|
|
95
100
|
|
96
101
|
== LICENSE
|
data/lib/linux/right_popen.rb
CHANGED
@@ -34,16 +34,18 @@ module RightScale
|
|
34
34
|
module StdOutHandler
|
35
35
|
|
36
36
|
# === Parameters
|
37
|
-
# target(Object):: Object defining handler methods to be called.
|
38
|
-
# stdout_handler(String):: Token for stdout handler method name.
|
39
|
-
# exit_handler(String):: Token for exit handler method name.
|
37
|
+
# options[:target](Object):: Object defining handler methods to be called.
|
38
|
+
# options[:stdout_handler(String):: Token for stdout handler method name.
|
39
|
+
# options[:exit_handler(String):: Token for exit handler method name.
|
40
|
+
# options[:exec_file](String):: Path to executed file
|
40
41
|
# stderr_eventable(Connector):: EM object representing stderr handler.
|
41
42
|
# read_fd(IO):: Standard output read file descriptor.
|
42
43
|
# write_fd(IO):: Standard output write file descriptor.
|
43
|
-
def initialize(
|
44
|
-
@target = target
|
45
|
-
@stdout_handler = stdout_handler
|
46
|
-
@exit_handler = exit_handler
|
44
|
+
def initialize(options, stderr_eventable, read_fd, write_fd)
|
45
|
+
@target = options[:target]
|
46
|
+
@stdout_handler = options[:stdout_handler]
|
47
|
+
@exit_handler = options[:exit_handler]
|
48
|
+
@exec_file = options[:exec_file]
|
47
49
|
@stderr_eventable = stderr_eventable
|
48
50
|
# Just so they don't get GCed before the process goes away
|
49
51
|
@read_fd = read_fd
|
@@ -59,6 +61,7 @@ module RightScale
|
|
59
61
|
def unbind
|
60
62
|
# We force the attached stderr handler to go away so that
|
61
63
|
# we don't end up with a broken pipe
|
64
|
+
File.delete(@exec_file) if File.file?(@exec_file)
|
62
65
|
@stderr_eventable.force_detach if @stderr_eventable
|
63
66
|
@target.method(@exit_handler).call(get_status) if @exit_handler
|
64
67
|
end
|
@@ -99,9 +102,19 @@ module RightScale
|
|
99
102
|
# Forks process to run given command asynchronously, hooking all three
|
100
103
|
# standard streams of the child process.
|
101
104
|
#
|
105
|
+
# === Parameters
|
106
|
+
# options[:temp_dir]:: Path to temporary directory where executable files are
|
107
|
+
# created, default to /tmp if not specified
|
108
|
+
#
|
102
109
|
# See RightScale.popen3
|
103
110
|
def self.popen3_imp(options)
|
104
|
-
|
111
|
+
# First write command to file so that it's possible to use popen3 with
|
112
|
+
# a bash command line (e.g. 'for i in 1 2 3 4 5; ...')
|
113
|
+
temp_dir = options[:temp_dir] || '/tmp'
|
114
|
+
exec_file = File.join(temp_dir, Time.new.to_i.to_s)
|
115
|
+
options[:exec_file] = exec_file
|
116
|
+
File.open(exec_file, 'w') { |f| f.puts options[:command] }
|
117
|
+
File.chmod(0700, exec_file)
|
105
118
|
GC.start # To garbage collect open file descriptors from passed executions
|
106
119
|
EM.next_tick do
|
107
120
|
saved_stderr = $stderr.dup
|
@@ -120,7 +133,7 @@ module RightScale
|
|
120
133
|
end
|
121
134
|
|
122
135
|
# Launch child process
|
123
|
-
EM.popen(
|
136
|
+
EM.popen(exec_file, StdOutHandler, options, c, r, w)
|
124
137
|
|
125
138
|
# Restore environment variables
|
126
139
|
unless envs.empty?
|
data/right_popen.gemspec
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
def is_windows?
|
4
|
+
return RUBY_PLATFORM =~ /mswin/
|
5
|
+
end
|
5
6
|
|
7
|
+
spec = Gem::Specification.new do |spec|
|
6
8
|
spec.name = 'right_popen'
|
7
|
-
spec.version = '1.0.
|
9
|
+
spec.version = '1.0.6'
|
8
10
|
spec.authors = ['Scott Messier', 'Raphael Simon']
|
9
11
|
spec.email = 'scott@rightscale.com'
|
10
12
|
spec.homepage = 'https://github.com/rightscale/right_popen'
|
11
|
-
if is_windows
|
13
|
+
if is_windows?
|
12
14
|
spec.platform = 'x86-mswin32-60'
|
13
15
|
else
|
14
16
|
spec.platform = Gem::Platform::RUBY
|
@@ -27,7 +29,7 @@ of its internal mechanisms. The Linux implementation is valid for any Linux
|
|
27
29
|
platform but there is also a native implementation for Windows platforms.
|
28
30
|
EOF
|
29
31
|
|
30
|
-
if is_windows
|
32
|
+
if is_windows?
|
31
33
|
extension_dir = "ext,"
|
32
34
|
else
|
33
35
|
extension_dir = ""
|
@@ -38,7 +40,7 @@ EOF
|
|
38
40
|
item.include?("Makefile") || item.include?(".obj") || item.include?(".pdb") || item.include?(".def") || item.include?(".exp") || item.include?(".lib")
|
39
41
|
end
|
40
42
|
candidates = candidates.delete_if do |item|
|
41
|
-
if is_windows
|
43
|
+
if is_windows?
|
42
44
|
item.include?("/linux/")
|
43
45
|
else
|
44
46
|
item.include?("/win32/")
|
@@ -46,9 +48,13 @@ EOF
|
|
46
48
|
end
|
47
49
|
spec.files = candidates.sort!
|
48
50
|
|
49
|
-
# Current implementation supports
|
51
|
+
# Current implementation supports >= 0.12.8
|
50
52
|
spec.add_runtime_dependency(%q<eventmachine>, [">= 0.12.8"])
|
51
|
-
if is_windows
|
53
|
+
if is_windows?
|
54
|
+
# Windows implementation currently depends on deprecated behavior from
|
55
|
+
# 0.12.8, but we also need to support the 0.12.8.1 patch version. the Linux
|
56
|
+
# side is free to use 0.12.10+
|
57
|
+
spec.add_runtime_dependency(%q<eventmachine>, ["< 0.12.9"])
|
52
58
|
spec.add_runtime_dependency(%q<win32-process>, [">= 0.6.1"])
|
53
59
|
end
|
54
60
|
end
|
data/spec/print_env.rb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
puts "__test__=#{ENV['__test__']}" if ENV['__test__']
|
2
|
+
puts "PATH=#{ENV['PATH']}"
|
data/spec/right_popen_spec.rb
CHANGED
@@ -12,6 +12,10 @@ LARGE_OUTPUT_COUNTER = 1000
|
|
12
12
|
# bump up count for most exhaustive leak detection.
|
13
13
|
REPEAT_TEST_COUNTER = 256
|
14
14
|
|
15
|
+
def is_windows?
|
16
|
+
return RUBY_PLATFORM =~ /mswin/
|
17
|
+
end
|
18
|
+
|
15
19
|
describe 'RightScale::popen3' do
|
16
20
|
|
17
21
|
module RightPopenSpec
|
@@ -191,6 +195,26 @@ describe 'RightScale::popen3' do
|
|
191
195
|
old_envs.each { |k, v| ENV[k].should == v }
|
192
196
|
end
|
193
197
|
|
198
|
+
if is_windows?
|
199
|
+
# FIX: this behavior is currently specific to Windows but should probably be
|
200
|
+
# implemented for Linux.
|
201
|
+
it 'should merge the PATH variable instead of overriding it' do
|
202
|
+
command = "\"#{RUBY_CMD}\" \"#{File.expand_path(File.join(File.dirname(__FILE__), 'print_env.rb'))}\""
|
203
|
+
runner = RightPopenSpec::Runner.new
|
204
|
+
runner.run_right_popen(command, 'PATH' => "c:/bogus\\bin")
|
205
|
+
runner.status.exitstatus.should == 0
|
206
|
+
runner.output_text.should include('PATH=c:\\bogus\\bin;')
|
207
|
+
end
|
208
|
+
else
|
209
|
+
it 'should allow running bash command lines starting with a built-in command' do
|
210
|
+
command = "for i in 1 2 3 4 5; do echo $i;done"
|
211
|
+
runner = RightPopenSpec::Runner.new
|
212
|
+
runner.run_right_popen(command)
|
213
|
+
runner.status.exitstatus.should == 0
|
214
|
+
runner.output_text.should == "1\n2\n3\n4\n5\n"
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
194
218
|
it 'should run repeatedly without leaking resources' do
|
195
219
|
command = "\"#{RUBY_CMD}\" \"#{File.expand_path(File.join(File.dirname(__FILE__), 'produce_output.rb'))}\" \"#{STANDARD_MESSAGE}\" \"#{ERROR_MESSAGE}\""
|
196
220
|
runner = RightPopenSpec::Runner.new
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: right_popen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Messier
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2010-
|
13
|
+
date: 2010-03-18 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|