exec-simple 0.1.2
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 +7 -0
- data/.gitignore +17 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +1 -0
- data/exec-simple.gemspec +24 -0
- data/lib/exec-simple.rb +72 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ab5a6b092749313ecd978461696154ffbef0a92
|
4
|
+
data.tar.gz: 5b77ccd07d905730c362b90cf671a95b2be83b1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d5465c60ac2e20c75faf85bf468c013002d4de5e2b9312f0037b250aeeb3e9ece59748289b2a604191875b4e617c324ff26990dabb06b5ecbbb116c00b8d138
|
7
|
+
data.tar.gz: a6f89a61a3bd0d901b47ac6e0108dccb662b27e72b1b68f01fbde74e65c0cdf58f368295f3d152c75f9ac5ab3292eb24c88532c2259ae49a3038a51f65bcd9c4
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
## 0.1.2
|
2
|
+
|
3
|
+
* bugfix: add missing concurrent-ruby dependency
|
4
|
+
|
5
|
+
## 0.1.1
|
6
|
+
|
7
|
+
* bugfix: prevent additional newlines in output if using Logger module
|
8
|
+
|
9
|
+
## 0.1.0
|
10
|
+
|
11
|
+
Basic functionality added:
|
12
|
+
|
13
|
+
* Run commands asynchronously
|
14
|
+
* Support timeouts
|
15
|
+
* Continously get output
|
16
|
+
* Support Loggers to continuously print output
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Dominik Richter
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# ExecSimple
|
2
|
+
|
3
|
+
A simple command runner in Ruby.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
ExecSimple.run 'echo 123'
|
7
|
+
=> ["123\n", "", 0]
|
8
|
+
```
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
ExecSimple.run 'echo "error message" >&2; exit 123'
|
12
|
+
=> ["", "error message\n", 123]
|
13
|
+
```
|
14
|
+
|
15
|
+
Handle timeouts:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
ExecSimple.run 'echo "Legen... wait for it"; sleep 100; echo "DARY"', timeout: 1
|
19
|
+
=> ["Legen... wait for it\n", "", nil]
|
20
|
+
```
|
21
|
+
|
22
|
+
Integrate with logging:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'logging'
|
26
|
+
log = Logging.logger['main']
|
27
|
+
log.add_appenders(Logging.appenders.stdout)
|
28
|
+
|
29
|
+
ExecSimple.run 'echo "You will read this"; sleep 3; echo "before the end."', log: log
|
30
|
+
INFO main : You will read this
|
31
|
+
INFO main : before the end.
|
32
|
+
=> 0
|
33
|
+
|
34
|
+
```
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
gem 'exec-simple'
|
41
|
+
|
42
|
+
And then execute:
|
43
|
+
|
44
|
+
$ bundle
|
45
|
+
|
46
|
+
Or install it yourself as:
|
47
|
+
|
48
|
+
$ gem install exec-simple
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it ( http://github.com/<my-github-username>/exec-simple/fork )
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
59
|
+
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/exec-simple.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'exec-simple'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "exec-simple"
|
8
|
+
spec.version = ExecSimple::VERSION
|
9
|
+
spec.authors = ["Dominik Richter"]
|
10
|
+
spec.email = ["dominik.richter@gmail.com"]
|
11
|
+
spec.summary = %q{Simply run commands.}
|
12
|
+
spec.description = %q{A simple command runner interface.}
|
13
|
+
spec.homepage = "http://github.com/arlimus/exec-simple"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency "concurrent-ruby", "~> 0.5"
|
24
|
+
end
|
data/lib/exec-simple.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'concurrent'
|
2
|
+
require 'open3'
|
3
|
+
require 'io/wait'
|
4
|
+
|
5
|
+
module ExecSimple
|
6
|
+
VERSION = "0.1.2"
|
7
|
+
|
8
|
+
# Run a command. Optionally log results from stadard output and error
|
9
|
+
# with a logger object. Optionally set a timeout as a maximum time to
|
10
|
+
# wait until the process is killed.
|
11
|
+
# @param [String] cmd The command to run
|
12
|
+
# @param [Logging::Logger] log An optional logger object to use for output
|
13
|
+
# @param [Int] timeout An optional timeout to wait until the process is killed.
|
14
|
+
# @returny [Array] Either return an array of [output, error, exit_code] or
|
15
|
+
# just the exit_code if a log-object was provided
|
16
|
+
def self.run cmd, log: nil, timeout: nil
|
17
|
+
# run the command concurrently to manage timeouts
|
18
|
+
|
19
|
+
out = ""
|
20
|
+
err = ""
|
21
|
+
stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
|
22
|
+
# we don't need stdin
|
23
|
+
stdin.close
|
24
|
+
# run readers on stdin and stderr
|
25
|
+
op = Concurrent::Promise.execute do
|
26
|
+
while( not stdout.eof? )
|
27
|
+
#stdout.wait_readable
|
28
|
+
#cur = stdout.read
|
29
|
+
cur = stdout.readpartial(4096)
|
30
|
+
if log.nil? then out << cur else log.info cur.chomp end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
ep = Concurrent::Promise.execute do
|
34
|
+
while( not stderr.eof? )
|
35
|
+
#stdout.wait_readable
|
36
|
+
#cur = stdout.read
|
37
|
+
cur = stderr.readpartial(4096)
|
38
|
+
if log.nil? then err << cur else log.error cur.chomp end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
#
|
42
|
+
pid = wait_thr[:pid]
|
43
|
+
|
44
|
+
future = Concurrent::Future.execute do
|
45
|
+
wait_thr.value
|
46
|
+
end
|
47
|
+
# get the future's value
|
48
|
+
exit_code = future.value(timeout)
|
49
|
+
exitstatus = if exit_code.nil? then nil else exit_code.exitstatus end
|
50
|
+
|
51
|
+
# if we didn't yet get a value, kill the process
|
52
|
+
if wait_thr.alive?
|
53
|
+
Process.kill('TERM', pid)
|
54
|
+
sleep 1
|
55
|
+
# check if it terminated successfully
|
56
|
+
if wait_thr.alive?
|
57
|
+
# if not give it some more time
|
58
|
+
sleep 3
|
59
|
+
# and then kill it
|
60
|
+
Process.kill('KILL', pid) if wait_thr.alive?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# finishing up
|
65
|
+
stdout.close
|
66
|
+
stderr.close
|
67
|
+
|
68
|
+
# get the results
|
69
|
+
return exitstatus if not log.nil?
|
70
|
+
[out, err, exitstatus]
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: exec-simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dominik Richter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: concurrent-ruby
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.5'
|
55
|
+
description: A simple command runner interface.
|
56
|
+
email:
|
57
|
+
- dominik.richter@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- CHANGELOG.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- exec-simple.gemspec
|
69
|
+
- lib/exec-simple.rb
|
70
|
+
homepage: http://github.com/arlimus/exec-simple
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Simply run commands.
|
94
|
+
test_files: []
|
95
|
+
has_rdoc:
|