woolen_common 0.0.1
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.
- checksums.yaml +15 -0
- data/README.md +31 -0
- data/ext/woolen_common/extconf.rb +26 -0
- data/ext/woolen_common/linux.h +3 -0
- data/ext/woolen_common/win.c +18 -0
- data/ext/woolen_common/win.h +4 -0
- data/ext/woolen_common/win/puts_color.c +139 -0
- data/ext/woolen_common/win/puts_color.h +5 -0
- data/ext/woolen_common/woolen_common.c +20 -0
- data/ext/woolen_common/woolen_common.h +7 -0
- data/lib/woolen_common.rb +39 -0
- data/lib/woolen_common/abstract_middleware/builder.rb +138 -0
- data/lib/woolen_common/abstract_middleware/map_cfg_manager.rb +52 -0
- data/lib/woolen_common/abstract_middleware/runner.rb +72 -0
- data/lib/woolen_common/action_pool_proxy.rb +28 -0
- data/lib/woolen_common/actionpool.rb +10 -0
- data/lib/woolen_common/actionpool/pool.rb +295 -0
- data/lib/woolen_common/actionpool/queue.rb +41 -0
- data/lib/woolen_common/actionpool/thread.rb +181 -0
- data/lib/woolen_common/addr_helper.rb +93 -0
- data/lib/woolen_common/cache.rb +305 -0
- data/lib/woolen_common/common_helper.rb +42 -0
- data/lib/woolen_common/config_manager.rb +36 -0
- data/lib/woolen_common/drb_helper.rb +125 -0
- data/lib/woolen_common/ffi/win32_kernel32.rb +86 -0
- data/lib/woolen_common/logger.rb +419 -0
- data/lib/woolen_common/pcap/mu/fixnum_ext.rb +8 -0
- data/lib/woolen_common/pcap/mu/pcap/ethernet.rb +164 -0
- data/lib/woolen_common/pcap/mu/pcap/header.rb +76 -0
- data/lib/woolen_common/pcap/mu/pcap/io_pair.rb +68 -0
- data/lib/woolen_common/pcap/mu/pcap/io_wrapper.rb +77 -0
- data/lib/woolen_common/pcap/mu/pcap/ip.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv4.rb +274 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv6.rb +149 -0
- data/lib/woolen_common/pcap/mu/pcap/packet.rb +106 -0
- data/lib/woolen_common/pcap/mu/pcap/pkthdr.rb +162 -0
- data/lib/woolen_common/pcap/mu/pcap/reader.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/reader/http_family.rb +175 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp.rb +369 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk.rb +124 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/data.rb +135 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init.rb +101 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init_ack.rb +69 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter.rb +111 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter/ip_address.rb +49 -0
- data/lib/woolen_common/pcap/mu/pcap/stream_packetizer.rb +74 -0
- data/lib/woolen_common/pcap/mu/pcap/tcp.rb +522 -0
- data/lib/woolen_common/pcap/mu/pcap/udp.rb +81 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap.rb +175 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/fields.rb +51 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/rtp.rb +72 -0
- data/lib/woolen_common/pcap/pcap.rb +115 -0
- data/lib/woolen_common/pcap/readme.md +72 -0
- data/lib/woolen_common/ruby_ext/blank.rb +126 -0
- data/lib/woolen_common/ruby_ext/drb_ext.rb +7 -0
- data/lib/woolen_common/ruby_ext/string.rb +116 -0
- data/lib/woolen_common/ruby_ext/win32_ole.rb +4 -0
- data/lib/woolen_common/ruby_proxy.rb +5 -0
- data/lib/woolen_common/ruby_proxy/client.rb +305 -0
- data/lib/woolen_common/ruby_proxy/config.rb +44 -0
- data/lib/woolen_common/ruby_proxy/exceptions.rb +17 -0
- data/lib/woolen_common/ruby_proxy/proxy.rb +157 -0
- data/lib/woolen_common/ruby_proxy/proxy_global_set.rb +44 -0
- data/lib/woolen_common/ruby_proxy/proxy_load.rb +34 -0
- data/lib/woolen_common/ruby_proxy/server.rb +53 -0
- data/lib/woolen_common/splib.rb +36 -0
- data/lib/woolen_common/splib/Array.rb +33 -0
- data/lib/woolen_common/splib/CodeReloader.rb +59 -0
- data/lib/woolen_common/splib/Constants.rb +44 -0
- data/lib/woolen_common/splib/Conversions.rb +47 -0
- data/lib/woolen_common/splib/Exec.rb +132 -0
- data/lib/woolen_common/splib/Float.rb +13 -0
- data/lib/woolen_common/splib/HumanIdealRandomIterator.rb +40 -0
- data/lib/woolen_common/splib/Monitor.rb +214 -0
- data/lib/woolen_common/splib/PriorityQueue.rb +110 -0
- data/lib/woolen_common/splib/Sleep.rb +10 -0
- data/lib/woolen_common/splib/UrlShorteners.rb +48 -0
- data/lib/woolen_common/ssh_proxy.rb +146 -0
- data/lib/woolen_common/system_helper.rb +123 -0
- data/lib/woolen_common/system_monitor.rb +23 -0
- data/lib/woolen_common/system_monitor/linux_monitor.rb +250 -0
- data/lib/woolen_common/system_monitor/windows_monitor.rb +145 -0
- data/lib/woolen_common/type_helper.rb +42 -0
- data/lib/woolen_common/ver_ctrl_middle_ware.rb +92 -0
- data/lib/woolen_common/version.rb +3 -0
- metadata +210 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module RubyProxy
|
3
|
+
class Config
|
4
|
+
@config = {
|
5
|
+
:ip => '127.0.0.1',
|
6
|
+
:port => 28889,
|
7
|
+
:command => 'ruby',
|
8
|
+
:autostart => false,
|
9
|
+
}
|
10
|
+
class <<self
|
11
|
+
def ip
|
12
|
+
@config[:ip]
|
13
|
+
end
|
14
|
+
|
15
|
+
def ip=(ip)
|
16
|
+
@config[:ip] = ip
|
17
|
+
end
|
18
|
+
|
19
|
+
def port=(port)
|
20
|
+
@config[:port] = port
|
21
|
+
end
|
22
|
+
|
23
|
+
def port
|
24
|
+
@config[:port]
|
25
|
+
end
|
26
|
+
|
27
|
+
def command
|
28
|
+
@config[:command]
|
29
|
+
end
|
30
|
+
|
31
|
+
def command=(command)
|
32
|
+
@config[:command] = command
|
33
|
+
end
|
34
|
+
|
35
|
+
def autostart
|
36
|
+
@config[:autostart]
|
37
|
+
end
|
38
|
+
|
39
|
+
def autostart=(bool)
|
40
|
+
@config[:autostart] = bool
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module RubyProxy
|
3
|
+
class Error < RuntimeError;
|
4
|
+
end
|
5
|
+
# can not connect to proxy server
|
6
|
+
class NotConError < Error;
|
7
|
+
end
|
8
|
+
#config command config error
|
9
|
+
class CommandNotFoundError < Error;
|
10
|
+
end
|
11
|
+
# start server fail error
|
12
|
+
class CannotStartServer < Error;
|
13
|
+
end
|
14
|
+
# format wrong with global variable
|
15
|
+
class GlobalVarFormatError < Error;
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'drb'
|
3
|
+
require "#{File.join(File.dirname(__FILE__), 'proxy_load')}"
|
4
|
+
require "#{File.join(File.dirname(__FILE__), 'proxy_global_set')}"
|
5
|
+
|
6
|
+
module RubyProxy
|
7
|
+
class Proxy
|
8
|
+
include WoolenCommon::ToolLogger
|
9
|
+
JOBS_QUEUE = Queue.new
|
10
|
+
JOBS_FINISH_ARRAY = []
|
11
|
+
JOBS_RETURN_ARRAY = []
|
12
|
+
MAX_HANDLE_THREAD = 1
|
13
|
+
$__proxy_job_id ||= 0
|
14
|
+
class << self
|
15
|
+
attr_accessor :worker_flag
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.proxy(klass_name, method=nil, *arg)
|
19
|
+
job = {}
|
20
|
+
job[:klass_name] = Marshal.load(Marshal.dump(klass_name))
|
21
|
+
job[:method] = Marshal.load(Marshal.dump(method))
|
22
|
+
job[:arg] = Marshal.load(Marshal.dump(arg))
|
23
|
+
job[:id] = $__proxy_job_id + 1
|
24
|
+
$__proxy_job_id += 1
|
25
|
+
JOBS_QUEUE.push job
|
26
|
+
job[:id]
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.set_work_flag(flag)
|
30
|
+
info "setting set_work_flag #{flag}"
|
31
|
+
self.worker_flag = flag
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.worker_start
|
35
|
+
self.worker_flag = true
|
36
|
+
MAX_HANDLE_THREAD.times do |cnt|
|
37
|
+
debug "starting worker :#{cnt}"
|
38
|
+
Thread.new do
|
39
|
+
loop do
|
40
|
+
the_job = nil
|
41
|
+
begin
|
42
|
+
if self.worker_flag
|
43
|
+
the_job = JOBS_QUEUE.pop
|
44
|
+
if the_job
|
45
|
+
handle_job the_job
|
46
|
+
end
|
47
|
+
else
|
48
|
+
sleep 0.5
|
49
|
+
end
|
50
|
+
rescue Exception => e
|
51
|
+
error "handle job error :#{e}", e
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.handle_job(job)
|
59
|
+
debug "doing job :#{job}"
|
60
|
+
klass_name = job[:klass_name]
|
61
|
+
method = job[:method]
|
62
|
+
arg = job[:arg]
|
63
|
+
begin
|
64
|
+
if method.nil?
|
65
|
+
ret = proxy_module(klass_name)
|
66
|
+
else
|
67
|
+
ret = proxy_module(klass_name).send(method, *arg)
|
68
|
+
end
|
69
|
+
debug "finish proxy action:#{method}"
|
70
|
+
JOBS_RETURN_ARRAY[job[:id]] = ret
|
71
|
+
# return ret
|
72
|
+
rescue Exception => e
|
73
|
+
error "proxy invoke error:#{e.message}", e
|
74
|
+
ensure
|
75
|
+
JOBS_FINISH_ARRAY[job[:id]] = true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.is_job_done?(job_id)
|
80
|
+
if JOBS_FINISH_ARRAY[job_id]
|
81
|
+
true
|
82
|
+
else
|
83
|
+
false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.get_the_job_ret(job_id)
|
88
|
+
the_ret = JOBS_RETURN_ARRAY[job_id]
|
89
|
+
JOBS_RETURN_ARRAY[job_id] = nil
|
90
|
+
the_ret
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.copy_env(env_hash)
|
94
|
+
env_hash.each do |key,value|
|
95
|
+
if key.blank? || value.blank?
|
96
|
+
info 'try to copy empty_env'
|
97
|
+
next
|
98
|
+
end
|
99
|
+
ENV["#{key}"] = "#{value}"
|
100
|
+
end
|
101
|
+
nil
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.proxy_load(file_or_gem)
|
105
|
+
ProxyLoad.load_file(file_or_gem)
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.proxy_global_set(arg, var)
|
109
|
+
ProxyGlobalSet.set(arg, var)
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.proxy_global_get(arg)
|
113
|
+
ProxyGlobalSet.get(arg)
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.add_load_path(path)
|
117
|
+
ProxyGlobalSet.add("$LOAD_PATH", path)
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.proxy_type(klass_name)
|
121
|
+
return proxy_const_get(klass_name).class.to_s
|
122
|
+
end
|
123
|
+
|
124
|
+
def self.proxy_module(klass_name)
|
125
|
+
trace "need to proxy module: [#{klass_name}]"
|
126
|
+
m = proxy_const_get(klass_name)
|
127
|
+
trace "finish get const class:#{m}"
|
128
|
+
m.class_eval {
|
129
|
+
include DRb::DRbUndumped
|
130
|
+
extend DRb::DRbUndumped
|
131
|
+
} if (m.class.to_s == 'Class' or m.class.to_s == 'Module') and !m.include?(DRb::DRbUndumped)
|
132
|
+
return m
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.proxy_const_get(klass_name)
|
136
|
+
atu = nil
|
137
|
+
klass_name_array = klass_name.split('::')
|
138
|
+
klass_name_array.shift if klass_name_array[0] == 'ATU'
|
139
|
+
trace "try to get class :#{klass_name},the class name array:#{klass_name_array}"
|
140
|
+
klass_name_array.each do |m|
|
141
|
+
if atu.nil?
|
142
|
+
atu = Kernel.const_get(m)
|
143
|
+
trace "get atu: [#{atu}]"
|
144
|
+
else
|
145
|
+
trace "get const [#{m}] in atu: [#{atu}]"
|
146
|
+
atu = atu.const_get(m)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
atu
|
150
|
+
end
|
151
|
+
|
152
|
+
def self.stop_proxy
|
153
|
+
DRb.stop_service
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "#{File.join(File.dirname(__FILE__), 'exceptions')}"
|
3
|
+
module RubyProxy
|
4
|
+
class ProxyGlobalSet
|
5
|
+
MAGIC_GLOBAL = ["$:", "$!", "$@", "$_", "$.", "$&", "$~", "$n", "$=", "$/", "$0", "$*", "$$", "$?"]
|
6
|
+
class <<self
|
7
|
+
def set(arg, value)
|
8
|
+
arg = convert_to_global(arg)
|
9
|
+
info "global set: #{arg} = #{value}"
|
10
|
+
eval("#{arg} = #{value}")
|
11
|
+
end
|
12
|
+
|
13
|
+
# add to $LOAD_PATH
|
14
|
+
def add(arg, value)
|
15
|
+
arg = convert_to_global(arg)
|
16
|
+
info "global add: #{arg} << #{value}"
|
17
|
+
eval("#{arg} << \"#{value}\"")
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(arg)
|
21
|
+
arg = convert_to_global(arg)
|
22
|
+
info "global get: #{arg}"
|
23
|
+
eval(arg)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def convert_to_global(arg)
|
28
|
+
check_format(arg)
|
29
|
+
add_dollar_if_noexist(arg)
|
30
|
+
end
|
31
|
+
|
32
|
+
def check_format(arg)
|
33
|
+
if !MAGIC_GLOBAL.include?(arg) and !arg.match(/^\$?[a-zA-Z_]{1}[a-zA-Z_0-9]*$/)
|
34
|
+
raise GlobalVarFormatError, "global variable format wrong: #{arg}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_dollar_if_noexist(arg)
|
39
|
+
arg = "$" + arg unless arg.match(/^\$/)
|
40
|
+
arg
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module RubyProxy
|
3
|
+
class ProxyLoad
|
4
|
+
include WoolenCommon::ToolLogger
|
5
|
+
|
6
|
+
@load_path = []
|
7
|
+
class <<self
|
8
|
+
attr_accessor :load_path
|
9
|
+
|
10
|
+
def load
|
11
|
+
load_path.uniq.each do |p|
|
12
|
+
if File.directory?(p)
|
13
|
+
Dir[p.chomp("/") + "/*.rb"].each do |file|
|
14
|
+
load_file(file)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
load_file(p)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_file(file)
|
23
|
+
begin
|
24
|
+
trace "require file : #{file}"
|
25
|
+
require file
|
26
|
+
trace "finish require file : #{file}"
|
27
|
+
rescue Exception => e
|
28
|
+
warn "require file : #{file} fail,exception:\n#{e}", e
|
29
|
+
raise e
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'drb'
|
4
|
+
require "#{File.join(File.dirname(__FILE__), 'config')}"
|
5
|
+
require "#{File.join(File.dirname(__FILE__), 'proxy')}"
|
6
|
+
require 'drb/timeridconv'
|
7
|
+
module RubyProxy
|
8
|
+
class DRbServer
|
9
|
+
include WoolenCommon::ToolLogger
|
10
|
+
@ip = Config.ip
|
11
|
+
@port = Config.port
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :ip, :port
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.start_service
|
18
|
+
DRb.install_id_conv(DRb::TimerIdConv.new)
|
19
|
+
DRb.start_service("druby://#{ip}:#{port}", Proxy)
|
20
|
+
#~ trap("INT") { DRb.stop_service }
|
21
|
+
# for ruby1.9
|
22
|
+
begin
|
23
|
+
Proxy.worker_start
|
24
|
+
DRb.thread.join
|
25
|
+
rescue Exception => e
|
26
|
+
error "服务线程异常:#{e.message}",e
|
27
|
+
ensure
|
28
|
+
DRb.stop_service
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.stop_service
|
33
|
+
DRb.stop_service
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
if $0 == __FILE__
|
39
|
+
#~ ['INT'].each do |signal|
|
40
|
+
#~ trap(signal) {
|
41
|
+
#~ puts 'exit...'
|
42
|
+
#~ RubyProxy::DRbServer.stop_service
|
43
|
+
#~ }
|
44
|
+
#~ end
|
45
|
+
#~ puts "start server ip:#{ARGV[0] || "127.0.0.1"}"
|
46
|
+
#~ puts "start server port:#{ARGV[1] || 9000}"
|
47
|
+
RubyProxy::DRbServer.ip = ARGV[0] unless ARGV[0].nil?
|
48
|
+
RubyProxy::DRbServer.port = ARGV[1] unless ARGV[1].nil?
|
49
|
+
puts "start server ip: #{RubyProxy::DRbServer.ip}"
|
50
|
+
puts "start server port:#{RubyProxy::DRbServer.port}"
|
51
|
+
#~ Dir.chdir ARGV[2] unless ARGV[2].nil?
|
52
|
+
RubyProxy::DRbServer.start_service
|
53
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Splib
|
2
|
+
LIBS = [:Array,
|
3
|
+
:CodeReloader,
|
4
|
+
:Constants,
|
5
|
+
:Conversions,
|
6
|
+
:Exec,
|
7
|
+
:Float,
|
8
|
+
:HumanIdealRandomIterator,
|
9
|
+
:Monitor,
|
10
|
+
:PriorityQueue,
|
11
|
+
:Sleep,
|
12
|
+
:UrlShorteners
|
13
|
+
]
|
14
|
+
# args:: name of library to load
|
15
|
+
# Loads the given library. Currently available:
|
16
|
+
# :CodeReloader
|
17
|
+
# :Constants
|
18
|
+
# :Conversions
|
19
|
+
# :Exec
|
20
|
+
# :HumanIdealRandomIterator
|
21
|
+
# :PriorityQueue
|
22
|
+
# :UrlShorteners
|
23
|
+
# :all
|
24
|
+
def self.load(*args)
|
25
|
+
if args.include?(:all)
|
26
|
+
LIBS.each do |lib|
|
27
|
+
require File.join(File.dirname(__FILE__), 'splib', lib.to_s)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
args.each do |lib|
|
31
|
+
raise NameError.new("Unknown library name: #{lib}") unless LIBS.include?(lib)
|
32
|
+
require File.join(File.dirname(__FILE__), 'splib', lib.to_s)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Splib
|
2
|
+
# Only add this if we are in the 1.8 world and no one
|
3
|
+
# else has added it yet
|
4
|
+
if(Object::RUBY_VERSION < '1.9.0' && ![].respond_to?(:fixed_flatten))
|
5
|
+
class ::Array
|
6
|
+
# 1.9 compatible flatten method that allows
|
7
|
+
# a level parameter
|
8
|
+
def fixed_flatten(level = -1)
|
9
|
+
arr = self
|
10
|
+
case
|
11
|
+
when level < 0
|
12
|
+
arr.flatten!
|
13
|
+
when level == 0
|
14
|
+
self
|
15
|
+
when level > 0
|
16
|
+
arr = []
|
17
|
+
curr = self
|
18
|
+
level.times do
|
19
|
+
curr.each do |elm|
|
20
|
+
if(elm.respond_to?(:to_ary))
|
21
|
+
elm.each{|e| arr << e }
|
22
|
+
else
|
23
|
+
arr << elm
|
24
|
+
end
|
25
|
+
end
|
26
|
+
curr = arr.dup
|
27
|
+
end
|
28
|
+
end
|
29
|
+
arr
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Splib
|
2
|
+
# path:: path to file
|
3
|
+
# Read contents of file
|
4
|
+
def self.read_file(path)
|
5
|
+
file = File.open(path, 'rb')
|
6
|
+
cont = file.read
|
7
|
+
file.close
|
8
|
+
cont
|
9
|
+
end
|
10
|
+
# path:: path to ruby file
|
11
|
+
# type:: return constants only of given type
|
12
|
+
# Find all constants in a given ruby file. Array of
|
13
|
+
# symbols is returned
|
14
|
+
def self.discover_constants(path, type=nil)
|
15
|
+
raise ArgumentError.new('Failed to locate plugin file') unless File.exists?(path)
|
16
|
+
consts = []
|
17
|
+
sandbox = Module.new
|
18
|
+
sandbox.module_eval(self.read_file(path))
|
19
|
+
sandbox.constants.each do |const|
|
20
|
+
klass = sandbox.const_get(const)
|
21
|
+
if(type.nil? || (type && klass < type))
|
22
|
+
sklass = klass.to_s.slice(klass.to_s.rindex(':')+1, klass.to_s.length)
|
23
|
+
consts << sklass.to_sym
|
24
|
+
end
|
25
|
+
end
|
26
|
+
return consts
|
27
|
+
end
|
28
|
+
|
29
|
+
# path:: path to ruby file
|
30
|
+
# holder:: module holding loaded ruby code
|
31
|
+
# Load code from a ruby file into a module
|
32
|
+
def self.load_code(path, holder=nil)
|
33
|
+
if(holder)
|
34
|
+
raise ArgumentError.new('Expecting a module containing loaded code') unless holder.respond_to?(:path)
|
35
|
+
else
|
36
|
+
holder = self.create_holder(path)
|
37
|
+
end
|
38
|
+
holder.module_eval(self.read_file(path))
|
39
|
+
holder
|
40
|
+
end
|
41
|
+
|
42
|
+
# holder:: module holding loaded ruby code
|
43
|
+
# Reload the code within the module
|
44
|
+
def self.reload_code(holder)
|
45
|
+
raise ArgumentError.new('Expecting a module containing loaded code') unless holder.respond_to?(:path)
|
46
|
+
self.load_code(holder.path)
|
47
|
+
end
|
48
|
+
|
49
|
+
# path:: path to ruby code
|
50
|
+
# Creates a module to hold loaded code
|
51
|
+
def self.create_holder(path)
|
52
|
+
return Module.new do
|
53
|
+
@path = path
|
54
|
+
def self.path
|
55
|
+
@path
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|