Class: Dev::SystemCall
- Inherits:
-
Object
- Object
- Dev::SystemCall
- Defined in:
- lib/dev/SystemCall.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#command ⇒ Object
Returns the value of attribute command.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#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
- #cache_name ⇒ Object
- #elapsed ⇒ Object
- #execute(cmd) ⇒ Object
- #get_hash ⇒ Object
- #has_cache ⇒ Object
-
#initialize(cmd) ⇒ SystemCall
constructor
A new instance of SystemCall.
- #load_cache ⇒ Object
- #puts_summary ⇒ Object
- #set_hash(hash) ⇒ Object
- #write_cache ⇒ Object
Constructor Details
#initialize(cmd) ⇒ SystemCall
Returns a new instance of SystemCall.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dev/SystemCall.rb', line 10 def initialize(cmd) if(cmd.kind_of?(Hash)) @command=cmd[:cmd] @dir=cmd[:dir] unless cmd[:dir].nil? @cache=cmd[:cache] unless[:cache].nil? else @command=cmd end execute(@command); end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def cache @cache end |
#command ⇒ Object
Returns the value of attribute command.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def command @command end |
#dir ⇒ Object
Returns the value of attribute dir.
8 9 10 |
# File 'lib/dev/SystemCall.rb', line 8 def dir @dir 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
#cache_name ⇒ Object
23 24 25 26 27 |
# File 'lib/dev/SystemCall.rb', line 23 def cache_name name = @dir+"/" + @command.gsub(" ","_").gsub("\\","-").gsub("/","-") unless @dir.nil? name = @command.gsub(" ","_").gsub("\\","-").gsub("/","-") if @dir.nil? return name end |
#elapsed ⇒ Object
21 |
# File 'lib/dev/SystemCall.rb', line 21 def elapsed; @end_time-@start_time; end |
#execute(cmd) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 |
# File 'lib/dev/SystemCall.rb', line 73 def execute(cmd) if !@cache.nil? && has_cache load_cache return end @start_time=Time.now filename=Dir.tmpdir + "/" + (0...8).map{65.+(rand(25)).chr}.join begin if(@dir.nil?) if(!@outfile.nil?) system("#{@command} > #{outfile}") else system("#{@command} >#{filename}.out 2>#{filename}.err") end else Dir.chdir(dir) do if(!@outfile.nil?) system("#{@command} > #{outfile}") else system("#{@command} >#{filename}.out 2>#{filename}.err") end end end if(File.exist?("#{filename}.out")) File.open("#{filename}.out",'r') {|f| @output = f.read f.close } end if(File.exist?("#{filename}.err")) File.open("#{filename}.err",'r') {|f| @error = f.read f.close } end @status=$?.exitstatus @end_time=Time.now rescue puts "error executing ruby code" ensure write_cache if !@cache.nil? 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 |
#get_hash ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dev/SystemCall.rb', line 33 def get_hash hash = Hash.new() hash[:cmd]=@command hash[:dir]=@dir hash[:output]=@output hash[:error]=@error hash[:status]=@status hash[:start_time]=@start_time hash[:end_time]=@end_time hash[:timed_out]=@timed_out hash[:cache]=@cache return hash end |
#has_cache ⇒ Object
29 30 31 |
# File 'lib/dev/SystemCall.rb', line 29 def has_cache File.exist?(cache_name) end |
#load_cache ⇒ Object
66 67 68 69 70 71 |
# File 'lib/dev/SystemCall.rb', line 66 def load_cache file=File.open(cache_name,'rb') hash = Marshal.load file.read file.close set_hash hash end |
#puts_summary ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dev/SystemCall.rb', line 124 def puts_summary if(@status != 0) summary = " [".foreground(:cyan) + "X".foreground(:red).bright + "]".foreground(:cyan) + " system(\"" + @command.foreground(:green) + "\") has exit status of " + @status.to_s summary += " dir: " + @dir unless @dir.nil? summary += " cache: true" unless @cache.nil? puts summary puts @output if !@output.nil? warn @error if !@error.nil? throw "exit status was " + @status.to_s else elapsed_str="[" + "%.0f" %(elapsed) + "s]" summary = " [".foreground(:cyan) + "+".foreground(:green) + "]".foreground(:cyan) + " system(\"" + @command.foreground(:green) + "\") " + elapsed_str.foreground(:cyan) summary += " dir: " + @dir unless @dir.nil? summary += " cache: true" unless @cache.nil? puts summary end end |
#set_hash(hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dev/SystemCall.rb', line 47 def set_hash(hash) @command=hash[:cmd] unless hash[:cmd].nil? @dir=hash[:dir] unless hash[:dir].nil? @output=hash[:output] unless hash[:output].nil? @error=hash[:error] unless hash[:error].nil? @status=hash[:status] unless hash[:status].nil? @start_time=hash[:start_time] unless hash[:start_time].nil? @end_time=hash[:end_time] unless hash[:end_time].nil? @timed_out=hash[:timed_out] unless hash[:timed_out].nil? @cache=hash[:cache] unless hash[:cache].nil? end |
#write_cache ⇒ Object
59 60 61 62 63 64 |
# File 'lib/dev/SystemCall.rb', line 59 def write_cache marshal_dump = Marshal.dump(get_hash) file=File.new(cache_name,'wb') file.write marshal_dump file.close end |