puts_debug "read " + __FILE__.foreground(:green) require 'tmpdir' module Dev class SystemCall attr_accessor :output,:error,:status,:command,:start_time,:end_time,:timed_out,:dir,:cache 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 def elapsed; @end_time-@start_time; end def cache_name name = @dir+"/" + @command.gsub(" ","_").gsub("\\","-").gsub("/","-") unless @dir.nil? name = @command.gsub(" ","_").gsub("\\","-").gsub("/","-") if @dir.nil? return name end def has_cache File.exist?(cache_name) end 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 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 def write_cache marshal_dump = Marshal.dump(get_hash) file=File.new(cache_name,'wb') file.write marshal_dump file.close end def load_cache file=File.open(cache_name,'rb') hash = Marshal.load file.read file.close set_hash hash end 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?) system("#{@command} >#{filename}.out 2>#{filename}.err") else Dir.chdir(dir) do system("#{@command} >#{filename}.out 2>#{filename}.err") end end 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 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 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 warn @error 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 end # class SystemCall end # module Dev