execute 0.1.72 → 0.1.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/execute.rb +58 -56
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06d1f79bc465057309e7d9c2404bd100c4b7aa1f
4
- data.tar.gz: 7389d6585865be0fb15455bebdcf63a5351ee6e6
3
+ metadata.gz: 55471a7bdfc605188b00b9398701e5eb7930ba5a
4
+ data.tar.gz: 65572161753f59536c950cb281ca4eadf5e7d7ec
5
5
  SHA512:
6
- metadata.gz: a79946b707b1c0571a95fae17f92477dcb8ed6a86bea62def6c023477b067971c8389abc9c1773b57a40d68d088bc25c5d4bdf187ce5d0b219d6dd317e276219
7
- data.tar.gz: b627f683659f1116f36171e06fe3e5dfeed57fa7e1e09f42abcb43081d84a4d19db462d23d0e981640c1f628212f70e0ab42394d8a6ed8bc6e014eec845faeb7
6
+ metadata.gz: e5dea0908aca1cf83e27a7442cf869ab40e2ed906eec4a7a55c291e742e3c7b68d0e687a46a0698e45099b590d0499176f75c5f0c07356bf482769c57628a3e6
7
+ data.tar.gz: 2158ac84626394f6a3d7d94bab3dc39335243a499a9bde908fcea1bc4628cfd6a3a486fec2ba2498057fb28d7c7758285019ab01c3dc7a4e628b8e32fda16ef1
@@ -3,6 +3,9 @@ require 'sys/proctable'
3
3
  require 'timeout'
4
4
  require 'benchmark'
5
5
 
6
+ class TimeoutException < Exception
7
+ end
8
+
6
9
  class Execute < Hash
7
10
  private
8
11
  @@default_options = { echo_command: true, echo_output: true, ignore_exit_code: false, debug: false }
@@ -59,7 +62,7 @@ class Execute < Hash
59
62
  puts "exit_code: #{self[:exit_code]}"
60
63
  end
61
64
 
62
- raise TimeoutError.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
65
+ raise TimeoutException.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
63
66
 
64
67
  if((self[:exit_code] != 0) && !self[:ignore_exit_code])
65
68
  exception_text = "Command: '#{self[:command]}'"
@@ -70,9 +73,9 @@ class Execute < Hash
70
73
  end
71
74
  end
72
75
 
73
- def call_popen
76
+ def call_popen
74
77
  begin
75
- output = ''
78
+ output = ''
76
79
  error = ''
77
80
 
78
81
  threads = []
@@ -82,9 +85,9 @@ class Execute < Hash
82
85
  timeout = self[:timeout] if(key?(:timeout))
83
86
 
84
87
  Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr|
85
- self[:pid] = wait_thr.pid
88
+ self[:pid] = wait_thr.pid
86
89
 
87
- unless(timeout.nil?)
90
+ unless(timeout.nil?)
88
91
  start_time = Time.now
89
92
  threads << Thread.new do
90
93
  begin
@@ -104,85 +107,84 @@ class Execute < Hash
104
107
  mutex.synchronize { stop_threads = true }
105
108
  end
106
109
  end
107
- end
110
+ end
108
111
 
109
- {:output => stdout,:error => stderr}.each do |key, stream|
110
- threads << Thread.new do
111
- begin
112
- last_pass_time = Time.now
112
+ {:output => stdout,:error => stderr}.each do |key, stream|
113
+ threads << Thread.new do
114
+ begin
115
+ last_pass_time = Time.now
113
116
  while wait_thr.alive? do
114
- while !stream.closed? &&
115
- !(char = stream.getc).nil? do
116
- case key
117
- when :output
118
- output << char
119
- putc char if(self[:echo_output])
120
- when :error
121
- error << char
122
- end
117
+ while !stream.closed? &&
118
+ !(char = stream.getc).nil? do
119
+ case key
120
+ when :output
121
+ output << char
122
+ putc char if(self[:echo_output])
123
+ when :error
124
+ error << char
125
+ end
123
126
 
124
- if(wait_thr.alive? && ((Time.now - last_pass_time).to_i > 15))
125
- last_pass_time = Time.now
126
- Thread.pass
127
- end
127
+ if(wait_thr.alive? && ((Time.now - last_pass_time).to_i > 15))
128
+ last_pass_time = Time.now
129
+ Thread.pass
130
+ end
131
+ end
132
+ break if(stop_threads)
133
+ sleep(0.1)
128
134
  end
129
- break if(stop_threads)
130
- sleep(0.1)
131
- end
132
- mutex.synchronize { stop_threads = true }
133
- rescue Exception
134
- mutex.synchronize { stop_threads = true }
135
- end
135
+ mutex.synchronize { stop_threads = true }
136
+ rescue Exception
137
+ mutex.synchronize { stop_threads = true }
138
+ end
136
139
  end
137
140
  end
138
141
 
139
- threads.each { |thr| thr.join }
140
-
142
+ threads.each { |thr| thr.join }
141
143
  self[:output] = output unless(output.empty?)
142
- self[:error] = error unless(error.empty?)
143
- self[:exit_code] = wait_thr.value.to_i
144
+ self[:error] = error unless(error.empty?)
145
+ self[:exit_code] = wait_thr.value.to_i
144
146
  end
145
147
  rescue Exception => e
146
148
  self[:error] = "#{self[:error]}\nException: #{e.to_s}"
147
- self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
149
+ self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
148
150
  end
149
- end
150
- def call_capture
151
+ end
152
+ def call_capture
151
153
  begin
152
- if(key?(:timeout))
154
+ if(key?(:timeout))
153
155
  start_time = Time.now
154
- Thread.new do
155
- while !key?(:exit_code) do
156
- sleep(0.1)
157
- if((Time.now - start_time).to_f > self[:timeout])
158
- self[:timed_out] = true
159
- interrupt
160
- sleep(0.1)
161
- break
162
- end
163
- end
164
- end
156
+ Thread.new do
157
+ while !key?(:exit_code) do
158
+ sleep(0.1)
159
+ if((Time.now - start_time).to_f > self[:timeout])
160
+ self[:timed_out] = true
161
+ interrupt
162
+ sleep(0.1)
163
+ break
164
+ end
165
+ end
166
+ end
165
167
  end
166
168
 
167
- self[:output] = self[:error] = ''
169
+ self[:output] = self[:error] = ''
168
170
  self[:output], self[:error], status = Open3.capture3(self[:command])
169
171
  self[:exit_code] = status.to_i
170
172
 
171
173
  puts self[:output] if(self[:echo_output] && !self[:output].empty?)
172
174
 
173
- raise TimeoutError.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
175
+ raise TimeoutException.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
174
176
  rescue Exception => e
175
177
  self[:error] = "#{self[:error]}\nException: #{e.to_s}"
176
178
  self[:exit_code]=1 unless(!self[:exit_code].nil? || (self[:exit_code] == 0))
177
179
  end
178
- end
180
+ end
179
181
 
180
- def []=(key,value)
181
- mutex = Mutex.new
182
+ def []=(key,value)
183
+ mutex = Mutex.new
182
184
  mutex.synchronize { super(key, value) }
183
- end
185
+ end
184
186
 
185
- def [](key)
187
+ def [](key)
186
188
  value = nil
187
189
  mutex = Mutex.new
188
190
  mutex.synchronize { value = super(key) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: execute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.72
4
+ version: 0.1.73
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Marshall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-02 00:00:00.000000000 Z
11
+ date: 2018-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  requirements: []
112
112
  rubyforge_project:
113
- rubygems_version: 2.4.5.1
113
+ rubygems_version: 2.5.2
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Class wrapper for system commands