lib/systemu.rb in systemu-1.2.0 vs lib/systemu.rb in systemu-2.2.0
- old
+ new
@@ -12,11 +12,12 @@
class SystemUniversal
#
# constants
#
- SystemUniversal::VERSION = '1.2.0' unless defined? SystemUniversal::VERSION
+ SystemUniversal::VERSION = '2.2.0' unless SystemUniversal.send(:const_defined?, :VERSION)
+ def SystemUniversal.version() SystemUniversal::VERSION end
def version() SystemUniversal::VERSION end
#
# class methods
#
@@ -31,12 +32,16 @@
ruby
else
system('%s -e 42' % 'ruby') ? 'ruby' : warn('no ruby in PATH/CONFIG')
end
- class << self
+ class << SystemUniversal
%w( host ppid pid ruby turd ).each{|a| attr_accessor a}
+
+ def quote(*words)
+ words.map{|word| word.inspect}.join(' ')
+ end
end
#
# instance methods
#
@@ -66,11 +71,11 @@
begin
thread = nil
quietly{
- IO.popen "#{ @ruby } #{ c['program'] }", 'r+' do |pipe|
+ IO.popen "#{ quote(@ruby) } #{ quote(c['program']) }", 'r+' do |pipe|
line = pipe.gets
case line
when %r/^pid: \d+$/
cid = Integer line[%r/\d+/]
else
@@ -110,10 +115,14 @@
[status, IO.read(c['stdout']), IO.read(c['stderr'])]
end
end
end
+ def quote *args, &block
+ SystemUniversal.quote(*args, &block)
+ end
+
def new_thread cid, block
q = Queue.new
Thread.new(cid) do |cid|
current = Thread.current
current.abort_on_exception = true
@@ -198,11 +207,15 @@
def relay srcdst
src, dst, ignored = srcdst.to_a.first
if src.respond_to? 'read'
while((buf = src.read(8192))); dst << buf; end
else
- src.each{|buf| dst << buf}
+ if src.respond_to?(:each_line)
+ src.each_line{|buf| dst << buf}
+ else
+ src.each{|buf| dst << buf}
+ end
end
end
def tmpdir d = Dir.tmpdir, max = 42, &b
i = -1 and loop{
@@ -232,22 +245,67 @@
end
def getopts opts = {}
lambda do |*args|
keys, default, ignored = args
- catch('opt') do
+ catch(:opt) do
[keys].flatten.each do |key|
[key, key.to_s, key.to_s.intern].each do |key|
- throw 'opt', opts[key] if opts.has_key?(key)
+ throw :opt, opts[key] if opts.has_key?(key)
end
end
default
end
end
end
end
+# some monkeypatching for JRuby
+if defined? JRUBY_VERSION
+ require 'jruby'
+ import org.jruby.RubyProcess
+
+ class SystemUniversal
+ def systemu
+ split_argv = JRuby::PathHelper.smart_split_command @argv
+ process = java.lang.Runtime.runtime.exec split_argv.to_java(:string)
+
+ stdout, stderr = [process.input_stream, process.error_stream].map do |stream|
+ StreamReader.new(stream)
+ end
+
+ exit_code = process.wait_for
+ [
+ RubyProcess::RubyStatus.new_process_status(JRuby.runtime, exit_code),
+ stdout.join,
+ stderr.join
+ ]
+ end
+
+ class StreamReader
+ def initialize(stream)
+ @data = ""
+ @thread = Thread.new do
+ reader = java.io.BufferedReader.new java.io.InputStreamReader.new(stream)
+
+ while line = reader.read_line
+ @data << line << "\n"
+ end
+ end
+ end
+
+ def join
+ @thread.join
+ @data
+ end
+ end
+ end
+end
+
+
+
SystemU = SystemUniversal unless defined? SystemU
+Systemu = SystemUniversal unless defined? Systemu