net-ssh-open3 0.1.2 → 0.1.3

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/net-ssh-open3.rb +48 -20
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03ff6821052cebdec70bfe367fa1c3da6211b1ae
4
- data.tar.gz: 3aff420588c6faa56eabfec790a29acd6ec45566
3
+ metadata.gz: 6cc5b2173309f5acbd7d9b1dfef343daea71cc6f
4
+ data.tar.gz: 769b292fabbab5b0aba1e91c977a85be3362b956
5
5
  SHA512:
6
- metadata.gz: 0c51a2d3c56cb849ffc46540c70579cc40529223f210b3a268369919412e986a47897c75fdd070ccec2222b6ff9918c6dc098dfc077e8578d0224ecc610135cc
7
- data.tar.gz: 5b8caa2194cf369a502f1f6f7064c98e3e8f4f6d26e91b2f2c49fc29d6334291d4df4c0aa03c73aa9deba8630f1219cd51b9ec4100b2f7dd9bda6447bbc02388
6
+ metadata.gz: df50ddf6685051878ea33fb7bb351fbe23d3d5b34da2c94f9888c0bb258968947ded31eb7ee09af9b2b8a2a82c95f488651a2377dcd4ee690f61ad6f764f400a
7
+ data.tar.gz: 5a2a4df652343408c8ab4fbd418129d607f38a0e261677ef16812dd8029aacdc5ae10da81e0761f1ebf27ff9306d6f079a212a1d3326dd1e748bc5e561a59b89
data/lib/net-ssh-open3.rb CHANGED
@@ -109,11 +109,14 @@ module Net::SSH
109
109
  #
110
110
  # *optional* +options+: options hash, keys:
111
111
  # * +redirects+: Hash of redirections which will be appended to a command line (you can't transfer a pipe to a remote system).
112
- # Key: one of +:in+, +:out+, +:err+ or a +String+, value: +Integer+ to redirect to fd, +String+ to redirect to a file.
112
+ # Key: one of +:in+, +:out+, +:err+ or a +String+, value: +Integer+ to redirect to remote fd, +String+ to redirect to a file.
113
+ # If a key is a Symbol, local +IO+ may be specified as a value. In this case, block receives +nil+ for the corresponding IO.
113
114
  # Example:
114
115
  # { '>>' => '/tmp/log', err: 1 }
115
116
  # translates to
116
117
  # '>>/tmp/log 2>&1'
118
+ # Another example:
119
+ # { in: $stdin, out: $stdout, err: $stderr }
117
120
  # * +channel_retries+: +Integer+ number of retries in case of channel open failure (ssh server usually limits a session to 10 channels),
118
121
  # or an array of [+retries+, +delay+]
119
122
  # * +stdin_data+: for +capture*+ only, specifies data to be immediately sent to +stdin+ of a remote process.
@@ -137,15 +140,10 @@ module Net::SSH
137
140
  # # => ["", #<Net::SSH::Process::Status: pid 1744 QUIT (signal 3) core true>]
138
141
  # Note that just closing stdin is not enough for PTY. You should explicitly send VEOF as a first char of a line, see termios(3).
139
142
  module Open3
140
- SSH_EXTENDED_DATA_STDERR = 1 #:nodoc:
141
- REMOTE_PACKET_THRESHOLD = 512 # headers etc #:nodoc:
142
-
143
- private_constant :SSH_EXTENDED_DATA_STDERR, :REMOTE_PACKET_THRESHOLD
144
-
145
143
  # Captures stdout only. Returns [String, Net::SSH::Process::Status]
146
144
  def capture2(*args)
147
145
  stdout = StringIO.new
148
- stdin_data = args.last[:stdin_data] if Hash === args.last
146
+ stdin_data = extract_open3_options(args)[:stdin_data]
149
147
 
150
148
  run_popen(*args,
151
149
  stdin: stdin_data,
@@ -158,7 +156,7 @@ module Net::SSH
158
156
  # Captures stdout and stderr into one string. Returns [String, Net::SSH::Process::Status]
159
157
  def capture2e(*args)
160
158
  stdout = StringIO.new
161
- stdin_data = args.last[:stdin_data] if Hash === args.last
159
+ stdin_data = extract_open3_options(args)[:stdin_data]
162
160
 
163
161
  run_popen(*args,
164
162
  stdin: stdin_data,
@@ -172,7 +170,7 @@ module Net::SSH
172
170
  # Captures stdout and stderr into separate strings. Returns [String, String, Net::SSH::Process::Status]
173
171
  def capture3(*args)
174
172
  stdout, stderr = StringIO.new, StringIO.new
175
- stdin_data = args.last[:stdin_data] if Hash === args.last
173
+ stdin_data = extract_open3_options(args)[:stdin_data]
176
174
 
177
175
  run_popen(*args,
178
176
  stdin: stdin_data,
@@ -190,9 +188,10 @@ module Net::SSH
190
188
  # Careful: don't forget to read +stderr+, otherwise if your process generates too much stderr output
191
189
  # the pipe may overload and ssh loop will get stuck writing to it.
192
190
  def popen3(*args, &block)
193
- stdin_inner, stdin_outer = IO.pipe
194
- stdout_outer, stdout_inner = IO.pipe
195
- stderr_outer, stderr_inner = IO.pipe
191
+ redirects = extract_open3_options(args)[:redirects]
192
+ stdin_inner, stdin_outer = open3_ios_for(:in, redirects)
193
+ stdout_outer, stdout_inner = open3_ios_for(:out, redirects)
194
+ stderr_outer, stderr_inner = open3_ios_for(:err, redirects)
196
195
 
197
196
  run_popen(*args,
198
197
  stdin: stdin_inner,
@@ -204,8 +203,9 @@ module Net::SSH
204
203
 
205
204
  # Yields +stdin+, +stdout-stderr+, +waiter_thread+ into a block.
206
205
  def popen2e(*args, &block)
207
- stdin_inner, stdin_outer = IO.pipe
208
- stdout_outer, stdout_inner = IO.pipe
206
+ redirects = extract_open3_options(args)[:redirects]
207
+ stdin_inner, stdin_outer = open3_ios_for(:in, redirects)
208
+ stdout_outer, stdout_inner = open3_ios_for(:out, redirects)
209
209
 
210
210
  run_popen(*args,
211
211
  stdin: stdin_inner,
@@ -217,8 +217,9 @@ module Net::SSH
217
217
 
218
218
  # Yields +stdin+, +stdout+, +waiter_thread+ into a block.
219
219
  def popen2(*args, &block)
220
- stdin_inner, stdin_outer = IO.pipe
221
- stdout_outer, stdout_inner = IO.pipe
220
+ redirects = extract_open3_options(args)[:redirects]
221
+ stdin_inner, stdin_outer = open3_ios_for(:in, redirects)
222
+ stdout_outer, stdout_inner = open3_ios_for(:out, redirects)
222
223
 
223
224
  run_popen(*args,
224
225
  stdin: stdin_inner,
@@ -228,6 +229,27 @@ module Net::SSH
228
229
  end
229
230
 
230
231
  private
232
+ def extract_open3_options(args, pop = false)
233
+ if Hash === args.last
234
+ pop ? args.pop : args.last
235
+ else
236
+ {}
237
+ end
238
+ end
239
+
240
+ def open3_ios_for(name, redirects)
241
+ if redirects and user_supplied_io = redirects[name] and IO === user_supplied_io
242
+ name == :in ? user_supplied_io : [nil, user_supplied_io]
243
+ else
244
+ IO.pipe
245
+ end
246
+ end
247
+
248
+ SSH_EXTENDED_DATA_STDERR = 1 #:nodoc:
249
+ REMOTE_PACKET_THRESHOLD = 512 # headers etc #:nodoc:
250
+
251
+ private_constant :SSH_EXTENDED_DATA_STDERR, :REMOTE_PACKET_THRESHOLD
252
+
231
253
  def install_channel_callbacks(channel, options)
232
254
  logger, stdin, stdout, stderr =
233
255
  options[:logger], options[:stdin], options[:stdout], options[:stderr]
@@ -317,19 +339,22 @@ module Net::SSH
317
339
  end
318
340
  end
319
341
 
320
- REDIRECT_MAPPING = { #:nodoc:
342
+ REDIRECT_MAPPING = {
321
343
  in: '<',
322
344
  out: '>',
323
345
  err: '2>'
324
346
  }
347
+ private_constant :REDIRECT_MAPPING
325
348
 
326
349
  def run_popen(*args, internal_options)
327
- options = (args.pop if Hash === args.last) || {}
350
+ options = extract_open3_options(args, true)
328
351
  env = (args.shift if Hash === args.first) || {}
329
352
  cmdline = args.size == 1 ? args.first : Shellwords.join(args.map(&:to_s))
330
353
 
331
354
  redirects = options[:redirects] and redirects.each_pair do |fd_and_dir, destination|
332
- cmdline += " #{REDIRECT_MAPPING[fd_and_dir] || fd_and_dir}#{popen_io_name(destination)}"
355
+ if destination = popen_io_name(destination)
356
+ cmdline << " #{REDIRECT_MAPPING[fd_and_dir] || fd_and_dir}#{destination}"
357
+ end
333
358
  end
334
359
  logger = options[:logger]
335
360
  pty_options = options[:pty]
@@ -377,7 +402,10 @@ module Net::SSH
377
402
  end
378
403
 
379
404
  def popen_io_name(name)
380
- Fixnum === name ? "&#{name}" : Shellwords.shellescape(name)
405
+ case name
406
+ when Fixnum then "&#{name}"
407
+ when String then Shellwords.shellescape(name)
408
+ end
381
409
  end
382
410
  end
383
411
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-ssh-open3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Sheremet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-25 00:00:00.000000000 Z
11
+ date: 2013-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh