Class: Dev::SystemCall

Inherits:
Object
  • Object
show all
Defined in:
lib/dev/SystemCall.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#commandObject

Returns the value of attribute command.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def command
  @command
end

#end_timeObject

Returns the value of attribute end_time.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def end_time
  @end_time
end

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def error
  @error
end

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def output
  @output
end

#start_timeObject

Returns the value of attribute start_time.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def status
  @status
end

#timed_outObject

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

#elapsedObject



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_summaryObject



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