ruby_ex 0.1.2 → 0.2.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.
Files changed (122) hide show
  1. data/ChangeLog +693 -0
  2. data/NEWS +74 -0
  3. data/SPEC.dyn.yml +6 -6
  4. data/SPEC.gemspec +14 -0
  5. data/SPEC.yml +4 -4
  6. data/lib/abstract.rb +2 -4
  7. data/lib/abstract_node.rb +1 -2
  8. data/lib/algorithms/simulated_annealing.rb +50 -29
  9. data/lib/attributed_class.rb +50 -21
  10. data/lib/auto_object.rb +102 -0
  11. data/lib/blank_slate.rb +102 -0
  12. data/lib/cache.rb +1 -2
  13. data/lib/choose.rb +165 -163
  14. data/lib/commands.rb +2 -3
  15. data/lib/commands/command.rb +47 -20
  16. data/lib/commands/datas.rb +1 -1
  17. data/lib/commands/datas/composite.rb +5 -1
  18. data/lib/commands/datas/data.rb +102 -5
  19. data/lib/commands/datas/factory.rb +13 -6
  20. data/lib/commands/datas/temp.rb +3 -5
  21. data/lib/commands/factory.rb +1 -1
  22. data/lib/commands/helpers.rb +1 -1
  23. data/lib/commands/pipe.rb +10 -1
  24. data/lib/commands/runners.rb +1 -1
  25. data/lib/commands/runners/exec.rb +1 -1
  26. data/lib/commands/runners/fork.rb +3 -16
  27. data/lib/commands/runners/mock.rb +67 -0
  28. data/lib/commands/runners/runner.rb +5 -3
  29. data/lib/commands/runners/system.rb +1 -1
  30. data/lib/commands/seq.rb +2 -1
  31. data/lib/config_file.rb +10 -2
  32. data/lib/const_regexp.rb +1 -2
  33. data/lib/{dlogger.rb → d_logger.rb} +1 -2
  34. data/lib/daemon.rb +1 -2
  35. data/lib/diff.rb +1 -2
  36. data/lib/drb/drb_observable.rb +1 -2
  37. data/lib/drb/drb_observable_pool.rb +2 -2
  38. data/lib/drb/drb_service.rb +1 -2
  39. data/lib/drb/drb_undumped_attributes.rb +1 -2
  40. data/lib/drb/drb_undumped_indexed_object.rb +1 -2
  41. data/lib/drb/insecure_protected_methods.rb +1 -2
  42. data/lib/drb_ex.rb +2 -2
  43. data/lib/file_type.rb +466 -0
  44. data/lib/generate_id.rb +12 -6
  45. data/lib/genpasswd.rb +22 -0
  46. data/lib/hash_eval.rb +83 -0
  47. data/lib/histogram.rb +1 -2
  48. data/lib/hookable.rb +3 -4
  49. data/lib/hooker.rb +1 -3
  50. data/lib/html_encode.rb +191 -0
  51. data/lib/indexed_node.rb +0 -1
  52. data/lib/io_marshal.rb +4 -2
  53. data/lib/ioo.rb +3 -2
  54. data/lib/kill_all.rb +46 -0
  55. data/lib/labeled_node.rb +0 -1
  56. data/lib/logger_observer.rb +8 -4
  57. data/lib/md5sum.rb +3 -3
  58. data/lib/meta_factory.rb +99 -0
  59. data/lib/method_call.rb +87 -0
  60. data/lib/mocks.rb +12 -0
  61. data/lib/mocks/assertions.rb +50 -0
  62. data/lib/mocks/method_logger.rb +40 -0
  63. data/lib/mocks/mock.rb +64 -0
  64. data/lib/mocks/object.rb +47 -0
  65. data/lib/mocks/observer.rb +38 -0
  66. data/lib/module/autoload_tree.rb +30 -29
  67. data/lib/module/hierarchy.rb +176 -171
  68. data/lib/module/instance_method_visibility.rb +39 -38
  69. data/lib/node.rb +0 -1
  70. data/lib/object_monitor.rb +1 -2
  71. data/lib/object_monitor_activity.rb +1 -2
  72. data/lib/observable.rb +1 -2
  73. data/lib/observable_pool.rb +1 -2
  74. data/lib/{orderedhash.rb → ordered_hash.rb} +41 -5
  75. data/lib/pp_hierarchy.rb +7 -2
  76. data/lib/r_path.rb +307 -0
  77. data/lib/random_generators.rb +7 -20
  78. data/lib/random_generators/random_generator.rb +2 -4
  79. data/lib/random_generators/ruby.rb +4 -2
  80. data/lib/regex_path.rb +124 -0
  81. data/lib/ruby_ex.rb +28 -98
  82. data/lib/safe_eval.rb +1 -2
  83. data/lib/sendmail.rb +14 -17
  84. data/lib/service_manager.rb +1 -2
  85. data/lib/shuffle.rb +6 -2
  86. data/lib/spring.rb +1 -2
  87. data/lib/spring_set.rb +1 -2
  88. data/lib/{symtbl.rb → sym_tbl.rb} +90 -5
  89. data/lib/sym_tbl_gsub.rb +227 -0
  90. data/lib/{synflow.rb → syn_flow.rb} +1 -2
  91. data/lib/text.rb +218 -0
  92. data/lib/timeout_ex.rb +1 -2
  93. data/lib/trace.rb +9 -8
  94. data/lib/uri/druby.rb +1 -2
  95. data/lib/uri/file.rb +1 -1
  96. data/lib/uri/ftp_ex.rb +1 -1
  97. data/lib/uri/http_ex.rb +1 -1
  98. data/lib/uri/mysql.rb +121 -0
  99. data/lib/uri/pgsql.rb +19 -38
  100. data/lib/uri/svn.rb +1 -2
  101. data/lib/uri_ex.rb +45 -3
  102. data/lib/verbose_object.rb +181 -38
  103. data/lib/yaml/chop_header.rb +19 -11
  104. data/lib/yaml/transform.rb +17 -11
  105. data/lib/yaml/yregexpath.rb +11 -5
  106. data/test/algorithms/simulated_annealing_test.rb +2 -2
  107. data/test/resources/foo.tar.gz +0 -0
  108. data/test/resources/tar.gz.log +49 -0
  109. data/test/sanity-suite.yml +5 -7
  110. data/test/sanity/multiple-requires.yml +17 -7
  111. data/test/sanity/single-requires.yml +38 -20
  112. data/test/stress-tests/threads_and_exceptions.yml +13 -0
  113. data/test/test-unit-setup.rb +3 -1
  114. data/test/unit-suite.yml +7 -8
  115. metadata +42 -31
  116. data/lib/algorithms.rb +0 -12
  117. data/lib/ask.rb +0 -100
  118. data/lib/checkout.rb +0 -12
  119. data/lib/dumpable_proc.rb +0 -57
  120. data/lib/filetype.rb +0 -229
  121. data/lib/thread_mutex.rb +0 -11
  122. data/lib/yaml/basenode_ext.rb +0 -63
data/lib/commands.rb CHANGED
@@ -1,10 +1,9 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: commands.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: commands.rb 343 2005-09-08 01:32:57Z ertai $
5
5
 
6
- require 'ruby_ex'
7
- require 'module/autoload_tree'
6
+ AutoloadTree.import!
8
7
  require 'abstract'
9
8
  require 'hookable'
10
9
  require 'hooker'
@@ -1,7 +1,7 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: command.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: command.rb 341 2005-09-07 00:01:43Z ertai $
5
5
 
6
6
  require 'commands'
7
7
 
@@ -15,6 +15,7 @@ module Commands
15
15
  attr_accessor :output
16
16
  attr_accessor :error
17
17
  attr_accessor :args
18
+ attr_accessor :open_mode
18
19
 
19
20
  #
20
21
  # Construction methods.
@@ -23,6 +24,8 @@ module Commands
23
24
  def initialize ( command_name, *args )
24
25
  @command = command_name
25
26
  @args = args.dup
27
+ @open_mode = :w
28
+ @input, @output, @error = nil, nil, nil
26
29
  end
27
30
 
28
31
  #
@@ -66,18 +69,21 @@ module Commands
66
69
 
67
70
 
68
71
  # Use Kernel#system() but with a string to have the shell expansion.
72
+ # FIXME make me a runner
69
73
  def sh
70
74
  Kernel.system(to_sh)
71
75
  end
72
76
 
73
77
 
74
78
  # Use Kernel#exec() but with a string to have the shell expansion.
79
+ # FIXME make me a runner
75
80
  def sh!
76
81
  Kernel.exec(to_sh)
77
82
  end
78
83
 
79
84
 
80
85
  # Use Kernel#` and return the resulting string.
86
+ # FIXME make me a runner
81
87
  def expand
82
88
  `#{to_sh}`
83
89
  end
@@ -93,12 +99,16 @@ module Commands
93
99
 
94
100
 
95
101
  # FIXME design me!
102
+ # FIXME make me a runner
96
103
  def ruby
97
104
  end
98
105
 
99
106
 
100
107
  # Use a Commands::Runners::Runner and return a Commands::Datas::Data.
101
108
  def run ( runner=@runner )
109
+ unless runner.respond_to? :run
110
+ raise ArgumentError, "need a runner not: #{runner.inspect}"
111
+ end
102
112
  runner.run(self)
103
113
  end
104
114
 
@@ -142,14 +152,10 @@ module Commands
142
152
  end
143
153
 
144
154
 
145
- def add_outputs ( out, err, mode )
155
+ def add_outputs ( out, err )
146
156
  cmd = dup
147
- cmd.output = out.dup
148
- cmd.output.open_mode = mode if cmd.output.is_a? Pathname
149
- if err
150
- cmd.error = err.dup
151
- cmd.error.open_mode = mode if cmd.error.is_a? Pathname
152
- end
157
+ cmd.output = out.dup if out
158
+ cmd.error = err.dup if err
153
159
  cmd
154
160
  end
155
161
  protected :add_outputs
@@ -158,14 +164,15 @@ module Commands
158
164
  # Return a new command with the given command output.
159
165
  def > ( arg )
160
166
  out, err = (arg.is_a?(Array))? arg : [arg, nil]
161
- add_outputs(out, err, 'w')
167
+ add_outputs(out, err)
162
168
  end
163
169
 
164
170
 
165
171
  # Like > but open the output in append mode.
166
172
  def >> ( arg )
167
173
  out, err = (arg.is_a?(Array))? arg : [arg, nil]
168
- add_outputs(out, err, 'a')
174
+ @open_mode = :a
175
+ add_outputs(out, err)
169
176
  end
170
177
 
171
178
 
@@ -206,9 +213,30 @@ module Commands
206
213
 
207
214
 
208
215
  def to_sh
209
- "#{to_s}#{sh_args}"
216
+ cmd = instanciate_args
217
+ "#{cmd.to_s}#{cmd.sh_args}"
218
+ end
219
+
220
+ def instanciate_args!
221
+ if defined? @input and @input and @args.include? '%i'
222
+ @args.map! { |a| (a == '%i')? @input : a }
223
+ @input = nil
224
+ end
225
+ if defined? @output and @output and @args.include? '%o'
226
+ @args.map! { |a| (a == '%o')? @output : a }
227
+ @output = nil
228
+ end
229
+ if defined? @error and @error and @args.include? '%e'
230
+ @args.map! { |a| (a == '%e')? @error : a }
231
+ @error = nil
232
+ end
210
233
  end
211
234
 
235
+ def instanciate_args
236
+ copy = dup
237
+ copy.instanciate_args!
238
+ copy
239
+ end
212
240
 
213
241
  def sh_args
214
242
  args = ''
@@ -262,6 +290,7 @@ module Commands
262
290
 
263
291
  def test_system_dne
264
292
  @dne.system
293
+ assert_equal(127, $?.exitstatus)
265
294
  end
266
295
 
267
296
  def test_system
@@ -410,7 +439,7 @@ module Commands
410
439
  assert_equal('foo', @cat.input)
411
440
  cmd = @cat < 'bar'
412
441
  assert_equal('bar', cmd.input)
413
- assert_not_equal(cmd.object_id, @bar.object_id)
442
+ assert_not_equal(cmd.object_id, @cat.object_id)
414
443
  end
415
444
 
416
445
  def test_add_output # test_>
@@ -420,14 +449,13 @@ module Commands
420
449
  assert_equal('bar', cmd.output)
421
450
  cmd = @cat > @file
422
451
  assert_equal(@file, cmd.output)
423
- assert_equal('w', cmd.output.open_mode)
424
- assert_not_equal(cmd.object_id, @bar.object_id)
452
+ assert_equal(:w, cmd.open_mode)
453
+ assert_not_equal(cmd.object_id, @cat.object_id)
425
454
  cmd = @cat > [ 'outbar', 'errbar' ]
426
455
  assert_equal('outbar', cmd.output)
427
456
  assert_equal('errbar', cmd.error)
428
457
  cmd = @cat > [ @file, @file ]
429
- assert_equal('w', cmd.output.open_mode)
430
- assert_equal('w', cmd.error.open_mode)
458
+ assert_equal(:w, cmd.open_mode)
431
459
  end
432
460
 
433
461
  def test_append_output # test_>>
@@ -435,16 +463,15 @@ module Commands
435
463
  assert_equal('foo', @cat.output)
436
464
  cmd = @cat >> 'bar'
437
465
  assert_equal('bar', cmd.output)
438
- assert_not_equal(cmd.object_id, @bar.object_id)
466
+ assert_not_equal(cmd.object_id, @cat.object_id)
439
467
  cmd = @cat >> @file
440
468
  assert_equal(@file, cmd.output)
441
- assert_equal('a', cmd.output.open_mode)
469
+ assert_equal(:a, cmd.open_mode)
442
470
  cmd = @cat >> [ 'outbar', 'errbar' ]
443
471
  assert_equal('outbar', cmd.output)
444
472
  assert_equal('errbar', cmd.error)
445
473
  cmd = @cat >> [ @file, @file ]
446
- assert_equal('a', cmd.output.open_mode)
447
- assert_equal('a', cmd.error.open_mode)
474
+ assert_equal(:a, cmd.open_mode)
448
475
  end
449
476
 
450
477
  def test_args
@@ -1,7 +1,7 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: datas.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: datas.rb 255 2005-06-01 00:08:46Z ertai $
5
5
 
6
6
  require 'commands'
7
7
 
@@ -1,7 +1,7 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: composite.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: composite.rb 358 2005-09-16 09:54:04Z ertai $
5
5
 
6
6
  require 'commands'
7
7
 
@@ -12,6 +12,7 @@ module Commands
12
12
  class Composite < Data
13
13
 
14
14
  def initialize ( *cmd )
15
+ super()
15
16
  @commands = cmd.flatten
16
17
  end
17
18
 
@@ -23,6 +24,9 @@ module Commands
23
24
  each { |x| x.waitpid }
24
25
  end
25
26
 
27
+ def kill ( *a, &b )
28
+ each { |x| x.kill(*a, &b) }
29
+ end
26
30
 
27
31
  end # class Composite
28
32
 
@@ -1,7 +1,7 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: data.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: data.rb 360 2005-09-16 12:17:16Z ertai $
5
5
 
6
6
  require 'commands'
7
7
 
@@ -23,22 +23,38 @@ module Commands
23
23
  class Data
24
24
 
25
25
  attr_accessor :pid
26
- attr_accessor :input
27
- attr_accessor :output
28
- attr_accessor :error
26
+ attr_reader :input
27
+ attr_reader :output
28
+ attr_reader :error
29
29
  attr_accessor :status
30
+ attr_reader :open_mode
31
+
32
+ @@datas ||= Set.new
33
+
34
+ def initialize
35
+ @open_mode = :w
36
+ @status, @pid = nil, nil
37
+ @@datas << self
38
+ end
30
39
 
31
40
 
32
41
  def waitpid
33
42
  return @status unless @status.nil?
43
+ return if @pid.nil?
34
44
  Process.waitpid(@pid)
35
45
  @status = $?
46
+ @@datas.delete(self)
47
+ self
36
48
  end
37
49
 
38
50
 
39
51
  def | ( rhs ) # FIXME CHECK ME
40
52
  raise unless rhs.is_a? Command
41
- rhs < output
53
+ case output
54
+ when Pathname then rhs < output
55
+ when File then rhs < output.path.to_path
56
+ else raise "Unexpected class #{rhs.class}"
57
+ end
42
58
  end
43
59
 
44
60
 
@@ -57,6 +73,87 @@ module Commands
57
73
  err.print @error.read
58
74
  end
59
75
 
76
+ def input= ( anObject )
77
+ if anObject.is_a? Pathname
78
+ anObject = anObject.dup
79
+ anObject.open_mode = :r
80
+ end
81
+ @input = anObject
82
+ end
83
+
84
+ def output= ( anObject )
85
+ if anObject.is_a? Pathname
86
+ anObject = anObject.dup
87
+ anObject.open_mode = @open_mode
88
+ end
89
+ @output = anObject
90
+ end
91
+
92
+
93
+ def error= ( anObject )
94
+ if anObject.is_a? Pathname
95
+ anObject = anObject.dup
96
+ anObject.open_mode = @open_mode
97
+ end
98
+ @error = anObject
99
+ end
100
+
101
+
102
+ def open_mode= ( mode )
103
+ raise ArgumentError, "bad mode #{mode} need :w or :a" if mode == :r
104
+ @open_mode = mode
105
+ end
106
+
107
+
108
+ have YamlExtension, :'commands::data'
109
+
110
+ def to_yaml_string
111
+ out = output.read
112
+ err = error.read
113
+ out ||= ''
114
+ err ||= ''
115
+
116
+ msg = []
117
+ msg << "exit: #{status.exitstatus}" unless status.nil?
118
+ if out.empty?
119
+ msg << 'output: ""'
120
+ else
121
+ out.gsub!(/^/, ' ')
122
+ msg << "output: |\n #{out}"
123
+ end
124
+ if err.empty?
125
+ msg << 'error: ""'
126
+ else
127
+ err.gsub!(/^/, ' ')
128
+ msg << "error: |\n #{err}"
129
+ end
130
+ msg.join("\n")
131
+ end
132
+
133
+ def kill ( sig='KILL' )
134
+ Process.kill sig, @pid if @status.nil? and not @pid.nil?
135
+ waitpid
136
+ end
137
+
138
+ def clean
139
+ kill
140
+ [@input, @output, @error].each do |stream|
141
+ if stream.is_a? File
142
+ stream.close
143
+ stream = stream.path.to_path
144
+ end
145
+ unless stream.is_a? Pathname
146
+ raise ArgumentError, "need a pathname not #{stream}"
147
+ end
148
+ stream.unlink if stream.exist? and stream.temp?
149
+ end
150
+ end
151
+
152
+ def self.clean_all
153
+ @@datas.dup.each { |data| data.kill }
154
+ end
155
+
156
+ at_exit { clean_all }
60
157
 
61
158
  end # class Data
62
159
 
@@ -1,9 +1,9 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: factory.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: factory.rb 344 2005-09-08 01:44:18Z ertai $
5
5
 
6
- require 'commands'
6
+ Commands.import!
7
7
 
8
8
  module Commands
9
9
 
@@ -13,21 +13,28 @@ module Commands
13
13
 
14
14
  attr_accessor :command_data_class
15
15
 
16
- def initialize
17
- @command_data_class = Temp
16
+ def initialize ( values={}, *a, &b )
17
+ super(*a, &b)
18
+ values.each do |k, v|
19
+ send("#{k}=", v)
20
+ end
21
+ @command_data_class ||= Temp
18
22
  end
19
23
 
20
24
  def new_command_data ( *a, &b )
21
25
  data = @command_data_class.new(*a, &b)
22
26
  instance_variables.each do |var|
23
- next if var =~ /command_data_class/
27
+ next if var == '@command_data_class'
28
+ next if var =~ /^@__/
24
29
  val = instance_variable_get(var)
25
30
  data.send(var[1..-1] + '=', val) unless val.nil?
26
31
  end
27
32
  data
28
33
  end
29
34
 
30
- alias :create :new_command_data
35
+ def create ( *a, &b )
36
+ new_command_data(*a, &b)
37
+ end
31
38
 
32
39
  end # class Factory
33
40
 
@@ -1,9 +1,7 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: temp.rb 279 2005-06-05 17:46:53Z ertai $
5
-
6
- require 'commands'
4
+ # Revision:: $Id: temp.rb 343 2005-09-08 01:32:57Z ertai $
7
5
 
8
6
  module Commands
9
7
 
@@ -14,9 +12,9 @@ module Commands
14
12
  def initialize ( *a, &b )
15
13
  super
16
14
  @output = TempPath.new('cmd-output')
17
- @output.open_mode = 'w'
15
+ @output.open_mode = :w
18
16
  @error = TempPath.new('cmd-error')
19
- @error.open_mode = 'w'
17
+ @error.open_mode = :w
20
18
  end
21
19
 
22
20
  end # class Temp
@@ -1,7 +1,7 @@
1
1
  # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
2
2
  # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
3
3
  # License:: GNU General Public License (GPL).
4
- # Revision:: $Id: factory.rb 279 2005-06-05 17:46:53Z ertai $
4
+ # Revision:: $Id: factory.rb 255 2005-06-01 00:08:46Z ertai $
5
5
 
6
6
  require 'commands'
7
7