Class: Command

Inherits:
Hash
  • Object
show all
Defined in:
lib/base/command.rb

Overview

Command

execution of system commands

Keys

  • :input The input of the commands.

  • :timeout The timeout in seconds.

    a value of zero is to infinite timeout.
    defaults to zero
    
  • :directory The working directory for the command.

    defaults to the current directory
    
  • :exit_code The exit code of the command

  • :output The output contains the stdout output of the command

  • :error The error contains stderr output of the command

  • :machine The name of the machine the command executed on

  • :user The user name

  • :start_time

  • :end_time

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Command

Returns a new instance of Command.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/base/command.rb', line 31

def initialize command

   self[:input] = ''
	self[:timeout] = 0
	self[:directory] = ''
	self[:exit_code] = 0
	self[:output] = ''
	self[:error] = ''
	self[:machine] = ''
	self[:user] = ''
	self[:start_time] = nil
	self[:end_time] = nil

	if(command.kind_of?(String))
	  self[:input] = command
   end

   if(command.kind_of?(Hash))
     command.each{|k,v|self[k.to_sym]=v}
   end
end

Class Method Details

.dev_rootObject



167
168
169
170
171
172
173
# File 'lib/base/command.rb', line 167

def self.dev_root
  ["DEV_HOME","DEV_ROOT"].each {|v|
    return ENV[v].gsub('\\','/') unless ENV[v].nil?
  }
  dir=home
 return dir
end

.error(command) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/base/command.rb', line 197

def self.error command
  cmd = Command.new(command)
  cmd[:ignore_failure]=true
  cmd[:quiet]=true
  cmd.execute
  cmd[:error]
end

.execute(command) ⇒ Object



175
176
177
178
179
# File 'lib/base/command.rb', line 175

def self.execute command
  cmd = Command.new(command)
  cmd.execute
  cmd[:exit_code]
end

.exit_code(command) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/base/command.rb', line 181

def self.exit_code command
	cmd = Command.new(command)
	cmd[:ignore_failure]=true
  cmd[:quiet]=true
	cmd.execute
	cmd[:exit_code]
end

.homeObject



157
158
159
160
161
162
163
164
165
# File 'lib/base/command.rb', line 157

def self.home 
  ["USERPROFILE","HOME"].each {|v|
    return ENV[v].gsub('\\','/') unless ENV[v].nil?
  }
  dir="~"
  dir=ENV["HOME"] unless ENV["HOME"].nil?
  dir=ENV["USERPROFILE"].gsub('\\','/') unless ENV["USERPROFILE"].nil?
  return dir
end

.machineObject



143
144
145
146
147
148
149
150
151
# File 'lib/base/command.rb', line 143

def self.machine
  if !ENV['COMPUTERNAME'].nil? 
return ENV['COMPUTERNAME']
	  end

  machine = `hostname`
  machine = machine.split('.')[0] if machine.include?('.')
	  return machine.strip
end

.output(command) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/base/command.rb', line 189

def self.output command
	cmd = Command.new(command)
	cmd[:ignore_failure]=true
  cmd[:quiet]=true
	cmd.execute
	cmd[:output]
end

.userObject



153
154
155
# File 'lib/base/command.rb', line 153

def self.user
  ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER']
end

Instance Method Details

#execute(value = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/base/command.rb', line 64

def execute value=nil

   if(!value.nil? && value.is_a?(Hash))
     value.each{|k,v|self[k]=v}
   end

	pwd=Dir.pwd
	self[:directory] = pwd if(!self.has_key?(:directory) || self[:directory].length==0)

   if(self[:timeout] > 0)
     puts "#{self[:input]} (#{self[:directory]}) timeout #{self[:timeout].to_s}" if(!quiet?)
   else
	  puts "#{self[:input]} (#{self[:directory]})" if(!quiet?)
   end

	self[:machine] = Command.machine
	self[:user] = Command.user

	self[:start_time]=Time.now
	timer=Timer.new

   Dir.chdir(self[:directory]) do
 		if self[:input].include?('<%') && self[:input].include?('%>')
 		  ruby = self[:input].gsub("<%","").gsub("%>","")

 		  begin
 		    self[:output]=eval(ruby)
 		  rescue 
 		  	self[:exit_code]=1
 		  	self[:error]="unable to eval(#{ruby})"
 		  end

 		  self[:elapsed] = timer.elapsed_str
 		  self[:end_time] = Time.now
 		else
 			begin
         if(self[:timeout] <= 0)
           self[:output],self[:error],status= Open3.capture3(self[:input])
           self[:exit_code]=status.to_i
   			  self[:elapsed] = timer.elapsed_str
   			  self[:end_time] = Time.now
         else
           require_relative 'timeout.rb'
           result=run_with_timeout(self[:directory],self[:input], self[:timeout],2)
           self[:output]=result[0]
           self[:error]=result[1]
           self[:exit_code]=result[2]

           self[:elapsed] = timer.elapsed_str
           self[:end_time] = Time.now
           
           if(timer.elapsed >= self[:timeout])
             self[:exit_code]=1 
             self[:error] = self[:error] + "timed out"
           end
         end
 			rescue Exception => e
 			  self[:elapsed] = timer.elapsed_str
 			  self[:end_time] = Time.now
 			  self[:error] = "Exception: " + e.to_s
 			  self[:exit_code]=1
 		  end
 		end
   end
      

   if(self[:exit_code] != 0)
     if(!quiet?)
   	  puts "exit_code=#{self[:exit_code]}"
   	  puts self[:output]
   	  puts self[:error]
     end
   	if(!self.has_key?(:ignore_failure) || !self[:ignore_failure])
   		raise "#{self[:input]} failed\n#{self[:output]}\n#{self[:error]}" 
   	end
   end
     
end

#getFormattedTimeSpan(timespan) ⇒ Object



205
206
207
208
# File 'lib/base/command.rb', line 205

def getFormattedTimeSpan timespan
  seconds = timespan
  seconds.to_s + " sec"
end

#open(filename = '') ⇒ Object



53
54
55
56
57
58
# File 'lib/base/command.rb', line 53

def open filename=''
  @filename=filename if filename.length > 0
  self.clear
  JSON.parse(IO.read(@filename)).each{|k,v| self[k.to_s]=v}
  update
end

#quiet?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/base/command.rb', line 60

def quiet?
  (self.has_key?(:quiet) && self[:quiet])
end

#summaryObject



210
211
212
213
214
# File 'lib/base/command.rb', line 210

def summary
  duration=""
  duration=getFormattedTimeSpan(self[:end_time]-self[:start_time]) + " - " if(!self[:end_time].nil?)
  duration + "#{self[:exit_code].to_s} #{self[:input]} (#{self[:directory]})"
end

#to_htmlObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/base/command.rb', line 216

def to_html
  if self[:exit_code] == 0
  [
  	'<div><table><tr><td width="20"></td><td><pre>',
  	self[:input],
  	'</pre></td></tr></table></div>'
  ].join
  else
  [
  	'<div><table><tr><td width="20"></td><td><pre>',
  	self[:input],
    '</pre><table><tr><td width="20"></td><td><table>',
    map { |k, v| ["<tr><td><strong>#{k}</strong></td>", v.respond_to?(:to_html) ? v.to_html : "<td><span><pre>#{v}</pre></span></td></tr>"] },
    '</table>',
    '</td></tr></table></td></tr></table></div>'
  ].join
  end
end