execute 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cmd.rb +59 -21
  3. metadata +16 -3
  4. data/lib/IOAdapter.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 792b072f3492decab09d816547b3124be213c101
4
- data.tar.gz: ceaecac7539819cc201eec6019027daf36196b36
3
+ metadata.gz: 7b28878be7892166a674abab1f00bb7919049a43
4
+ data.tar.gz: 15d599ee83edd61212cb3e29a79d785d598978f5
5
5
  SHA512:
6
- metadata.gz: fe2fba3aac76f475db3491a01c64df80607e33227902c7ef87bcc13df502b6e8292e4ddf50247aeb132916661b6bf97e5fe194d0616abac7ac55cc0a7f9937b1
7
- data.tar.gz: 5d1c9566d177ecab0d016ee47eb990fbdadbfd023496a198d2686ebecde4951a315c47b630dabbaab318ae582438e454555f2f44038badb2bf566d0ba7fd736e
6
+ metadata.gz: 36578b7eb8ee3e861eba87a16ff8c322fb6d3d644bec4153c75aca919f5a6b7f82677e5527f85f3387531396860c28236a59c255afd78f44b7098afbf8536149
7
+ data.tar.gz: 6948248f5a73c703543fec42dfdebea68070e280893d3fb19e3f8c5df4509e782d2be043d63fe7f5ae54313bd8d42bf890bd3c1eec9550e29cb69f3fa3bc8412
data/lib/cmd.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'open3'
2
2
  require 'sys/proctable'
3
- require_relative 'IOAdapter.rb'
4
3
 
5
4
  class CMD < Hash
6
5
  private
@@ -21,45 +20,84 @@ class CMD < Hash
21
20
  def self.default_options(hash)
22
21
  hash.each { |key, value| @@default_options[key] = value}
23
22
  end
24
-
23
+
25
24
  def execute
26
- self[:output] = ''
27
- self[:error] = ''
25
+ windows_command(self, self[:command])
26
+ end
27
+
28
+ def execute_as(username)
29
+ raise "Unsported 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(cmd) { windows_command(self, cmd) }
32
+ end
33
+
34
+ private
35
+ def windows_command(hash, cmd)
28
36
  begin
29
- puts self[:command] if(self[:echo_command])
37
+ puts cmd if(hash[:echo_command] || hash[:debug])
30
38
 
31
39
  output = {output: [], error: [] }
32
- Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr|
40
+ Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
33
41
  {:output => stdout,:error => stderr}.each do |key, stream|
34
42
  Thread.new do
35
43
  until(raw_line = stream.gets).nil? do
36
44
  output[key] << raw_line
37
- puts raw_line if(self[:echo_output])
45
+ puts raw_line if(hash[:echo_output])
38
46
  end
39
47
  end
40
48
  end
41
49
  wait_thr.join
42
- self[:output] = output[:output].join
43
- self[:error] = output[:error].join
44
- self[:exit_code] = wait_thr.value.to_i
50
+ hash[:output] = output[:output].join
51
+ hash[:error] = output[:error].join
52
+ hash[:exit_code] = wait_thr.value.to_i
45
53
  end
46
54
  rescue Exception => e
47
- self[:error] = "#{self[:error]}\nException: #{e.to_s}"
48
- self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
55
+ hash[:error] = "#{hash[:error]}\nException: #{e.to_s}"
56
+ hash[:exit_code]=1 unless(hash[:exit_code].nil? || (hash[:exit_code] == 0))
49
57
  end
50
58
 
51
- if(self[:debug])
52
- puts "command: #{self[:command]}" if(self[:quiet])
53
- puts "output: #{self[:output]}"
54
- puts "error: #{self[:error]}"
55
- puts "exit_code: #{self[:exit_code]}"
59
+ if(hash[:debug])
60
+ puts "command: #{cmd}" if(hash[:quiet])
61
+ puts "output: #{hash[:output]}"
62
+ puts "error: #{hash[:error]}"
63
+ puts "exit_code: #{hash[:exit_code]}"
56
64
  end
57
65
 
58
- if((self[:exit_code] != 0) && !self[:ignore_exit_code])
59
- exception_text = "Exit code: #{self[:exit_code]}"
60
- exception_text = "#{exception_text}\n#{self[:error]}"
61
- exception_text = "#{exception_text}\n#{self[:output]}" if(self[:error].empty?)
66
+ if((hash[:exit_code] != 0) && !hash[:ignore_exit_code])
67
+ exception_text = "Exit code: #{hash[:exit_code]}"
68
+ exception_text = "#{exception_text}\n#{hash[:error]}"
69
+ exception_text = "#{exception_text}\n#{hash[:output]}" if(hash[:error].empty?)
62
70
  raise exception_text
63
71
  end
64
72
  end
73
+
74
+ def wait_on_spawned_process(cmd)
75
+ pre_execute = Sys::ProcTable.ps
76
+
77
+ pre_pids = []
78
+ pre_execute.each { |ps| pre_pids << ps.pid }
79
+
80
+ yield
81
+
82
+ match = cmd.match(/\\(?<path>.+\.exe)/i)
83
+ return if(match.nil?)
84
+
85
+ exe = match.named_captures['path']
86
+ exe = File.basename(exe)
87
+ #puts "Exe: #{exe}"
88
+
89
+ msiexe_pid = 0
90
+ post_execute = Sys::ProcTable.ps
91
+ post_execute.each do |ps|
92
+ msiexe_pid = ps.pid if((ps.name.downcase == exe.downcase) && pre_pids.index(ps.pid).nil?)
93
+ end
94
+
95
+ if(msiexe_pid != 0)
96
+ loop do
97
+ s = Sys::ProcTable.ps(msiexe_pid)
98
+ break if(s.nil?)
99
+ sleep(1)
100
+ end
101
+ end
102
+ end
65
103
  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.11
4
+ version: 0.1.12
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-24 00:00:00.000000000 Z
11
+ date: 2015-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sys-proctable
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.8
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.8
69
83
  description: Class wrapper for system commands
70
84
  email: KCCKSMarshall@gmail.com
71
85
  executables: []
@@ -74,7 +88,6 @@ extra_rdoc_files: []
74
88
  files:
75
89
  - LICENSE
76
90
  - README.md
77
- - lib/IOAdapter.rb
78
91
  - lib/cmd.rb
79
92
  homepage: http://rubygems.org/gems/execute
80
93
  licenses:
data/lib/IOAdapter.rb DELETED
@@ -1,14 +0,0 @@
1
- require 'securerandom'
2
- require 'tempfile'
3
- class IOAdapter < DelegateClass(IO)
4
- attr_accessor :output_buffer
5
- def initialize(io)
6
- __setobj__(io)
7
- @output_buffer = []
8
- end
9
-
10
- def write(string)
11
- @output_buffer << string
12
- self.__getobj__.write(string)
13
- end
14
- end