rtimeout 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ bin/rtimeout
2
+ lib/rtimeout.rb
3
+ README
4
+ test/rtimeout_test.rb
5
+ test/slow_script.rb
6
+ Manifest
data/README ADDED
@@ -0,0 +1,4 @@
1
+ $ rtimeout <timeout> <script to run>
2
+
3
+ For: Github
4
+ By : chris@ozmm.org
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'rtimeout'
4
+
5
+ if ARGV.size < 2
6
+ STDERR.puts "Usage: $ rtimeout <timeout> <command to run>"
7
+ exit! 1
8
+ end
9
+
10
+ Rtimeout.read(ARGV.shift, ARGV.join(' '))
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'open4'
3
+
4
+ class Rtimeout
5
+ def initialize(timeout, command)
6
+ @timeout = timeout.to_i
7
+ @command = command
8
+ @pid, _, @io, _ = Open4.popen4(command)
9
+ end
10
+
11
+ def read
12
+ while result = IO.select([@io], nil, nil, @timeout)
13
+ out = @io.read(1)
14
+ break if out.nil?
15
+ $stdout.print out
16
+ end
17
+
18
+ if result.nil?
19
+ system "kill -9 #{@pid}"
20
+ $stderr.puts "rtimeout: Command `#{@command}` timed out."
21
+ end
22
+ end
23
+
24
+ def self.read(timeout, command)
25
+ new(timeout, command).read
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+
2
+ # Gem::Specification for Rtimeout-1.0
3
+ # Originally generated by Echoe
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{rtimeout}
7
+ s.version = "1.0"
8
+ s.date = %q{2008-03-27}
9
+ s.summary = %q{For timing out system commands.}
10
+ s.email = %q{chris@ozmm.org}
11
+ s.homepage = %q{http://github.com/defunkt/rtimeout}
12
+ s.rubyforge_project = %q{github}
13
+ s.description = %q{For timing out system commands.}
14
+ s.default_executable = %q{rtimeout}
15
+ s.has_rdoc = true
16
+ s.authors = ["Chris Wanstrath"]
17
+ s.files = ["bin/rtimeout", "lib/rtimeout.rb", "README", "test/rtimeout_test.rb", "test/slow_script.rb", "Manifest", "rtimeout.gemspec"]
18
+ s.test_files = ["test/rtimeout_test.rb"]
19
+ s.executables = ["rtimeout"]
20
+ s.add_dependency(%q<open4>, [">= 0.9.6"])
21
+ end
22
+
23
+
24
+ # # Original Rakefile source (requires the Echoe gem):
25
+ #
26
+ # require 'rubygems'
27
+ # require 'rake'
28
+ #
29
+ # version = '1.0'
30
+ #
31
+ # begin
32
+ # require 'echoe'
33
+ #
34
+ # Echoe.new('rtimeout', version) do |p|
35
+ # p.rubyforge_name = 'github'
36
+ # p.summary = "For timing out system commands."
37
+ # p.url = "http://github.com/defunkt/rtimeout"
38
+ # p.author = 'Chris Wanstrath'
39
+ # p.email = "chris@ozmm.org"
40
+ # p.dependencies = ['open4 >=0.9.6']
41
+ # end
42
+ #
43
+ # rescue LoadError => boom
44
+ # puts "You are missing a dependency required for meta-operations on this gem."
45
+ # puts "#{boom.to_s.capitalize}."
46
+ # end
@@ -0,0 +1,68 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+
5
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
6
+ require 'rtimeout'
7
+
8
+ def context(*args, &block)
9
+ return super unless (name = args.first) && block
10
+ require 'test/unit'
11
+ klass = Class.new(Test::Unit::TestCase) do
12
+ def self.specify(name, &block)
13
+ define_method("test_#{name.gsub(/\W/,'_')}", &block)
14
+ end
15
+ def self.xspecify(*args) end
16
+ def self.setup(&block) define_method(:setup, &block) end
17
+ def self.teardown(&block) define_method(:teardown, &block) end
18
+ end
19
+ klass.class_eval &block
20
+ end
21
+
22
+ module FakeIO
23
+ def fake_stdout
24
+ old_io = $stdout.dup
25
+ tempio = StringIO.new
26
+
27
+ $stdout = tempio
28
+ yield
29
+ tempio.string
30
+ ensure
31
+ $stdout = old_io
32
+ end
33
+
34
+ def fake_stderr
35
+ old_io = $stderr.dup
36
+ tempio = StringIO.new
37
+
38
+ $stderr = tempio
39
+ yield
40
+ tempio.string
41
+ ensure
42
+ $stderr = old_io
43
+ end
44
+ end
45
+
46
+ context "rtimeout" do
47
+ include FakeIO
48
+
49
+ setup do
50
+ @test_script = 'ruby ' + File.dirname(__FILE__) + '/slow_script.rb'
51
+ end
52
+
53
+ specify "prints to stdout on success" do
54
+ out = fake_stdout do
55
+ Rtimeout.read(3, @test_script)
56
+ end
57
+
58
+ assert_equal "It worked!\n", out
59
+ end
60
+
61
+ specify "prints to stderr on timeout" do
62
+ out = fake_stderr do
63
+ Rtimeout.read(1, @test_script)
64
+ end
65
+
66
+ assert_equal "rtimeout: Command `#{@test_script}` timed out.\n", out
67
+ end
68
+ end
@@ -0,0 +1,2 @@
1
+ sleep 1.5
2
+ puts "It worked!"
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: rtimeout
5
+ version: !ruby/object:Gem::Version
6
+ version: "1.0"
7
+ date: 2008-03-27 00:00:00 -04:00
8
+ summary: For timing out system commands.
9
+ require_paths:
10
+ - lib
11
+ email: chris@ozmm.org
12
+ homepage: http://github.com/defunkt/rtimeout
13
+ rubyforge_project: github
14
+ description: For timing out system commands.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Chris Wanstrath
31
+ files:
32
+ - bin/rtimeout
33
+ - lib/rtimeout.rb
34
+ - README
35
+ - test/rtimeout_test.rb
36
+ - test/slow_script.rb
37
+ - Manifest
38
+ - rtimeout.gemspec
39
+ test_files:
40
+ - test/rtimeout_test.rb
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ executables:
46
+ - rtimeout
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies:
52
+ - !ruby/object:Gem::Dependency
53
+ name: open4
54
+ version_requirement:
55
+ version_requirements: !ruby/object:Gem::Version::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.9.6
60
+ version: