execute 0.1.26 → 0.1.27

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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cmd.rb +18 -62
  3. data/lib/cmd_windows.rb +44 -0
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f810d7646d2b721fb71344f109e3243f4f4bc96
4
- data.tar.gz: ee1aff1b3da2a980e752161aea388bcd7312c11f
3
+ metadata.gz: 8092c81d49b0155e507821497351080cd849e2fd
4
+ data.tar.gz: a7d996ca8087684e3011f54eb7ce5c44b66fc8ad
5
5
  SHA512:
6
- metadata.gz: 3d5126fa636fc472c350e46137e7c4650d2bdbdf2955eef2b527c1c52d725c89222e1bb0c9937c1304e0d194f4f578462579bc6b0b757b21d76979922304a84b
7
- data.tar.gz: 466bc8fb18db1cb87905c1708a9b8db1261e795d7aeca020ea122204df6567894bd3e008e6272722fc7f848fb69c966cead035cc303cf4d6e9e7ef9792581d00
6
+ metadata.gz: 48009fb0c04c7f0f624444053bb8fc16d80204d5a9d041b2dbe5d4e834f05d4f85d9c8f6d0d3848d6df00428b1634e4f73ce351c89cbae2256e2d65d6e941566
7
+ data.tar.gz: 0701ebd759826e07725561b604230e45728f57c33acff06294c639e571bc94e2bd97f912f0c021189568d3b9643695e66779aabb5a52ef1d14128e9914774a5b
data/lib/cmd.rb CHANGED
@@ -22,29 +22,18 @@ class CMD < Hash
22
22
  end
23
23
 
24
24
  def execute
25
- windows_command(self, self[:command])
26
- end
27
-
28
- def execute_as(username)
29
- raise "Unsupported on operating system #{RbConfig::CONFIG["host_os"]}" unless(RbConfig::CONFIG["host_os"].include?("mingw"))
30
- cmd = "runas /noprofile /savecred /user:#{username} \"#{self[:command]}\""
31
- wait_on_spawned_process(self,cmd) { windows_command(self, cmd) }
32
- end
33
-
34
- private
35
- def windows_command(hash, cmd)
36
25
  begin
37
- puts cmd if(hash[:echo_command] || hash[:debug])
26
+ puts self[:command] if(self[:echo_command] || self[:debug])
38
27
 
39
28
  output = {output: [], error: [] }
40
- Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
41
- hash[:pid] = wait_thr.pid
29
+ Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr|
30
+ self[:pid] = wait_thr.pid
42
31
  {:output => stdout,:error => stderr}.each do |key, stream|
43
32
  Thread.new do
44
33
  while wait_thr.alive? do
45
34
  if(!(char = stream.getc).nil?)
46
35
  output[key] << char
47
- putc char if(hash[:echo_output])
36
+ putc char if(self[:echo_output])
48
37
  else
49
38
  sleep(0.1)
50
39
  end
@@ -71,60 +60,27 @@ class CMD < Hash
71
60
  #end
72
61
 
73
62
  wait_thr.join
74
- hash[:output] = output[:output].join unless(output[:output].empty?)
75
- hash[:error] = output[:error].join unless(output[:error].empty?)
76
- hash[:exit_code] = wait_thr.value.to_i
63
+ self[:output] = output[:output].join unless(output[:output].empty?)
64
+ self[:error] = output[:error].join unless(output[:error].empty?)
65
+ self[:exit_code] = wait_thr.value.to_i
77
66
  end
78
67
  rescue Exception => e
79
- hash[:error] = "#{hash[:error]}\nException: #{e.to_s}"
80
- hash[:exit_code]=1 unless(hash[:exit_code].nil? || (hash[:exit_code] == 0))
68
+ self[:error] = "#{self[:error]}\nException: #{e.to_s}"
69
+ self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
81
70
  end
82
71
 
83
- if(hash[:debug])
84
- puts "command: #{cmd}" if(hash[:quiet])
85
- puts "output: #{hash[:output]}"
86
- puts "error: #{hash[:error]}"
87
- puts "exit_code: #{hash[:exit_code]}"
72
+ if(self[:debug])
73
+ puts "command: #{self[:command]}" if(self[:quiet])
74
+ puts "output: #{self[:output]}"
75
+ puts "error: #{self[:error]}"
76
+ puts "exit_code: #{self[:exit_code]}"
88
77
  end
89
78
 
90
- if((hash[:exit_code] != 0) && !hash[:ignore_exit_code])
91
- exception_text = "Exit code: #{hash[:exit_code]}"
92
- exception_text = "#{exception_text}\n#{hash[:error]}"
93
- exception_text = "#{exception_text}\n#{hash[:output]}" if(hash[:error].empty?)
79
+ if((self[:exit_code] != 0) && !self[:ignore_exit_code])
80
+ exception_text = "Exit code: #{self[:exit_code]}"
81
+ exception_text = "#{exception_text}\n#{self[:error]}"
82
+ exception_text = "#{exception_text}\n#{self[:output]}" if(self[:error].empty?)
94
83
  raise exception_text
95
84
  end
96
85
  end
97
-
98
- def wait_on_spawned_process(hash, cmd)
99
- yield
100
- post_execute = Sys::ProcTable.ps
101
-
102
- child_processes = []
103
- post_execute.each { |ps| child_processes << ps.pid if(ps.include?(hash[:pid])) }
104
-
105
- trap("INT") do
106
- child_processes.each do |pid|
107
- s = Sys::ProcTable.ps(pid)
108
- begin
109
- if(!s.nil?)
110
- out_rd,out_wr = IO.pipe
111
- err_rd,err_wr = IO.pipe
112
- system("taskkill /pid #{pid}", :out => out_wr, :err => err_wr)
113
- end
114
- rescue Exception => e
115
- end
116
- end
117
- exit
118
- end
119
-
120
- loop do
121
- all_exited = true
122
- child_processes.each do |pid|
123
- s = Sys::ProcTable.ps(pid)
124
- all_exited = false unless(s.nil?)
125
- end
126
- break if(all_exited)
127
- sleep(0.2)
128
- end
129
- end
130
86
  end
@@ -0,0 +1,44 @@
1
+ require 'open3'
2
+ require 'sys/proctable'
3
+ require_relative ('cmd')
4
+
5
+ class CMD < Hash
6
+ def execute_as(username)
7
+ self[:command] = "runas /noprofile /savecred /user:#{username} \"#{self[:command]}\""
8
+ wait_on_spawned_process(self) { self.execute }
9
+ end
10
+
11
+ private
12
+ def wait_on_spawned_process(cmd)
13
+ yield
14
+ post_execute = Sys::ProcTable.ps
15
+
16
+ child_processes = []
17
+ post_execute.each { |ps| child_processes << ps.pid if(ps.include?(cmd[:pid])) }
18
+
19
+ trap("INT") do
20
+ child_processes.each do |pid|
21
+ s = Sys::ProcTable.ps(pid)
22
+ begin
23
+ if(!s.nil?)
24
+ out_rd,out_wr = IO.pipe
25
+ err_rd,err_wr = IO.pipe
26
+ system("taskkill /pid #{pid}", :out => out_wr, :err => err_wr)
27
+ end
28
+ rescue Exception => e
29
+ end
30
+ end
31
+ exit
32
+ end
33
+
34
+ loop do
35
+ all_exited = true
36
+ child_processes.each do |pid|
37
+ s = Sys::ProcTable.ps(pid)
38
+ all_exited = false unless(s.nil?)
39
+ end
40
+ break if(all_exited)
41
+ sleep(0.2)
42
+ end
43
+ end
44
+ end
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.26
4
+ version: 0.1.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Marshall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-29 00:00:00.000000000 Z
11
+ date: 2015-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,6 +89,7 @@ files:
89
89
  - LICENSE
90
90
  - README.md
91
91
  - lib/cmd.rb
92
+ - lib/cmd_windows.rb
92
93
  homepage: http://rubygems.org/gems/execute
93
94
  licenses:
94
95
  - Apache 2.0