2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# shell/system-command.rb -
|
2011-05-18 20:07:25 -04:00
|
|
|
# $Release Version: 0.7 $
|
|
|
|
# $Revision$
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
require "shell/filter"
|
|
|
|
|
|
|
|
class Shell
|
|
|
|
class SystemCommand < Filter
|
|
|
|
def initialize(sh, command, *opts)
|
2002-10-02 12:45:35 -04:00
|
|
|
if t = opts.find{|opt| !opt.kind_of?(String) && opt.class}
|
2017-06-22 06:56:49 -04:00
|
|
|
Shell.Fail TypeError, t.class, "String"
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
super(sh)
|
|
|
|
@command = command
|
|
|
|
@opts = opts
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2016-08-30 02:22:30 -04:00
|
|
|
@input_queue = Thread::Queue.new
|
2001-05-17 06:02:47 -04:00
|
|
|
@pid = nil
|
|
|
|
|
|
|
|
sh.process_controller.add_schedule(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :command
|
|
|
|
alias name command
|
|
|
|
|
|
|
|
def wait?
|
|
|
|
@shell.process_controller.waiting_job?(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
|
|
|
@shell.process_controller.active_job?(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def input=(inp)
|
|
|
|
super
|
|
|
|
if active?
|
2011-05-18 17:19:18 -04:00
|
|
|
start_export
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
2007-03-22 11:25:58 -04:00
|
|
|
notify([@command, *@opts].join(" "))
|
|
|
|
|
2001-05-17 06:02:47 -04:00
|
|
|
@pid, @pipe_in, @pipe_out = @shell.process_controller.sfork(self) {
|
2011-05-18 17:19:18 -04:00
|
|
|
Dir.chdir @shell.pwd
|
|
|
|
$0 = @command
|
|
|
|
exec(@command, *@opts)
|
2001-05-17 06:02:47 -04:00
|
|
|
}
|
|
|
|
if @input
|
2011-05-18 17:19:18 -04:00
|
|
|
start_export
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
start_import
|
|
|
|
end
|
|
|
|
|
|
|
|
def flush
|
|
|
|
@pipe_out.flush if @pipe_out and !@pipe_out.closed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def terminate
|
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
@pipe_in.close
|
2001-05-17 06:02:47 -04:00
|
|
|
rescue IOError
|
|
|
|
end
|
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
@pipe_out.close
|
2001-05-17 06:02:47 -04:00
|
|
|
rescue IOError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def kill(sig)
|
|
|
|
if @pid
|
2011-05-18 17:19:18 -04:00
|
|
|
Process.kill(sig, @pid)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_import
|
|
|
|
notify "Job(%id) start imp-pipe.", @shell.debug?
|
|
|
|
_eop = true
|
2010-11-08 15:59:01 -05:00
|
|
|
Thread.start {
|
2011-05-18 17:19:18 -04:00
|
|
|
begin
|
|
|
|
while l = @pipe_in.gets
|
|
|
|
@input_queue.push l
|
|
|
|
end
|
|
|
|
_eop = false
|
|
|
|
rescue Errno::EPIPE
|
|
|
|
_eop = false
|
|
|
|
ensure
|
|
|
|
if !ProcessController::USING_AT_EXIT_WHEN_PROCESS_EXIT and _eop
|
|
|
|
notify("warn: Process finishing...",
|
|
|
|
"wait for Job[%id] to finish pipe importing.",
|
|
|
|
"You can use Shell#transact or Shell#check_point for more safe execution.")
|
|
|
|
redo
|
|
|
|
end
|
|
|
|
notify "job(%id}) close imp-pipe.", @shell.debug?
|
|
|
|
@input_queue.push :EOF
|
|
|
|
@pipe_in.close
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_export
|
|
|
|
notify "job(%id) start exp-pipe.", @shell.debug?
|
|
|
|
_eop = true
|
2010-11-08 15:59:01 -05:00
|
|
|
Thread.start{
|
2011-05-18 17:19:18 -04:00
|
|
|
begin
|
|
|
|
@input.each do |l|
|
|
|
|
ProcessController::block_output_synchronize do
|
|
|
|
@pipe_out.print l
|
|
|
|
end
|
|
|
|
end
|
|
|
|
_eop = false
|
|
|
|
rescue Errno::EPIPE, Errno::EIO
|
|
|
|
_eop = false
|
|
|
|
ensure
|
|
|
|
if !ProcessController::USING_AT_EXIT_WHEN_PROCESS_EXIT and _eop
|
|
|
|
notify("shell: warn: Process finishing...",
|
|
|
|
"wait for Job(%id) to finish pipe exporting.",
|
|
|
|
"You can use Shell#transact or Shell#check_point for more safe execution.")
|
|
|
|
redo
|
|
|
|
end
|
|
|
|
notify "job(%id) close exp-pipe.", @shell.debug?
|
|
|
|
@pipe_out.close
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
alias super_each each
|
|
|
|
def each(rs = nil)
|
|
|
|
while (l = @input_queue.pop) != :EOF
|
2011-05-18 17:19:18 -04:00
|
|
|
yield l
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ex)
|
2009-03-05 22:56:38 -05:00
|
|
|
# if you wish to output:
|
2011-05-18 20:07:25 -04:00
|
|
|
# "shell: job(#{@command}:#{@pid}) close pipe-out."
|
|
|
|
# then
|
|
|
|
# mes: "job(%id) close pipe-out."
|
2001-05-17 06:02:47 -04:00
|
|
|
# yorn: Boolean(@shell.debug? or @shell.verbose?)
|
* ext/pathname/lib/pathname.rb, ext/tk/lib/multi-tk.rb,
ext/tk/sample/demos-en/widget, lib/benchmark.rb, lib/irb/cmd/fork.rb,
lib/mkmf.rb, lib/net/ftp.rb, lib/net/smtp.rb, lib/open3.rb,
lib/pstore.rb, lib/rexml/element.rb, lib/rexml/light/node.rb,
lib/rinda/tuplespace.rb, lib/rss/maker/base.rb,
lib/rss/maker/entry.rb, lib/scanf.rb, lib/set.rb, lib/shell.rb,
lib/shell/command-processor.rb, lib/shell/process-controller.rb,
lib/shell/system-command.rb, lib/uri/common.rb: remove unused block
arguments to avoid creating Proc objects.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33638 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-11-05 03:37:47 -04:00
|
|
|
def notify(*opts)
|
2007-03-22 11:25:58 -04:00
|
|
|
@shell.notify(*opts) do |mes|
|
2011-05-18 17:19:18 -04:00
|
|
|
yield mes if iterator?
|
2007-03-22 11:25:58 -04:00
|
|
|
|
2011-05-18 17:19:18 -04:00
|
|
|
mes.gsub!("%id", "#{@command}:##{@pid}")
|
|
|
|
mes.gsub!("%name", "#{@command}")
|
|
|
|
mes.gsub!("%pid", "#{@pid}")
|
|
|
|
mes
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|