dtr 0.0.4 → 1.0.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 (93) hide show
  1. data/CHANGES +7 -0
  2. data/LICENSE.txt +203 -0
  3. data/README.rdoc +208 -0
  4. data/Rakefile +64 -205
  5. data/TODO +8 -2
  6. data/bin/dtr +27 -60
  7. data/dtr.gemspec +8 -11
  8. data/lib/dtr.rb +5 -94
  9. data/lib/dtr/agent.rb +38 -0
  10. data/lib/dtr/agent/brain.rb +57 -0
  11. data/lib/dtr/agent/herald.rb +60 -0
  12. data/lib/dtr/agent/runner.rb +87 -0
  13. data/lib/dtr/agent/sync_codebase.rb +44 -0
  14. data/lib/dtr/agent/sync_logger.rb +70 -0
  15. data/lib/dtr/agent/test_case.rb +53 -0
  16. data/lib/dtr/agent/test_unit.rb +40 -0
  17. data/lib/dtr/agent/worker.rb +89 -0
  18. data/lib/dtr/agent/working_env_ext.rb +47 -0
  19. data/lib/dtr/facade.rb +65 -0
  20. data/lib/dtr/master.rb +40 -0
  21. data/lib/dtr/monitor.rb +95 -0
  22. data/lib/dtr/raketasks.rb +155 -22
  23. data/lib/dtr/shared.rb +24 -0
  24. data/lib/dtr/shared/adapter.rb +115 -0
  25. data/lib/dtr/shared/configuration.rb +104 -0
  26. data/lib/dtr/shared/message_decorator.rb +28 -0
  27. data/lib/dtr/shared/ruby_ext.rb +129 -0
  28. data/lib/dtr/shared/service.rb +19 -0
  29. data/lib/dtr/shared/service/agent.rb +37 -0
  30. data/lib/dtr/shared/service/file.rb +28 -0
  31. data/lib/dtr/shared/service/rinda.rb +48 -0
  32. data/lib/dtr/shared/service/runner.rb +34 -0
  33. data/lib/dtr/shared/service/working_env.rb +28 -0
  34. data/lib/dtr/shared/sync_codebase.rb +18 -0
  35. data/lib/dtr/shared/sync_codebase/copiable_package.rb +40 -0
  36. data/lib/dtr/shared/sync_codebase/master_ext.rb +40 -0
  37. data/lib/dtr/shared/sync_codebase/package.rb +53 -0
  38. data/lib/dtr/shared/sync_codebase/sync_service.rb +36 -0
  39. data/lib/dtr/shared/sync_logger.rb +64 -0
  40. data/lib/dtr/shared/utils.rb +17 -0
  41. data/lib/dtr/shared/utils/cmd.rb +30 -0
  42. data/lib/dtr/shared/utils/env_store.rb +60 -0
  43. data/lib/dtr/shared/utils/logger.rb +87 -0
  44. data/lib/dtr/shared/working_env.rb +38 -0
  45. data/lib/dtr/test_unit.rb +9 -275
  46. data/lib/dtr/test_unit/drb_test_runner.rb +48 -0
  47. data/lib/dtr/test_unit/injection.rb +29 -0
  48. data/lib/dtr/test_unit/test_case_injection.rb +37 -0
  49. data/lib/dtr/test_unit/test_suite_injection.rb +24 -0
  50. data/lib/dtr/test_unit/testrunnermediator_injection.rb +72 -0
  51. data/lib/dtr/test_unit/thread_safe_test_result.rb +38 -0
  52. data/lib/dtr/test_unit/worker_club.rb +72 -0
  53. data/lib/dtr/test_unit_injection.rb +1 -2
  54. data/test/acceptance/agent_working_env_test.rb +86 -0
  55. data/test/acceptance/dtr_package_task_test.rb +36 -0
  56. data/test/acceptance/general_test.rb +331 -0
  57. data/test/acceptance/raketasks_test.rb +23 -0
  58. data/test/acceptance/sync_codebase_test.rb +66 -0
  59. data/test/acceptance/sync_logger_test.rb +32 -0
  60. data/test/agent_helper.rb +37 -0
  61. data/test/logger_stub.rb +34 -0
  62. data/test/test_helper.rb +71 -0
  63. data/test/unit/adapter_test.rb +149 -0
  64. data/test/unit/configuration_test.rb +44 -0
  65. data/test/unit/facade_test.rb +41 -0
  66. data/test/unit/logger_test.rb +72 -0
  67. data/test/unit/test_unit_test.rb +26 -0
  68. data/test/unit/working_env_test.rb +71 -0
  69. data/testdata/Rakefile +11 -0
  70. data/testdata/a_failed_test_case.rb +8 -0
  71. data/testdata/a_file_system_test_case.rb +8 -0
  72. data/testdata/a_test_case.rb +13 -0
  73. data/testdata/a_test_case2.rb +6 -0
  74. data/testdata/an_error_test_case.rb +9 -0
  75. data/testdata/another_project/Rakefile +6 -0
  76. data/testdata/another_project/passed_test_case.rb +7 -0
  77. data/testdata/hacked_run_method_test_case.rb +15 -0
  78. data/testdata/is_required_by_a_test.rb +9 -0
  79. data/testdata/lib/lib_test_case.rb +7 -0
  80. data/testdata/package_task_test_rakefile +8 -0
  81. data/testdata/raketasks/Rakefile +7 -0
  82. data/testdata/raketasks/success_test_case.rb +6 -0
  83. data/testdata/scenario_test_case.rb +34 -0
  84. data/testdata/setup_agent_env_test_case.rb +9 -0
  85. data/testdata/sleep_3_secs_test_case.rb +9 -0
  86. data/testdata/verify_dir_pwd/Rakefile +6 -0
  87. data/testdata/verify_dir_pwd/verify_dir_pwd_test_case.rb +10 -0
  88. metadata +101 -34
  89. data/README +0 -291
  90. data/install.rb +0 -88
  91. data/lib/dtr/base.rb +0 -172
  92. data/lib/dtr/runner.rb +0 -270
  93. data/lib/dtr/service_provider.rb +0 -160
data/README DELETED
@@ -1,291 +0,0 @@
1
- = DTR -- Distributed Test Runner
2
-
3
- Supporting DTR version: 0.0.x
4
-
5
- This package contains DTR, a distributed test runner program for decreasing
6
- time of running ruby tests, only supporting Test::Unit ruby testing framework
7
- right now.
8
-
9
- DTR has the following features:
10
-
11
- * Run tests in mutli processes or on distributed machines.
12
-
13
- * Hot plug distributed runner agents.
14
-
15
- * Runtime injection, all tests run in same environment.
16
-
17
- DTR works in three parts: DTR Server, Runner Agent and DTR Master.
18
-
19
- * DTR Server is a Rinda Server which allows DRb services and clients to automatically find each other without knowing where they live.
20
-
21
- * Runner Agent is a DRb service hosting on distributed machines to run tests. For using 'fork' to create runner process, Runner Agent can't run on Windows directly.
22
-
23
- * DTR Master is the process finding runner service to run tests and collect test results. It works by loading 'dtr/test_unit_injection.rb' with all test files.
24
-
25
- DTR doesn't synchronize codebase, so you need to consider using synchronize tools
26
- (e.g. rsync(http://samba.anu.edu.au/rsync/)) or distributed version control system
27
- (e.g. mercurial(http://www.selenic.com/mercurial/wiki/)) to synchronize project
28
- files between machines. The distributed version control system Mercurial is cool for
29
- synchronizing codebase between machines, see the example "Setup a development build
30
- grid".
31
-
32
- == Download
33
-
34
- The latest version of DTR can be found at
35
-
36
- * http://github.com/xli/dtr/tree/master
37
-
38
- == Installation
39
-
40
- === Normal Installation
41
-
42
- You can install DTR with the following command.
43
-
44
- % ruby install.rb
45
-
46
- from its distribution directory.
47
-
48
- === GEM Installation
49
-
50
- Download and install DTR with the following.
51
-
52
- gem install --remote dtr
53
-
54
- === Running the DTR Test Suite
55
-
56
- If you wish to run the unit and functional tests that come with DTR:
57
-
58
- * CD into the top project directory of dtr.
59
- * Type the following:
60
-
61
- rake # You need a version of rake installed
62
-
63
- == Simple Example
64
-
65
- Once installed, you can run DTR server anywhere:
66
-
67
- % dtr -s
68
-
69
- If you are running Runner Agent on remote machine, please specify the DTR server machine ip:
70
-
71
- % dtr -s --server_address dtr_server_ip
72
-
73
- After server started, you can run DTR runner agent at your project directory as follows:
74
-
75
- % dtr --server_address dtr_server_ip -r runner_name
76
-
77
- Type "dtr --help" for an up-to-date option summary.
78
- Invoking <tt>dtr</tt> without any options causes dtr to show help too.
79
-
80
- Most of time your project test suite need setup environment before run tests, you
81
- can set setup command by option '--setup', for example:
82
-
83
- % dtr --server_address dtr_server_ip -r runner1 --setup "rake db:test:prepare"
84
-
85
- At last, you need to add 'dtr/test_unit_injection.rb' into test_files of your test
86
- task in your Rakefile, and for load it, you also need add dtr lib path:
87
-
88
- require 'dtr'
89
-
90
- DTR.broadcast_list = ['dtr server ip']
91
-
92
- Rake::TestTask.new(:dtr_tests) do |t|
93
- t.libs << DTR.lib_path
94
- t.test_files = FileList['dtr/test_unit_injection.rb', 'test/*test.rb']
95
- t.warning = false
96
- t.verbose = false
97
- end
98
-
99
- In the case above we changed dtr broadcast list for listening remote dtr server.
100
- And you also can use *DTR.port = 1234* to change the port listening. These two
101
- options would be cached in the directory running the test or any other dtr processes
102
- (e.g. dtr runner agent or dtr server).
103
-
104
- == Run tests in multi-processes on one machine
105
-
106
- For running Runner in multi-processes, you need install gem 'daemons'.
107
- The following is the test task example in the rake file:
108
-
109
- require 'dtr/raketasks'
110
-
111
- DTR::MPTask.new :test do |t|
112
- t.test_files = FileList['test/*test.rb']
113
- t.processes = 2 #default is 2
114
- end
115
-
116
- == Setup a development build grid
117
-
118
- This example is a build grid shared within developers to run unit tests
119
- before commit code. We'll use Mercurial to synchronize code between Runner
120
- Agent machines and developer machine.
121
-
122
- === Background
123
-
124
- Suppose I am a developer working on a rails project pet_store checked out
125
- at '~/pet_store'. I have a MacBook called M1 and another 2 Mac-minis called
126
- M2 and M3. My plan is distributing tests running on M1 to M2 and M3 before
127
- commit code. M3 also will be DTR Server. All machines are accessable in
128
- local network and IPs are M1_IP, M2_IP and M3_IP. Mercurial and DTR are
129
- installed on all machines. And for running dtr runner agent in daemons mode,
130
- you need install gem daemons. The test environment will be running on Mysql.
131
- All machines are installed Mysql accessed by the following database.yml
132
- configuration:
133
-
134
- development:
135
- database: pet_store_development
136
- adapter: mysql
137
- username: root
138
- password:
139
- host: localhost
140
- test:
141
- database: pet_store_test
142
- adapter: mysql
143
- username: root
144
- password:
145
- host: localhost
146
-
147
- === DTR Server
148
-
149
- * Start DTR Server on M3: dtr -s -a M3_IP.
150
-
151
- === Setup Mercurial repository on M1
152
-
153
- * Init Mercurial repository in '~/pet_store' directory: hg init
154
- * Edit .hgignore file to ignore files don't need while running tests. In this example, we will ignore files: db/schema.rb, .dtr_env_pstore, dtr_runners.pid and dtr_server*; and directories: public, tmp and log.
155
- * Commit .hgignore and then commit code into Mercurial repository.
156
- * Start 'hg serve' in '~/pet_store', the default port would be 8000. We'll use the default port in this example.
157
- * We'll clone the repository to M2 and M3 later.
158
-
159
- === Build Script
160
-
161
- * Add an environment variable on all machines: export DEV_BUILD_SERVER=M3_IP
162
- * Add an environment variable on development machine M1: export DTR_MASTER_ENV=http: //M1_IP:8000/pet_store. The environment variable DTR_MASTER_ENV would be copied to all DTR Runner Agent for sharing information between DTR Master machine and DTR Runner Agents.
163
- * Create a rake file at '~/pet_store' directory called: dtr_tasks.rake
164
-
165
- DEV_BUILD_SERVER = ENV['DEV_BUILD_SERVER']
166
- DEV_REPOSITORY_URL = ENV['DTR_MASTER_ENV']
167
-
168
- task :dtr => ['hg:check', 'dtr:uf']
169
-
170
- namespace :hg do
171
- task :reinit do
172
- Dir.glob("*").each do |f|
173
- #remove all files except files start with 'dtr_' including dtr runner pid file and current file dtr_tasks.rake
174
- next if f =~ /^dtr_/
175
- FileUtils.rm_rf(f)
176
- end
177
- FileUtils.rm_rf(".hg")
178
- FileUtils.rm_rf(".hgignore")
179
- puts %x[hg init]
180
- puts %x[hg pull -u -y #{DEV_REPOSITORY_URL}]
181
- puts %x[mkdir tmp]
182
- puts %x[mkdir log]
183
- end
184
- task :check do
185
- http = Net::HTTP.new("localhost", "8000")
186
- begin
187
- http.get("/")
188
- rescue Exception
189
- raise "Mercurial server is not running!"
190
- end
191
-
192
- raise "Please commit changes into Mercurial repository" if %x[hg st] =~ /.+/
193
- end
194
- end
195
-
196
- namespace :dtr do
197
- task :runner_agent do
198
- puts %x[dtr -R;dtr -r runner -a '#{DEV_BUILD_SERVER}' -i 'rake --rakefile dtr_tasks.rake hg:reinit;rake db:migrate db:test:prepare' -D]
199
- end
200
-
201
- require 'dtr'
202
-
203
- DTR.broadcast_list = [DEV_BUILD_SERVER]
204
-
205
- DTR::MPTask.new(:uf) do |t|
206
- t.libs << "test"
207
- t.test_files = FileList['test/unit/**/*test.rb', 'test/functional/**/*test.rb']
208
- t.processes = ENV['P'] || 1
209
- t.runner_options = "-a '#{DEV_BUILD_SERVER}' -i 'rake db:migrate db:test:prepare'"
210
- t.start_server = false
211
- t.verbose = false
212
- end
213
- end
214
-
215
- * The script above can re-init Mercurial repository. Runner Agent will run the 'hg:reinit' task before start Runner(see the 'runner_agent' task in the script above), so that Runner Agent can support any developer's codebase to run tests.
216
- * Commit dtr_tasks.rake into Mercurial repository.
217
-
218
- === Runner Agent
219
-
220
- * Clone repository from M1 to M2 in home directory: hg clone http: //M1_IP:8000/pet_store
221
- * Start runner agent in the directory ~/pet_store on M2: rake --rakefile dtr_tasks.rake dtr:runner_agent
222
- * Start runner agent on M3 same with above.
223
-
224
- === Run DTR Master on M1
225
-
226
- * Commit all changes into Mercurial
227
- * rake --rakefile dtr_tasks.rake dtr
228
- * After you started the DTR Master, you can type the following to monitor the DTR Server status: dtr -a $DEV_BUILD_SERVER -m
229
- * For output into console, open the debug option: dtr -a $DEV_BUILD_SERVER -d -m
230
-
231
- == Credits
232
-
233
- [<b>Josh Price</b>] For fixing tests packer in release 0.0.1.
234
-
235
- [<b>Wang Pengchao</b>] For sharing lots of ideas.
236
-
237
- [<b>Barrow H Kwan</b>] For patch of specifying DTR server port.
238
-
239
- [<b>Mingle team</b>(http://studios.thoughtworks.com/mingle-project-intelligence)] For making all these happen.
240
-
241
- == License
242
-
243
- DTR is available under an Apache License Version 2.
244
-
245
- == Support
246
-
247
- Feel free to submit commits or feature requests. If you send a patch,
248
- remember to update the corresponding unit tests. If fact, I prefer
249
- new feature to be submitted in the form of new unit tests.
250
-
251
- For other information, feel free to contact mailto:swing1979@gmail.com.
252
-
253
- == Usage
254
-
255
- DTR client is invoked from the command line using:
256
-
257
- % dtr [<em>options</em> ...]
258
-
259
- === Options are:
260
-
261
- -p, --port PORT Port number DTR will listen to. Default is 3344.
262
- -r runner1_name,runner2_name Start DTR test runner agent with unique runner names.
263
- -a, --server_address ADDRESS Specify dtr server address, domain name or ip address, e.g. 192.168.0.1. Default is 'localhost'.
264
- -i, --setup COMMAND Set command for initializing test runner test environment, e.g. 'rake db:test:prepare'. Default is do nothing.
265
- -m, --monitor Monitor the status of the dtr rinda server, e.g. dtr -a 10.11.1.2 -m
266
- -c, --clean_server Clean server environment, shutdown all runners registered.
267
- -b, --be_silent Only show error messages
268
- -s, --server Start DTR service server. There must be a DTR server running.
269
- -D, --daemon Start server/runners in daemon mode. Gem 'daemons' must be installed
270
- -S, --stop_server Stop server run in daemon mode.
271
- -R, --stop_runners Stop runners run in daemon mode.
272
- -d, --debug output debug log
273
- -v, --version Show version
274
- -h, --help Show this help doc
275
-
276
- Type "dtr --help" for an up-to-date option summary.
277
-
278
- = Other stuff
279
-
280
- Author:: Li Xiao <swing1979@gmail.com>
281
- Requires:: Ruby 1.8.5 or later
282
- License:: Copyright 2007-2008 by Li Xiao.
283
- Released under an Apache License 2. See the LICENSE file
284
- included in the distribution.
285
-
286
- == Warranty
287
-
288
- This software is provided "as is" and without any express or
289
- implied warranties, including, without limitation, the implied
290
- warranties of merchantibility and fitness for a particular
291
- purpose.
data/install.rb DELETED
@@ -1,88 +0,0 @@
1
- require 'rbconfig'
2
- require 'find'
3
- require 'ftools'
4
-
5
- include Config
6
-
7
- $ruby = CONFIG['ruby_install_name']
8
-
9
- ##
10
- # Install a binary file. We patch in on the way through to
11
- # insert a #! line. If this is a Unix install, we name
12
- # the command (for example) 'dtr' and let the shebang line
13
- # handle running it. Under windows, we add a '.rb' extension
14
- # and let file associations to their stuff
15
- #
16
-
17
- def installBIN(from, opfile)
18
-
19
- tmp_dir = nil
20
- for t in [".", "/tmp", "c:/temp", $bindir]
21
- stat = File.stat(t) rescue next
22
- if stat.directory? and stat.writable?
23
- tmp_dir = t
24
- break
25
- end
26
- end
27
-
28
- fail "Cannot find a temporary directory" unless tmp_dir
29
- tmp_file = File.join(tmp_dir, "_tmp")
30
-
31
- File.open(from) do |ip|
32
- File.open(tmp_file, "w") do |op|
33
- ruby = File.join($realbindir, $ruby)
34
- op.puts "#!#{ruby}"
35
- op.write ip.read
36
- end
37
- end
38
-
39
- opfile += ".rb" if CONFIG["target_os"] =~ /mswin/i
40
- File::install(tmp_file, File.join($bindir, opfile), 0755, true)
41
- File::unlink(tmp_file)
42
- end
43
-
44
- $sitedir = CONFIG["sitelibdir"]
45
- unless $sitedir
46
- version = CONFIG["MAJOR"]+"."+CONFIG["MINOR"]
47
- $libdir = File.join(CONFIG["libdir"], "ruby", version)
48
- $sitedir = $:.find {|x| x =~ /site_ruby/}
49
- if !$sitedir
50
- $sitedir = File.join($libdir, "site_ruby")
51
- elsif $sitedir !~ Regexp.quote(version)
52
- $sitedir = File.join($sitedir, version)
53
- end
54
- end
55
-
56
- $bindir = CONFIG["bindir"]
57
-
58
- $realbindir = $bindir
59
-
60
- bindir = CONFIG["bindir"]
61
- if (destdir = ENV['DESTDIR'])
62
- $bindir = destdir + $bindir
63
- $sitedir = destdir + $sitedir
64
-
65
- File::makedirs($bindir)
66
- File::makedirs($sitedir)
67
- end
68
-
69
- dtr_dest = File.join($sitedir, "dtr")
70
- File::makedirs(dtr_dest, true)
71
- File::chmod(0755, dtr_dest)
72
-
73
- # The library files
74
-
75
- files = Dir.chdir('lib') { Dir['**/*.rb', '**/*.rake'] }
76
-
77
- for fn in files
78
- fn_dir = File.dirname(fn)
79
- target_dir = File.join($sitedir, fn_dir)
80
- if ! File.exist?(target_dir)
81
- File.makedirs(target_dir)
82
- end
83
- File::install(File.join('lib', fn), File.join($sitedir, fn), 0644, true)
84
- end
85
-
86
- # and the executable
87
-
88
- installBIN("bin/dtr", "dtr")
@@ -1,172 +0,0 @@
1
- # Copyright (c) 2007-2008 Li Xiao
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- require 'pstore'
16
-
17
- require 'logger'
18
-
19
- class Array
20
- def blank?
21
- empty?
22
- end
23
- end
24
-
25
- class NilClass
26
- def blank?
27
- true
28
- end
29
- end
30
-
31
- module DTR
32
-
33
- MESSAGE_KEY = :message
34
-
35
- def logger
36
- return DTROPTIONS[:logger] if DTROPTIONS[:logger]
37
- DTROPTIONS[:logger] = if DTROPTIONS[:log_level] == Logger::DEBUG
38
- Logger.new(STDOUT)
39
- else
40
- log_file = if File.exist?('log')
41
- File.join("log", DTROPTIONS[:log_file] || 'dtr.log')
42
- else
43
- DTROPTIONS[:log_file] || 'dtr.log'
44
- end
45
- Logger.new("./#{log_file}", 1, 5*1024*1024)
46
- end
47
- DTROPTIONS[:logger].datetime_format = "%m-%d %H:%M:%S"
48
- DTROPTIONS[:logger].level = DTROPTIONS[:log_level] || Logger::INFO
49
- DTROPTIONS[:logger]
50
- end
51
-
52
- def debug(message=nil, &block)
53
- output(:debug, message, &block)
54
- end
55
-
56
- def info(message=nil, &block)
57
- output(:info, message, &block)
58
- end
59
-
60
- def error(message=nil, &block)
61
- output(:error, message, &block)
62
- end
63
-
64
- def output(level, msg=nil, &block)
65
- logger.send(level) do
66
- message = block_given? ? block.call : msg.to_s
67
- EnvStore.new << [MESSAGE_KEY, "[#{Process.pid}-#{level.to_s.upcase}] #{message}"] if DTROPTIONS[:run_with_monitor]
68
- message
69
- end
70
- end
71
-
72
- def silent?
73
- logger.level == Logger::ERROR
74
- end
75
-
76
- def with_monitor
77
- DTROPTIONS[:run_with_monitor] = true
78
- EnvStore.new[MESSAGE_KEY] = []
79
- yield
80
- rescue Exception => e
81
- info {"stopping by Exception => #{e.class.name}, message => #{e.message}"}
82
- wait_times = 0
83
- until EnvStore.new[MESSAGE_KEY].empty? || wait_times > 14
84
- wait_times += 1
85
- sleep(1)
86
- end
87
- raise e
88
- end
89
-
90
- module_function :debug, :info, :error, :output, :with_monitor, :logger, :silent?
91
-
92
- class CmdInterrupt < StandardError; end
93
-
94
- class Cmd
95
- def self.execute(cmd)
96
- return true if cmd.nil? || cmd.empty?
97
- DTR.info {"Executing: #{cmd.inspect}"}
98
- output = %x[#{cmd} 2>&1]
99
- DTR.info {"Execution is done, status: #{$?.exitstatus}"}
100
- DTR.error {"#{cmd.inspect} output:\n#{output}"} if $?.exitstatus != 0
101
- $?.exitstatus == 0
102
- end
103
- end
104
-
105
- class EnvStore
106
-
107
- FILE_NAME = '.dtr_env_pstore'
108
-
109
- def self.destroy
110
- File.delete(FILE_NAME) if File.exist?(FILE_NAME)
111
- end
112
-
113
- def [](key)
114
- return nil unless File.exist?(FILE_NAME)
115
-
116
- repository = PStore.new(FILE_NAME)
117
- repository.transaction(true) do
118
- repository[key]
119
- end
120
- end
121
-
122
- def []=(key, value)
123
- repository = PStore.new(FILE_NAME)
124
- repository.transaction do
125
- repository[key] = value
126
- end
127
- end
128
-
129
- def <<(key_value)
130
- key, value = key_value
131
- repository = PStore.new(FILE_NAME)
132
- repository.transaction do
133
- repository[key] = (repository[key] || []) << value
134
- end
135
- end
136
-
137
- def shift(key)
138
- repository = PStore.new(FILE_NAME)
139
- repository.transaction do
140
- if array = repository[key]
141
- array.shift
142
- repository[key] = array
143
- end
144
- end
145
- end
146
- end
147
-
148
- class WorkingEnv
149
-
150
- @@current = nil
151
- def self.refresh
152
- @@current = self.new
153
- end
154
-
155
- def self.current
156
- @@current
157
- end
158
-
159
- def initialize
160
- files = (defined?($argv_dup) ? $argv_dup : []).dup
161
- @env = {:libs => $LOAD_PATH.dup, :files => files, :created_at => Time.now.to_s, :dtr_master_env => ENV['DTR_MASTER_ENV'], :identifier => "#{Time.now.to_s}:#{rand}:#{object_id}"}
162
- end
163
-
164
- def [](key)
165
- @env[key]
166
- end
167
-
168
- def to_s
169
- @env.inspect
170
- end
171
- end
172
- end