timeoutx 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,22 @@
1
+ == 0.2.0 2007-12-05
2
+
3
+ * Keita Yamaguchi
4
+ * registered rubyforge.org and made the gem.
5
+ * fixed some bugs.
6
+ * removed timeout function, please use Timeout.timeout.
7
+
8
+ == 0.1.2 2002-09-27
9
+
10
+ * patch by GOTOU Yuuzou, suitable for timeout.rb of ruby 1.7.
11
+ second argument of timeout() can specify exception type.
12
+ * license was changed from LGPL to Ruby's.
13
+
14
+
15
+ == 0.1.1 2002-08-27
16
+
17
+ * fix typo by GOTOU Yuuzou, [webrickja:40].
18
+
19
+
20
+ == 0.1.0 2002-08-25
21
+
22
+ * Initial release by Keita Yamaguchi.
data/License.txt ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see the file GPL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/Manifest.txt ADDED
@@ -0,0 +1,18 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ examples/benchmark.rb
7
+ lib/timeoutx.rb
8
+ lib/timeoutx/version.rb
9
+ scripts/txt2html
10
+ setup.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
13
+ spec/timeoutx_spec.rb
14
+ website/index.html
15
+ website/index.txt
16
+ website/javascripts/rounded_corners_lite.inc.js
17
+ website/stylesheets/screen.css
18
+ website/template.rhtml
data/README.txt ADDED
@@ -0,0 +1,7 @@
1
+ README for timeoutx
2
+ ===================
3
+ timeoutx provides lightweight timeout function.
4
+ See http://timeoutx.rubyforge.org.
5
+
6
+ Copyright (c) 2002-2007 Keita Yamaguchi
7
+ Mail: keita.yamaguchi@gmail.com
data/Rakefile ADDED
@@ -0,0 +1,138 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+ begin
12
+ require 'spec/rake/spectask'
13
+ rescue LoadError
14
+ puts 'To use rspec for testing you must install rspec gem:'
15
+ puts '$ sudo gem install rspec'
16
+ exit
17
+ end
18
+
19
+ include FileUtils
20
+ require File.join(File.dirname(__FILE__), 'lib', 'timeoutx', 'version')
21
+
22
+ AUTHOR = 'Keita Yamaguchi' # can also be an array of Authors
23
+ EMAIL = "keita.yamaguchi@gmail.com"
24
+ DESCRIPTION = "timeoutx provides lightweight timeout function."
25
+ GEM_NAME = 'timeoutx' # what ppl will type to install your gem
26
+
27
+ @config_file = "~/.rubyforge/user-config.yml"
28
+ @config = nil
29
+ def rubyforge_username
30
+ unless @config
31
+ begin
32
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
33
+ rescue
34
+ puts <<-EOS
35
+ ERROR: No rubyforge config file found: #{@config_file}"
36
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
37
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
38
+ EOS
39
+ exit
40
+ end
41
+ end
42
+ @rubyforge_username ||= @config["username"]
43
+ end
44
+
45
+ RUBYFORGE_PROJECT = 'timeoutx' # The unix name for your project
46
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
47
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
48
+
49
+ NAME = "timeoutx"
50
+ REV = nil
51
+ # UNCOMMENT IF REQUIRED:
52
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
53
+ VERS = TimeoutX::VERSION::STRING + (REV ? ".#{REV}" : "")
54
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
55
+ RDOC_OPTS = ['--quiet', '--title', 'timeoutx documentation',
56
+ "--opname", "index.html",
57
+ "--line-numbers",
58
+ "--main", "README",
59
+ "--inline-source"]
60
+
61
+ class Hoe
62
+ def extra_deps
63
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
64
+ end
65
+ end
66
+
67
+ # Generate all the Rake tasks
68
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
69
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
70
+ p.author = AUTHOR
71
+ p.description = DESCRIPTION
72
+ p.email = EMAIL
73
+ p.summary = DESCRIPTION
74
+ p.url = HOMEPATH
75
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
76
+ p.test_globs = ["test/**/test_*.rb"]
77
+ p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
78
+
79
+ # == Optional
80
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
81
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
82
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
83
+ end
84
+
85
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
86
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
87
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
88
+
89
+ desc 'Generate website files'
90
+ task :website_generate do
91
+ Dir['website/**/*.txt'].each do |txt|
92
+ sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
93
+ end
94
+ end
95
+
96
+ desc 'Upload website files to rubyforge'
97
+ task :website_upload do
98
+ host = "#{rubyforge_username}@rubyforge.org"
99
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
100
+ local_dir = 'website'
101
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
102
+ end
103
+
104
+ desc 'Generate and upload website files'
105
+ task :website => [:website_generate, :website_upload, :publish_docs]
106
+
107
+ desc 'Release the website and new gem version'
108
+ task :deploy => [:check_version, :website, :release] do
109
+ puts "Remember to create SVN tag:"
110
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
111
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
112
+ puts "Suggested comment:"
113
+ puts "Tagging release #{CHANGES}"
114
+ end
115
+
116
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
117
+ task :local_deploy => [:website_generate, :install_gem]
118
+
119
+ task :check_version do
120
+ unless ENV['VERSION']
121
+ puts 'Must pass a VERSION=x.y.z release version'
122
+ exit
123
+ end
124
+ unless ENV['VERSION'] == VERS
125
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
126
+ exit
127
+ end
128
+ end
129
+
130
+ desc "Run the specs under spec/models"
131
+ Spec::Rake::SpecTask.new do |t|
132
+ t.spec_opts = ['--options', "spec/spec.opts"]
133
+ t.spec_files = FileList['spec/*_spec.rb']
134
+ end
135
+
136
+ desc "Default task is to run specs"
137
+ task :default => :spec
138
+
@@ -0,0 +1,10 @@
1
+ require "benchmark"
2
+
3
+ require "timeout"
4
+ require "timeoutx"
5
+
6
+ n = 100000
7
+ Benchmark.bmbm(10) do |x|
8
+ x.report("Timeout") { 1.upto(n) do ; Timeout.timeout(1){true}; end }
9
+ x.report("TimeoutX") { 1.upto(n) do ; TimeoutX.timeout(1){true}; end }
10
+ end
@@ -0,0 +1,9 @@
1
+ class TimeoutX #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/timeoutx.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'singleton'
2
+
3
+ class TimeoutX
4
+ include Singleton
5
+
6
+ attr_accessor :interval
7
+ attr_reader :thread
8
+ attr_reader :table
9
+
10
+ def initialize #:nodoc:
11
+ @table = {}
12
+ @interval = 0.5
13
+ @thread = Thread.start do
14
+ while true
15
+ sleep(@interval)
16
+ countdown
17
+ end
18
+ end
19
+ end
20
+
21
+ # Sets the interval second of intrenal countdown loop.
22
+ def self.set_interval(sec)
23
+ instance.interval = sec
24
+ end
25
+
26
+ def countdown #:nodoc:
27
+ @table.each do |th, attr|
28
+ attr[0] -= @interval
29
+ if attr[0] <= 0
30
+ th.raise(attr[1], "execution expired") if th.alive?
31
+ @table.delete(th)
32
+ end
33
+ end
34
+ end
35
+
36
+ def self.append(th, sec, exception) #:nodoc:
37
+ instance.table[th] = [sec, exception]
38
+ end
39
+
40
+ def self.delete(th) #:nodoc:
41
+ instance.table.delete(th)
42
+ end
43
+
44
+ # Same as Timeout#timeout.
45
+ def self.timeout(sec, exception=TimeoutX::Error)
46
+ append(Thread.current, sec, exception)
47
+ begin
48
+ yield if block_given?
49
+ rescue => ex
50
+ raise(ex)
51
+ ensure
52
+ delete(Thread.current)
53
+ end
54
+ end
55
+
56
+ # Replaces Timeout.timeout into TimeoutX.timeout.
57
+ def self.replace_timeout
58
+ require "timeout"
59
+ def Timeout.timeout(sec, exception=Timeout::Error)
60
+ TimeoutX.timeout(sec, exception)
61
+ end
62
+ end
63
+
64
+ # Raised by TimeoutX#timeout when the block times out.
65
+ class Error < Interrupt; end
66
+ end
67
+
68
+ require 'timeoutx/version'
data/scripts/txt2html ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'redcloth'
5
+ require 'syntax/convertors/html'
6
+ require 'erb'
7
+ require File.dirname(__FILE__) + '/../lib/timeoutx/version.rb'
8
+
9
+ version = TimeoutX::VERSION::STRING
10
+ download = 'http://rubyforge.org/projects/timeoutx'
11
+
12
+ class Fixnum
13
+ def ordinal
14
+ # teens
15
+ return 'th' if (10..19).include?(self % 100)
16
+ # others
17
+ case self % 10
18
+ when 1: return 'st'
19
+ when 2: return 'nd'
20
+ when 3: return 'rd'
21
+ else return 'th'
22
+ end
23
+ end
24
+ end
25
+
26
+ class Time
27
+ def pretty
28
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
29
+ end
30
+ end
31
+
32
+ def convert_syntax(syntax, source)
33
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
34
+ end
35
+
36
+ if ARGV.length >= 1
37
+ src, template = ARGV
38
+ template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
39
+
40
+ else
41
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
42
+ exit!
43
+ end
44
+
45
+ template = ERB.new(File.open(template).read)
46
+
47
+ title = nil
48
+ body = nil
49
+ File.open(src) do |fsrc|
50
+ title_text = fsrc.readline
51
+ body_text = fsrc.read
52
+ syntax_items = []
53
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</>!m){
54
+ ident = syntax_items.length
55
+ element, syntax, source = $1, $2, $3
56
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
57
+ "syntax-temp-#{ident}"
58
+ }
59
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
60
+ body = RedCloth.new(body_text).to_html
61
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
62
+ end
63
+ stat = File.stat(src)
64
+ created = stat.ctime
65
+ modified = stat.mtime
66
+
67
+ $stdout << template.result(binding)