dev_commands 0.0.38 → 0.0.39

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee6609a2668a49ad907533a4b1798373f216628f
4
- data.tar.gz: 38827b7705161ae96bfa17e2c75c6dc7cca95a7b
3
+ metadata.gz: 7474a5054ce8e1846be673781d0418af177fbe82
4
+ data.tar.gz: c05e5655ac9b8bf7c1f5b6fffda3007e517659ba
5
5
  SHA512:
6
- metadata.gz: 463b15a0bbe02aa586816a7e33c52ec454b47926dd3ddd07839bcba6c3f02735e3561f5a444fffaa5cc8a30256281daa3c3a1c9e448d44bea7201759957177ad
7
- data.tar.gz: ec7ec0b2896dc8471f9f2d4c5c336b5c38a1bc6fa2ba1a612abf885de54baa54259ce546b51050f8b7c770403a4af30cf5cf643970fd4f6e6681475a48c911ff
6
+ metadata.gz: 463a0a95113d8d2a41bb244457c5493f340169842d003778005a48efa7dc8efd5b26fd347e07667704259522ff25428f4eec13946de643da14e7cfe166bcffa1
7
+ data.tar.gz: 22dea8db2b587cbeb0659de3f2401f7f515d4dd9c9e84d552cc4a6a74eadaaac5c420ef8e6e0f25f59163117e7bb996481ad68c2696da9859c0a4b283e509684
data/lib/command.rb CHANGED
@@ -3,10 +3,13 @@ require_relative('./array.rb')
3
3
  require_relative('./hash.rb')
4
4
  require_relative('./timer.rb')
5
5
 
6
+ BUFFER_SIZE=1024 if(!defined?(BUFFER_SIZE))
7
+
8
+
6
9
  class Command < Hash
7
10
  def initialize command
8
11
 
9
- self[:input] = ''
12
+ self[:input] = ''
10
13
  self[:timeout] = 0
11
14
  self[:directory] = ''
12
15
  self[:exit_code] = 0
@@ -28,12 +31,19 @@ class Command < Hash
28
31
  end
29
32
  end
30
33
 
34
+ def quiet?
35
+ (self.has_key?(:quiet) && self[:quiet])
36
+ end
37
+
31
38
  def execute
32
- puts "#{self[:input]}" if(!self.has_key?(:quiet) || !self[:quiet])
33
39
  pwd=Dir.pwd
34
40
  Dir.chdir(self[:directory]) if(self.has_key?(:directory) && File.exists?(self[:directory]))
35
41
  self[:directory] = pwd if(self[:directory].length==0)
36
-
42
+ if(self[:timeout] > 0)
43
+ puts "#{self[:input]} (#{self[:directory]}) timeout #{self[:timeout].to_s}" if(!quiet?)
44
+ else
45
+ puts "#{self[:input]} (#{self[:directory]})" if(!quiet?)
46
+ end
37
47
  self[:machine] = Command.machine
38
48
  self[:user] = Command.user
39
49
 
@@ -53,10 +63,23 @@ class Command < Hash
53
63
  self[:end_time] = Time.now
54
64
  else
55
65
  begin
56
- self[:output],self[:error],status= Open3.capture3(self[:input])
57
- self[:exit_code]=status.to_i
58
- self[:elapsed] = timer.elapsed_str
59
- self[:end_time] = Time.now
66
+ if(self[:timeout] <= 0)
67
+ self[:output],self[:error],status= Open3.capture3(self[:input])
68
+ self[:exit_code]=status.to_i
69
+ self[:elapsed] = timer.elapsed_str
70
+ self[:end_time] = Time.now
71
+ else
72
+ require_relative 'timeout.rb'
73
+ self[:output] = run_with_timeout(self[:input], self[:timeout], 1)
74
+
75
+ self[:elapsed] = timer.elapsed_str
76
+ self[:end_time] = Time.now
77
+
78
+ if(timer.elapsed >= self[:timeout])
79
+ self[:exit_code]=1
80
+ self[:error] = self[:error] + "timed out"
81
+ end
82
+ end
60
83
  rescue Exception => e
61
84
  self[:elapsed] = timer.elapsed_str
62
85
  self[:end_time] = Time.now
@@ -68,13 +91,10 @@ class Command < Hash
68
91
  Dir.chdir(pwd) if pwd != Dir.pwd
69
92
 
70
93
  if(self[:exit_code] != 0)
71
- if(!self.has_key?(:quiet) || !self[:quiet])
72
- puts ' '
94
+ if(!quiet?)
73
95
  puts "exit_code=#{self[:exit_code]}"
74
- puts ' '
75
96
  puts self[:output]
76
97
  puts self[:error]
77
- puts ' '
78
98
  end
79
99
  if(!self.has_key?(:ignore_failure) || !self[:ignore_failure])
80
100
  raise "#{self[:input]} failed"
data/lib/timeout.rb ADDED
@@ -0,0 +1,48 @@
1
+ ############################################################################
2
+ # The following code was copied from https://gist.github.com/lpar/1032297
3
+ # Gist title: lpar/timeout.rb
4
+ ############################################################################
5
+ # Runs a specified shell command in a separate thread.
6
+ # If it exceeds the given timeout in seconds, kills it.
7
+ # Returns any output produced by the command (stdout or stderr) as a String.
8
+ # Uses Kernel.select to wait up to the tick length (in seconds) between
9
+ # checks on the command's status
10
+ #
11
+ # If you've got a cleaner way of doing this, I'd be interested to see it.
12
+ # If you think you can do it with Ruby's Timeout module, think again.
13
+ def run_with_timeout(command, timeout, tick)
14
+ output = ''
15
+ begin
16
+ # Start task in another thread, which spawns a process
17
+ stdin, stderrout, thread = Open3.popen2e(command)
18
+ # Get the pid of the spawned process
19
+ pid = thread[:pid]
20
+ start = Time.now
21
+
22
+ while (Time.now - start) < timeout and thread.alive?
23
+ # Wait up to `tick` seconds for output/error data
24
+ Kernel.select([stderrout], nil, nil, tick)
25
+ # Try to read the data
26
+ begin
27
+ output << stderrout.read_nonblock(BUFFER_SIZE)
28
+ rescue IO::WaitReadable
29
+ # A read would block, so loop around for another select
30
+ rescue EOFError
31
+ # Command has completed, not really an error...
32
+ break
33
+ end
34
+ end
35
+ # Give Ruby time to clean up the other thread
36
+ sleep 1
37
+
38
+ if thread.alive?
39
+ # We need to kill the process, because killing the thread leaves
40
+ # the process alive but detached, annoyingly enough.
41
+ Process.kill("TERM", pid)
42
+ end
43
+ ensure
44
+ stdin.close if stdin
45
+ stderrout.close if stderrout
46
+ end
47
+ return output
48
+ end
data/spec/command_spec.rb CHANGED
@@ -66,6 +66,12 @@ describe Command do
66
66
  expect(cmd2[:input]).to eq("ruby --version")
67
67
  end
68
68
 
69
+ it "should be able to timeout" do
70
+ cmd=Command.new({ :input => 'ftp', :timeout => 0.5, :ignore_failure => true, :quiet => true})
71
+ cmd.execute
72
+ expect(cmd[:exit_code]).not_to eq(0)
73
+ end
74
+
69
75
  it "should be able to execute rake command in specific directory" do
70
76
  dir="#{File.dirname(__FILE__)}/tmp/rake_test"
71
77
  FileUtils.mkdir_p(dir) if(!File.exists?(dir))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dev_commands
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.38
4
+ version: 0.0.39
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lou Parslow
@@ -151,6 +151,7 @@ files:
151
151
  - lib/setup.rb
152
152
  - lib/test.rb
153
153
  - lib/text.rb
154
+ - lib/timeout.rb
154
155
  - lib/timer.rb
155
156
  - lib/update.rb
156
157
  - lib/upgrade.rb