rbbt-util 5.21.38 → 5.21.39

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd8af3e8cd66f4c08804641788f1ae6384343f09
4
- data.tar.gz: ee6b16cea96ef6699bc8f9b1fa359c6af2bc6d5a
3
+ metadata.gz: 97680be71c7f39a7c72f307cf0fa154ef8fbb524
4
+ data.tar.gz: 50b7802d5af30c133c85092bef090eff33ae44bd
5
5
  SHA512:
6
- metadata.gz: ca555f21bd75ca53a3c74c92fa9e611dbbad7c09249c8d4bd3b041e0c498e7712fbe28384f3d397708cadda1efe4231e416d5e13b28fb313260b9de5ff1abecc
7
- data.tar.gz: 5ea4b7dd1719264f3e9688aa8655b4ecee425306a5181e67de03b30ee592982c2217e3a7ed9f3f0203d0cb7b1265024a246ee2420c3ea3e77df81a447dbacea2
6
+ metadata.gz: 41a0673225580d3bb28ab960edcdf85a5a5564ddf044ff7445e3267cf3697a7af0d0d2b0b4b10214a489f8fdf16d2055e35bf4ef17dd644744af9c857ba41716
7
+ data.tar.gz: 9b5211a011ac1fc4bf7c338a372781d41e2471679802cad0e4ca4047a7a498344922a088ad957bb606c867e8ba1f625587beea688ddc0bbfa211a907ca052107
@@ -93,13 +93,15 @@ module TSV
93
93
  file.open(open_options)
94
94
  when (defined? Tempfile and Tempfile)
95
95
  begin
96
- file.rewind if file.respond_to?(:rewind) and file.eof?
97
- rescue
96
+ pos = file.pos
97
+ file.rewind if file.respond_to?(:rewind) and pos != 0
98
+ rescue Exception
98
99
  end
99
100
  file
100
101
  when IO, StringIO, File
101
102
  begin
102
- file.rewind if file.respond_to?(:rewind) and file.eof?
103
+ pos = file.pos
104
+ file.rewind if file.respond_to?(:rewind) and pos != 0
103
105
  rescue
104
106
  end
105
107
  file
@@ -53,28 +53,31 @@ module CMD
53
53
 
54
54
  pid = fork {
55
55
  begin
56
+ Misc.purge_pipes(sin.last,sout.last,serr.last)
57
+
56
58
  sin.last.close
57
59
  sout.first.close
58
60
  serr.first.close
59
61
 
60
- io = in_content
61
- while IO === io
62
- io.join if io.respond_to?(:join) and not io.joined?
63
- io.close if io.respond_to?(:close) and not io.closed?
64
- io = nil
62
+ if IO === in_content
63
+ in_content.close if in_content.respond_to?(:close) and not in_content.closed?
64
+ in_content.join if in_content.respond_to?(:join) and not in_content.joined?
65
65
  end
66
66
 
67
- STDIN.reopen sin.first
68
- sin.first.close
69
67
 
70
68
  STDERR.reopen serr.last
71
69
  serr.last.close
72
70
 
71
+ STDIN.reopen sin.first
72
+ sin.first.close
73
+
73
74
  STDOUT.reopen sout.last
74
75
  sout.last.close
75
76
 
77
+
76
78
  STDOUT.sync = STDERR.sync = true
77
-
79
+
80
+
78
81
  exec(ENV, cmd)
79
82
 
80
83
  exit(-1)
@@ -89,32 +92,38 @@ module CMD
89
92
  sout.last.close
90
93
  serr.last.close
91
94
 
95
+
92
96
  sin = sin.last
93
97
  sout = sout.first
94
98
  serr = serr.first
95
-
99
+
96
100
 
97
101
  Log.debug{"CMD: [#{pid}] #{cmd}" if log}
98
102
 
99
103
  if in_content.respond_to?(:read)
100
- Thread.new do
104
+ in_thread = Thread.new do |parent|
101
105
  begin
102
- loop do
103
- break if in_content.closed?
104
- block = in_content.read Misc::BLOCK_SIZE
105
- break if block.nil? or block.empty?
106
- sin.write block
106
+ begin
107
+ while c = in_content.readpartial(Misc::BLOCK_SIZE)
108
+ sin << c
109
+ end
110
+ rescue EOFError
107
111
  end
112
+ sin.close unless sin.closed?
108
113
 
109
- sin.close unless sin.closed?
110
- in_content.join if in_content.respond_to? :join and not dont_close_in
111
- in_content.close unless in_content.closed? or dont_close_in
114
+ unless dont_close_in
115
+ in_content.close unless in_content.closed?
116
+ in_content.join if in_content.respond_to? :join
117
+ end
112
118
  rescue
119
+ parent.raise $!
113
120
  Process.kill "INT", pid
114
- raise $!
121
+ ensure
122
+ sin.close unless sin.closed?
115
123
  end
116
124
  end
117
125
  else
126
+ in_thread = nil
118
127
  sin.close
119
128
  end
120
129
 
@@ -124,15 +133,16 @@ module CMD
124
133
  pids = [pid]
125
134
  end
126
135
 
136
+
127
137
  if pipe
128
- Thread.new do
138
+ err_thread = Thread.new do
129
139
  while line = serr.gets
130
140
  Log.log line, stderr if Integer === stderr and log
131
141
  end
132
142
  serr.close
133
143
  end
134
144
 
135
- ConcurrentStream.setup sout, :pids => pids, :autojoin => no_wait, :no_fail => no_fail
145
+ ConcurrentStream.setup sout, :pids => pids, :threads => [in_thread, err_thread].compact, :autojoin => no_wait, :no_fail => no_fail
136
146
 
137
147
  sout
138
148
  else
@@ -141,23 +151,23 @@ module CMD
141
151
  while not serr.eof?
142
152
  err << serr.gets if Integer === stderr
143
153
  end
144
- serr.close
145
- end
154
+ serr.close
155
+ end
146
156
 
147
- ConcurrentStream.setup sout, :pids => pids, :autojoin => no_wait, :no_fail => no_fail
157
+ ConcurrentStream.setup sout, :pids => pids, :threads => [in_thread, err_thread].compact, :autojoin => no_wait, :no_fail => no_fail
148
158
 
149
- out = StringIO.new sout.read
150
- sout.close unless sout.closed?
159
+ out = StringIO.new sout.read
160
+ sout.close unless sout.closed?
151
161
 
152
- Process.waitpid pid
162
+ Process.waitpid pid
153
163
 
154
- if not $?.success? and not no_fail
155
- raise ProcessFailed.new "Command [#{pid}] #{cmd} failed with error status #{$?.exitstatus}.\n#{err}"
156
- else
157
- Log.log err, stderr if Integer === stderr and log
158
- end
164
+ if not $?.success? and not no_fail
165
+ raise ProcessFailed.new "Command [#{pid}] #{cmd} failed with error status #{$?.exitstatus}.\n#{err}"
166
+ else
167
+ Log.log err, stderr if Integer === stderr and log
168
+ end
159
169
 
160
- out
170
+ out
161
171
  end
162
172
  end
163
173
  end
@@ -260,6 +260,7 @@ module Misc
260
260
  def self.consume_stream(io, in_thread = false, into = nil, into_close = true, &block)
261
261
  return if Path === io
262
262
  return unless io.respond_to? :read
263
+
263
264
  if io.respond_to? :closed? and io.closed?
264
265
  io.join if io.respond_to? :join
265
266
  return
@@ -426,25 +427,13 @@ module Misc
426
427
  end
427
428
 
428
429
  line_stream = Misc.open_pipe do |line_stream_in|
429
- begin
430
- while line
431
- line_stream_in.puts line
432
- line = stream.gets
433
- end
434
- stream.join if stream.respond_to? :join
435
- rescue
436
- stream.abort if stream.respond_to? :abort
437
- raise $!
438
- end
430
+ line_stream_in.puts line
431
+ Misc.consume_stream(stream, false, line_stream_in)
439
432
  end
440
433
 
441
434
  sorted = CMD.cmd("env LC_ALL=C sort #{cmd_args || ""}", :in => line_stream, :pipe => true)
442
435
 
443
- while block = sorted.read(BLOCK_SIZE)
444
- sin.write block
445
- end
446
-
447
- sorted.join if sorted.respond_to? :join
436
+ Misc.consume_stream(sorted, false, sin)
448
437
  rescue
449
438
  if defined? step and step
450
439
  step.abort
@@ -626,7 +615,7 @@ module Misc
626
615
  else
627
616
  file1 = TmpFile.tmp_file
628
617
  erase << file1
629
- Open.write(file1, TSV.get_stream(stream1))
618
+ Misc.consume_stream(TSV.get_stream(stream1), false, file1)
630
619
  end
631
620
 
632
621
  if Path === stream2 or (String === stream2 and File.exist? stream2)
@@ -634,7 +623,7 @@ module Misc
634
623
  else
635
624
  file2 = TmpFile.tmp_file
636
625
  erase << file2
637
- Open.write(file2, TSV.get_stream(stream2))
626
+ Misc.consume_stream(TSV.get_stream(stream2), false, file2)
638
627
  end
639
628
 
640
629
  CMD.cmd("env LC_ALL=C comm #{args} '#{file1}' '#{file2}'", :pipe => true, :post => Proc.new{ erase.each{|f| FileUtils.rm f } })
@@ -107,6 +107,22 @@ row1 A B C
107
107
  assert_equal %w(## ## ## #Row row1 row2 row3), sorted.read.split("\n").collect{|l| l.split(" ").first}
108
108
  end
109
109
 
110
+ def test_sort_stream2
111
+ text =<<-EOF
112
+ ##
113
+ ##
114
+ ##
115
+ #Row LabelA LabelB LabelC
116
+ row2 AA BB CC
117
+ row3 AAA BBB CCC
118
+ row1 A B C
119
+ EOF
120
+ s = StringIO.new text
121
+ sorted = Misc.sort_stream(Misc.sort_stream(s))
122
+ assert_equal %w(## ## ## #Row row2 row3 row1), text.split("\n").collect{|l| l.split(" ").first}
123
+ assert_equal %w(## ## ## #Row row1 row2 row3), sorted.read.split("\n").collect{|l| l.split(" ").first}
124
+ end
125
+
110
126
  def test_dup_stream
111
127
  text =<<-EOF
112
128
  #: :sep=" "
@@ -201,6 +217,7 @@ line4
201
217
  text2 =<<-EOF
202
218
  line3
203
219
  line1
220
+ line5
204
221
  EOF
205
222
 
206
223
  Log.severity = 0
@@ -53,11 +53,13 @@ line33
53
53
  EOF
54
54
 
55
55
  TmpFile.with_file(text * 100) do |file|
56
- CMD.cmd("gzip #{ file }")
57
56
 
58
- gz = CMD.cmd("gunzip", :in => File.open(file + '.gz'), :pipe => true)
59
- io = CMD.cmd('tail -n 10', :in => gz, :pipe => true)
60
- assert_equal 10, io.read.split(/\n/).length
57
+ Open.open(file) do |f|
58
+ io = CMD.cmd('tail -n 10', :in => f, :pipe => true)
59
+ io2 = CMD.cmd('head -n 10', :in => io, :pipe => true)
60
+ io3 = CMD.cmd('head -n 10', :in => io2, :pipe => true)
61
+ assert_equal 10, io3.read.split(/\n/).length
62
+ end
61
63
  end
62
64
  end
63
65
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.21.38
4
+ version: 5.21.39
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-03 00:00:00.000000000 Z
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake