rb-process 0.2.3 → 0.2.4
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +10 -1
- data/README.md +1 -1
- data/lib/rb/io/multi_writer.rb +1 -2
- data/lib/rb/io/stapled.rb +6 -23
- data/lib/rb/process/version.rb +1 -1
- data/lib/rb/process.rb +48 -31
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9db5267a9444b8ccfd63444afaf52f0464cede6a9ca0057cbbb7394260dc786b
|
|
4
|
+
data.tar.gz: 19733804591980297c8abd49ebcab099f4079e5278bb7c79577ccde9f38954c9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a5cc08c8e200885ad9fb41443f95659af1caad485df71634aace7dc9ba0416034a28f849c9aefeda557c08bb9bb71ae8373a0bb9fe45464891d23a6ada540bee
|
|
7
|
+
data.tar.gz: b24cc79693d1f1a2aaca5be41896ed15329eb831eeeb7383ef928fe4e80f9e34b88987d10f6093e93a35a623985557ab8cef55671c48e0c9a1e1b4b131b38802
|
data/.rubocop.yml
CHANGED
|
@@ -10,8 +10,17 @@ Metrics/BlockLength:
|
|
|
10
10
|
Metrics/AbcSize:
|
|
11
11
|
Enabled: false
|
|
12
12
|
|
|
13
|
+
Metrics/CyclomaticComplexity:
|
|
14
|
+
Enabled: false
|
|
15
|
+
|
|
16
|
+
Metrics/PerceivedComplexity:
|
|
17
|
+
Enabled: false
|
|
18
|
+
|
|
13
19
|
Style/ConditionalAssignment:
|
|
14
20
|
Enabled: false
|
|
15
21
|
|
|
16
22
|
Style/RaiseArgs:
|
|
17
|
-
Enabled: false
|
|
23
|
+
Enabled: false
|
|
24
|
+
|
|
25
|
+
Style/SpecialGlobalVars:
|
|
26
|
+
EnforcedStyle: use_builtin_english_names
|
data/README.md
CHANGED
|
@@ -17,7 +17,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
|
17
17
|
```ruby
|
|
18
18
|
require "rb/process"
|
|
19
19
|
|
|
20
|
-
Process.run("bash"
|
|
20
|
+
Process.run("bash") { |pipe| pipe.puts "uname" }
|
|
21
21
|
# => "Linux\n"
|
|
22
22
|
|
|
23
23
|
Process.output("ls spec").each_line(chomp: true).to_a
|
data/lib/rb/io/multi_writer.rb
CHANGED
|
@@ -18,12 +18,11 @@ class IO
|
|
|
18
18
|
attr_accessor :writers
|
|
19
19
|
attr_reader :closed
|
|
20
20
|
|
|
21
|
-
@closed = false
|
|
22
|
-
|
|
23
21
|
alias_method :sync_close?, :sync_close
|
|
24
22
|
alias_method :closed?, :closed
|
|
25
23
|
|
|
26
24
|
def initialize(*writers, sync_close: false)
|
|
25
|
+
@closed = false
|
|
27
26
|
@writers = writers
|
|
28
27
|
@sync_close = sync_close
|
|
29
28
|
end
|
data/lib/rb/io/stapled.rb
CHANGED
|
@@ -23,32 +23,25 @@ class IO
|
|
|
23
23
|
# Underlying `IO`s might have a different status.
|
|
24
24
|
attr_reader :closed
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
WRITER_DELEGATE = %w[
|
|
29
|
-
<<
|
|
30
|
-
].freeze
|
|
31
|
-
|
|
32
|
-
READER_DELEGATE = %w[
|
|
33
|
-
eof
|
|
34
|
-
eof?
|
|
35
|
-
].freeze
|
|
26
|
+
WRITER_DELEGATE = IO.instance_methods.grep(/^put|^print|write|<</).freeze
|
|
27
|
+
READER_DELEGATE = IO.instance_methods.grep(/^get[a-z]*|read|each|eof/).freeze
|
|
36
28
|
|
|
37
29
|
alias_method :sync_close?, :sync_close
|
|
38
30
|
alias_method :closed?, :closed
|
|
39
31
|
|
|
40
32
|
# Creates a new `IO::Stapled` which reads from *reader* and writes to *writer*.
|
|
41
33
|
def initialize(reader, writer, sync_close: false)
|
|
34
|
+
@closed = false
|
|
42
35
|
@reader = reader
|
|
43
36
|
@writer = writer
|
|
44
37
|
@sync_close = sync_close
|
|
45
38
|
end
|
|
46
39
|
|
|
47
40
|
def method_missing(name, *args, &block)
|
|
48
|
-
if
|
|
41
|
+
if WRITER_DELEGATE.include?(name)
|
|
49
42
|
check_open
|
|
50
43
|
@writer.send(name, *args, &block)
|
|
51
|
-
elsif
|
|
44
|
+
elsif READER_DELEGATE.include?(name)
|
|
52
45
|
check_open
|
|
53
46
|
@reader.send(name, *args, &block)
|
|
54
47
|
else
|
|
@@ -57,17 +50,7 @@ class IO
|
|
|
57
50
|
end
|
|
58
51
|
|
|
59
52
|
def respond_to_missing?(name, include_private = false)
|
|
60
|
-
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def write_methods?(name)
|
|
64
|
-
name.include?("put") || name.include?("prin") ||
|
|
65
|
-
name.include?("write") || WRITER_DELEGATE.include?(name)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def read_methods?(name)
|
|
69
|
-
name.include?("get") || name.include?("read") ||
|
|
70
|
-
name.include?("each") || READER_DELEGATE.include?(name)
|
|
53
|
+
WRITER_DELEGATE.include?(name) || READER_DELEGATE.include?(name) || super
|
|
71
54
|
end
|
|
72
55
|
|
|
73
56
|
# Flushes `writer`.
|
data/lib/rb/process/version.rb
CHANGED
data/lib/rb/process.rb
CHANGED
|
@@ -17,11 +17,11 @@ module Process
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def method_missing(name, *args, &block)
|
|
20
|
-
@status.send(name, *args, &block)
|
|
20
|
+
@status.respond_to?(name) ? @status.send(name, *args, &block) : super
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def respond_to_missing?(name, include_private = false)
|
|
24
|
-
|
|
24
|
+
@status.respond_to?(name, include_private) || super
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def exit_code
|
|
@@ -35,47 +35,47 @@ module Process
|
|
|
35
35
|
alias_method :ok?, :success?
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
def self.run(*args, out: $stdout, err: $stderr, **options)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
parent_io = [
|
|
38
|
+
def self.run(*args, out: $stdout, err: $stderr, exception: false, **options)
|
|
39
|
+
stdout_reader, stdout_writer = IO.pipe
|
|
40
|
+
stderr_reader, stderr_writer = IO.pipe
|
|
41
|
+
childs_io = [stdout_writer, stderr_writer]
|
|
42
|
+
parent_io = [stdout_reader, stderr_reader]
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
out_strio = StringIO.new
|
|
45
|
+
err_strio = StringIO.new
|
|
46
|
+
out_multiwriter = out.is_a?(Array) ? IO::MultiWriter.new(*out, out_strio) : IO::MultiWriter.new(out, out_strio)
|
|
47
|
+
err_multiwriter = err.is_a?(Array) ? IO::MultiWriter.new(*err, err_strio) : IO::MultiWriter.new(err, err_strio)
|
|
48
48
|
|
|
49
49
|
if block_given?
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
parent_io <<
|
|
50
|
+
stdin_reader, stdin_writer = IO.pipe
|
|
51
|
+
stdin_writer.sync = true
|
|
52
|
+
childs_io << stdin_reader
|
|
53
|
+
parent_io << stdin_writer
|
|
54
54
|
|
|
55
|
-
pid = Process.spawn(*args, **options, in:
|
|
56
|
-
|
|
55
|
+
pid = Process.spawn(*args, **options, in: stdin_reader, out: stdout_writer, err: stderr_writer)
|
|
56
|
+
childs_io.each(&:close)
|
|
57
57
|
|
|
58
|
-
pipe = IO::Stapled.new(
|
|
58
|
+
pipe = IO::Stapled.new(stdout_reader, stdin_writer)
|
|
59
59
|
begin
|
|
60
60
|
yield pipe
|
|
61
61
|
ensure
|
|
62
|
-
|
|
62
|
+
stdin_writer.close unless stdin_writer.closed?
|
|
63
63
|
pipe.close
|
|
64
64
|
end
|
|
65
65
|
else
|
|
66
|
-
pid = Process.spawn(*args, **options, out:
|
|
67
|
-
|
|
66
|
+
pid = Process.spawn(*args, **options, out: stdout_writer, err: stderr_writer)
|
|
67
|
+
childs_io.each(&:close)
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
t1 = Thread.new do
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
stdout_reader.each_line do |line|
|
|
72
|
+
out_multiwriter.write(line)
|
|
73
73
|
end
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
t2 = Thread.new do
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
stderr_reader.each_line do |line|
|
|
78
|
+
err_multiwriter.write(line)
|
|
79
79
|
end
|
|
80
80
|
end
|
|
81
81
|
|
|
@@ -83,21 +83,38 @@ module Process
|
|
|
83
83
|
t1.join
|
|
84
84
|
t2.join
|
|
85
85
|
ensure
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
out_multiwriter.close
|
|
87
|
+
err_multiwriter.close
|
|
88
88
|
parent_io.each { |io| io.close unless io.closed? }
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
pid, status = Process.wait2(pid)
|
|
92
92
|
if status.success?
|
|
93
|
-
|
|
93
|
+
out_strio.string
|
|
94
94
|
else
|
|
95
|
-
Err.new(
|
|
95
|
+
Err.new(out_strio.string, err_strio.string, status)
|
|
96
96
|
end
|
|
97
|
+
rescue Errno::ENOENT => e
|
|
98
|
+
raise e if exception
|
|
99
|
+
|
|
100
|
+
Err.new(nil, nil, $?)
|
|
97
101
|
end
|
|
98
102
|
|
|
99
|
-
def self.output(
|
|
100
|
-
IO.
|
|
103
|
+
def self.output(*args, **options)
|
|
104
|
+
stdout_reader, stdout_writer = IO.pipe
|
|
105
|
+
pid = Process.spawn(*args, **options, out: stdout_writer)
|
|
106
|
+
stdout_writer.close
|
|
107
|
+
|
|
108
|
+
pid, status = Process.wait2(pid)
|
|
109
|
+
if status.exited? && status.success?
|
|
110
|
+
stdout_reader.read
|
|
111
|
+
else
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
rescue Errno::ENOENT
|
|
115
|
+
nil
|
|
116
|
+
ensure
|
|
117
|
+
stdout_reader.close
|
|
101
118
|
end
|
|
102
119
|
|
|
103
120
|
def self.code(...)
|