Class: Patir::ShellCommand
- Inherits:
-
Object
- Object
- Patir::ShellCommand
- Includes:
- Command
- Defined in:
- lib/patir/command.rb
Overview
This class wraps the Command interface around github.com/ahoward/systemu
It allows for execution of any shell command on any platform.
Accepted keys are :cmd - the shell command to execute (required - ParameterException will be raised). :working_directory - specify the working directory (default is ‘.’) :name - assign a name to the command (default is “”). :timeout - if the command runs longer than timeout, it will be interrupted and an error will be set.
The timeout is set in seconds
Instance Attribute Summary
Attributes included from Command
#error, #exec_time, #name, #number, #output, #status, #strategy
Instance Method Summary collapse
-
#initialize(params) ⇒ ShellCommand
constructor
The constructor will throw CommandError if :cmd is missing.
-
#run(context = nil) ⇒ Object
Executes the shell command and returns the status.
- #to_s ⇒ Object
Methods included from Command
#executed?, #reset, #run?, #success?
Constructor Details
#initialize(params) ⇒ ShellCommand
The constructor will throw CommandError if :cmd is missing.
CommandError will also be thrown if :working_directory does not exist.
111 112 113 114 115 116 117 118 119 |
# File 'lib/patir/command.rb', line 111 def initialize params @name=params[:name] @working_directory=params[:working_directory] || "." #we need a command line :) raise ParameterException,"No :command defined" unless params[:cmd] @command=params[:cmd] @status=:not_executed @timeout=params[:timeout] end |
Instance Method Details
#run(context = nil) ⇒ Object
Executes the shell command and returns the status
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/patir/command.rb', line 122 def run context=nil start_time=Time.now begin #create the working directory if it does not exist FileUtils::mkdir_p(@working_directory,:verbose=>false) #create the actual command, run it, grab stderr and stdout and set output,error, status and execution time if @timeout @error="" exited=nil exitstatus=0 status, @output, err = systemu(@command,:cwd=>@working_directory) do |cid| sleep @timeout @error<<"Command timed out after #{@timeout}s" exited=true exitstatus=23 Process.kill 9,cid end @error<<"\n#{err}" unless err.empty? else status, @output, @error = systemu(@command,:cwd=>@working_directory) exitstatus = status.exitstatus end begin exited||= status.exited? rescue NotImplementedError #oh look, it's jruby exited=true end #lets get the status how we want it if exited if exitstatus ==0 @status=:success else @status=:error end else @status=:warning end rescue #if it blows in systemu it will be nil @error||="" @error<<"\n#{$!.}" @error<<"\n#{$!.backtrace}" if $DEBUG @status=:error end #set the time it took us @exec_time=Time.now-start_time return @status end |
#to_s ⇒ Object
172 173 174 |
# File 'lib/patir/command.rb', line 172 def to_s return "#{@name}: #{@command} in #{@working_directory}" end |