dev_commands 0.0.49 → 0.0.50

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,41 +1,41 @@
1
- class Timer
2
- attr_accessor :start_time
3
-
4
- def initialize
5
- @start_time=Time.now
6
- end
7
-
8
- def elapsed # in seconds
9
- return Time.now-@start_time
10
- end
11
-
12
- def elapsed_str
13
- elapsed_str="[" + "%.0f" %(elapsed) + "s]"
14
- end
15
-
16
- def self.elapsed_exceeds?(name,duration_seconds)
17
- if(Timer.get_elapsed(name).nil? || Timer.get_elapsed(name) > duration_seconds)
18
- return true
19
- end
20
- return false
21
- end
22
-
23
- def self.get_elapsed(name)
24
- timestamp=get_timestamp(name)
25
- return Time.now-timestamp if(!timestamp.nil?)
26
- nil
27
- end
28
-
29
- def self.get_timestamp(name)
30
- dir=Rake.application.original_dir
31
- if(File.exists?("#{DEV[:dev_root]}/log/#{name}.timestamp"))
32
- return Time.parse(File.read("#{DEV[:dev_root]}/log/#{name}.timestamp").strip)
33
- end
34
- nil
35
- end
36
-
37
- def self.set_timestamp(name)
38
- Dir.mkdir("#{DEV_TASKS[:dev_root]}/log") if(!Dir.exists?("#{DEV_TASKS[:dev_root]}/log"))
39
- File.open("#{DEV_TASKS[:dev_root]}/log/#{name}.timestamp",'w'){|f|f.puts(Time.now.to_s)}
40
- end
1
+ class Timer
2
+ attr_accessor :start_time
3
+
4
+ def initialize
5
+ @start_time=Time.now
6
+ end
7
+
8
+ def elapsed # in seconds
9
+ return Time.now-@start_time
10
+ end
11
+
12
+ def elapsed_str
13
+ elapsed_str="[" + "%.0f" %(elapsed) + "s]"
14
+ end
15
+
16
+ def self.elapsed_exceeds?(name,duration_seconds)
17
+ if(Timer.get_elapsed(name).nil? || Timer.get_elapsed(name) > duration_seconds)
18
+ return true
19
+ end
20
+ return false
21
+ end
22
+
23
+ def self.get_elapsed(name)
24
+ timestamp=get_timestamp(name)
25
+ return Time.now-timestamp if(!timestamp.nil?)
26
+ nil
27
+ end
28
+
29
+ def self.get_timestamp(name)
30
+ dir=Rake.application.original_dir
31
+ if(File.exists?("#{DEV[:dev_root]}/log/#{name}.timestamp"))
32
+ return Time.parse(File.read("#{DEV[:dev_root]}/log/#{name}.timestamp").strip)
33
+ end
34
+ nil
35
+ end
36
+
37
+ def self.set_timestamp(name)
38
+ Dir.mkdir("#{DEV_TASKS[:dev_root]}/log") if(!Dir.exists?("#{DEV_TASKS[:dev_root]}/log"))
39
+ File.open("#{DEV_TASKS[:dev_root]}/log/#{name}.timestamp",'w'){|f|f.puts(Time.now.to_s)}
40
+ end
41
41
  end
@@ -1,8 +1,8 @@
1
- class Analyze < Array
2
- def update
3
- if(`gem list countloc`.include?('countloc ('))
4
- FileUtils.mkdir('doc') if(!File.exists?('doc'))
5
- add 'countloc -r * --html doc/countloc.html'
6
- end
7
- end
1
+ class Analyze < Array
2
+ def update
3
+ if(`gem list countloc`.include?('countloc ('))
4
+ FileUtils.mkdir('doc') if(!File.exists?('doc'))
5
+ add 'countloc -r * --html doc/countloc.html'
6
+ end
7
+ end
8
8
  end
@@ -1,30 +1,30 @@
1
- class Array
2
- def execute value=nil
3
- i=0
4
- while i < self.length
5
- self[i]=Command.new(self[i]) if(self[i].is_a?(String))
6
- self[i]=Command.new(self[i]) if(self[i].is_a?(Hash) && !self[i].is_a?(Command))
7
-
8
- if(!value.nil? && value.is_a?(Hash))
9
- value.each{|k,v|self[i][k]=v}
10
- end
11
-
12
- self[i].execute if(self[i].is_a?(Command))
13
- i=i+1
14
- end
15
- end
16
-
17
- def add command
18
- self << command if(!include?(command))
19
- end
20
-
21
- def to_html
22
- html=Array.new
23
- html << '<div>'
24
- self.each{|e|
25
- html << e.to_html if e.respond_to?(:to_html)
26
- }
27
- html << '</div>'
28
- html.join
29
- end
1
+ class Array
2
+ def execute value=nil
3
+ i=0
4
+ while i < self.length
5
+ self[i]=Command.new(self[i]) if(self[i].is_a?(String))
6
+ self[i]=Command.new(self[i]) if(self[i].is_a?(Hash) && !self[i].is_a?(Command))
7
+
8
+ if(!value.nil? && value.is_a?(Hash))
9
+ value.each{|k,v|self[i][k]=v}
10
+ end
11
+
12
+ self[i].execute if(self[i].is_a?(Command))
13
+ i=i+1
14
+ end
15
+ end
16
+
17
+ def add command
18
+ self << command if(!include?(command))
19
+ end
20
+
21
+ def to_html
22
+ html=Array.new
23
+ html << '<div>'
24
+ self.each{|e|
25
+ html << e.to_html if e.respond_to?(:to_html)
26
+ }
27
+ html << '</div>'
28
+ html.join
29
+ end
30
30
  end
@@ -1,204 +1,208 @@
1
- require 'open3'
2
- require_relative('./array.rb')
3
- require_relative('./hash.rb')
4
- require_relative('./timer.rb')
5
-
6
- BUFFER_SIZE=1024 if(!defined?(BUFFER_SIZE))
7
-
8
- # = Command
9
- #
10
- # execution of system commands
11
- #
12
- # = Keys
13
- #
14
- # - :input The input of the commands.
15
- # - :timeout The timeout in seconds.
16
- # a value of zero is to infinite timeout.
17
- # defaults to zero
18
- # - :directory The working directory for the command.
19
- # defaults to the current directory
20
- # - :exit_code The exit code of the command
21
- # - :output The output contains the stdout output of the command
22
- # - :error The error contains stderr output of the command
23
- # - :machine The name of the machine the command executed on
24
- # - :user The user name
25
- # - :start_time
26
- # - :end_time
27
- #
28
- class Command < Hash
29
- def initialize command
30
-
31
- self[:input] = ''
32
- self[:timeout] = 0
33
- self[:directory] = ''
34
- self[:exit_code] = 0
35
- self[:output] = ''
36
- self[:error] = ''
37
- self[:machine] = ''
38
- self[:user] = ''
39
- self[:start_time] = nil
40
- self[:end_time] = nil
41
-
42
- if(command.kind_of?(String))
43
- self[:input] = command
44
- end
45
-
46
- if(command.kind_of?(Hash))
47
- command.each{|k,v|
48
- self[k.to_sym]=v
49
- }
50
- end
51
- end
52
-
53
- def quiet?
54
- (self.has_key?(:quiet) && self[:quiet])
55
- end
56
-
57
- def execute value=nil
58
-
59
- if(!value.nil? && value.is_a?(Hash))
60
- value.each{|k,v|self[k]=v}
61
- end
62
-
63
- pwd=Dir.pwd
64
- self[:directory] = pwd if(!self.has_key?(:directory) || self[:directory].length==0)
65
-
66
- if(self[:timeout] > 0)
67
- puts "#{self[:input]} (#{self[:directory]}) timeout #{self[:timeout].to_s}" if(!quiet?)
68
- else
69
- puts "#{self[:input]} (#{self[:directory]})" if(!quiet?)
70
- end
71
-
72
- self[:machine] = Command.machine
73
- self[:user] = Command.user
74
-
75
- self[:start_time]=Time.now
76
- timer=Timer.new
77
-
78
- Dir.chdir(self[:directory]) do
79
- if self[:input].include?('<%') && self[:input].include?('%>')
80
- ruby = self[:input].gsub("<%","").gsub("%>","")
81
-
82
- begin
83
- self[:output]=eval(ruby)
84
- rescue
85
- self[:exit_code]=1
86
- self[:error]="unable to eval(#{ruby})"
87
- end
88
-
89
- self[:elapsed] = timer.elapsed_str
90
- self[:end_time] = Time.now
91
- else
92
- begin
93
- if(self[:timeout] <= 0)
94
- self[:output],self[:error],status= Open3.capture3(self[:input])
95
- self[:exit_code]=status.to_i
96
- self[:elapsed] = timer.elapsed_str
97
- self[:end_time] = Time.now
98
- else
99
- require_relative 'timeout.rb'
100
- #puts run_with_timeout(self[:input], self[:timeout], 1).to_s
101
- #self[:output] = run_with_timeout(self[:input], self[:timeout], 1)
102
- result=run_with_timeout(self[:directory],self[:input], self[:timeout], 1)
103
- self[:output]=result[0]
104
- self[:exit_code]=result[1]
105
-
106
- self[:elapsed] = timer.elapsed_str
107
- self[:end_time] = Time.now
108
-
109
- if(timer.elapsed >= self[:timeout])
110
- self[:exit_code]=1
111
- self[:error] = self[:error] + "timed out"
112
- end
113
- end
114
- rescue Exception => e
115
- self[:elapsed] = timer.elapsed_str
116
- self[:end_time] = Time.now
117
- self[:error] = "Exception: " + e.to_s
118
- self[:exit_code]=1
119
- end
120
- end
121
- end
122
-
123
-
124
- if(self[:exit_code] != 0)
125
- if(!quiet?)
126
- puts "exit_code=#{self[:exit_code]}"
127
- puts self[:output]
128
- puts self[:error]
129
- end
130
- if(!self.has_key?(:ignore_failure) || !self[:ignore_failure])
131
- raise "#{self[:input]} failed"
132
- end #unless (self.has_key?(:ignore_failure) && self[:ignore_failure]==true)
133
- end
134
-
135
- end
136
-
137
- def self.machine
138
- if !ENV['COMPUTERNAME'].nil?
139
- return ENV['COMPUTERNAME']
140
- end
141
-
142
- machine = `hostname`
143
- machine = machine.split('.')[0] if machine.include?('.')
144
- return machine.strip
145
- end
146
-
147
- def self.user
148
- ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER']
149
- end
150
-
151
- def self.home
152
- ["USERPROFILE","HOME"].each {|v|
153
- return ENV[v].gsub('\\','/') unless ENV[v].nil?
154
- }
155
- dir="~"
156
- dir=ENV["HOME"] unless ENV["HOME"].nil?
157
- dir=ENV["USERPROFILE"].gsub('\\','/') unless ENV["USERPROFILE"].nil?
158
- return dir
159
- end
160
-
161
- def self.dev_root
162
- ["DEV_HOME","DEV_ROOT"].each {|v|
163
- return ENV[v].gsub('\\','/') unless ENV[v].nil?
164
- }
165
- dir=home
166
- #dir=ENV["DEV_ROOT"].gsub('\\','/') unless ENV["DEV_ROOT"].nil?
167
- return dir
168
- end
169
-
170
- def self.exit_code command
171
- cmd = Command.new(command)
172
- cmd[:ignore_failure]=true
173
- cmd[:quiet]=true
174
- cmd.execute
175
- cmd[:exit_code]
176
- end
177
-
178
- def self.output command
179
- cmd = Command.new(command)
180
- cmd[:ignore_failure]=true
181
- cmd[:quiet]=true
182
- cmd.execute
183
- cmd[:output]
184
- end
185
-
186
- def to_html
187
- if self[:exit_code] == 0
188
- [
189
- '<div><table><tr><td width="20"></td><td><pre>',
190
- self[:input],
191
- '</pre></td></tr></table></div>'
192
- ].join
193
- else
194
- [
195
- '<div><table><tr><td width="20"></td><td><pre>',
196
- self[:input],
197
- '</pre><table><tr><td width="20"></td><td><table>',
198
- 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>"] },
199
- '</table>',
200
- '</td></tr></table></td></tr></table></div>'
201
- ].join
202
- end
203
- end
1
+ require 'open3'
2
+ require_relative('./array.rb')
3
+ require_relative('./hash.rb')
4
+ require_relative('./timer.rb')
5
+
6
+ BUFFER_SIZE=1024 if(!defined?(BUFFER_SIZE))
7
+
8
+ # = Command
9
+ #
10
+ # execution of system commands
11
+ #
12
+ # = Keys
13
+ #
14
+ # - :input The input of the commands.
15
+ # - :timeout The timeout in seconds.
16
+ # a value of zero is to infinite timeout.
17
+ # defaults to zero
18
+ # - :directory The working directory for the command.
19
+ # defaults to the current directory
20
+ # - :exit_code The exit code of the command
21
+ # - :output The output contains the stdout output of the command
22
+ # - :error The error contains stderr output of the command
23
+ # - :machine The name of the machine the command executed on
24
+ # - :user The user name
25
+ # - :start_time
26
+ # - :end_time
27
+ #
28
+ class Command < Hash
29
+ def initialize command
30
+
31
+ self[:input] = ''
32
+ self[:timeout] = 0
33
+ self[:directory] = ''
34
+ self[:exit_code] = 0
35
+ self[:output] = ''
36
+ self[:error] = ''
37
+ self[:machine] = ''
38
+ self[:user] = ''
39
+ self[:start_time] = nil
40
+ self[:end_time] = nil
41
+
42
+ if(command.kind_of?(String))
43
+ self[:input] = command
44
+ end
45
+
46
+ if(command.kind_of?(Hash))
47
+ command.each{|k,v|
48
+ self[k.to_sym]=v
49
+ }
50
+ end
51
+ end
52
+
53
+ def quiet?
54
+ (self.has_key?(:quiet) && self[:quiet])
55
+ end
56
+
57
+ def execute value=nil
58
+
59
+ if(!value.nil? && value.is_a?(Hash))
60
+ value.each{|k,v|self[k]=v}
61
+ end
62
+
63
+ pwd=Dir.pwd
64
+ self[:directory] = pwd if(!self.has_key?(:directory) || self[:directory].length==0)
65
+
66
+ if(self[:timeout] > 0)
67
+ puts "#{self[:input]} (#{self[:directory]}) timeout #{self[:timeout].to_s}" if(!quiet?)
68
+ else
69
+ puts "#{self[:input]} (#{self[:directory]})" if(!quiet?)
70
+ end
71
+
72
+ self[:machine] = Command.machine
73
+ self[:user] = Command.user
74
+
75
+ self[:start_time]=Time.now
76
+ timer=Timer.new
77
+
78
+ Dir.chdir(self[:directory]) do
79
+ if self[:input].include?('<%') && self[:input].include?('%>')
80
+ ruby = self[:input].gsub("<%","").gsub("%>","")
81
+
82
+ begin
83
+ self[:output]=eval(ruby)
84
+ rescue
85
+ self[:exit_code]=1
86
+ self[:error]="unable to eval(#{ruby})"
87
+ end
88
+
89
+ self[:elapsed] = timer.elapsed_str
90
+ self[:end_time] = Time.now
91
+ else
92
+ begin
93
+ if(self[:timeout] <= 0)
94
+ self[:output],self[:error],status= Open3.capture3(self[:input])
95
+ self[:exit_code]=status.to_i
96
+ self[:elapsed] = timer.elapsed_str
97
+ self[:end_time] = Time.now
98
+ else
99
+ require_relative 'timeout.rb'
100
+ #puts run_with_timeout(self[:input], self[:timeout], 1).to_s
101
+ #self[:output] = run_with_timeout(self[:input], self[:timeout], 1)
102
+ result=run_with_timeout(self[:directory],self[:input], self[:timeout], 1)
103
+ self[:output]=result[0]
104
+ self[:exit_code]=result[1]
105
+
106
+ self[:elapsed] = timer.elapsed_str
107
+ self[:end_time] = Time.now
108
+
109
+ if(timer.elapsed >= self[:timeout])
110
+ self[:exit_code]=1
111
+ self[:error] = self[:error] + "timed out"
112
+ end
113
+ end
114
+ rescue Exception => e
115
+ self[:elapsed] = timer.elapsed_str
116
+ self[:end_time] = Time.now
117
+ self[:error] = "Exception: " + e.to_s
118
+ self[:exit_code]=1
119
+ end
120
+ end
121
+ end
122
+
123
+
124
+ if(self[:exit_code] != 0)
125
+ if(!quiet?)
126
+ puts "exit_code=#{self[:exit_code]}"
127
+ puts self[:output]
128
+ puts self[:error]
129
+ end
130
+ if(!self.has_key?(:ignore_failure) || !self[:ignore_failure])
131
+ raise "#{self[:input]} failed"
132
+ end #unless (self.has_key?(:ignore_failure) && self[:ignore_failure]==true)
133
+ end
134
+
135
+ end
136
+
137
+ def self.machine
138
+ if !ENV['COMPUTERNAME'].nil?
139
+ return ENV['COMPUTERNAME']
140
+ end
141
+
142
+ machine = `hostname`
143
+ machine = machine.split('.')[0] if machine.include?('.')
144
+ return machine.strip
145
+ end
146
+
147
+ def self.user
148
+ ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER']
149
+ end
150
+
151
+ def self.home
152
+ ["USERPROFILE","HOME"].each {|v|
153
+ return ENV[v].gsub('\\','/') unless ENV[v].nil?
154
+ }
155
+ dir="~"
156
+ dir=ENV["HOME"] unless ENV["HOME"].nil?
157
+ dir=ENV["USERPROFILE"].gsub('\\','/') unless ENV["USERPROFILE"].nil?
158
+ return dir
159
+ end
160
+
161
+ def self.dev_root
162
+ ["DEV_HOME","DEV_ROOT"].each {|v|
163
+ return ENV[v].gsub('\\','/') unless ENV[v].nil?
164
+ }
165
+ dir=home
166
+ #dir=ENV["DEV_ROOT"].gsub('\\','/') unless ENV["DEV_ROOT"].nil?
167
+ return dir
168
+ end
169
+
170
+ def self.exit_code command
171
+ cmd = Command.new(command)
172
+ cmd[:ignore_failure]=true
173
+ cmd[:quiet]=true
174
+ cmd.execute
175
+ cmd[:exit_code]
176
+ end
177
+
178
+ def self.output command
179
+ cmd = Command.new(command)
180
+ cmd[:ignore_failure]=true
181
+ cmd[:quiet]=true
182
+ cmd.execute
183
+ cmd[:output]
184
+ end
185
+
186
+ def self.summary
187
+ "#{self[:exit_code].to_s} #{self[:input]} (#{self[:directory]})"
188
+ end
189
+
190
+ def to_html
191
+ if self[:exit_code] == 0
192
+ [
193
+ '<div><table><tr><td width="20"></td><td><pre>',
194
+ self[:input],
195
+ '</pre></td></tr></table></div>'
196
+ ].join
197
+ else
198
+ [
199
+ '<div><table><tr><td width="20"></td><td><pre>',
200
+ self[:input],
201
+ '</pre><table><tr><td width="20"></td><td><table>',
202
+ 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>"] },
203
+ '</table>',
204
+ '</td></tr></table></td></tr></table></div>'
205
+ ].join
206
+ end
207
+ end
204
208
  end