system_run 1.0 → 1.0.1
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/Rakefile +7 -7
- data/system_run.gemspec +2 -2
- metadata +2 -3
- data/lib/system_run.rb +0 -105
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c6bd63c23713abf1d6a2ce4c456048e313fd7a3
|
4
|
+
data.tar.gz: 304ba5539f1f046ad9eb73bcc0d139fda00b27e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 107eafe0bccfa161b5450977034a1b860bf6f9a73c3db07726c902ad23e260e7616001f1caf045fb2a0e53d9f15f3cc7a678f48c2121af1bb13a080831213057
|
7
|
+
data.tar.gz: 07d0a846819655436dcbbac434d28d48031f1038725a823266d1d02ecb8a8b0fc82fc51bdc58cd26641313ae5db6d9a03da5419c3ddc3d3a6affcc66200840b2
|
data/Rakefile
CHANGED
@@ -1,13 +1,6 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rubygems/package_task'
|
3
3
|
|
4
|
-
# require 'rake/testtask'
|
5
|
-
# Rake::TestTask.new do |t|
|
6
|
-
# t.libs << "test"
|
7
|
-
# t.test_files = FileList['./test/tc_*.rb']
|
8
|
-
# t.verbose = false
|
9
|
-
# end
|
10
|
-
|
11
4
|
spec = eval(File.read('system_run.gemspec'))
|
12
5
|
Gem::PackageTask.new(spec) do |pkg|
|
13
6
|
pkg.need_zip = true
|
@@ -23,3 +16,10 @@ end
|
|
23
16
|
task :install => :gem do |r|
|
24
17
|
`sudo gem install pkg/system_run.gem`
|
25
18
|
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
require 'rspec/core/rake_task'
|
22
|
+
RSpec::Core::RakeTask.new(:spec)
|
23
|
+
task :test => :spec
|
24
|
+
rescue LoadError
|
25
|
+
end
|
data/system_run.gemspec
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "system_run"
|
3
|
-
s.version = "1.0"
|
3
|
+
s.version = "1.0.1"
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.license = "MIT"
|
6
6
|
s.summary = "Tiny wrapper for running commands. Inspired by systemu."
|
7
7
|
|
8
8
|
s.description = "see https://github.com/biederfrau/system_run"
|
9
9
|
|
10
|
-
s.files = Dir['
|
10
|
+
s.files = Dir['lib/**/*,spec/*}'] + %w(LICENSE Rakefile system_run.gemspec README.md)
|
11
11
|
s.require_path = 'lib'
|
12
12
|
s.extra_rdoc_files = ['README.md']
|
13
13
|
s.test_files = []
|
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:
|
4
|
+
version: 1.0.1
|
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-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: see https://github.com/biederfrau/system_run
|
14
14
|
email: s.bdrmnn@gmail.com
|
@@ -20,7 +20,6 @@ files:
|
|
20
20
|
- LICENSE
|
21
21
|
- README.md
|
22
22
|
- Rakefile
|
23
|
-
- lib/system_run.rb
|
24
23
|
- system_run.gemspec
|
25
24
|
homepage: https://github.com/biederfrau/system_run
|
26
25
|
licenses:
|
data/lib/system_run.rb
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'timeout'
|
3
|
-
require 'tempfile'
|
4
|
-
|
5
|
-
module System
|
6
|
-
def self.run(cmd, *args, timeout: nil, env: {}, capture: :default, file: nil, cwd: '.', on_timeout: nil)
|
7
|
-
cmd = cmd.dup << ' ' << args.join(' ')
|
8
|
-
env = env.dup.map { |k, v| [k.to_s, v.to_s] }.to_h
|
9
|
-
|
10
|
-
raise ArgumentError, "file(s) must be instance of File or Tempfile" unless file.nil? || file.all? { |f| f.is_a?(File) || f.is_a?(Tempfile) }
|
11
|
-
out, err = case capture
|
12
|
-
when :default then
|
13
|
-
raise ArgumentError, "invalid file redirects for default capture, expected array of two" unless file.nil? || file.is_a?(Array) && file.size == 2
|
14
|
-
file ? [file.first, file.last] : [Tempfile.new('sysrun-out'), Tempfile.new('sysrun-err')]
|
15
|
-
when :out then [file || Tempfile.new('sysrun-out'), '/dev/null']
|
16
|
-
when :err then ['/dev/null', file || Tempfile.new('sysrun-err')]
|
17
|
-
when :both then [file || Tempfile.new('sysrun-outerr')] * 2
|
18
|
-
else raise ArgumentError, "unknown capture: #{capture}"
|
19
|
-
end
|
20
|
-
|
21
|
-
pid = spawn env, cmd, out: out, err: err, chdir: cwd
|
22
|
-
status = wait_or_kill pid, timeout, on_timeout
|
23
|
-
[out, err].each { |f| f.rewind if f.respond_to? :rewind }
|
24
|
-
|
25
|
-
file ? status : case capture
|
26
|
-
when :default then [status, out.read, err.read]
|
27
|
-
when :out, :both then [status, out.read]
|
28
|
-
when :err then [status, err.read]
|
29
|
-
end
|
30
|
-
ensure
|
31
|
-
[out, err].each { |f| f.unlink if f.is_a?(Tempfile) && f.path&.include?('sysrun') }
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.wait_or_kill(pid, timeout=nil, on_timeout=nil)
|
35
|
-
begin
|
36
|
-
Timeout::timeout(timeout) do
|
37
|
-
Process.wait pid
|
38
|
-
end
|
39
|
-
rescue Timeout::Error
|
40
|
-
if on_timeout then
|
41
|
-
on_timeout.call pid
|
42
|
-
else
|
43
|
-
Process.kill 9, pid
|
44
|
-
end
|
45
|
-
Process.wait pid
|
46
|
-
end
|
47
|
-
|
48
|
-
$?
|
49
|
-
end
|
50
|
-
|
51
|
-
private_class_method :wait_or_kill
|
52
|
-
end
|
53
|
-
|
54
|
-
if __FILE__ == $0 then
|
55
|
-
date = %q(ruby -e "t = Time.now; STDOUT.puts t; STDERR.puts t")
|
56
|
-
|
57
|
-
# capture stdout and stderr separately.
|
58
|
-
status, stdout, stderr = System.run date
|
59
|
-
p [status, stdout, stderr]
|
60
|
-
|
61
|
-
# capture only stdout, discard stderr.
|
62
|
-
status, stdout = System.run date, capture: :out
|
63
|
-
p [status, stdout]
|
64
|
-
|
65
|
-
# capture stderr and discard stdout.
|
66
|
-
status, stderr = System.run date, capture: :err
|
67
|
-
p [status, stderr]
|
68
|
-
|
69
|
-
# capture both stdout and stderr.
|
70
|
-
status, all = System.run date, capture: :both
|
71
|
-
p [status, all]
|
72
|
-
|
73
|
-
# capture stderr. kill process after 2 seconds.
|
74
|
-
loop_time = %q(ruby -e "loop { STDERR.puts Time.now.to_i; sleep 2 }")
|
75
|
-
status, stderr = System.run loop_time, capture: :err, timeout: 2
|
76
|
-
p [status, stderr]
|
77
|
-
|
78
|
-
# set timeout to 2 seconds and provide an action for when timeout occurs. default
|
79
|
-
# action is to send signal 9. this will be overridden!
|
80
|
-
status, _, _ = System.run %q(ruby -e "sleep 10"), timeout: 2, on_timeout: ->(pid) do
|
81
|
-
puts 'oh no, request timed out'
|
82
|
-
Process.kill 9, pid
|
83
|
-
end
|
84
|
-
p status
|
85
|
-
|
86
|
-
# set environment variables.
|
87
|
-
env = %q(ruby -e "puts ENV['HELLO']")
|
88
|
-
status, stdout = System.run env, env: {'HELLO' => 'WORLD'}, capture: :out
|
89
|
-
p [status, stdout]
|
90
|
-
|
91
|
-
# switch to a different directory before executing the command.
|
92
|
-
dir = %q(ruby -e "puts Dir.pwd")
|
93
|
-
status, stdout = System.run dir, cwd: Dir.tmpdir, capture: :out
|
94
|
-
p [status, stdout]
|
95
|
-
|
96
|
-
# write to a file instead of loading output into a string.
|
97
|
-
out = File.open 'test_out.txt', 'w+'
|
98
|
-
err = File.open 'test_err.txt', 'w+'
|
99
|
-
|
100
|
-
hello = %q(ruby -e "STDERR.puts 'hello'; STDOUT.puts 'world'")
|
101
|
-
status = System.run hello, file: [out, err]
|
102
|
-
p [status, out.read, err.read]
|
103
|
-
|
104
|
-
[out, err].each { |f| f.close; File.unlink f.path }
|
105
|
-
end
|