ztk 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -45,6 +45,11 @@ module ZTK
45
45
  super(config)
46
46
  end
47
47
 
48
+ def inspect
49
+ @hostname ||= %x(hostname -f).chomp
50
+ "#{ENV['USER']}@#{@hostname}"
51
+ end
52
+
48
53
  # Executes a local command.
49
54
  #
50
55
  # @param [String] command The command to execute.
@@ -60,10 +65,13 @@ module ZTK
60
65
  # puts cmd.exec("hostname -f").inspect
61
66
  def exec(command, options={})
62
67
  options = OpenStruct.new({ :exit_code => 0, :silence => false }.merge(options))
63
- log(:debug) { "config(#{@config.inspect})" }
64
68
  log(:debug) { "options(#{options.inspect})" }
65
69
  log(:debug) { "command(#{command.inspect})" }
66
70
 
71
+ output = ""
72
+ stdout_header = false
73
+ stderr_header = false
74
+
67
75
  parent_stdout_reader, child_stdout_writer = IO.pipe
68
76
  parent_stderr_reader, child_stderr_writer = IO.pipe
69
77
 
@@ -83,14 +91,43 @@ module ZTK
83
91
  child_stdout_writer.close
84
92
  child_stderr_writer.close
85
93
 
86
- Process.waitpid(pid)
87
-
88
- stdout = parent_stdout_reader.read
89
- stderr = parent_stderr_reader.read
90
- output = (stdout || '') + (stderr || '')
94
+ reader_writer_key = {parent_stdout_reader => :stdout, parent_stderr_reader => :stderr}
95
+ reader_writer_map = {parent_stdout_reader => @config.stdout, parent_stderr_reader => @config.stderr}
96
+
97
+ direct_log(:debug) { "===[STARTED]===[STARTED]===[#{self.inspect}]===[STARTED]===[STARTED]===\n" }
98
+ loop do
99
+ break if reader_writer_map.keys.all?{ |reader| reader.eof? }
100
+
101
+ sockets = IO.select(reader_writer_map.keys).first
102
+ sockets.each do |socket|
103
+ data = socket.read
104
+ next if (data.nil? || data.empty?)
105
+
106
+ case reader_writer_key[socket]
107
+ when :stdout then
108
+ if !stdout_header
109
+ direct_log(:debug) { "===[STDOUT]===[STDOUT]===[#{self.inspect}]===[STDOUT]===[STDOUT]===\n" }
110
+ stdout_header = true
111
+ stderr_header = false
112
+ end
113
+ reader_writer_map[socket].write(data) unless options.silence
114
+
115
+ when :stderr then
116
+ if !stderr_header
117
+ direct_log(:debug) { "===[STDERR]===[STDERR]===[#{self.inspect}]===[STDERR]===[STDERR]===\n" }
118
+ stderr_header = true
119
+ stdout_header = false
120
+ end
121
+ reader_writer_map[socket].write(data) unless options.silence
122
+ end
123
+
124
+ direct_log(:debug) { data }
125
+ output += data
126
+ end
127
+ end
128
+ direct_log(:debug) { "===[FINISHED]===[FINISHED]===[#{self.inspect}]===[FINISHED]===[FINISHED]===\n" }
91
129
 
92
- @config.stdout.write(stdout) unless options.silence
93
- @config.stderr.write(stderr) unless options.silence
130
+ Process.waitpid(pid)
94
131
 
95
132
  parent_stdout_reader.close
96
133
  parent_stderr_reader.close
@@ -204,7 +204,7 @@ module ZTK
204
204
  direct_log(:debug) { data }
205
205
 
206
206
  @config.stdout.print(data) unless options.silence
207
- output += data.chomp.strip
207
+ output += data
208
208
  end
209
209
 
210
210
  ch.on_extended_data do |c, type, data|
@@ -216,7 +216,7 @@ module ZTK
216
216
  direct_log(:debug) { data }
217
217
 
218
218
  @config.stderr.print(data) unless options.silence
219
- output += data.chomp.strip
219
+ output += data
220
220
  end
221
221
 
222
222
  ch.on_open_failed do |c, code, desc|
@@ -19,5 +19,5 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK
22
- VERSION = "0.2.0" unless const_defined?(:VERSION)
22
+ VERSION = "0.2.1" unless const_defined?(:VERSION)
23
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ztk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -212,7 +212,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
212
212
  version: '0'
213
213
  segments:
214
214
  - 0
215
- hash: -981934524806514468
215
+ hash: -2277072188839223318
216
216
  required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  none: false
218
218
  requirements:
@@ -221,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
221
  version: '0'
222
222
  segments:
223
223
  - 0
224
- hash: -981934524806514468
224
+ hash: -2277072188839223318
225
225
  requirements: []
226
226
  rubyforge_project:
227
227
  rubygems_version: 1.8.24