process-pipeline 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a67cb5a35b096ecb1073235621e07330879ebe4c
4
+ data.tar.gz: '029fb6df9ca4908eb223af97e66f5ed2a2c1f7a8'
5
+ SHA512:
6
+ metadata.gz: 04b22c3ef7fe98b6f2e754faf84d6838fdbd57d376925dff1f7a4e07c00e2561cac079e1bd68760883984e50a1cc675bc78b39ed803f8e646aff5ec8ed4b5ea1
7
+ data.tar.gz: 2facbb5fb37cfe8179f22dc5d2a7c25dc6c4c1fb293df59096df5fe811d801b8a0f8f4cd6d9d1fbafcaf53accdb80a3bc79260fa80c8e9c9a920b799d7cf016e
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in process-pipeline.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem "pry"
10
+ end
@@ -0,0 +1,128 @@
1
+ # Process::Pipeline
2
+
3
+ Provides a simple API for making composable shell pipelines.
4
+
5
+ [![Build Status](https://travis-ci.org/ioquatix/graphviz.svg)](https://travis-ci.org/ioquatix/graphviz)
6
+ [![Coverage Status](https://coveralls.io/repos/ioquatix/graphviz/badge.svg)](https://coveralls.io/r/ioquatix/graphviz)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'process-pipeline'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install process-pipeline
21
+
22
+ ## Usage
23
+
24
+ You can create a basic pipeline:
25
+
26
+ ```ruby
27
+ pipeline = Process::Pipeline.("head /dev/random").("strings").("wc -c")
28
+
29
+ puts pipeline.read.to_i
30
+ ```
31
+
32
+ You can do more complex tasks:
33
+
34
+ ```ruby
35
+ Process::Pipeline.("curl http://www.google.com").each_line.to_a
36
+ => ["<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n",
37
+ "<TITLE>302 Moved</TITLE></HEAD><BODY>\n",
38
+ "<H1>302 Moved</H1>\n",
39
+ "The document has moved\n",
40
+ "<A HREF=\"http://www.google.co.nz/?gfe_rd=cr&amp;dcr=0&amp;ei=vcLmWdihI6bu8we2qpD4Cg\">here</A>.\r\n",
41
+ "</BODY></HTML>\r\n"]
42
+ ```
43
+
44
+ ### Output
45
+
46
+ You can write output to either a file or a pipe.
47
+
48
+ ```ruby
49
+ pipeline = Process::Pipeline.("head /dev/random")
50
+
51
+ # Read all data until pipe is closed:
52
+ buffer = pipeline.read
53
+
54
+ # Read directly from pipe:
55
+ pipeline.read do |output|
56
+ # Read 8 bytes:
57
+ buffer = output.read(8)
58
+ end
59
+
60
+ # Read all data into a file:
61
+ pipeline.write("entropy.dat")
62
+ ```
63
+
64
+ ### Input
65
+
66
+ Input can be either a file or a pipe.
67
+
68
+ ```ruby
69
+ pipeline = Process::Pipeline.("head")
70
+
71
+ pipeline.read(input: "/usr/share/dict/words")
72
+ => "A\na\naa\naal\naalii\naam\nAani\naardvark\naardwolf\nAaron\n"
73
+ ```
74
+
75
+ Using a pipe can be a bit more tricky since you need to feed data into it incrementally.
76
+
77
+ ```ruby
78
+ pipeline = Process::Pipeline.("sort")
79
+
80
+ output, input = IO.pipe
81
+
82
+ thread = Thread.new do
83
+ %w{The quick brown fox jumps over the lazy dog}.each do |word|
84
+ input.puts word
85
+ end
86
+
87
+ input.close
88
+ end
89
+
90
+ # The input to the command is the output of the pipe
91
+ buffer = pipeline.read(input: output)
92
+
93
+ expect(buffer).to be == "The\nbrown\ndog\nfox\njumps\nlazy\nover\nquick\nthe\n"
94
+
95
+ thread.join
96
+ ```
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create new Pull Request
105
+
106
+ ## License
107
+
108
+ Released under the MIT license.
109
+
110
+ Copyright, 2013, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
111
+
112
+ Permission is hereby granted, free of charge, to any person obtaining a copy
113
+ of this software and associated documentation files (the "Software"), to deal
114
+ in the Software without restriction, including without limitation the rights
115
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
116
+ copies of the Software, and to permit persons to whom the Software is
117
+ furnished to do so, subject to the following conditions:
118
+
119
+ The above copyright notice and this permission notice shall be included in
120
+ all copies or substantial portions of the Software.
121
+
122
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
123
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
124
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
125
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
126
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
127
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
128
+ THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ require "bundler/setup"
10
+ require_relative "lib/process/pipeline"
11
+
12
+ require "pry"
13
+ Pry.start
14
+ end
@@ -0,0 +1,32 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'pipeline/version'
22
+ require_relative 'pipeline/step'
23
+
24
+ require 'process/group'
25
+
26
+ module Process
27
+ module Pipeline
28
+ def self.call(*command)
29
+ return Step.new(nil, command)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Process
22
+ module Pipeline
23
+ class CommandError < StandardError
24
+ def initialize(step, status)
25
+ @step = step
26
+ @status = status
27
+
28
+ super "Command #{step.command.inspect} exited with status #{@status.to_i}!"
29
+ end
30
+
31
+ attr :step
32
+ attr :status
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,133 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'command_error'
22
+ require 'process/group'
23
+
24
+ module Process
25
+ module Pipeline
26
+ Step = Struct.new(:tail, :command) do
27
+ def dup(tail)
28
+ self.class.new(self.tail&.dup(tail) || tail, self.command)
29
+ end
30
+
31
+ def call(*command)
32
+ self.class.new(self, command)
33
+ end
34
+
35
+ def spawn(group, input, output, error)
36
+ if self.command.nil?
37
+ return self.tail&.call(group, input, output, error)
38
+ end
39
+
40
+ if Hash === self.command.last
41
+ *command, options = self.command
42
+ else
43
+ command = self.command
44
+ options = {}
45
+ end
46
+
47
+ if self.tail
48
+ pipe = IO.pipe
49
+
50
+ self.tail.spawn(group, input, pipe[1], error)
51
+ pipe[1].close
52
+
53
+ options[:in] = pipe[0]
54
+ elsif input
55
+ options[:in] = input
56
+ end
57
+
58
+ options[:out] = output
59
+
60
+ # puts "run #{command.inspect} #{options.inspect}"
61
+ group.run(*command, **options) do |exit_status|
62
+ unless exit_status.success?
63
+ raise CommandError.new(self, exit_status)
64
+ end
65
+ end
66
+ end
67
+
68
+ def read(input: nil, error: $stderr)
69
+ pipe = IO.pipe
70
+
71
+ Process::Group.wait do |group|
72
+ self.spawn(group, input, pipe[1], error)
73
+ end
74
+
75
+ pipe[1].close
76
+
77
+ if block_given?
78
+ yield pipe[0]
79
+ else
80
+ buffer = pipe[0].read
81
+ end
82
+
83
+ pipe[0].close
84
+
85
+ return buffer
86
+ end
87
+
88
+ def each_line(*args, &block)
89
+ return to_enum(:each_line, *args) unless block_given?
90
+
91
+ read(*args) do |output|
92
+ output.each_line(&block)
93
+ end
94
+ end
95
+
96
+ def write(path, input: nil, error: $stderr)
97
+ File.open(path, "w") do |file|
98
+ Process::Group.wait do |group|
99
+ self.spawn(group, input, file, error)
100
+ end
101
+ end
102
+
103
+ return self
104
+ end
105
+
106
+ def steps(&block)
107
+ return to_enum(:steps) unless block_given?
108
+
109
+ if self.tail
110
+ self.tail.each(&block)
111
+ end
112
+
113
+ yield self
114
+ end
115
+
116
+ def to_s
117
+ if self.command
118
+ self.command.join(" ")
119
+ else
120
+ nil
121
+ end
122
+ end
123
+
124
+ def inspect
125
+ self.steps.collect(&:to_s).join(" | ")
126
+ end
127
+
128
+ def + pipeline
129
+ pipeline.dup(self)
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Process
22
+ module Pipeline
23
+ VERSION = "1.0.0"
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "process/pipeline/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "process-pipeline"
8
+ spec.version = Process::Pipeline::VERSION
9
+ spec.authors = ["Samuel Williams"]
10
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
11
+
12
+ spec.summary = %q{Execute composable shell-like pipelines.}
13
+ spec.homepage = "https://github.com/ioquatix/process-pipeline"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "process-group"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.15"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: process-pipeline
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: process-group
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - samuel.williams@oriontransfer.co.nz
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - lib/process/pipeline.rb
83
+ - lib/process/pipeline/command_error.rb
84
+ - lib/process/pipeline/step.rb
85
+ - lib/process/pipeline/version.rb
86
+ - process-pipeline.gemspec
87
+ homepage: https://github.com/ioquatix/process-pipeline
88
+ licenses: []
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.6.12
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Execute composable shell-like pipelines.
110
+ test_files: []