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.
-
#throw_on_error ⇒ Object
Returns the value of attribute throw_on_error.
-
#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
- #log ⇒ Object
- #puts_summary ⇒ Object
- #puts_summary2(throw_on_error) ⇒ Object
- #set_hash(hash) ⇒ Object
- #split(input) ⇒ Object
- #system2(working_dir, command, capture_output, capture_error) ⇒ Object
- #user_profile ⇒ Object
- #write_cache ⇒ Object
Constructor Details
#initialize(cmd) ⇒ SystemCall
Returns a new instance of SystemCall.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dev/SystemCall.rb', line 13 def initialize(cmd) @throw_on_error=true 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.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def cache @cache end |
#capture_error ⇒ Object
Returns the value of attribute capture_error.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def capture_error @capture_error end |
#capture_output ⇒ Object
Returns the value of attribute capture_output.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def capture_output @capture_output end |
#command ⇒ Object
Returns the value of attribute command.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def command @command end |
#dir ⇒ Object
Returns the value of attribute dir.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def dir @dir end |
#end_time ⇒ Object
Returns the value of attribute end_time.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def end_time @end_time end |
#error ⇒ Object
Returns the value of attribute error.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def error @error end |
#output ⇒ Object
Returns the value of attribute output.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def output @output end |
#start_time ⇒ Object
Returns the value of attribute start_time.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def start_time @start_time end |
#status ⇒ Object
Returns the value of attribute status.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def status @status end |
#throw_on_error ⇒ Object
Returns the value of attribute throw_on_error.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def throw_on_error @throw_on_error end |
#timed_out ⇒ Object
Returns the value of attribute timed_out.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def timed_out @timed_out end |
#timeout ⇒ Object
Returns the value of attribute timeout.
11 12 13 |
# File 'lib/dev/SystemCall.rb', line 11 def timeout @timeout end |
Class Method Details
.puts_hash_summary(h) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/dev/SystemCall.rb', line 208 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
27 28 29 30 31 |
# File 'lib/dev/SystemCall.rb', line 27 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
25 |
# File 'lib/dev/SystemCall.rb', line 25 def elapsed; @end_time-@start_time; end |
#execute(cmd) ⇒ Object
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 |
# File 'lib/dev/SystemCall.rb', line 86 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) @end_time=Time.now log 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dev/SystemCall.rb', line 37 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 hash[:throw_on_error]=@throw_on_error return hash end |
#has_cache ⇒ Object
33 34 35 |
# File 'lib/dev/SystemCall.rb', line 33 def has_cache File.exist?(cache_name) end |
#load_cache ⇒ Object
79 80 81 82 83 84 |
# File 'lib/dev/SystemCall.rb', line 79 def load_cache file=File.open(cache_name,'rb') hash = Marshal.load file.read file.close set_hash hash end |
#log ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/dev/SystemCall.rb', line 235 def log filename = user_profile + "/CommandHistory.sql" sql="" if(File.exists?(filename)) begin words = split(@command) cmd = words[0] arguments = @command.gsub("#{cmd} ",""); executable = cmd cmd = File.basename(executable).gsub(File.extname(executable),""); db = SQLite3::Database.new filename @timeout=0 if @timeout.nil? @dir=Rake.original_dir() if(@dir.nil?) values = "VALUES(null,'" + @start_time.strftime('%Y-%m-%dT%H:%M:%S') + "','" + @end_time.strftime('%Y-%m-%dT%H:%M:%S') + "','" + Time.at(elapsed).gmtime.strftime('%H:%M:%S') + "'," + @status.to_s + ",'" + cmd + #@command.gsub("'","''") + "','" + arguments + "','" + @dir.to_s + #"','" + @output.gsub("'", "''") + #"','" + @error.gsub("'", "''") + "','" + executable + "'," + @timeout.to_s + ")" sql="insert into Commands #{values};" db.execute(sql) if(@status != 0) id=nil db.execute("select * from Commands where startTime='" + @start_time.strftime('%Y-%m-%dT%H:%M:%S') + "' AND exitCode=" + @status.to_s + ";") do |row| id=row[0] end if(!id.nil?) sql="insert or replace INTO Output VALUES(" + id.to_s + ",'" + @output.gsub("'","''") + "','" + @error.gsub("'","''") + "')"; db.execute(sql) end end db.close db=nil rescue #puts_debug "failed to execute '#{sql}'" end end end |
#puts_summary ⇒ Object
176 177 178 |
# File 'lib/dev/SystemCall.rb', line 176 def puts_summary puts_summary2(@throw_on_error) end |
#puts_summary2(throw_on_error) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/dev/SystemCall.rb', line 180 def puts_summary2(throw_on_error) if(@status != 0) if(RUBY_VERSION == "1.8.7") summary = " [" + "X" + "]" + " " + @command + " has exit status of " + @status.to_s else summary = Rainbow(" [").foreground(:cyan) + Rainbow("X").foreground(:red).bright + Rainbow("]").foreground(:cyan) + " " + Rainbow(@command).foreground(:green) + " has exit status of " + @status.to_s end 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 if throw_on_error else elapsed_str="[" + "%.0f" %(elapsed) + "s]" if(RUBY_VERSION == "1.8.7") summary = " [" + "+" + "]" + " " + @command + " " + elapsed_str else summary = Rainbow(" [").foreground(:cyan) + Rainbow("+").foreground(:green) + Rainbow("]").foreground(:cyan) + " " + Rainbow(@command).foreground(:green) + " " + Rainbow(elapsed_str).foreground(:cyan) end 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dev/SystemCall.rb', line 56 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? @throw_on_error=hash[:throw_on_error] unless hash[:throw_on_error].nil? end |
#split(input) ⇒ Object
231 232 233 |
# File 'lib/dev/SystemCall.rb', line 231 def split(input) m = /(?:'(?:\\.|[^'])*'|[^' ])+/.match(input) end |
#system2(working_dir, command, capture_output, capture_error) ⇒ Object
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 171 172 173 174 |
# File 'lib/dev/SystemCall.rb', line 116 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 |
#user_profile ⇒ Object
224 225 226 227 228 229 |
# File 'lib/dev/SystemCall.rb', line 224 def user_profile ["USERPROFILE","HOME"].each {|v| return ENV[v].gsub('\\','/') unless ENV[v].nil? } dir="~" end |
#write_cache ⇒ Object
72 73 74 75 76 77 |
# File 'lib/dev/SystemCall.rb', line 72 def write_cache marshal_dump = Marshal.dump(get_hash) file=File.new(cache_name,'wb') file.write marshal_dump file.close end |