half_shell 0.0.3
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 +5 -0
- data/Gemfile +7 -0
- data/LICENSE +19 -0
- data/README.md +43 -0
- data/Rakefile +22 -0
- data/benchmark.rb +16 -0
- data/half_shell.gemspec +24 -0
- data/lib/half_shell.rb +48 -0
- data/lib/half_shell/result.rb +21 -0
- data/lib/half_shell/version.rb +3 -0
- data/test/half_shell_spec.rb +53 -0
- data/test/result_spec.rb +14 -0
- data/test/test_helper.rb +6 -0
- metadata +109 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Chris Gibson
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
Half Shell
|
2
|
+
=========
|
3
|
+
|
4
|
+
`half_shell` is a simple ruby gem for executing shell commands
|
5
|
+
|
6
|
+
Installation:
|
7
|
+
|
8
|
+
gem install half_shell
|
9
|
+
|
10
|
+
Examples:
|
11
|
+
|
12
|
+
require 'half_shell'
|
13
|
+
|
14
|
+
# execute a command and capture the result
|
15
|
+
result = HalfShell.execute 'ls'
|
16
|
+
=> #<HalfShell::Result:0x101270f18 @stdout="Gemfile\nGemfile.lock\nREADME.md\nRakefile\nbenchmark.rb\ncoverage\nhalf_shell.gemspec\nlib\ntest\n", @status=0, @command="ls", @stderr="">
|
17
|
+
|
18
|
+
# status
|
19
|
+
result.status
|
20
|
+
=> 0
|
21
|
+
|
22
|
+
# stdout
|
23
|
+
result.stdout
|
24
|
+
=> "Gemfile\nGemfile.lock\nREADME.md\nRakefile\nbenchmark.rb\ncoverage\nhalf_shell.gemspec\nlib\ntest\n"
|
25
|
+
|
26
|
+
# stderr
|
27
|
+
result.stderr
|
28
|
+
=> ""
|
29
|
+
|
30
|
+
# raise an exception if the command exits with a non-zero status code
|
31
|
+
HalfShell.execute '(exit 123)'
|
32
|
+
=> HalfShell::ExecuteError: command <(exit 123)> failed with status <123>
|
33
|
+
|
34
|
+
# don't raise an exception if the command exits with a non-zero status code
|
35
|
+
HalfShell.execute '(exit 123)', :raise => false
|
36
|
+
=> #<HalfShell::Result:0x1012380a0 @stdout="", @status=123, @command="(exit 123)", @stderr="">
|
37
|
+
|
38
|
+
# execute a command, but only care about the status
|
39
|
+
HalfShell.execute! 'ls'
|
40
|
+
=> true
|
41
|
+
|
42
|
+
HalfShell.execute! '(exit 123)'
|
43
|
+
=> false
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.test_files = FileList['test/**/*_{test,spec}.rb']
|
7
|
+
# t.warning = true
|
8
|
+
end
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'rcov/rcovtask'
|
12
|
+
Rcov::RcovTask.new do |t|
|
13
|
+
t.test_files = FileList['test/**/*_{test,spec}.rb']
|
14
|
+
t.verbose = true
|
15
|
+
t.rcov_opts << %w{ --exclude .rvm }
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
desc 'Analyze code coverage with tests'
|
19
|
+
task :rcov do
|
20
|
+
puts "Install rcov"
|
21
|
+
end
|
22
|
+
end
|
data/benchmark.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'benchmark'
|
3
|
+
require 'systemu'
|
4
|
+
require File.expand_path '../lib/half_shell', __FILE__
|
5
|
+
|
6
|
+
commands = [
|
7
|
+
'ls',
|
8
|
+
'pwd',
|
9
|
+
'echo 123'
|
10
|
+
] * 100
|
11
|
+
|
12
|
+
Benchmark.bm 15 do |bm|
|
13
|
+
bm.report('backticks') { commands.each { |command| `#{command}` } }
|
14
|
+
bm.report('systemu') { commands.each { |command| systemu command } }
|
15
|
+
bm.report('halfshell') { commands.each { |command| HalfShell.execute(command) } }
|
16
|
+
end
|
data/half_shell.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "half_shell/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "half_shell"
|
7
|
+
s.version = HalfShell::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Chris Gibson"]
|
10
|
+
s.email = ["chrislgibson@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/chrisgibson/half_shell"
|
12
|
+
s.summary = %q{a simple ruby gem for executing shell commands}
|
13
|
+
s.description = %q{a bit cleaner than built-in methods of executing commands and less complicated under the hood than systemu}
|
14
|
+
|
15
|
+
s.rubyforge_project = "half_shell"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency 'open4'
|
23
|
+
s.add_development_dependency 'minitest'
|
24
|
+
end
|
data/lib/half_shell.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
require 'open4'
|
3
|
+
|
4
|
+
module HalfShell
|
5
|
+
|
6
|
+
class ExecuteError < StandardError
|
7
|
+
|
8
|
+
attr_reader :command, :status
|
9
|
+
|
10
|
+
def initialize(command, status)
|
11
|
+
@command, @status = command, status
|
12
|
+
super "command <#{@command}> failed with status <#{@status}>"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
|
19
|
+
def execute(*args)
|
20
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
21
|
+
opts[:raise] = true unless opts.has_key? :raise
|
22
|
+
|
23
|
+
command = args.join(' ')
|
24
|
+
|
25
|
+
stdout, stderr = nil
|
26
|
+
status = Open4.popen4(command) do |pid, _stdin, _stdout, _stderr|
|
27
|
+
stdout = _stdout.read
|
28
|
+
stderr = _stderr.read
|
29
|
+
end
|
30
|
+
|
31
|
+
if opts[:raise] && status.exitstatus != 0
|
32
|
+
raise ExecuteError.new command, status.exitstatus
|
33
|
+
end
|
34
|
+
|
35
|
+
Result.new command, status.exitstatus, stdout, stderr
|
36
|
+
end
|
37
|
+
|
38
|
+
def execute!(*args)
|
39
|
+
command = args.join(' ')
|
40
|
+
`#{command}`
|
41
|
+
$?.success?
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
require File.expand_path '../half_shell/result', __FILE__
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class HalfShell::Result
|
2
|
+
|
3
|
+
attr_reader :command, :status, :stderr, :stdout
|
4
|
+
|
5
|
+
def initialize(command, status, stdout, stderr)
|
6
|
+
@command = command
|
7
|
+
@status, @stdout, @stderr = status, stdout, stderr
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
%Q{#<HalfShell::Result command="#{shorten(command)}" status=#{status} stdout="#{shorten(stdout)}" stderr="#{shorten(stderr)}">}
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def shorten(str)
|
17
|
+
chars = 10
|
18
|
+
str.gsub(/\n/, '\n')[0..(str.size > chars ? chars - 3 : -1)] + (str.size > chars ? '...' : '')
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
describe HalfShell do
|
4
|
+
|
5
|
+
describe '#execute' do
|
6
|
+
|
7
|
+
describe 'when raising exception on non-zero status' do
|
8
|
+
|
9
|
+
it 'should raise an exception on non-zero status' do
|
10
|
+
lambda { HalfShell.execute('(exit 123)') }.must_raise HalfShell::ExecuteError
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should read stdout' do
|
14
|
+
HalfShell.execute('echo "foo bar"').stdout.chomp.must_equal "foo bar"
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should read stderr' do
|
18
|
+
HalfShell.execute('echo "foo bar" 1>&2').stderr.chomp.must_equal "foo bar"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'when NOT raising exception on non-zero status' do
|
24
|
+
|
25
|
+
it 'should return the correct status code' do
|
26
|
+
HalfShell.execute('(exit 123)', :raise => false).status.must_equal 123
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should read stdout' do
|
30
|
+
HalfShell.execute('echo "foo bar"').stdout.chomp.must_equal "foo bar"
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should read stderr' do
|
34
|
+
HalfShell.execute('echo "foo bar" 1>&2').stderr.chomp.must_equal "foo bar"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#execute!' do
|
42
|
+
|
43
|
+
it 'should return true when command exited with a zero status' do
|
44
|
+
HalfShell.execute!('(exit 0)').must_equal true
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should return false when command exited with a non-zero status' do
|
48
|
+
HalfShell.execute!('(exit 123)').must_equal false
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/test/result_spec.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
describe HalfShell::Result do
|
4
|
+
|
5
|
+
describe '.to_s' do
|
6
|
+
|
7
|
+
it 'should include command, status, stdout, and stderr' do
|
8
|
+
HalfShell::Result.new('ls', 0, 'stdout', 'stderr').to_s.must_equal \
|
9
|
+
'#<HalfShell::Result command="ls" status=0 stdout="stdout" stderr="stderr">'
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: half_shell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Chris Gibson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-24 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: open4
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: minitest
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description: a bit cleaner than built-in methods of executing commands and less complicated under the hood than systemu
|
50
|
+
email:
|
51
|
+
- chrislgibson@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- LICENSE
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- benchmark.rb
|
65
|
+
- half_shell.gemspec
|
66
|
+
- lib/half_shell.rb
|
67
|
+
- lib/half_shell/result.rb
|
68
|
+
- lib/half_shell/version.rb
|
69
|
+
- test/half_shell_spec.rb
|
70
|
+
- test/result_spec.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
has_rdoc: true
|
73
|
+
homepage: http://github.com/chrisgibson/half_shell
|
74
|
+
licenses: []
|
75
|
+
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
requirements: []
|
100
|
+
|
101
|
+
rubyforge_project: half_shell
|
102
|
+
rubygems_version: 1.5.3
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: a simple ruby gem for executing shell commands
|
106
|
+
test_files:
|
107
|
+
- test/half_shell_spec.rb
|
108
|
+
- test/result_spec.rb
|
109
|
+
- test/test_helper.rb
|