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.
-
#capture_error ⇒ Object
Returns the value of attribute capture_error.
-
#capture_output ⇒ Object
Returns the value of attribute capture_output.
-
#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.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
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
- #system2(working_dir, command, capture_output, capture_error) ⇒ Object
- #write_cache ⇒ Object
Constructor Details
#initialize(cmd) ⇒ SystemCall
Returns a new instance of SystemCall.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dev/SystemCall.rb', line 12 def initialize(cmd) if(cmd.kind_of?(Hash)) set_hash(cmd) else hash=Dev::Environment.s_to_hash(cmd) set_hash(hash) unless hash.nil? @command=cmd if hash.nil? end execute(@command); end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def cache @cache end |
#capture_error ⇒ Object
Returns the value of attribute capture_error.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def capture_error @capture_error end |
#capture_output ⇒ Object
Returns the value of attribute capture_output.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def capture_output @capture_output end |
#command ⇒ Object
Returns the value of attribute command.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def command @command end |
#dir ⇒ Object
Returns the value of attribute dir.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def dir @dir end |
#end_time ⇒ Object
Returns the value of attribute end_time.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def end_time @end_time end |
#error ⇒ Object
Returns the value of attribute error.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def error @error end |
#output ⇒ Object
Returns the value of attribute output.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def output @output end |
#start_time ⇒ Object
Returns the value of attribute start_time.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def start_time @start_time end |
#status ⇒ Object
Returns the value of attribute status.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def status @status end |
#timed_out ⇒ Object
Returns the value of attribute timed_out.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def timed_out @timed_out end |
#timeout ⇒ Object
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/dev/SystemCall.rb', line 10 def timeout @timeout end |
Class Method Details
.clean_tmp_files ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dev/SystemCall.rb', line 172 def self.clean_tmp_files puts "clean temporary files in #{Dir.tmpdir}" Dir.glob("#{Dir.tmpdir}/*.{out,err}").each{ |f| begin FileUtils.rm(f) rescue warn "unable to remove file #{f}.err" end } end |
.puts_hash_summary(h) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/dev/SystemCall.rb', line 203 def self.puts_hash_summary(h) if(h[:status] != 0 && h[:status] != "0") summary = " [".foreground(:cyan) + "X".foreground(:red).bright + "]".foreground(:cyan) + " " + h[:cmd].foreground(:green) + " has exit status of " + h[:status].to_s summary += " dir: " + h[:dir] unless h[:dir].nil? puts summary puts h[:output] if !h[:output].nil? warn h[:error] if !h[:error].nil? throw "exit status was " + h[:status].to_s else elapsed_str="[" + "%.0f" %(h[:elapsed]) + "s]" summary = " [".foreground(:cyan) + "+".foreground(:green) + "]".foreground(:cyan) + " " + h[:cmd].foreground(:green) + " " + elapsed_str.foreground(:cyan) summary += " dir: " + h[:dir] unless h[:dir].nil? puts summary end end |
Instance Method Details
#cache_name ⇒ Object
25 26 27 28 29 |
# File 'lib/dev/SystemCall.rb', line 25 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
23 |
# File 'lib/dev/SystemCall.rb', line 23 def elapsed; @end_time-@start_time; end |
#execute(cmd) ⇒ Object
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 |
# File 'lib/dev/SystemCall.rb', line 82 def execute(cmd) if !@cache.nil? && has_cache load_cache return end pwd=Dir.pwd @start_time=Time.now begin capture_output=true if capture_output.nil? capture_output=false if capture_output == "false" capture_error=true if capture_error.nil? capture_error=false unless capture_output capture_error=false if capture_error == "false" puts_debug "system2('#{@dir}','#{@command}',#{@capture_output.to_s},#{@capture_error.to_s}) (in SystemCall.execute)" @status,@output,@error=system2(@dir,@command,@capture_output,@capture_error) #puts_debug "system2 call complete" @end_time=Time.now rescue Exception=>e puts "error executing ruby code: #{@command}" warn "exception:" warn e.to_s @status="1" @end_time=Time.now ensure Dir.chdir(pwd) if Dir.pwd != pwd write_cache if !@cache.nil? end end |
#get_hash ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dev/SystemCall.rb', line 35 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[:elapsed]=elapsed.to_s hash[:time_out]=@time_out hash[:timeout]=@timeout hash[:cache]=@cache hash[:capture_output]=@capture_output hash[:capture_error]=@capture_error return hash end |
#has_cache ⇒ Object
31 32 33 |
# File 'lib/dev/SystemCall.rb', line 31 def has_cache File.exist?(cache_name) end |
#load_cache ⇒ Object
75 76 77 78 79 80 |
# File 'lib/dev/SystemCall.rb', line 75 def load_cache file=File.open(cache_name,'rb') hash = Marshal.load file.read file.close set_hash hash end |
#puts_summary ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/dev/SystemCall.rb', line 183 def puts_summary if(@status != 0) summary = " [".foreground(:cyan) + "X".foreground(:red).bright + "]".foreground(:cyan) + " " + @command.foreground(:green) + " has exit status of " + @status.to_s summary += " dir: " + @dir unless @dir.nil? summary += " cache: true" unless @cache.nil? summary += " capture_output: false" unless @capture_output.nil? || @capture_output 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) + " " + @command.foreground(:green) + " " + elapsed_str.foreground(:cyan) summary += " dir: " + @dir unless @dir.nil? summary += " cache: true" unless @cache.nil? summary += " capture_output: false" unless @capture_output.nil? || @capture_output puts summary end end |
#set_hash(hash) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dev/SystemCall.rb', line 53 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? @time_out=hash[:timeout] unless hash[:timeout].nil? @timed_out=hash[:timed_out] unless hash[:timed_out].nil? @cache=hash[:cache] unless hash[:cache].nil? @capture_output=hash[:capture_output] unless hash[:capture_output].nil? @capture_error=hash[:capture_error] unless hash[:capture_error].nil? end |
#system2(working_dir, command, capture_output, capture_error) ⇒ Object
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 |
# File 'lib/dev/SystemCall.rb', line 112 def system2(working_dir,command,capture_output,capture_error) capture_output=true if capture_output.nil? capture_output=false if capture_output == "false" capture_error=true if capture_error.nil? capture_error=false unless capture_output capture_error=false if capture_error == "false" filename=Dir.tmpdir + "/" + (0...2).map{65.+(rand(25)).chr}.join filename=Dir.tmpdir + "/" + (0...2).map{65.+(rand(25)).chr}.join if File.exist? "#{filename}.out" working_dir = Dir.pwd if working_dir.nil? || working_dir.empty? status=0 output="" error="" FileUtils.rm("#{filename}.out") if File.exist? "#{filename}.out" FileUtils.rm("#{filename}.err") if File.exist? "#{filename}.err" Dir.chdir(working_dir) do if(capture_output) if(capture_error) system("#{@command} >#{filename}.out 2>#{filename}.err") else system("#{@command} >#{filename}.out") end else if(capture_error) system("#{@command} 2>#{filename}.err") else system(@command) end end end if(File.exist?("#{filename}.out")) File.open("#{filename}.out",'r') {|f| output = f.read f.close } begin FileUtils.rm("#{filename}.out") rescue warn "unable to remove file #{filename}.out" end end if(File.exist?("#{filename}.err")) File.open("#{filename}.err",'r') {|f| error = f.read f.close } begin FileUtils.rm("#{filename}.err") rescue warn "unable to remove file #{filename}.err" end end status=$?.exitstatus return status,output,error end |
#write_cache ⇒ Object
68 69 70 71 72 73 |
# File 'lib/dev/SystemCall.rb', line 68 def write_cache marshal_dump = Marshal.dump(get_hash) file=File.new(cache_name,'wb') file.write marshal_dump file.close end |