rubyment 0.6.25684806 → 0.7.25693512

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubyment.rb +412 -28
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c84f2638cb5c7384c5fbe3dda410b4a7a3fb68e
4
- data.tar.gz: a2afc9c7797578ecec3348c5bfe88a5e1144e531
3
+ metadata.gz: 7acb74408f53c3d0d2040adc01891858d1eb6b0c
4
+ data.tar.gz: a572838aecf8a54e3512dc9d63bc72b7f7773a0f
5
5
  SHA512:
6
- metadata.gz: 8acc5caa6980bfdc9230ae653eb0ac498198b2c43f19b2bbe110b5c30f597dddc177be477ab4aa629b9f94b9a8f7be2f58c79c51fb8af090983a858096c04bda
7
- data.tar.gz: 49f0c32fb97be2bb0bd9f7675a989a8b006c28999f8a137cf8607a0a925225a1b2768822a2978b48f5d028580c52f8a52142464c481cf2470fa4e47557693f58
6
+ metadata.gz: dfb898a6646adb78ae6dfb64fa39e0eccb5eca0cc69177199e4c21d24ecf0d8aca79bc472d4c23b273b8d025743ca89fb8ab73a8c4480349757a921058a8115f
7
+ data.tar.gz: 8add6eb6c03e67ea2695971196bb72e40a9604c605efdb149c141455f0bea5a2726e433a99951c0aa4886360f261df03511db495f65e573bf99d2f89334655ac
data/lib/rubyment.rb CHANGED
@@ -95,6 +95,112 @@ class Object
95
95
  end
96
96
 
97
97
 
98
+ =begin
99
+ # begin_documentation
100
+
101
+ this module provides functions to generate html code.
102
+
103
+ # end_documentation
104
+ =end
105
+ module RubymentHTMLModule
106
+
107
+
108
+ =begin
109
+ this is a template for functions in this module.
110
+ =end
111
+ def html_content__ args=[]
112
+ html =<<-ENDHEREDOC
113
+
114
+ ENDHEREDOC
115
+ payload = "#{html}"
116
+ end
117
+
118
+
119
+ =begin
120
+ generates an html code with javascript,
121
+ to display a shell/invocation line to call
122
+ a GET <contents of the invocation line> on
123
+ the current server. The output is output
124
+ on a <PRE> tag.
125
+ If a server such
126
+ #test__experiment__web_http_https_server
127
+ is running, this html may provide an interface
128
+ to rubyment itself. By calling, eg
129
+ #autoreload on the invocation line, the
130
+ Ruby code can be even reloaded dynamically
131
+ without requiring the server to restart.
132
+ =end
133
+ def html_content__basic_shell
134
+ html =<<-ENDHEREDOC
135
+ <!DOCTYPE html>
136
+ <html>
137
+ <body>
138
+
139
+ <form>
140
+ Invoke:<br>
141
+ <input id="invocation_line" size=100 type="text" name="username"><br>
142
+ </form>
143
+ <button type="button" onclick="loadDoc()">Ok</button>
144
+ <pre id="demo">
145
+ </pre>
146
+
147
+
148
+ <script>
149
+ function loadDoc() {
150
+ var xhttp = new XMLHttpRequest();
151
+ xhttp.onreadystatechange = function() {
152
+ if (this.readyState == 4 && this.status == 200) {
153
+ ih = document.getElementById("demo").innerHTML
154
+ document.getElementById("demo").innerHTML =
155
+ this.responseText;
156
+ }
157
+ };
158
+ xhttp.open("GET", document.getElementById("invocation_line").value, true);
159
+ xhttp.send();
160
+ }
161
+ </script>
162
+
163
+ </body>
164
+ </html>
165
+ ENDHEREDOC
166
+ payload = "#{html}"
167
+ end
168
+
169
+
170
+ end
171
+
172
+
173
+ =begin
174
+ # begin_documentation
175
+ This module offers functions to manipulate
176
+ Strings.
177
+
178
+ # end_documentation
179
+ =end
180
+ module RubymentStringsModule
181
+
182
+
183
+ =begin
184
+ returns a quoted version of a string; does not escape against
185
+ quoting chars inside it.
186
+ =end
187
+ def quoted_string__no_escaping s
188
+ "\"#{s}\""
189
+ end
190
+
191
+
192
+ =begin
193
+ returns a quoted version of a string; does not escape against
194
+ quoting chars inside it.
195
+ =end
196
+ def quoted_string__no_escaping_single s
197
+ "\'#{s}\'"
198
+ end
199
+
200
+
201
+ end
202
+
203
+
98
204
  =begin
99
205
  # begin_documentation
100
206
  This module offers function to interface with certain
@@ -146,6 +252,20 @@ module InternalRubymentModule
146
252
  end
147
253
 
148
254
 
255
+ =begin
256
+ load __FILE__ -- so a threaded invocation,
257
+ like the functions running a tcp server,
258
+ can call this function and reload without
259
+ having to restart the process.
260
+
261
+ =end
262
+ def autoreload wtime=1
263
+ rubyment_memory__set_key :file_reloading, true
264
+ load rubyment_memory__get_key :filepath
265
+ rubyment_memory__set_key :file_reloading, false
266
+ end
267
+
268
+
149
269
  end
150
270
 
151
271
 
@@ -1787,6 +1907,200 @@ trying to get the interface compatible with
1787
1907
  end
1788
1908
 
1789
1909
 
1910
+ =begin
1911
+ will call puts on the stderr
1912
+ =end
1913
+ def stderr_puts *args
1914
+ stderr = rubyment_memory__get_key :stderr
1915
+ stderr.puts *args
1916
+ end
1917
+
1918
+
1919
+ =begin
1920
+ will call puts on the stdout
1921
+ =end
1922
+ def stdout_puts *args
1923
+ stdout = rubyment_memory__get_key :stdout
1924
+ stdout.puts *args
1925
+ end
1926
+
1927
+
1928
+ =begin
1929
+ force arguments to be given to method
1930
+ as an array
1931
+
1932
+ =end
1933
+ def invoke_as_array *args
1934
+ method_to_invoke,
1935
+ *args_to_method = args
1936
+ send method_to_invoke, args_to_method
1937
+ end
1938
+
1939
+
1940
+ =begin
1941
+ force arguments to be given to method
1942
+ as a splat
1943
+ =end
1944
+ def invoke_as_splat args=[]
1945
+ method_to_invoke,
1946
+ *args_to_method = args
1947
+ send method_to_invoke, *args_to_method
1948
+ end
1949
+
1950
+
1951
+ =begin
1952
+ # short_desc = "creates an http response, after executing the GET "
1953
+
1954
+ @memory[:documentation].push = {
1955
+ :function => :experiment__http_response_invoke_clone,
1956
+ :short_desc => short_desc,
1957
+ :description => "",
1958
+ :params => [
1959
+ {
1960
+ :name => :args,
1961
+ :description => "list of parameters",
1962
+ :duck_type => Array,
1963
+ :default_behavior => "interpreted as empty array",
1964
+ :params => [
1965
+ {
1966
+ :name => :request,
1967
+ :duck_type => String,
1968
+ :default_behavior => :nil
1969
+ :description => "ignored, will redirect whatever request is made. nil is used.",
1970
+ },
1971
+ {
1972
+ :name => :location,
1973
+ :duck_type => String,
1974
+ :default_behavior => "",
1975
+ :description => "Location: field value (the url to redirect to)",
1976
+ },
1977
+ {
1978
+ :name => :code,
1979
+ :duck_type => String,
1980
+ :default_behavior => "200 OK",
1981
+ :description => "Request code",
1982
+ },
1983
+ {
1984
+ :name => :version,
1985
+ :duck_type => String,
1986
+ :default_behavior => "1.1",
1987
+ :description => "HTTP protocol version",
1988
+ },
1989
+ {
1990
+ :name => :debug,
1991
+ :duck_type => Object,
1992
+ :default_behavior => :nil,
1993
+ :description => "prints debug information to the __IO__ specified by __@memory[:stderr]__ (STDERR by default)",
1994
+ },
1995
+ {
1996
+ :name => :eol,
1997
+ :duck_type => Object,
1998
+ :default_behavior => "\r\n",
1999
+ :description => "separator to join each line of the response",
2000
+ },
2001
+ {
2002
+ :name => :debug_request_parse,
2003
+ :duck_type => :boolean,
2004
+ :default_behavior => :nil,
2005
+ :forwarded => [
2006
+ { :to => experiment__http_request_parse , :as => :debug }
2007
+ ],
2008
+ },
2009
+ {
2010
+ :name => :output_exceptions,
2011
+ :duck_type => :boolean,
2012
+ :default_behavior => :nil,
2013
+ :description => "exceptions are normally properly handled by inner functions, but setting this to true can be helpful to debug some cases",
2014
+ },
2015
+ {
2016
+ :name => :reserved,
2017
+ :duck_type => Object,
2018
+ :default_behavior => "interpreted as nil",
2019
+ :description => "for future use",
2020
+ },
2021
+ ],
2022
+ },
2023
+ ],
2024
+ }
2025
+
2026
+ =end
2027
+ def experiment__http_response_invoke_clone args = []
2028
+ request,
2029
+ location,
2030
+ code,
2031
+ version,
2032
+ debug,
2033
+ eol,
2034
+ debug_request_parse,
2035
+ output_exceptions,
2036
+ reserved = args
2037
+ stderr = @memory[:stderr]
2038
+ stdout = @memory[:stdout]
2039
+ debug = debug.nne
2040
+ debug && (stderr.puts "{#{__method__} starting")
2041
+ debug && (stderr.puts "caller=#{caller_label}")
2042
+ debug && (stderr.puts "args.each_with_index=#{args.each_with_index.entries.inspect}")
2043
+ request_parse = experiment__http_request_parse [
2044
+ request,
2045
+ debug_request_parse,
2046
+ :methods_to_call.to_nil,
2047
+ output_exceptions,
2048
+ ]
2049
+ debug && (stderr.puts "request_parse[2]=#{request_parse[2]}")
2050
+ require "shellwords"
2051
+ # request_parse[2][0] is the the request_uri
2052
+ # remove any starting slashes before sending to invoke:
2053
+ args_for_rubyment = Shellwords.split(request_parse[2][0].to_s.gsub /\/*/, "")
2054
+ debug && (stderr.puts "args_for_rubyment=#{args_for_rubyment}")
2055
+ request_stdout = StringIO.new
2056
+ request_stderr = StringIO.new
2057
+ # not thread-safe:
2058
+ @memory[:stdout] = request_stdout
2059
+ @memory[:stderr] = request_stderr
2060
+ block = bled [
2061
+ :no_answer.to_nil,
2062
+ :no_rescue.negate_me,
2063
+ :output.negate_me,
2064
+ ] {
2065
+ invoke args_for_rubyment
2066
+ }
2067
+ invoke_result = block.first.call
2068
+ @memory[:stdout] = stdout
2069
+ @memory[:stderr] = stderr
2070
+ html_request_stdout = request_stdout.string.split("\n")
2071
+ html_request_stderr = request_stderr.string.split("\n")
2072
+ require 'json'
2073
+ payload = CGI.escapeHTML JSON.pretty_generate({
2074
+ "invoke_result" => invoke_result,
2075
+ "request_stdout" => html_request_stdout,
2076
+ "request_stderr" => html_request_stderr,
2077
+ })
2078
+
2079
+ debug && (stderr.puts "args_for_rubyment.size=#{args_for_rubyment.size}")
2080
+ debug && (stderr.puts "args_for_rubyment.size.nne=#{args_for_rubyment.size.nne}")
2081
+ debug && (stderr.puts "args_for_rubyment.size.nne.negate_me=#{args_for_rubyment.size.nne.negate_me}")
2082
+ args_for_rubyment.size.nne.negate_me && (payload = html_content__basic_shell)
2083
+ debug && (stderr.puts "payload=#{payload}")
2084
+ location = location.nne ""
2085
+ version = version.nne "1.1"
2086
+ code = code.nne "200 OK"
2087
+ eol = eol.nne "\r\n"
2088
+ rv = http_response_base [
2089
+ payload,
2090
+ :content_type.to_nil,
2091
+ code,
2092
+ version,
2093
+ :keep_alive.to_nil,
2094
+ debug,
2095
+ eol,
2096
+ location,
2097
+ ]
2098
+ debug && (stderr.puts "will return #{rv.inspect}")
2099
+ debug && (stderr.puts "#{__method__} returning}")
2100
+ rv
2101
+ end
2102
+
2103
+
1790
2104
  end
1791
2105
 
1792
2106
 
@@ -2934,6 +3248,8 @@ end
2934
3248
  module RubymentModule
2935
3249
 
2936
3250
  Object.class_eval { include ModifierForClassObjectModule }
3251
+ include RubymentHTMLModule
3252
+ include RubymentStringsModule
2937
3253
  include InternalRubymentModule
2938
3254
  include RubymentExperimentModule
2939
3255
  include RubymentMaintainedModule
@@ -2993,7 +3309,7 @@ module RubymentModule
2993
3309
  :stdout => STDOUT,
2994
3310
  :stdin => STDIN,
2995
3311
  :time => Time.now,
2996
- :major_version => "0.6",
3312
+ :major_version => "0.7",
2997
3313
  :basic_version => (Time.now.to_i / 60), # new one every minute
2998
3314
  :filepath => __FILE__,
2999
3315
  :running_dir => Dir.pwd,
@@ -3022,10 +3338,21 @@ module RubymentModule
3022
3338
  memory = @memory
3023
3339
  major_version = memory[:major_version]
3024
3340
  basic_version = memory[:basic_version]
3025
- major, minor = args
3341
+ major, minor, debug = args
3342
+ stderr = @memory[:stderr]
3343
+ debug = debug.nne
3344
+ debug && (stderr.puts "{#{__method__} starting")
3345
+ debug && (stderr.puts "caller=#{caller_label}")
3346
+ debug && (stderr.puts "args=#{args.inspect}")
3347
+ debug && (stderr.puts "args.each_with_index=#{args.each_with_index.entries.inspect}")
3348
+ debug && (stderr.puts "major_version=#{major_version}")
3349
+ debug && (stderr.puts "basic_version=#{basic_version}")
3026
3350
  major ||= major_version
3027
3351
  minor ||= basic_version
3028
- "#{major}.#{minor}"
3352
+ rv = "#{major}.#{minor}"
3353
+ debug && (stderr.puts "#{__method__} will return #{rv.inspect}")
3354
+ debug && (stderr.puts "#{__method__} returning}")
3355
+ rv
3029
3356
  end
3030
3357
 
3031
3358
 
@@ -3346,11 +3673,16 @@ module RubymentModule
3346
3673
  #
3347
3674
  def object_method_args_call args=ARGV
3348
3675
  stderr = @memory[:stderr]
3676
+ debug = @memory[:debug]
3677
+ debug && (stderr.puts "{#{__method__} starting")
3678
+ debug && (stderr.puts "caller=#{caller_label}")
3679
+ debug && (stderr.puts "args=#{args.inspect}")
3680
+ debug && (stderr.puts "args.each_with_index=#{args.each_with_index.entries.inspect}")
3349
3681
  method, object, *call_args = containerize args
3350
3682
  object ||= self
3351
3683
  method = to_object_method [method, object]
3352
3684
  call_args = call_args && (containerize call_args)
3353
- begin
3685
+ rv = begin
3354
3686
  call_args && (method.call *call_args) || method.call
3355
3687
  rescue NameError => nameError
3356
3688
  # every object (even nil) has :method,
@@ -3359,6 +3691,10 @@ module RubymentModule
3359
3691
  stderr.puts nameError
3360
3692
  nil
3361
3693
  end
3694
+ debug && (stderr.puts "#{__method__} will return #{rv.inspect}")
3695
+ # if raises exception before it will be unbalanced :
3696
+ debug && (stderr.puts "#{__method__} returning}")
3697
+ rv
3362
3698
  end
3363
3699
 
3364
3700
 
@@ -4909,29 +5245,36 @@ require '#{gem_name}'
4909
5245
  # +gem_spect+:: [String]
4910
5246
  # +user_install+:: [Object]
4911
5247
  # +quiet+:: [Object] if calling the object +nne+ method returns a +false+ value, will print debug information
5248
+ # +ignored+:: [Object] ignored parameter
5249
+ # +gem_version+:: [Object]
4912
5250
  # @return [String] console output of gem uninstall
4913
5251
  def gem_uninstall_all args=[]
4914
5252
  stderr = @memory[:stderr]
4915
- gem_spec, user_install, quiet, all = args
5253
+ gem_spec, user_install, quiet, all, gem_version = args
4916
5254
  all = "-a"
4917
- gem_uninstall [gem_spec, user_install, quiet, all]
5255
+ gem_uninstall [gem_spec, user_install, quiet, all, gem_version]
4918
5256
  end
4919
5257
 
4920
5258
 
4921
5259
  # gem_uninstall
4922
5260
  # args:
4923
- # [gem_spec (String)]
4924
- # returns:
4925
- # console output of gem uninstall (String)
5261
+ # @param [Array] +args+, an +Array+ whose elements are expected to be:
5262
+ # +gem_spect+:: [String]
5263
+ # +user_install+:: [Object]
5264
+ # +quiet+:: [Object] if calling the object +nne+ method returns a +false+ value, will print debug information
5265
+ # +ignored+:: [Object] ignored parameter
5266
+ # +gem_version+:: [Object]
5267
+ # @return [String] console output of gem uninstall
4926
5268
  def gem_uninstall args=ARGV
4927
5269
  stderr = @memory[:stderr]
4928
5270
  system_user_is_super = @memory[:system_user_is_super]
4929
- gem_spec, user_install, quiet, all = args
5271
+ gem_spec, user_install, quiet, all, gem_version = args
4930
5272
  quiet = quiet.nne
4931
5273
  debug = quiet.negate_me
4932
5274
  all = all.nne ""
5275
+ gem_version = gem_version && ("--version #{gem_version}") || ""
4933
5276
  user_install ||= (!system_user_is_super) && "--user-install" || ""
4934
- command="gem uninstall #{all} -x #{user_install} #{gem_spec}"
5277
+ command="gem uninstall #{all} -x #{user_install} #{gem_spec} #{gem_version}"
4935
5278
  debug && (stderr.puts "command=#{command}")
4936
5279
  `#{command}`
4937
5280
  end
@@ -4948,9 +5291,16 @@ require '#{gem_name}'
4948
5291
  reserved = args
4949
5292
  quiet = quiet.nne
4950
5293
  debug = quiet.negate_me
4951
- command="gem list | grep #{gem_spec}"
4952
- debug && (stderr.puts "command=#{command}")
4953
- `#{command}`
5294
+ effective_command="gem list | grep #{gem_spec}"
5295
+ command="gem list"
5296
+ debug && (stderr.puts "command=#{effective_command}")
5297
+ command_output = shell_popen3_command([command])[0]
5298
+ grep_command_output = command_output.select { |l|
5299
+ l.match /#{gem_spec}/
5300
+ }
5301
+ spec_grep_output = grep_command_output.join("\n")
5302
+ puts spec_grep_output
5303
+ spec_grep_output
4954
5304
  end
4955
5305
 
4956
5306
  # validate_require
@@ -4958,22 +5308,40 @@ require '#{gem_name}'
4958
5308
  # returns nil if not found
4959
5309
  # args:
4960
5310
  # [requirement (String), validator_class (Class or String or nil),
4961
- # validator_args (Array), validator_method (Method or String)]
5311
+ # validator_args (Array), validator_method (Method or String),
5312
+ # file_to_load_instead (String)]
4962
5313
  # returns:
4963
5314
  # Rubyment, true or false
4964
5315
  def validate_require args=ARGV
4965
5316
  stderr = @memory[:stderr]
4966
- requirement, validator_class, validator_args, validator_method = containerize args
5317
+ debug = @memory[:debug]
5318
+ debug && (stderr.puts "{#{__method__} starting")
5319
+ debug && (stderr.puts "caller=#{caller_label}")
5320
+ debug && (stderr.puts "args=#{args.inspect}")
5321
+ debug && (stderr.puts "args.each_with_index=#{args.each_with_index.entries.inspect}")
5322
+ requirement,
5323
+ validator_class,
5324
+ validator_args,
5325
+ validator_method,
5326
+ file_to_load_instead,
5327
+ reserved = containerize args
4967
5328
  validate_call = validator_class && true
4968
5329
  validator_class = to_class validator_class
4969
5330
  validator_method ||= "new"
4970
- begin
4971
- require requirement
5331
+ rv = begin
5332
+ debug && (file_to_load_instead.nne.negate_me) && (stderr.puts "will require #{requirement}")
5333
+ (file_to_load_instead.nne.negate_me) && (require requirement)
5334
+ debug && (file_to_load_instead) && (stderr.puts "will load #{file_to_load_instead}")
5335
+ (file_to_load_instead) && (load file_to_load_instead)
4972
5336
  validate_call && (object_method_args_call [validator_method, validator_class, validator_args]) || (!validate_call) && true
4973
5337
  rescue LoadError => e
4974
5338
  stderr.puts e
4975
5339
  nil
4976
5340
  end
5341
+
5342
+ debug && (stderr.puts "#{__method__} will return #{rv.inspect}")
5343
+ debug && (stderr.puts "#{__method__} returning}")
5344
+ rv
4977
5345
  end
4978
5346
 
4979
5347
  # system_rubyment
@@ -5070,6 +5438,7 @@ require '#{gem_name}'
5070
5438
  gem_validate_class,
5071
5439
  gem_validate_class_args,
5072
5440
  gem_validate_class_method,
5441
+ gem_is_current_file,
5073
5442
  ]
5074
5443
  end
5075
5444
 
@@ -5097,6 +5466,14 @@ require '#{gem_name}'
5097
5466
  # true or false
5098
5467
  def gem_validate args=ARGV
5099
5468
  memory = @memory
5469
+ debug = @memory[:debug]
5470
+ stderr = @memory[:stderr]
5471
+
5472
+ debug = debug.nne
5473
+ debug && (stderr.puts "{#{__method__} starting")
5474
+ debug && (stderr.puts "caller=#{caller_label}")
5475
+ debug && (stderr.puts "args=#{args.inspect}")
5476
+ debug && (stderr.puts "args.each_with_index=#{args.each_with_index.entries.inspect}")
5100
5477
  gem_defaults = rubyment_gem_defaults args
5101
5478
  gem_name, gem_version = gem_defaults
5102
5479
  gem_files, gem_is_current_file = gem_files_args gem_defaults
@@ -5110,21 +5487,28 @@ require '#{gem_name}'
5110
5487
  gem_bin_generate,
5111
5488
  gem_bin_contents,
5112
5489
  ]
5113
- already_installed = (
5114
- validate_require gem_validate_args gem_defaults
5115
- )
5116
- sleep 1
5117
- already_installed && (gem_uninstall_all [gem_name])
5118
- puts gem_list [gem_name]
5119
5490
  p (gem_path [gem_name, gem_version])
5120
5491
  gem_install [(gem_path [gem_name, gem_version])]
5121
- puts gem_list [gem_name]
5122
- v = (
5492
+ listing = gem_list [gem_name]
5493
+ v = listing.index version([])
5494
+ debug && v && (stderr.puts "gem installed(#{v})")
5495
+ debug && v.negate_me && (stderr.puts "gem not installed(#{v})")
5496
+ v &&= (
5123
5497
  validate_require gem_validate_args gem_defaults
5124
5498
  )
5125
- gem_uninstall_all [gem_name]
5126
- already_installed && (gem_install [gem_name])
5499
+ gem_uninstall [
5500
+ gem_name,
5501
+ :user_install.to_nil,
5502
+ :quiet.to_nil,
5503
+ :all.to_nil,
5504
+ gem_version,
5505
+ ]
5127
5506
  v
5507
+ rv = v
5508
+ debug && (stderr.puts "#{__method__} will return #{rv.inspect}")
5509
+ # if raises exception before it will be unbalanced :
5510
+ debug && (stderr.puts "#{__method__} returning}")
5511
+ rv
5128
5512
  end
5129
5513
 
5130
5514
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.25684806
4
+ version: 0.7.25693512
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribamar Santarosa