Fingertips-executioner 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/LICENSE +18 -0
- data/README.rdoc +0 -0
- data/Rakefile +28 -0
- data/VERSION.yml +4 -0
- data/lib/executioner.rb +98 -0
- data/test/executioner_test.rb +139 -0
- data/test/test_helper.rb +6 -0
- metadata +61 -0
data/LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
(c) 2009 Fingertips, Eloy Duran <eloy@fngtps.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
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell 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
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
task :default => :test
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.test_files = FileList['test/**/*_test.rb']
|
7
|
+
t.verbose = true
|
8
|
+
end
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'jeweler'
|
12
|
+
Jeweler::Tasks.new do |s|
|
13
|
+
s.name = "executioner"
|
14
|
+
s.summary = s.description = "Execute CLI utilities"
|
15
|
+
s.email = "eloy@fngtps.com"
|
16
|
+
s.homepage = "http://fingertips.github.com"
|
17
|
+
s.authors = ["Eloy Duran"]
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
end
|
21
|
+
|
22
|
+
begin
|
23
|
+
require 'jewelry_portfolio/tasks'
|
24
|
+
JewelryPortfolio::Tasks.new do |p|
|
25
|
+
p.account = 'Fingertips'
|
26
|
+
end
|
27
|
+
rescue LoadError
|
28
|
+
end
|
data/VERSION.yml
ADDED
data/lib/executioner.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
module Executioner
|
4
|
+
class ExecutionerError < StandardError; end
|
5
|
+
class ProcessError < ExecutionerError; end
|
6
|
+
class ExecutableNotFoundError < ExecutionerError; end
|
7
|
+
|
8
|
+
SEARCH_PATHS = %w{ /bin /usr/bin /usr/local/bin /opt/local/bin }
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :logger
|
12
|
+
|
13
|
+
def included(klass)
|
14
|
+
klass.extend ClassMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def execute(command, options={})
|
19
|
+
options[:switch_stdout_and_stderr] = false if options[:switch_stdout_and_stderr].nil?
|
20
|
+
|
21
|
+
Executioner.logger.debug("Executing: `#{command}'") if Executioner.logger
|
22
|
+
|
23
|
+
output = nil
|
24
|
+
Open3.popen3(command) do |stdin, stdout, stderr|
|
25
|
+
stdout, stderr = stderr, stdout if options[:switch_stdout_and_stderr]
|
26
|
+
|
27
|
+
output = stdout.gets(nil)
|
28
|
+
if output.nil? && (error_message = stderr.gets(nil))
|
29
|
+
if error_message =~ /:in\s`exec':\s(.+)\s\(.+\)$/
|
30
|
+
error_message = $1
|
31
|
+
end
|
32
|
+
raise ProcessError, "Command: \"#{command}\"\nOutput: \"#{error_message.chomp}\""
|
33
|
+
end
|
34
|
+
end
|
35
|
+
output
|
36
|
+
end
|
37
|
+
module_function :execute
|
38
|
+
|
39
|
+
def concat_args(args)
|
40
|
+
args.map { |a,v| "-#{a} #{v}" }.join(' ')
|
41
|
+
end
|
42
|
+
|
43
|
+
def queue(command)
|
44
|
+
@commands ||= []
|
45
|
+
@commands << command
|
46
|
+
end
|
47
|
+
|
48
|
+
def queued_commands
|
49
|
+
@commands ? @commands.join(' && ') : ''
|
50
|
+
end
|
51
|
+
|
52
|
+
def execute_queued(options={})
|
53
|
+
execute(queued_commands, options)
|
54
|
+
@commands = []
|
55
|
+
end
|
56
|
+
|
57
|
+
module ClassMethods
|
58
|
+
def executable(executable, options={})
|
59
|
+
options[:switch_stdout_and_stderr] = false if options[:switch_stdout_and_stderr].nil?
|
60
|
+
options[:use_queue] = false if options[:use_queue].nil?
|
61
|
+
|
62
|
+
executable = executable.to_s if executable.is_a? Symbol
|
63
|
+
use_queue = options.delete(:use_queue)
|
64
|
+
|
65
|
+
if selection_proc = options.delete(:select_if)
|
66
|
+
advance_from = nil
|
67
|
+
while executable_path = find_executable(executable, advance_from)
|
68
|
+
break if selection_proc.call(executable_path)
|
69
|
+
advance_from = File.dirname(executable_path)
|
70
|
+
end
|
71
|
+
else
|
72
|
+
executable_path = options[:path] || find_executable(executable)
|
73
|
+
end
|
74
|
+
|
75
|
+
if executable_path
|
76
|
+
if use_queue
|
77
|
+
body = "queue(\"#{executable_path} \#{args}\")"
|
78
|
+
else
|
79
|
+
body = "execute(\"#{executable_path} \#{args}\", #{options.inspect})"
|
80
|
+
end
|
81
|
+
else
|
82
|
+
body = "raise Executioner::ExecutableNotFoundError, \"Unable to find the executable '#{executable}' in: #{Executioner::SEARCH_PATHS.join(', ')}\""
|
83
|
+
end
|
84
|
+
|
85
|
+
class_eval "def #{executable.gsub(/-/, '_')}(args); #{body}; end", __FILE__, __LINE__
|
86
|
+
end
|
87
|
+
|
88
|
+
def find_executable(executable, advance_from = nil)
|
89
|
+
search_paths = Executioner::SEARCH_PATHS
|
90
|
+
search_paths = search_paths[(search_paths.index(advance_from) + 1)..-1] if advance_from
|
91
|
+
|
92
|
+
if executable_in_path = search_paths.find { |path| File.exist? File.join(path, executable) }
|
93
|
+
File.join executable_in_path, executable
|
94
|
+
end
|
95
|
+
end
|
96
|
+
module_function :find_executable
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class AClassThatUsesSubshells
|
4
|
+
include Executioner
|
5
|
+
executable :sh
|
6
|
+
executable :doesnotexistforsure
|
7
|
+
executable 'executable-with-dash'
|
8
|
+
executable :with_path, :path => '/path/to/executable'
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "Executioner, the module" do
|
12
|
+
it "should assign an optional logger" do
|
13
|
+
begin
|
14
|
+
logger = Object.new
|
15
|
+
Executioner.logger = logger
|
16
|
+
Executioner.logger.should.be logger
|
17
|
+
ensure
|
18
|
+
Executioner.logger = nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "Executioner, instance methods" do
|
24
|
+
before do
|
25
|
+
@object = AClassThatUsesSubshells.new
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise a Executioner::ProcessError if a command could not be executed" do
|
29
|
+
proc = lambda { @object.send(:execute, "/bin/sh -M") }
|
30
|
+
|
31
|
+
proc.should.raise Executioner::ProcessError
|
32
|
+
|
33
|
+
begin
|
34
|
+
proc.call
|
35
|
+
rescue Executioner::ProcessError => error
|
36
|
+
error.message.should =~ %r%Command: "/bin/sh -M"%
|
37
|
+
error.message.should =~ %r%Output: "/bin/sh: -M: invalid option%
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be able to switch stdout and stderr, for instance for ffmpeg" do
|
42
|
+
lambda { @object.send(:execute, "/bin/sh -M", :switch_stdout_and_stderr => true) }.should.not.raise Executioner::ProcessError
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should help concat arguments" do
|
46
|
+
@object.send(:concat_args, [[:foo, 'foo'], [:bar, 'bar']]).should == "-foo foo -bar bar"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should queue one command" do
|
50
|
+
@object.send(:queue, 'ls')
|
51
|
+
@object.send(:queued_commands).should == 'ls'
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should queue multiple commands" do
|
55
|
+
@object.send(:queue, 'ls')
|
56
|
+
@object.send(:queue, 'cat')
|
57
|
+
@object.send(:queued_commands).should == 'ls && cat'
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should execute queued commands" do
|
61
|
+
@object.send(:queue, 'ls')
|
62
|
+
@object.send(:queue, 'ls')
|
63
|
+
@object.expects(:execute).with(@object.send(:queued_commands), {})
|
64
|
+
@object.send(:execute_queued)
|
65
|
+
@object.send(:queued_commands).should == ''
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "Executioner, class methods" do
|
70
|
+
before do
|
71
|
+
@object = AClassThatUsesSubshells.new
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should define an instance method for the specified binary that's needed" do
|
75
|
+
AClassThatUsesSubshells.instance_methods.should.include 'sh'
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should define an instance method which calls #execute with the correct path to the executable" do
|
79
|
+
@object.expects(:execute).with('/bin/sh with some args', {:switch_stdout_and_stderr => false})
|
80
|
+
@object.sh 'with some args'
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should define an instance method for an executable with dashes replaced by underscores" do
|
84
|
+
@object.should.respond_to :executable_with_dash
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should be possible to switch stdin and stderr" do
|
88
|
+
AClassThatUsesSubshells.class_eval { executable(:sh, {:switch_stdout_and_stderr => true}) }
|
89
|
+
@object.expects(:execute).with('/bin/sh with some args', {:switch_stdout_and_stderr => true})
|
90
|
+
@object.sh 'with some args'
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should be possible to use the queue by default" do
|
94
|
+
AClassThatUsesSubshells.class_eval { executable(:sh, {:use_queue => true}) }
|
95
|
+
@object.expects(:execute).with('/bin/sh arg1 && /bin/sh arg2', {})
|
96
|
+
@object.sh 'arg1'
|
97
|
+
@object.sh 'arg2'
|
98
|
+
@object.execute_queued
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should be possible to specify the path to the executable" do
|
102
|
+
@object.expects(:execute).with { |command, options| command == "/path/to/executable arg1" }
|
103
|
+
@object.with_path 'arg1'
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should be possible to find an executable" do
|
107
|
+
File.stubs(:exist?).with('/bin/sh').returns(true)
|
108
|
+
Executioner::ClassMethods.find_executable('sh').should == '/bin/sh'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should be possible to find an executable advancing from a given path" do
|
112
|
+
File.stubs(:exist?).with('/usr/bin/sh').returns(true)
|
113
|
+
Executioner::ClassMethods.find_executable('sh', '/bin').should == '/usr/bin/sh'
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should yield all found executables, but use the one for which the proc returns a truthful value" do
|
117
|
+
File.stubs(:exist?).with('/bin/with_selection_proc').returns(true)
|
118
|
+
File.stubs(:exist?).with('/usr/bin/with_selection_proc').returns(true)
|
119
|
+
File.stubs(:exist?).with('/usr/local/bin/with_selection_proc').returns(true)
|
120
|
+
File.stubs(:exist?).with('/opt/local/bin/with_selection_proc').returns(true)
|
121
|
+
|
122
|
+
AClassThatUsesSubshells.executable(:with_selection_proc, :select_if => lambda { |executable| nil })
|
123
|
+
lambda { @object.with_selection_proc('foo') }.should.raise Executioner::ExecutableNotFoundError
|
124
|
+
|
125
|
+
AClassThatUsesSubshells.executable(:with_selection_proc, :select_if => lambda { |executable| executable == '/usr/bin/with_selection_proc' })
|
126
|
+
@object.expects(:execute).with("/usr/bin/with_selection_proc foo", {:switch_stdout_and_stderr => false})
|
127
|
+
@object.with_selection_proc('foo')
|
128
|
+
|
129
|
+
AClassThatUsesSubshells.executable(:with_selection_proc, :select_if => lambda { |executable| executable == '/opt/local/bin/with_selection_proc' })
|
130
|
+
@object.expects(:execute).with("/opt/local/bin/with_selection_proc foo", {:switch_stdout_and_stderr => false})
|
131
|
+
@object.with_selection_proc('foo')
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should define an instance method which raises a Executioner::ExecutableNotFoundError error if the executable could not be found" do
|
135
|
+
lambda {
|
136
|
+
@object.doesnotexistforsure 'right?'
|
137
|
+
}.should.raise Executioner::ExecutableNotFoundError
|
138
|
+
end
|
139
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Fingertips-executioner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eloy Duran
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-08 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Execute CLI utilities
|
17
|
+
email: eloy@fngtps.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- VERSION.yml
|
30
|
+
- lib/executioner.rb
|
31
|
+
- test/executioner_test.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
has_rdoc: false
|
34
|
+
homepage: http://fingertips.github.com
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Execute CLI utilities
|
59
|
+
test_files:
|
60
|
+
- test/executioner_test.rb
|
61
|
+
- test/test_helper.rb
|