execute 0.1.26 → 0.1.27
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cmd.rb +18 -62
- data/lib/cmd_windows.rb +44 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8092c81d49b0155e507821497351080cd849e2fd
|
4
|
+
data.tar.gz: a7d996ca8087684e3011f54eb7ce5c44b66fc8ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
26
|
+
puts self[:command] if(self[:echo_command] || self[:debug])
|
38
27
|
|
39
28
|
output = {output: [], error: [] }
|
40
|
-
Open3.popen3(
|
41
|
-
|
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(
|
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
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
80
|
-
|
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(
|
84
|
-
puts "command: #{
|
85
|
-
puts "output: #{
|
86
|
-
puts "error: #{
|
87
|
-
puts "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((
|
91
|
-
exception_text = "Exit code: #{
|
92
|
-
exception_text = "#{exception_text}\n#{
|
93
|
-
exception_text = "#{exception_text}\n#{
|
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
|
data/lib/cmd_windows.rb
ADDED
@@ -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.
|
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-
|
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
|