simple-spawn 0.1.0
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.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/COPYING +27 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +69 -0
- data/Rakefile +15 -0
- data/lib/simple/spawn.rb +54 -0
- data/lib/simple-spawn.rb +1 -0
- data/simple-spawn.gemspec +15 -0
- data/test/simple/test_spawn.rb +10 -0
- data/test/test_helper.rb +8 -0
- metadata +60 -0
data/.gitignore
ADDED
data/COPYING
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2012 by Jose Pablo Barrantes <xjpablobrx@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person ob-
|
4
|
+
taining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without restric-
|
6
|
+
tion, including without limitation the rights to use, copy, modi-
|
7
|
+
fy, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
+
the Software, and to permit persons to whom the Software is fur-
|
9
|
+
nished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
16
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONIN-
|
17
|
+
FRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
23
|
+
A small portion of the environ dup'ing code in ext/posix-spawn.c
|
24
|
+
was taken from glibc <http://www.gnu.org/s/libc/> and is maybe
|
25
|
+
Copyright (c) 2011 by The Free Software Foundation or maybe
|
26
|
+
by others mentioned in the glibc LICENSES file. glibc is
|
27
|
+
distributed under the terms of the LGPL license.
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 jpablobr
|
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,69 @@
|
|
1
|
+
# Simple::Spawn [][travis]
|
2
|
+
|
3
|
+
[travis]: http://travis-ci.org/jpablobr/simple-spawn
|
4
|
+
|
5
|
+
Simple processes spawner that makes scripting UNIX pipes a breeze.
|
6
|
+
|
7
|
+
Most of it was inspired by https://github.com/jstorimer/shirt Thx! :)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'simple-spawn'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install simple-spawn
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
It only requires a valid `shell` command as a string.
|
26
|
+
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
spawn('cat ./stars.txt | grep charlie | sed s/winning/bi-winning/')
|
30
|
+
```
|
31
|
+
|
32
|
+
### Simple::Spawn as a Mixin
|
33
|
+
|
34
|
+
The `Simple::Spawn` module can also be mixed in to classes and modules
|
35
|
+
to include the `spawn` method in that namespace:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require simple/spawn
|
39
|
+
|
40
|
+
class YourScriptClass
|
41
|
+
include Simple::Spawn
|
42
|
+
|
43
|
+
def replace(what, with)
|
44
|
+
spawn("cat ./stars.txt | grep charlie | sed s/#{what}/#{with}/")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
## <a name="versions"></a>Supported Ruby Versions
|
50
|
+
This library aims to support and is [tested against][travis] the
|
51
|
+
following Ruby implementations:
|
52
|
+
|
53
|
+
* Ruby 1.9.2
|
54
|
+
* Ruby 1.9.3
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
63
|
+
|
64
|
+
## ACKNOWLEDGEMENTS
|
65
|
+
|
66
|
+
Copyright (c) by
|
67
|
+
[Jose Pablo Barrantes](http://jpablobr.com)
|
68
|
+
|
69
|
+
See the `COPYING` file for more information on license and redistribution.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
|
6
|
+
namespace :test do
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.name = "minitest"
|
9
|
+
t.libs << "test"
|
10
|
+
t.test_files = FileList['test/simple/test_*.rb', 'test/test_*.rb']
|
11
|
+
t.verbose = true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => "test:minitest"
|
data/lib/simple/spawn.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
module Simple
|
4
|
+
module Spawn
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def spawn cmd
|
8
|
+
commands = pipeify(cmd)
|
9
|
+
|
10
|
+
tmp_in = $stdin
|
11
|
+
tmp_out = $stdout
|
12
|
+
pipe = []
|
13
|
+
|
14
|
+
commands.each_with_index do |command, index|
|
15
|
+
program, *args = Shellwords.shellsplit(command)
|
16
|
+
|
17
|
+
if index+1 < commands.size
|
18
|
+
pipe = IO.pipe
|
19
|
+
tmp_out = pipe.last
|
20
|
+
else
|
21
|
+
tmp_out = $stdout
|
22
|
+
end
|
23
|
+
|
24
|
+
spawn_program(program, *args, tmp_out, tmp_in)
|
25
|
+
|
26
|
+
tmp_out.close unless tmp_out == $stdout
|
27
|
+
tmp_in.close unless tmp_in == $stdin
|
28
|
+
tmp_in = pipe.first
|
29
|
+
end
|
30
|
+
|
31
|
+
Process.waitall
|
32
|
+
end
|
33
|
+
|
34
|
+
def pipeify(line)
|
35
|
+
line.scan( /([^"'|]+)|["']([^"']+)["']/ ).flatten.compact
|
36
|
+
end
|
37
|
+
|
38
|
+
def spawn_program(program, *args, tmp_out, tmp_in)
|
39
|
+
fork {
|
40
|
+
unless tmp_out == $stdout
|
41
|
+
$stdout.reopen(tmp_out)
|
42
|
+
tmp_out.close
|
43
|
+
end
|
44
|
+
|
45
|
+
unless tmp_in == $stdin
|
46
|
+
$stdin.reopen(tmp_in)
|
47
|
+
tmp_in.close
|
48
|
+
end
|
49
|
+
|
50
|
+
exec program, *args
|
51
|
+
}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/simple-spawn.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'simple/spawn'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |gem|
|
3
|
+
gem.authors = ["jpablobr"]
|
4
|
+
gem.email = ["xjpablobrx@gmail.com"]
|
5
|
+
gem.description = %q{Simple processes spawner that makes scripting UNIX pipes a breeze.}
|
6
|
+
gem.summary = %q{Simple processes spawner that makes scripting UNIX pipes a breeze.}
|
7
|
+
gem.homepage = "http://github.com/jpablobr/simple-spawner"
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^test/})
|
12
|
+
gem.name = 'simple-spawn'
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
gem.version = '0.1.0'
|
15
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
class Simple::SpawnTest < MiniTest::Unit::TestCase
|
6
|
+
def test_pipes
|
7
|
+
assert_equal 2, Simple::Spawn::spawn('echo Simple::Spawn test | grep -i spawn').count
|
8
|
+
assert_equal 3, Simple::Spawn::spawn('echo Simple::Spawn test | grep -i spawn | sed s/test//').count
|
9
|
+
end
|
10
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-spawn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- jpablobr
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple processes spawner that makes scripting UNIX pipes a breeze.
|
15
|
+
email:
|
16
|
+
- xjpablobrx@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .travis.yml
|
23
|
+
- COPYING
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/simple-spawn.rb
|
29
|
+
- lib/simple/spawn.rb
|
30
|
+
- simple-spawn.gemspec
|
31
|
+
- test/simple/test_spawn.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
homepage: http://github.com/jpablobr/simple-spawner
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.17
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Simple processes spawner that makes scripting UNIX pipes a breeze.
|
57
|
+
test_files:
|
58
|
+
- test/simple/test_spawn.rb
|
59
|
+
- test/test_helper.rb
|
60
|
+
has_rdoc:
|