rcscript 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rcscript.rb +75 -0
  2. data/lib/rcscript_base.rb +41 -0
  3. metadata +56 -0
data/lib/rcscript.rb ADDED
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/ruby
2
+ #file: rcscript.rb
3
+
4
+ # created: 1-Jul-2009
5
+ # updated: 22-Oct-2009
6
+
7
+ # description
8
+ # - This script executes Ruby script contained within an XML file.
9
+ # - The XML file can be stored locally or on a website.
10
+ # - The 'require' statement for foreign scripts has been replaced
11
+ # with the script tag (e.g. <script src="rexml_helper.rb"/>)
12
+
13
+ # arguments:
14
+ # - *job, the filename, and the *script arguments (* (asterisk) denotes
15
+ # these arguments are optional.
16
+
17
+ # usage:
18
+ # - rcscript //job:xxxx script_file.rsf [script arguments]
19
+
20
+ # MIT license - basically you can do anything you like with the script.
21
+ # http://www.opensource.org/licenses/mit-license.php
22
+
23
+
24
+ require 'rcscript_base'
25
+
26
+ class RScript < RScriptBase
27
+
28
+ def initialize()
29
+ end
30
+
31
+ def run(args=[])
32
+ threads = []
33
+ if args.to_s[/\/\/job:/] then
34
+
35
+ ajob = []
36
+
37
+ args.each_index do |i|
38
+ if args[i][/\/\/job:/] then
39
+ ajob << "@id='#{$'}'"; args[i] = nil
40
+ end
41
+ end
42
+
43
+ args.compact!
44
+
45
+ out = run_rsf(args) do |doc|
46
+ doc.root.elements.to_a("//job[#{ajob.join(' or ')}]").map do |job|
47
+ job.elements.to_a('script').map {|s| run_script(s)}.join("\n")
48
+ end.join("\n")
49
+ end
50
+
51
+ else
52
+ out = run_rsf(args) {|doc| doc.root.elements.to_a('//script').map {|s| run_script(s)}}
53
+ end
54
+
55
+ [out, args]
56
+ end
57
+
58
+ private
59
+
60
+ def run_rsf(args=[])
61
+ rsfile = args[0]; args.shift
62
+
63
+ $rsfile = rsfile[/[a-zA-z0-9]+(?=\.rsf)/]
64
+ yield(Document.new(read_sourcecode(rsfile)))
65
+ end
66
+
67
+
68
+ end
69
+
70
+ if __FILE__ == $0 then
71
+ raw_args = ARGV
72
+ rs = RScript.new()
73
+ code, args = rs.run(raw_args)
74
+ puts eval(code).join("\n")
75
+ end
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/ruby
2
+ #file: rcscript_base.rb
3
+
4
+ require 'cgi'
5
+ require 'open-uri'
6
+ require 'rexml/document'
7
+ include REXML
8
+
9
+ class RScriptBase
10
+
11
+ def initialize()
12
+ end
13
+
14
+ def run(doc)
15
+ doc.root.elements.to_a('//script').map {|s| run_script(s)}.join(';')
16
+ end
17
+
18
+ protected
19
+
20
+ def run_script(script)
21
+ out_buffer = ''
22
+ src = script.attribute('src')
23
+ if src then
24
+ out_buffer = read_sourcecode(script.attribute('src').value)
25
+ else
26
+ code = script.text.to_s.strip.length > 0 ? script.text : script.cdatas.join.strip
27
+ out_buffer = code
28
+ end
29
+ out_buffer
30
+ end
31
+
32
+ def read_sourcecode(rsf)
33
+ if rsf[/http:\/\//] then
34
+ return open(rsf, "UserAgent" => "Ruby-SourceCodeReader").read
35
+ else
36
+ return File.open(rsf,'r').read
37
+ end
38
+ end
39
+
40
+ end
41
+
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rcscript
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-17 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/rcscript.rb
26
+ - lib/rcscript_base.rb
27
+ has_rdoc: true
28
+ homepage:
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.3.5
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: rcscript
55
+ test_files: []
56
+