shellshot 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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +56 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/shellshot.rb +59 -0
- data/spec/shellshot_spec.rb +32 -0
- data/spec/spec_helper.rb +43 -0
- metadata +74 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Petyo Ivanov
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
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 OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
= shellshot
|
2
|
+
|
3
|
+
by Petyo Ivanov
|
4
|
+
http://dekaft.underlog.org
|
5
|
+
|
6
|
+
== DESCRIPTION:
|
7
|
+
|
8
|
+
A small library dealing with the obscurities of shell commands, piping and timeouts.
|
9
|
+
Most of the stuff comes from bad experience in http://beanstalkapp.com.
|
10
|
+
|
11
|
+
== FEATURES/PROBLEMS:
|
12
|
+
|
13
|
+
The whole thing would probably not work in Windows. Not my first time I guess.
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
# Basic usage
|
18
|
+
cmd = Shellshot.exec %q[ruby -e "puts 'Hello World'"]
|
19
|
+
|
20
|
+
# Pipes
|
21
|
+
|
22
|
+
Shellshot.exec "ruby -e %q[ruby -e "puts 'Hello World'"], :stdout => '/tmp/out', :stderr => '/tmp/err'
|
23
|
+
Shellshot.exec "ruby -e %q[ruby -e "puts 'Hello World'"], :stdall => '/tmp/all'
|
24
|
+
|
25
|
+
# Timeout
|
26
|
+
begin
|
27
|
+
Shellshot.exec "ruby -e 'sleep 10000'", :timeout => 2 # seconds
|
28
|
+
rescue Timeout::Error => e
|
29
|
+
# ...
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
== LICENSE:
|
34
|
+
|
35
|
+
(The MIT License)
|
36
|
+
|
37
|
+
Copyright (c) 2008 FIXME (different license?)
|
38
|
+
|
39
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
40
|
+
a copy of this software and associated documentation files (the
|
41
|
+
'Software'), to deal in the Software without restriction, including
|
42
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
43
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
44
|
+
permit persons to whom the Software is furnished to do so, subject to
|
45
|
+
the following conditions:
|
46
|
+
|
47
|
+
The above copyright notice and this permission notice shall be
|
48
|
+
included in all copies or substantial portions of the Software.
|
49
|
+
|
50
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
51
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
52
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
53
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
54
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
55
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
56
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "shellshot"
|
8
|
+
gem.summary = %Q{ruby proxy handling the complexities of system invocations}
|
9
|
+
gem.description = %Q{
|
10
|
+
A small library dealing with the obscurities of shell commands, piping and timeouts.
|
11
|
+
Most of the stuff comes from bad experience in http://beanstalkapp.com.
|
12
|
+
}
|
13
|
+
gem.email = "underlog@gmail.com"
|
14
|
+
gem.homepage = "http://github.com/underlog/shellshot"
|
15
|
+
gem.authors = ["Petyo Ivanov"]
|
16
|
+
gem.add_development_dependency "rspec"
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.spec_opts = %w[--format specdoc --color]
|
28
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
32
|
+
spec.libs << 'lib' << 'spec'
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :spec => :check_dependencies
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
if File.exist?('VERSION')
|
44
|
+
version = File.read('VERSION')
|
45
|
+
else
|
46
|
+
version = ""
|
47
|
+
end
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "shellshot #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/shellshot.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'english'
|
2
|
+
|
3
|
+
module Shellshot
|
4
|
+
|
5
|
+
class Command
|
6
|
+
|
7
|
+
alias_method :system_exec, :exec
|
8
|
+
|
9
|
+
DEFAULT_TIMEOUT = 60 #minutes
|
10
|
+
|
11
|
+
attr_accessor :pid, :status
|
12
|
+
|
13
|
+
def exec(command, options = {})
|
14
|
+
self.pid = fork do
|
15
|
+
redefine_stds(options)
|
16
|
+
system_exec(command)
|
17
|
+
end
|
18
|
+
|
19
|
+
begin
|
20
|
+
wait_for(options[:timeout] || DEFAULT_TIMEOUT)
|
21
|
+
rescue Timeout::Error => e
|
22
|
+
terminate_child_process
|
23
|
+
raise
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def wait_for(seconds)
|
30
|
+
Timeout.timeout(seconds) do
|
31
|
+
Process.wait(pid)
|
32
|
+
self.status = $CHILD_STATUS
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def terminate_child_process
|
37
|
+
if pid
|
38
|
+
Process.kill("KILL", pid)
|
39
|
+
Process.wait(pid) # reaping zombie processes. Not sure if correct.
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def redefine_stds(options)
|
44
|
+
$stdout.reopen(File.open(options[:stdout], "w+")) if options[:stdout]
|
45
|
+
$stderr.reopen(File.open(options[:stderr], "w+")) if options[:stderr]
|
46
|
+
|
47
|
+
if options[:stdall]
|
48
|
+
combined = File.open(options[:stdall], "w+")
|
49
|
+
$stdout.reopen(combined)
|
50
|
+
$stderr.reopen(combined)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.exec(command, options = {})
|
56
|
+
Shellshot::Command.new().exec(command, options)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
require File.join(File.dirname(__FILE__), %w[spec_helper])
|
3
|
+
|
4
|
+
describe Shellshot do
|
5
|
+
|
6
|
+
it "should execute the given command" do
|
7
|
+
Shellshot.exec("touch file")
|
8
|
+
"file".should be_a_file
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should pipe the out to the right file" do
|
12
|
+
Shellshot.exec(%q[ruby -e 'puts "Hello World"'], :stdout => "file")
|
13
|
+
"file".should contain("Hello World")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should pipe the err to the right file" do
|
17
|
+
Shellshot.exec(%q[ruby -e '$stderr << "error"'], :stderr => "file")
|
18
|
+
"file".should contain("error")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should pipe everything to the right file" do
|
22
|
+
Shellshot.exec(%q[ruby -e '$stderr << "Hello "; puts "World"'], :stdall => "file")
|
23
|
+
"file".should contain("Hello World")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should cancel execution on time if timeout provided" do
|
27
|
+
lambda { Shellshot.exec %q[ruby -e 'sleep 10000'], :timeout => 1 }.should raise_error(Timeout::Error)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
# EOF
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
require File.expand_path(
|
3
|
+
File.join(File.dirname(__FILE__), %w[.. lib shellshot]))
|
4
|
+
|
5
|
+
require 'tmpdir'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'ruby-debug'
|
8
|
+
|
9
|
+
Spec::Matchers.define :be_a_file do
|
10
|
+
match do |path|
|
11
|
+
File.exists?(path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Spec::Matchers.define :contain do |contents|
|
16
|
+
match do |path|
|
17
|
+
File.exists?(path) && File.read(path).strip == contents
|
18
|
+
end
|
19
|
+
|
20
|
+
failure_message_for_should do |path|
|
21
|
+
if File.exists?(path)
|
22
|
+
"expected #{path} to contain '#{contents}', got '#{File.read(path).strip}'"
|
23
|
+
else
|
24
|
+
"expected #{path} to contain '#{contents}', but the file does not exist."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Runner.configure do |config|
|
30
|
+
config.before(:each) do
|
31
|
+
@tmpdir = Dir.mktmpdir
|
32
|
+
Dir.chdir(@tmpdir)
|
33
|
+
end
|
34
|
+
|
35
|
+
config.after(:each) do
|
36
|
+
FileUtils.remove_entry_secure @tmpdir
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
# EOF
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shellshot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Petyo Ivanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-12 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: "\n A small library dealing with the obscurities of shell commands, piping and timeouts. \n Most of the stuff comes from bad experience in http://beanstalkapp.com.\n "
|
26
|
+
email: underlog@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/shellshot.rb
|
42
|
+
- spec/shellshot_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/underlog/shellshot
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.4
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: ruby proxy handling the complexities of system invocations
|
72
|
+
test_files:
|
73
|
+
- spec/shellshot_spec.rb
|
74
|
+
- spec/spec_helper.rb
|