ruby_ex 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/AUTHORS +51 -0
  2. data/ChangeLog +1763 -0
  3. data/NEWS +3 -0
  4. data/README +1 -0
  5. data/Rakefile +8 -0
  6. data/SPEC.dyn.yml +10 -0
  7. data/SPEC.gem.yml +269 -0
  8. data/SPEC.yml +36 -0
  9. data/src/abstract.rb +253 -0
  10. data/src/abstract_node.rb +85 -0
  11. data/src/algorithms.rb +12 -0
  12. data/src/algorithms/simulated_annealing.rb +142 -0
  13. data/src/ask.rb +100 -0
  14. data/src/attributed_class.rb +303 -0
  15. data/src/cache.rb +350 -0
  16. data/src/checkout.rb +12 -0
  17. data/src/choose.rb +271 -0
  18. data/src/commands.rb +20 -0
  19. data/src/commands/command.rb +492 -0
  20. data/src/commands/datas.rb +16 -0
  21. data/src/commands/datas/composite.rb +31 -0
  22. data/src/commands/datas/data.rb +65 -0
  23. data/src/commands/datas/factory.rb +69 -0
  24. data/src/commands/datas/temp.rb +26 -0
  25. data/src/commands/factory.rb +67 -0
  26. data/src/commands/helpers.rb +81 -0
  27. data/src/commands/pipe.rb +66 -0
  28. data/src/commands/runners.rb +16 -0
  29. data/src/commands/runners/exec.rb +50 -0
  30. data/src/commands/runners/fork.rb +130 -0
  31. data/src/commands/runners/runner.rb +140 -0
  32. data/src/commands/runners/system.rb +57 -0
  33. data/src/commands/seq.rb +32 -0
  34. data/src/config_file.rb +95 -0
  35. data/src/const_regexp.rb +57 -0
  36. data/src/daemon.rb +135 -0
  37. data/src/diff.rb +665 -0
  38. data/src/dlogger.rb +62 -0
  39. data/src/drb/drb_observable.rb +95 -0
  40. data/src/drb/drb_observable_pool.rb +27 -0
  41. data/src/drb/drb_service.rb +44 -0
  42. data/src/drb/drb_undumped_attributes.rb +56 -0
  43. data/src/drb/drb_undumped_indexed_object.rb +55 -0
  44. data/src/drb/insecure_protected_methods.rb +101 -0
  45. data/src/drb_ex.rb +12 -0
  46. data/src/dumpable_proc.rb +57 -0
  47. data/src/filetype.rb +229 -0
  48. data/src/generate_id.rb +44 -0
  49. data/src/histogram.rb +222 -0
  50. data/src/hookable.rb +283 -0
  51. data/src/hooker.rb +54 -0
  52. data/src/indexed_node.rb +65 -0
  53. data/src/io_marshal.rb +99 -0
  54. data/src/ioo.rb +193 -0
  55. data/src/labeled_node.rb +62 -0
  56. data/src/logger_observer.rb +24 -0
  57. data/src/md5sum.rb +70 -0
  58. data/src/module/autoload_tree.rb +65 -0
  59. data/src/module/hierarchy.rb +334 -0
  60. data/src/module/instance_method_visibility.rb +71 -0
  61. data/src/node.rb +81 -0
  62. data/src/object_monitor.rb +143 -0
  63. data/src/object_monitor_activity.rb +34 -0
  64. data/src/observable.rb +138 -0
  65. data/src/observable_pool.rb +291 -0
  66. data/src/orderedhash.rb +252 -0
  67. data/src/pp_hierarchy.rb +30 -0
  68. data/src/random_generators.rb +29 -0
  69. data/src/random_generators/random_generator.rb +33 -0
  70. data/src/random_generators/ruby.rb +25 -0
  71. data/src/ruby_ex.rb +124 -0
  72. data/src/safe_eval.rb +346 -0
  73. data/src/sendmail.rb +214 -0
  74. data/src/service_manager.rb +122 -0
  75. data/src/shuffle.rb +30 -0
  76. data/src/spring.rb +134 -0
  77. data/src/spring_set.rb +134 -0
  78. data/src/symtbl.rb +108 -0
  79. data/src/synflow.rb +474 -0
  80. data/src/thread_mutex.rb +11 -0
  81. data/src/timeout_ex.rb +79 -0
  82. data/src/trace.rb +26 -0
  83. data/src/uri/druby.rb +78 -0
  84. data/src/uri/file.rb +63 -0
  85. data/src/uri/ftp_ex.rb +36 -0
  86. data/src/uri/http_ex.rb +41 -0
  87. data/src/uri/pgsql.rb +136 -0
  88. data/src/uri/ssh.rb +87 -0
  89. data/src/uri/svn.rb +113 -0
  90. data/src/uri_ex.rb +71 -0
  91. data/src/verbose_object.rb +70 -0
  92. data/src/yaml/basenode_ext.rb +63 -0
  93. data/src/yaml/chop_header.rb +24 -0
  94. data/src/yaml/transform.rb +450 -0
  95. data/src/yaml/yregexpath.rb +76 -0
  96. data/test/algorithms/simulated_annealing_test.rb +102 -0
  97. data/test/check-pkg-ruby_ex.yml +15 -0
  98. data/test/check-ruby_ex.yml +12 -0
  99. data/test/resources/autoload_tree/A.rb +11 -0
  100. data/test/resources/autoload_tree/B.rb +10 -0
  101. data/test/resources/autoload_tree/foo/C.rb +18 -0
  102. data/test/resources/foo.txt +6 -0
  103. data/test/sanity-suite.yml +12 -0
  104. data/test/sanity/multiple-requires.yml +20 -0
  105. data/test/sanity/single-requires.yml +24 -0
  106. data/test/test-unit-setup.rb +6 -0
  107. data/test/unit-suite.yml +14 -0
  108. metadata +269 -0
@@ -0,0 +1,11 @@
1
+ require 'ruby_ex'
2
+ class Thread
3
+
4
+ alias :raise_without_exception_call :raise
5
+
6
+ def raise ( exc, *a, &b )
7
+ exc = exc.exception if exc.respond_to? :exception
8
+ raise_without_exception_call(exc, *a, &b)
9
+ end
10
+
11
+ end
data/src/timeout_ex.rb ADDED
@@ -0,0 +1,79 @@
1
+ # Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
+ # Author: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: timeout_ex.rb 266 2005-06-01 14:27:18Z ertai $
7
+
8
+
9
+ require 'ruby_ex'
10
+ require 'thread'
11
+
12
+
13
+ module TimeoutEx
14
+
15
+ class Error < Interrupt; end
16
+
17
+ module_function
18
+ def timeout(sec, exception=Error, mutex=nil, &block)
19
+ if sec > 0
20
+ thread = Thread.new(mutex) do |mutex|
21
+ Thread.current[:timeout_mutex] = mutex
22
+ block[]
23
+ end
24
+ sleep(sec)
25
+ if thread.alive?
26
+ unless mutex.nil?
27
+ mutex.synchronize { thread.kill if thread.alive? }
28
+ else
29
+ thread.kill
30
+ end
31
+ raise(exception, 'execution expired')
32
+ end
33
+ else
34
+ block[]
35
+ end
36
+ true
37
+ end
38
+
39
+ end # module TimeoutEx
40
+
41
+
42
+ test_section __FILE__ do
43
+
44
+
45
+ class TimeoutExTest < Test::Unit::TestCase
46
+
47
+ def test_timeout
48
+ run = false
49
+ assert_raises(TimeoutEx::Error) do
50
+ TimeoutEx.timeout(0.1) do
51
+ sleep(0.5)
52
+ run = true
53
+ end
54
+ end
55
+ assert(! run)
56
+ end
57
+
58
+ def test_mutex
59
+ run = false
60
+ assert_raises(TimeoutEx::Error) do
61
+ TimeoutEx.timeout(0.1, TimeoutEx::Error, Mutex.new) do
62
+ Thread.current[:timeout_mutex].synchronize do
63
+ sleep(0.5)
64
+ run = true
65
+ end
66
+ end
67
+ end
68
+ assert(run)
69
+ end
70
+
71
+ def test_no_timeout
72
+ assert(TimeoutEx.timeout(0.1) { nil })
73
+ end
74
+
75
+ end # class TimeoutExTest
76
+
77
+
78
+ end
79
+
data/src/trace.rb ADDED
@@ -0,0 +1,26 @@
1
+ # Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
+ # Author: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: polrop $
6
+ # $Id: trace.rb 90 2005-01-09 22:09:32Z polrop $
7
+
8
+
9
+ module Kernel
10
+
11
+ def trace_on
12
+ set_trace_func proc { |event, file, line, id, binding, classname|
13
+ printf("%8s %s:%-2d %10s %8s\n",
14
+ event,
15
+ file,
16
+ line,
17
+ id,
18
+ classname)
19
+ }
20
+ end
21
+
22
+ def trace_off
23
+ set_trace_func nil
24
+ end
25
+
26
+ end # module Kernel
data/src/uri/druby.rb ADDED
@@ -0,0 +1,78 @@
1
+ # Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
+ # Author: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: druby.rb 258 2005-06-01 00:22:51Z ertai $
7
+
8
+
9
+ require 'uri_ex'
10
+
11
+
12
+ module URI
13
+
14
+ class DRuby < Generic
15
+
16
+ SCHEME = 'druby'
17
+ DEFAULT_HOST = 'localhost'
18
+ DEFAULT_PORT = 0
19
+ DEFAULT_QUERY = ''
20
+ COMPONENT = [ :scheme, :host, :port ].freeze
21
+
22
+ def self.build(args)
23
+ if args.is_a?(Array)
24
+ args << DEFAULT_HOST if args.size < 1
25
+ args << DEFAULT_PORT if args.size < 2
26
+ elsif args.is_a?(Hash)
27
+ args[:host] = DEFAULT_HOST if args[:host].nil?
28
+ args[:port] = DEFAULT_PORT if args[:port].nil?
29
+ end
30
+ return super(Util::make_components_hash(self, args))
31
+ end
32
+
33
+ #FIXME: find a way to always print the port even if it is the default one.
34
+
35
+ end # class DRuby
36
+
37
+ @@schemes[DRuby::SCHEME.upcase] = DRuby
38
+
39
+ end # module URI
40
+
41
+
42
+ test_section __FILE__ do
43
+
44
+
45
+ module URI
46
+
47
+ class DRubyTest < ::Test::Unit::TestCase
48
+
49
+ def test_classic
50
+ uri = URI.parse('druby://www.foo.com:42000')
51
+ assert(uri.is_a?(DRuby))
52
+ assert_equal('druby', uri.scheme)
53
+ assert_nil(uri.user)
54
+ assert_nil(uri.password)
55
+ assert_nil(uri.userinfo)
56
+ assert_equal('www.foo.com', uri.host)
57
+ assert_equal(42000, uri.port)
58
+ assert_equal('druby://www.foo.com:42000', uri.to_s)
59
+ end
60
+
61
+ def test_build
62
+ assert_equal('druby://localhost', DRuby.build([]).to_s)
63
+ assert_equal('druby://www.foo.com:52000',
64
+ DRuby.build(['www.foo.com', 52000 ]).to_s)
65
+ end
66
+
67
+ def test_query
68
+ uri = DRuby.build(['www.foo.com', 52000 ])
69
+ uri.query = 'toto=tata&foo=bar'
70
+ assert_equal('druby://www.foo.com:52000?toto=tata&foo=bar', uri.to_s)
71
+ end
72
+
73
+ end # class DRubyTest
74
+
75
+ end # module URI
76
+
77
+
78
+ end
data/src/uri/file.rb ADDED
@@ -0,0 +1,63 @@
1
+ # Copyright: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: file.rb 258 2005-06-01 00:22:51Z ertai $
7
+
8
+ require 'uri_ex'
9
+
10
+ module URI
11
+
12
+ class File < Generic
13
+
14
+ COMPONENT = [
15
+ :scheme,
16
+ :path
17
+ ].freeze
18
+ TAR = 'tar'.to_cmd.freeze
19
+ GZIP = 'gzip'.to_cmd.freeze
20
+
21
+
22
+ def initialize ( *args )
23
+ super
24
+ unless @host.nil? or @host.empty?
25
+ raise ArgumentError,
26
+ "You cannot neither setup a host (#{@host}), nor a relative path"
27
+ end
28
+ end
29
+
30
+ def self.build ( args )
31
+ tmp = Util::make_components_hash(self, args)
32
+ return super(tmp)
33
+ end
34
+
35
+ def checkout
36
+ p = self.pathname
37
+ raise CheckoutError, to_s unless p.exist?
38
+ p
39
+ end
40
+
41
+ def commit ( aPath )
42
+ aPath.cp_r(self.pathname)
43
+ end
44
+
45
+ def save
46
+ p = self.pathname
47
+ raise SaveError, to_s unless p.exist?
48
+ if p.directory?
49
+ out = TempPath.new('save', "#{p.basename}.tar.gz")
50
+ cmd = TAR['czf', out, p]
51
+ else
52
+ out = TempPath.new('save', "#{p.basename}.gz")
53
+ cmd = GZIP['-c', p] > out
54
+ end
55
+ cmd.run(self.runner)
56
+ out
57
+ end
58
+
59
+ end # class File
60
+
61
+ @@schemes['FILE'] = File
62
+
63
+ end # module URI
data/src/uri/ftp_ex.rb ADDED
@@ -0,0 +1,36 @@
1
+ # Copyright: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: ftp_ex.rb 258 2005-06-01 00:22:51Z ertai $
7
+
8
+ require 'uri_ex'
9
+ require 'uri/ftp'
10
+ require 'net/ftp'
11
+
12
+ module URI
13
+
14
+ class FTP
15
+
16
+ def checkout
17
+ tmp = TempPath.new('checkout', pathname.basename.to_s)
18
+
19
+ begin
20
+ Net::FTP.open(host) do |ftp|
21
+ ftp.login
22
+ ftp.passive = true
23
+ ftp.getbinaryfile(path, tmp)
24
+ end
25
+
26
+ rescue Exception => ex
27
+ tmp.unlink if tmp.exist?
28
+ raise CheckoutError, "Cannot checkout #{to_s} (error: #{ex.short_pp})"
29
+ end
30
+
31
+ tmp
32
+ end
33
+
34
+ end # class FTP
35
+
36
+ end # module URI
@@ -0,0 +1,41 @@
1
+ # Copyright: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: http_ex.rb 258 2005-06-01 00:22:51Z ertai $
7
+
8
+ require 'uri_ex'
9
+ require 'uri/http'
10
+ require 'net/http'
11
+
12
+ module URI
13
+
14
+ class HTTP
15
+
16
+ def checkout
17
+ tmp = TempPath.new('checkout', pathname.basename.to_s)
18
+
19
+ begin
20
+ uri = URI.parse(ENV['http_proxy'] || '')
21
+ proxy_auth = [uri.host, uri.port, uri.user, uri.password]
22
+
23
+ Net::HTTP::Proxy(*proxy_auth).start(host) do |http|
24
+ tmp.open('w') do |out|
25
+ http.get(path) do |str|
26
+ out.print str
27
+ end
28
+ end
29
+ end
30
+
31
+ rescue Exception => ex
32
+ tmp.unlink if tmp.exist?
33
+ raise CheckoutError, "Cannot checkout #{to_s} (error: #{ex.short_pp})"
34
+ end
35
+
36
+ tmp
37
+ end
38
+
39
+ end # class HTTP
40
+
41
+ end # module URI
data/src/uri/pgsql.rb ADDED
@@ -0,0 +1,136 @@
1
+ # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License:: Gnu General Public License.
4
+ # Revision:: $Id: pgsql.rb 256 2005-06-01 00:11:05Z ertai $
5
+
6
+ require 'uri_ex'
7
+
8
+ module URI
9
+
10
+ class PgSQL < Generic
11
+
12
+ SCHEME = 'pgsql'.freeze
13
+ DEFAULT_HOST = 'localhost'.freeze
14
+ DEFAULT_PORT = 5432
15
+ DEFAULT_QUERY = ''.freeze
16
+ PG_DUMP = 'pg_dump'.to_cmd.freeze
17
+
18
+ COMPONENT = [
19
+ :scheme,
20
+ :userinfo,
21
+ :host,
22
+ :path,
23
+ :query
24
+ ].freeze
25
+
26
+
27
+ def self.build ( args )
28
+ tmp = Util::make_components_hash(self, args)
29
+ return super(tmp)
30
+ end
31
+
32
+
33
+ def mk_connection_opts
34
+ opts = []
35
+ opts << '-h' << @host
36
+ opts << '-p' << @port if @port != DEFAULT_PORT
37
+ opts << '-U' << @user if @user
38
+ opts
39
+ end
40
+
41
+
42
+ def mk_custom_opts
43
+ opts = []
44
+ return opts if @query.nil?
45
+ @query.split(/,/).map do |x|
46
+ k, v = x.split(/=/)
47
+ if k.size == 1
48
+ opts << "-#{k}"
49
+ else
50
+ opts << "--#{k}"
51
+ end
52
+ opts << v unless v.nil?
53
+ end
54
+ opts
55
+ end
56
+
57
+
58
+ def mk_pg_dump_opts ( out )
59
+ opts = %w[ -F c -Z 9 -C -f ]
60
+ opts << out
61
+ base, table = pathname.split
62
+ base, table = base.to_s, table.to_s
63
+ if base =~ /[\.\/]/
64
+ opts << table
65
+ else
66
+ if base =~ /\//
67
+ raise ArgumentError, "Use database/table not #{pathname}"
68
+ end
69
+ opts << '-t' << table << base
70
+ end
71
+ opts
72
+ end
73
+
74
+
75
+ def save
76
+ out = TempPath.new('dump', pathname.basename.to_s)
77
+ cmd = PG_DUMP + mk_connection_opts + mk_pg_dump_opts(out) + mk_custom_opts
78
+ cmd.run(self.runner)
79
+ out
80
+ end
81
+
82
+ end # class PgSQL
83
+
84
+ @@schemes[PgSQL::SCHEME.upcase] = PgSQL
85
+
86
+ test_section __FILE__ do
87
+ class PgSQLTest < Test::Unit::TestCase
88
+
89
+ def setup
90
+ assert_nothing_raised { @foo = URI.parse('pgsql://foo/qux/bar') }
91
+ assert_nothing_raised { @foo_at_bar = URI.parse('pgsql://foo@bar/baz/ben') }
92
+ assert_nothing_raised { @foo_at_bar_42 = URI.parse('pgsql://foo@bar:42/bing') }
93
+ assert_nothing_raised do
94
+ @cmplx = URI.parse('pgsql://foo@bar/qux/bob?a=b,c,d=e,f,ghi,jkl=mno')
95
+ end
96
+ end
97
+
98
+ def test_0_parse_basic
99
+ end
100
+
101
+ def test_with_query
102
+ ref = %w[-h bar -U foo -F c -Z 9 -C -f out bob -a b -c -d e -f --ghi --jkl mno]
103
+ assert_equal(ref, @cmplx.mk_connection_opts +
104
+ @cmplx.mk_pg_dump_opts('out') +
105
+ @cmplx.mk_custom_opts)
106
+ end
107
+
108
+ def test_mk_connection_opts
109
+ assert_equal(['-h', 'foo'], @foo.mk_connection_opts)
110
+ assert_equal(["-h", "bar", "-U", "foo"], @foo_at_bar.mk_connection_opts)
111
+ assert_equal(%w[-h bar -p]+[42, '-U', 'foo'], @foo_at_bar_42.mk_connection_opts)
112
+ assert_equal(["-h", "bar", "-U", "foo"], @cmplx.mk_connection_opts)
113
+ end
114
+
115
+ def test_mk_custom_opts
116
+ assert_equal([], @foo.mk_custom_opts)
117
+ assert_equal([], @foo_at_bar.mk_custom_opts)
118
+ assert_equal([], @foo_at_bar_42.mk_custom_opts)
119
+ assert_equal(%w[-a b -c -d e -f --ghi --jkl mno], @cmplx.mk_custom_opts)
120
+ end
121
+
122
+ def test_mk_pg_dump_opts
123
+ base = %w[-F c -Z 9 -C -f out]
124
+ assert_equal(base + ['bar'], @foo.mk_pg_dump_opts('out'))
125
+ assert_equal(base + ['ben'], @foo_at_bar.mk_pg_dump_opts('out'))
126
+ assert_equal(base + ['bing'], @foo_at_bar_42.mk_pg_dump_opts('out'))
127
+ assert_equal(base + ['bob'], @cmplx.mk_pg_dump_opts('out'))
128
+ end
129
+
130
+ def test_save
131
+ # FIXME
132
+ end
133
+ end # class PgSQLTest
134
+ end
135
+
136
+ end # module URI