system_run 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -2
- data/system_run.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82b98b36722d6fefa40acc60c9e71a3e88c4917a
|
4
|
+
data.tar.gz: 42d53eaffdc0d376a6541d8e6398c8fde8e0e6ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 805bb4f14de0dd05d69833a367452c891f5439cea6078602bc3d136f65c99ecd38dbf1e10301005b36e1849003e513e368cba3a7eb92a87906202a7a46b32532
|
7
|
+
data.tar.gz: f79a1e1ed744aea047fdc737a0d086bfaaea857286ddaca287b0b3e28cd4b562cf4eded8fba549ee9c4cbc90257396553879cc7aca2f5fc9c364cdd78a2d54b7
|
data/README.md
CHANGED
@@ -8,12 +8,19 @@ a timeout occurs and other options that are forwarded to `spawn` as-is.
|
|
8
8
|
|
9
9
|
## Examples
|
10
10
|
|
11
|
+
System.run will by default capture stderr and stdout separately and return
|
12
|
+
them as strings along with the process exit state.
|
13
|
+
|
11
14
|
```ruby
|
12
15
|
# capture both stderr and stdout, but separately.
|
13
16
|
program = %q{ruby -e "STDOUT.print 'hello'; STDERR.print 'world'"}
|
14
17
|
System.run program
|
15
18
|
# => [#<Process::Status: pid 20407 exit 0>, "hello", "world"]
|
19
|
+
```
|
16
20
|
|
21
|
+
You can also only pick just one by specifiying the `capture` keyword argument.
|
22
|
+
|
23
|
+
```ruby
|
17
24
|
# only capture stdout.
|
18
25
|
System.run program, capture: :out
|
19
26
|
# => [#<Process::Status: pid 20421 exit 0>, "hello"]
|
@@ -21,18 +28,36 @@ System.run program, capture: :out
|
|
21
28
|
# only capture stderr.
|
22
29
|
System.run program, capture: :err
|
23
30
|
# => [#<Process::Status: pid 20458 exit 0>, "world"]
|
31
|
+
```
|
32
|
+
|
33
|
+
Sometimes, it is also useful to capture both stdout _and_ stderr simultaneously.
|
34
|
+
Bear in mind, however, that stdout is buffered and thus might not appear if the
|
35
|
+
process dies before the buffer is flushed.
|
24
36
|
|
37
|
+
```ruby
|
25
38
|
# capture both stdout and stderr into the same string.
|
26
39
|
# stdout is buffered, stderr is not!
|
27
40
|
System.run program, capture: :both
|
28
41
|
# => [#<Process::Status: pid 20464 exit 0>, "worldhello"]
|
42
|
+
```
|
43
|
+
|
44
|
+
You can also specify a timeout interval after which System.run will kill
|
45
|
+
the process.
|
29
46
|
|
47
|
+
```ruby
|
30
48
|
# kill (send signal 9) process after the specified time.
|
31
49
|
program = %q{ruby -e "STDERR.print 'this can only end badly'; loop { sleep 1 }"}
|
32
50
|
System.run program, timeout: 2, capture: :both
|
33
51
|
# ...
|
34
52
|
# => [#<Process::Status: pid 20507 SIGKILL (signal 9)>, "this can only end badly"]
|
53
|
+
```
|
35
54
|
|
55
|
+
If outright killing the process is too harsh for you, or you want to do some
|
56
|
+
extra things like logging the timeout, you can specify a callable object that
|
57
|
+
will get called when the timeout expires. This callable will receive one argument,
|
58
|
+
the process id of the spawned child.
|
59
|
+
|
60
|
+
```ruby
|
36
61
|
# override default action with sending SIGTERM and setting a control variable.
|
37
62
|
state = :everything_is_great
|
38
63
|
System.run program, timeout: 2, capture: :both, on_timeout: ->(pid) do
|
@@ -41,16 +66,33 @@ System.run program, timeout: 2, capture: :both, on_timeout: ->(pid) do
|
|
41
66
|
end
|
42
67
|
# ...
|
43
68
|
# => [#<Process::Status: pid 20519 SIGTERM (signal 15)>, "this can only end badly"]
|
69
|
+
```
|
44
70
|
|
71
|
+
You can temporarily set some environment variables that will be accessible to
|
72
|
+
the child process. Keys and values will be converted to string by System.run
|
73
|
+
for you, don't worry about that.
|
74
|
+
|
75
|
+
```ruby
|
45
76
|
# set some environment variable.
|
46
77
|
program = %{ruby -e "STDERR.print ENV['hello']; STDOUT.print Dir.pwd"}
|
47
78
|
System.run program, capture: :err, env: {'hello' => 2}
|
48
79
|
# => [#<Process::Status: pid 20540 exit 0>, "2"]
|
80
|
+
```
|
81
|
+
|
82
|
+
You can also specify the directory which shall be the working directory
|
83
|
+
for the child process.
|
49
84
|
|
85
|
+
```ruby
|
50
86
|
# set working dir.
|
51
87
|
System.run program, capture: :out, cwd: Dir.tmpdir
|
52
88
|
# => [#<Process::Status: pid 20545 exit 0>, "/tmp"]
|
89
|
+
```
|
90
|
+
|
91
|
+
If the output is too large to be loaded into memory or you want to keep the
|
92
|
+
output, you can specify a file–or for default capture, two files–that
|
93
|
+
System.run should write to. System.run will rewind the file after writing.
|
53
94
|
|
95
|
+
```ruby
|
54
96
|
# redirect to file (File or Tempfile or descendants)
|
55
97
|
program = %q{ruby -e "STDOUT.print 'hello'; STDERR.print 'world'"}
|
56
98
|
out = Tempfile.new 'out'
|
@@ -61,16 +103,24 @@ out.read
|
|
61
103
|
# => "hello"
|
62
104
|
err.read
|
63
105
|
# => "world"
|
106
|
+
```
|
64
107
|
|
108
|
+
You can specify all options the same as when working with strings, System.run doesn't care.
|
109
|
+
|
110
|
+
```ruby
|
65
111
|
# everything works the same!
|
66
112
|
out = Tempfile.new 'out'
|
67
113
|
System.run program, file: out, capture: :both
|
68
114
|
# => #<Process::Status: pid 20591 exit 0>
|
69
115
|
out.read
|
70
116
|
# => "worldhello"
|
117
|
+
```
|
118
|
+
|
119
|
+
You can also supply text to be used as input for the child process by passing
|
120
|
+
the corresponding file object as `in` keyword parameter.
|
71
121
|
|
72
|
-
|
73
|
-
# directly to Kernel.spawn.
|
122
|
+
```ruby
|
123
|
+
# the in: option is passed directly to Kernel.spawn.
|
74
124
|
input = Tempfile.new.tap { |f| f.write('system_run'); f.rewind }
|
75
125
|
echo = %q{ruby -e "print STDIN.gets"}
|
76
126
|
System.run echo, in: input, capture: :out
|
data/system_run.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: system_run
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sonja Biedermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|