Class: Dev::SystemCall
- Inherits:
-
Object
- Object
- Dev::SystemCall
- Defined in:
- lib/dev/SystemCall.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#error ⇒ Object
Returns the value of attribute error.
-
#output ⇒ Object
Returns the value of attribute output.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#status ⇒ Object
Returns the value of attribute status.
-
#timed_out ⇒ Object
Returns the value of attribute timed_out.
Instance Method Summary collapse
- #elapsed ⇒ Object
- #execute(cmd) ⇒ Object
-
#initialize(cmd) ⇒ SystemCall
constructor
A new instance of SystemCall.
- #puts_summary ⇒ Object
Constructor Details
#initialize(cmd) ⇒ SystemCall
Returns a new instance of SystemCall.
10 |
# File 'lib/dev/SystemCall.rb', line 10 def initialize(cmd); execute(cmd); end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def command @command end |
#end_time ⇒ Object
Returns the value of attribute end_time.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def end_time @end_time end |
#error ⇒ Object
Returns the value of attribute error.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def error @error end |
#output ⇒ Object
Returns the value of attribute output.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def output @output end |
#start_time ⇒ Object
Returns the value of attribute start_time.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def start_time @start_time end |
#status ⇒ Object
Returns the value of attribute status.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def status @status end |
#timed_out ⇒ Object
Returns the value of attribute timed_out.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def timed_out @timed_out end |
Instance Method Details
#elapsed ⇒ Object
11 |
# File 'lib/dev/SystemCall.rb', line 11 def elapsed; @end_time-@start_time; end |
#execute(cmd) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dev/SystemCall.rb', line 12 def execute(cmd) @command=cmd @start_time=Time.now filename=Dir.tmpdir + "/" + (0...8).map{65.+(rand(25)).chr}.join begin system("#{@command} >#{filename}.out 2>#{filename}.err") File.open("#{filename}.out",'r') {|f| @output = f.read f.close } File.open("#{filename}.err",'r') {|f| @error = f.read f.close } @status=$?.exitstatus @end_time=Time.now rescue puts "error executing ruby code" ensure begin File.delete("#{filename}.out") if File.exists?("#{filename}.out") File.delete("#{filename}.err") if File.exists?("#{filename}.err") rescue puts "temp directory file was not cleaned up." end end end |
#puts_summary ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dev/SystemCall.rb', line 42 def puts_summary if(@status != 0) puts " [".foreground(:cyan) + "X".foreground(:red).bright + "]".foreground(:cyan) + " system(\"" + @command.foreground(:green) + "\") has exit status of " + @status.to_s puts @output warn @error throw "exit status was " + @status.to_s else elapsed_str="[" + "%.0f" %(elapsed) + "s]" puts " [".foreground(:cyan) + "+".foreground(:green) + "]".foreground(:cyan) + " system(\"" + @command.foreground(:green) + "\") " + elapsed_str.foreground(:cyan) end end |