Class: Dev::SystemCall

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ SystemCall

Returns a new instance of SystemCall.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dev/SystemCall.rb', line 12

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

#cacheObject

Returns the value of attribute cache.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def cache
  @cache
end

#capture_errorObject

Returns the value of attribute capture_error.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def capture_error
  @capture_error
end

#capture_outputObject

Returns the value of attribute capture_output.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def capture_output
  @capture_output
end

#commandObject

Returns the value of attribute command.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def command
  @command
end

#dirObject

Returns the value of attribute dir.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def dir
  @dir
end

#end_timeObject

Returns the value of attribute end_time.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def end_time
  @end_time
end

#errorObject

Returns the value of attribute error.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def error
  @error
end

#outputObject

Returns the value of attribute output.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def output
  @output
end

#start_timeObject

Returns the value of attribute start_time.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def status
  @status
end

#throw_on_errorObject

Returns the value of attribute throw_on_error.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def throw_on_error
  @throw_on_error
end

#timed_outObject

Returns the value of attribute timed_out.



10
11
12
# File 'lib/dev/SystemCall.rb', line 10

def timed_out
  @timed_out
end

#timeoutObject

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_filesObject



175
176
177
178
179
180
181
182
183
184
# File 'lib/dev/SystemCall.rb', line 175

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/dev/SystemCall.rb', line 210

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_nameObject



26
27
28
29
30
# File 'lib/dev/SystemCall.rb', line 26

def cache_name
  name = @dir+"/" + @command.gsub(" ","_").gsub("\\","-").gsub("/","-") unless @dir.nil?
  name = @command.gsub(" ","_").gsub("\\","-").gsub("/","-") if @dir.nil?
  return name
end

#elapsedObject



24
# File 'lib/dev/SystemCall.rb', line 24

def elapsed; @end_time-@start_time; end

#execute(cmd) ⇒ Object



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
# File 'lib/dev/SystemCall.rb', line 85

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_hashObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dev/SystemCall.rb', line 36

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_cacheObject



32
33
34
# File 'lib/dev/SystemCall.rb', line 32

def has_cache
  File.exist?(cache_name)
end

#load_cacheObject



78
79
80
81
82
83
# File 'lib/dev/SystemCall.rb', line 78

def load_cache
  file=File.open(cache_name,'rb')
  hash = Marshal.load file.read
  file.close
  set_hash hash
end

#puts_summaryObject



186
187
188
# File 'lib/dev/SystemCall.rb', line 186

def puts_summary
  puts_summary2(@throw_on_error)
end

#puts_summary2(throw_on_error) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dev/SystemCall.rb', line 190

def puts_summary2(throw_on_error)
  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 if throw_on_error
  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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dev/SystemCall.rb', line 55

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

#system2(working_dir, command, capture_output, capture_error) ⇒ Object



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
171
172
173
# File 'lib/dev/SystemCall.rb', line 115

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_cacheObject



71
72
73
74
75
76
# File 'lib/dev/SystemCall.rb', line 71

def write_cache
  marshal_dump = Marshal.dump(get_hash)
  file=File.new(cache_name,'wb')
  file.write marshal_dump
  file.close
end