lib/rbatch/cmd.rb in rbatch-2.1.1 vs lib/rbatch/cmd.rb in rbatch-2.1.2

- old
+ new

@@ -1,89 +1,54 @@ require 'fileutils' require 'tempfile' require 'tmpdir' - +require 'timeout' module RBatch - # External command runcher. - # - #This module is a wrapper of Kernel#spawn. - # - # * Arguments(cmd_params) are inputed to Kernel#spawn directly and run command. - # * Return an object of RBatch::CmdResult which includes stdout, stderr, and exit status. - # - # ==== Sample 1 - # require 'rbatch' - # result = RBatch::cmd("ls") - # p result.stdout - # => "fileA\nfileB\n" - # - # ==== Sample 2 (use option) - # require 'rbatch' - # result = RBatch::cmd("ls",{:timeout => 1}) - # p result.stdout - # => "fileA\nfileB\n" - # - # ==== Sample 3 (use instance) - # require 'rbatch' - # cmd = RBatch::Cmd.new("ls") - # result = cmd.run - # p result.stdout - # => "fileA\nfileB\n" - # class Cmd + @@def_vars + def Cmd.def_vars=(a) ; @@def_vars=a ; end @cmd_str @opt - - # Cmd instance - # - # ==== Params - # +cmd_str+ = Command string such as "ls -l" - # +opt+ = Option hash object. - # - +:raise+ (Boolean) = If command exit status is not 0, raise exception. Default is false. - # - +:timeout+ (Integer) = If command timeout , raise exception and kill process. Default is 0 sec ( 0 means disable) . + @vars def initialize(cmd_str,opt = nil) raise(CmdException,"Command string is nil") if cmd_str.nil? @cmd_str = cmd_str - tmp = {} - if opt.nil? - @opt=RBatch.run_conf.clone - else + @vars = @@def_vars.clone + if ! opt.nil? + # change opt key from "hoge" to "log_hoge" + tmp = {} opt.each_key do |key| tmp[("cmd_" + key.to_s).to_sym] = opt[key] end - @opt=RBatch.run_conf.merge(tmp) + @vars.merge!(tmp) end end - # Run command - # - # ==== Return - # instance of RBatch::CmdResult def run() stdout_file = Tempfile::new("rbatch_tmpout",Dir.tmpdir) stderr_file = Tempfile::new("rbatch_tmperr",Dir.tmpdir) pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"]) status = nil - if @opt[:cmd_timeout] != 0 + if @vars[:cmd_timeout] != 0 begin - timeout(@opt[:cmd_timeout]) do + timeout(@vars[:cmd_timeout]) do status = Process.waitpid2(pid)[1] >> 8 end rescue Timeout::Error => e begin Process.kill('SIGINT', pid) - raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@opt[:cmd_timeout].to_s} sec. Success to kill process : PID=#{pid}" ) + raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. Success to kill process : PID=#{pid}" ) rescue - raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@opt[:cmd_timeout].to_s} sec. Fail to kill process : PID=#{pid}" ) + raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. Fail to kill process : PID=#{pid}" ) end end else status = Process.waitpid2(pid)[1] >> 8 end result = RBatch::CmdResult.new(stdout_file,stderr_file,status,@cmd_str) - if @opt[:cmd_raise] && status != 0 + if @vars[:cmd_raise] && status != 0 raise(CmdException,"Command exit status is not 0. result: " + result.to_s) end return result end end @@ -115,15 +80,7 @@ def to_s to_h.to_s end end - class CmdException < Exception ; end - - module_function - - # shortcut of RBatch::Cmd - def cmd(cmd_str,opt = nil) - Cmd.new(cmd_str,opt).run - end - + class CmdException < StandardError ; end end