Class: Command
Overview
Command
execution of system commands
Keys
-
:input The input of the commands.
-
:timeout The timeout in seconds.
a value of zero is to infinite timeout. defaults to zero
-
:directory The working directory for the command.
defaults to the current directory
-
:exit_code The exit code of the command
-
:output The output contains the stdout output of the command
-
:error The error contains stderr output of the command
-
:machine The name of the machine the command executed on
-
:user The user name
-
:start_time
-
:end_time
Class Method Summary collapse
- .dev_root ⇒ Object
- .error(command) ⇒ Object
- .execute(command, working_directory = '') ⇒ Object
- .executes?(command) ⇒ Boolean
- .exit_code(command) ⇒ Object
- .home ⇒ Object
- .machine ⇒ Object
- .output(command) ⇒ Object
- .user ⇒ Object
Instance Method Summary collapse
- #error ⇒ Object
-
#execute(value = nil) ⇒ Object
todo: add log of execution.
- #exit_code ⇒ Object
- #format_property(name, value) ⇒ Object
- #getFormattedTimeSpan(timespan) ⇒ Object
- #info ⇒ Object
-
#initialize(command) ⇒ Command
constructor
A new instance of Command.
- #open(filename = '') ⇒ Object
- #output ⇒ Object
- #quiet? ⇒ Boolean
- #save(filename) ⇒ Object
- #summary(include_directory = false) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(command) ⇒ Command
Returns a new instance of Command.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/base/command.rb', line 36 def initialize command self[:input] = '' self[:timeout] = 0 self[:directory] = '' self[:exit_code] = 0 self[:output] = '' self[:error] = '' self[:machine] = '' self[:user] = '' self[:start_time] = nil self[:end_time] = nil if(command.kind_of?(String)) self[:input] = command end if(command.kind_of?(Hash)) command.each{|k,v|self[k.to_sym]=v} self[:start_time]=Time.parse(self[:start_time]) if(self.has_key?(:start_time) && !self[:start_time].nil?) self[:end_time]=Time.parse(self[:end_time]) if(self.has_key?(:end_time) && !self[:end_time].nil?) end end |
Class Method Details
.dev_root ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/base/command.rb', line 199 def self.dev_root ["DEV_HOME","DEV_ROOT"].each {|v| return ENV[v].gsub('\\','/') unless ENV[v].nil? } dir=home return dir end |
.error(command) ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/base/command.rb', line 233 def self.error command cmd = Command.new(command) cmd[:ignore_failure]=true cmd[:quiet]=true cmd.execute cmd[:error] end |
.execute(command, working_directory = '') ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/base/command.rb', line 207 def self.execute command, working_directory='' cmd = Command.new({ :input => command, :quiet => true}) if command.kind_of?(String) cmd[:directory] = working_directory if command.kind_of?(String) cmd = command if command.kind_of?(Command) cmd = Command.new(command) if command.kind_of?(Hash) cmd.execute cmd[:exit_code] cmd end |
.executes?(command) ⇒ Boolean
85 86 87 88 89 90 91 92 93 |
# File 'lib/base/command.rb', line 85 def self.executes?(command) cmd = Command.new({ :input => command, :quiet => true,:ignore_failure => true}) cmd.execute if(cmd[:exit_code] == 0) true else false end end |
.exit_code(command) ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/base/command.rb', line 217 def self.exit_code command cmd = Command.new(command) cmd[:ignore_failure]=true cmd[:quiet]=true cmd.execute cmd[:exit_code] end |
.home ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/base/command.rb', line 189 def self.home ["USERPROFILE","HOME"].each {|v| return ENV[v].gsub('\\','/') unless ENV[v].nil? } dir="~" dir=ENV["HOME"] unless ENV["HOME"].nil? dir=ENV["USERPROFILE"].gsub('\\','/') unless ENV["USERPROFILE"].nil? return dir end |
.machine ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/base/command.rb', line 175 def self.machine if !ENV['COMPUTERNAME'].nil? return ENV['COMPUTERNAME'] end machine = `hostname` machine = machine.split('.')[0] if machine.include?('.') return machine.strip end |
.output(command) ⇒ Object
225 226 227 228 229 230 231 |
# File 'lib/base/command.rb', line 225 def self.output command cmd = Command.new(command) cmd[:ignore_failure]=true cmd[:quiet]=true cmd.execute cmd[:output] end |
.user ⇒ Object
185 186 187 |
# File 'lib/base/command.rb', line 185 def self.user ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER'] end |
Instance Method Details
#error ⇒ Object
82 83 84 |
# File 'lib/base/command.rb', line 82 def error self[:error] end |
#execute(value = nil) ⇒ Object
todo: add log of execution
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 171 172 173 |
# File 'lib/base/command.rb', line 96 def execute value=nil puts "#{self[:input]}" if defined?(DEBUG) if(!value.nil? && value.is_a?(Hash)) value.each{|k,v|self[k]=v} end pwd=Dir.pwd self[:directory] = pwd if(!self.has_key?(:directory) || self[:directory].length==0) if(self[:timeout] > 0) puts "#{self[:input]} (#{self[:directory]}) timeout #{self[:timeout].to_s}" if(!quiet?) else puts "#{self[:input]} (#{self[:directory]})" if(!quiet?) end self[:machine] = Command.machine self[:user] = Command.user self[:start_time]=Time.now timer=Timer.new Dir.chdir(self[:directory]) do if self[:input].include?('<%') && self[:input].include?('%>') ruby = self[:input].gsub("<%","").gsub("%>","") begin self[:output]=eval(ruby) rescue self[:exit_code]=1 self[:error]="unable to eval(#{ruby})" end self[:elapsed] = timer.elapsed_str self[:end_time] = Time.now else begin if(self[:timeout] <= 0) self[:output],self[:error],status= Open3.capture3(self[:input]) self[:exit_code]=status.to_i self[:elapsed] = timer.elapsed_str self[:end_time] = Time.now else require_relative 'timeout.rb' result=run_with_timeout(self[:directory],self[:input], self[:timeout],2) self[:output]=result[0] self[:exit_code]=result[1] self[:elapsed] = timer.elapsed_str self[:end_time] = Time.now if(timer.elapsed >= self[:timeout]) self[:exit_code]=1 self[:error] = self[:error] + "timed out" end end rescue Exception => e self[:elapsed] = timer.elapsed_str self[:end_time] = Time.now self[:error] = "Exception: " + e.to_s self[:exit_code]=1 end end end if(self[:exit_code] != 0) if(!quiet?) puts "exit_code=#{self[:exit_code]}" puts self[:output] puts self[:error] end if(!self.has_key?(:ignore_failure) || !self[:ignore_failure]) raise "#{self[:input]} failed\n#{self[:output]}\n#{self[:error]}" end end self end |
#exit_code ⇒ Object
75 76 77 |
# File 'lib/base/command.rb', line 75 def exit_code self[:exit_code] end |
#format_property(name, value) ⇒ Object
275 276 277 278 279 280 281 282 |
# File 'lib/base/command.rb', line 275 def format_property name,value if(Environment.default.colorize?) require 'ansi/code' return "#{name}: " + ANSI.yellow + ANSI.bright + value.to_s.strip + ANSI.reset else return "#{name}: #{value}" end end |
#getFormattedTimeSpan(timespan) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/base/command.rb', line 241 def getFormattedTimeSpan timespan result='' seconds = timespan.round if(seconds > 99) minutes=(seconds/60).round result="#{minutes}m" else result="#{seconds}s" # 99s end result.fix(3) end |
#info ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/base/command.rb', line 284 def info result=format_property('input'.fix(15),self[:input]) + "\n" result=result + format_property('directory'.fix(15),self[:directory]) + "\n" result=result + format_property('exit_code'.fix(15),self[:exit_code]) + "\n" result=result + format_property('duration'.fix(15),getFormattedTimeSpan(self[:end_time]-self[:start_time])) + "\n" output=[''] output=self[:output].strip.split("\n") if !self[:output].nil? if(output.length <= 1) result=result + format_property('output'.fix(15),output[0]) + "\n" #result=result + format_property('output'.fix(15),'') + "\n" if(output.length==0) #result=result + format_property('output'.fix(15),output) + "\n" if(output.length==1) else result=result + format_property('output'.fix(15),'') + "\n" output.each{|line| result=result + ' '.fix(16) + line + "\n" } end error=[''] error=self[:error].strip.split("\n") if !self[:error].nil? if(error.length <= 1) result=result + format_property('error'.fix(15),error[0]) + "\n" #result=result + format_property('error'.fix(15),'') + "\n" if(error.length==0) #result=result + format_property('error'.fix(15),error) + "\n" if(error.length==1) else result=result + format_property('error'.fix(15),'') + "\n" error.each{|line| result=result + ' '.fix(16) + line + "\n" } end end |
#open(filename = '') ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/base/command.rb', line 63 def open filename='' @filename=filename if filename.length > 0 self.clear JSON.parse(IO.read(@filename)).each{|k,v| self[k.to_sym]=v} self[:start_time]=Time.parse(self[:start_time]) if(self.has_key?(:start_time)) self[:end_time]=Time.parse(self[:end_time]) if(self.has_key?(:end_time)) end |
#output ⇒ Object
79 80 81 |
# File 'lib/base/command.rb', line 79 def output self[:output] end |
#quiet? ⇒ Boolean
71 72 73 |
# File 'lib/base/command.rb', line 71 def quiet? (self.has_key?(:quiet) && self[:quiet]) end |
#save(filename) ⇒ Object
59 60 61 |
# File 'lib/base/command.rb', line 59 def save filename File.open(filename,'w'){|f|f.write(to_json)} end |
#summary(include_directory = false) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/base/command.rb', line 253 def summary include_directory=false duration="" duration=getFormattedTimeSpan(self[:end_time]-self[:start_time]) if(Environment.default.colorize?) require 'ansi/code' cduration = ANSI.reset + duration #code=ANSI.green + '+ ' + ANSI.reset #code=ANSI.red + '- ' + ANSI.reset if exit_code != 0 cinput = ANSI.reset + self[:input] + ANSI.reset cinput = ANSI.red + self[:input] + ANSI.reset if exit_code != 0 cdirectory = '' cdirectory = "(#{self[:directory]})" if include_directory " #{cduration} #{cinput} #{cdirectory}" else code=' ' code='X' if exit_code != 0 sdirectory = '' sdirectory = "(#{self[:directory]})" if include_directory "#{code} #{duration} #{self[:input]} #{sdirectory}" end end |
#to_html ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/base/command.rb', line 315 def to_html if self[:exit_code] == 0 [ '<div><table><tr><td width="20"></td><td><pre>', self[:input], '</pre></td></tr></table></div>' ].join else [ '<div><table><tr><td width="20"></td><td><pre>', self[:input], '</pre><table><tr><td width="20"></td><td><table>', map { |k, v| ["<tr><td><strong>#{k}</strong></td>", v.respond_to?(:to_html) ? v.to_html : "<td><span><pre>#{v}</pre></span></td></tr>"] }, '</table>', '</td></tr></table></td></tr></table></div>' ].join end end |