rbbt-util 5.44.1 → 6.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/bin/rbbt +67 -90
  3. data/etc/app.d/base.rb +2 -2
  4. data/etc/app.d/semaphores.rb +3 -3
  5. data/lib/rbbt/annotations/annotated_array.rb +207 -207
  6. data/lib/rbbt/annotations/refactor.rb +27 -0
  7. data/lib/rbbt/annotations/util.rb +282 -282
  8. data/lib/rbbt/annotations.rb +343 -320
  9. data/lib/rbbt/association/database.rb +200 -225
  10. data/lib/rbbt/association/index.rb +294 -291
  11. data/lib/rbbt/association/item.rb +227 -227
  12. data/lib/rbbt/association/open.rb +35 -34
  13. data/lib/rbbt/association/util.rb +0 -169
  14. data/lib/rbbt/association.rb +2 -4
  15. data/lib/rbbt/entity/identifiers.rb +119 -118
  16. data/lib/rbbt/entity/refactor.rb +12 -0
  17. data/lib/rbbt/entity.rb +319 -315
  18. data/lib/rbbt/hpc/batch.rb +72 -53
  19. data/lib/rbbt/hpc/lsf.rb +2 -2
  20. data/lib/rbbt/hpc/orchestrate/batches.rb +2 -2
  21. data/lib/rbbt/hpc/orchestrate/chains.rb +25 -5
  22. data/lib/rbbt/hpc/orchestrate/rules.rb +2 -2
  23. data/lib/rbbt/hpc/orchestrate.rb +19 -13
  24. data/lib/rbbt/hpc/slurm.rb +18 -18
  25. data/lib/rbbt/knowledge_base/entity.rb +13 -5
  26. data/lib/rbbt/knowledge_base/query.rb +2 -2
  27. data/lib/rbbt/knowledge_base/registry.rb +32 -31
  28. data/lib/rbbt/knowledge_base/traverse.rb +1 -1
  29. data/lib/rbbt/knowledge_base.rb +1 -1
  30. data/lib/rbbt/monitor.rb +36 -25
  31. data/lib/rbbt/persist/refactor.rb +166 -0
  32. data/lib/rbbt/persist/tsv/tokyocabinet.rb +105 -105
  33. data/lib/rbbt/persist/tsv.rb +187 -185
  34. data/lib/rbbt/persist.rb +556 -551
  35. data/lib/rbbt/refactor.rb +20 -0
  36. data/lib/rbbt/resource/path/refactor.rb +178 -0
  37. data/lib/rbbt/resource/path.rb +317 -497
  38. data/lib/rbbt/resource/util.rb +0 -48
  39. data/lib/rbbt/resource.rb +3 -390
  40. data/lib/rbbt/tsv/accessor.rb +2 -838
  41. data/lib/rbbt/tsv/attach.rb +303 -299
  42. data/lib/rbbt/tsv/change_id.rb +244 -245
  43. data/lib/rbbt/tsv/csv.rb +87 -85
  44. data/lib/rbbt/tsv/dumper.rb +2 -100
  45. data/lib/rbbt/tsv/excel.rb +26 -24
  46. data/lib/rbbt/tsv/field_index.rb +4 -1
  47. data/lib/rbbt/tsv/filter.rb +3 -2
  48. data/lib/rbbt/tsv/index.rb +2 -284
  49. data/lib/rbbt/tsv/manipulate.rb +750 -747
  50. data/lib/rbbt/tsv/marshal.rb +3 -3
  51. data/lib/rbbt/tsv/matrix.rb +2 -2
  52. data/lib/rbbt/tsv/parallel/through.rb +2 -1
  53. data/lib/rbbt/tsv/parallel/traverse.rb +783 -781
  54. data/lib/rbbt/tsv/parser.rb +678 -678
  55. data/lib/rbbt/tsv/refactor.rb +195 -0
  56. data/lib/rbbt/tsv/stream.rb +253 -251
  57. data/lib/rbbt/tsv/util.rb +420 -420
  58. data/lib/rbbt/tsv.rb +210 -208
  59. data/lib/rbbt/util/R/eval.rb +4 -4
  60. data/lib/rbbt/util/R/plot.rb +62 -166
  61. data/lib/rbbt/util/R.rb +21 -18
  62. data/lib/rbbt/util/cmd.rb +2 -318
  63. data/lib/rbbt/util/color.rb +269 -269
  64. data/lib/rbbt/util/colorize.rb +89 -89
  65. data/lib/rbbt/util/concurrency/processes/refactor.rb +22 -0
  66. data/lib/rbbt/util/concurrency/processes/worker.rb +2 -2
  67. data/lib/rbbt/util/concurrency/processes.rb +389 -386
  68. data/lib/rbbt/util/config.rb +169 -167
  69. data/lib/rbbt/util/iruby.rb +20 -0
  70. data/lib/rbbt/util/log/progress/report.rb +241 -241
  71. data/lib/rbbt/util/log/progress/util.rb +99 -99
  72. data/lib/rbbt/util/log/progress.rb +102 -102
  73. data/lib/rbbt/util/log/refactor.rb +49 -0
  74. data/lib/rbbt/util/log.rb +486 -532
  75. data/lib/rbbt/util/migrate.rb +1 -1
  76. data/lib/rbbt/util/misc/concurrent_stream.rb +248 -246
  77. data/lib/rbbt/util/misc/development.rb +12 -11
  78. data/lib/rbbt/util/misc/exceptions.rb +117 -112
  79. data/lib/rbbt/util/misc/format.rb +2 -230
  80. data/lib/rbbt/util/misc/indiferent_hash.rb +2 -107
  81. data/lib/rbbt/util/misc/inspect.rb +2 -476
  82. data/lib/rbbt/util/misc/lock.rb +109 -106
  83. data/lib/rbbt/util/misc/omics.rb +9 -1
  84. data/lib/rbbt/util/misc/pipes.rb +765 -793
  85. data/lib/rbbt/util/misc/refactor.rb +20 -0
  86. data/lib/rbbt/util/misc/ssw.rb +27 -17
  87. data/lib/rbbt/util/misc/system.rb +0 -15
  88. data/lib/rbbt/util/misc.rb +39 -20
  89. data/lib/rbbt/util/named_array/refactor.rb +4 -0
  90. data/lib/rbbt/util/named_array.rb +3 -220
  91. data/lib/rbbt/util/open/refactor.rb +7 -0
  92. data/lib/rbbt/util/open.rb +3 -857
  93. data/lib/rbbt/util/procpath.rb +6 -6
  94. data/lib/rbbt/util/python/paths.rb +27 -0
  95. data/lib/rbbt/util/python/run.rb +115 -0
  96. data/lib/rbbt/util/python/script.rb +110 -0
  97. data/lib/rbbt/util/python/util.rb +3 -3
  98. data/lib/rbbt/util/python.rb +22 -81
  99. data/lib/rbbt/util/semaphore.rb +152 -148
  100. data/lib/rbbt/util/simpleopt.rb +9 -8
  101. data/lib/rbbt/util/ssh/refactor.rb +19 -0
  102. data/lib/rbbt/util/ssh.rb +122 -118
  103. data/lib/rbbt/util/tar.rb +117 -115
  104. data/lib/rbbt/util/tmpfile.rb +69 -67
  105. data/lib/rbbt/util/version.rb +2 -0
  106. data/lib/rbbt/workflow/refactor/entity.rb +11 -0
  107. data/lib/rbbt/workflow/refactor/export.rb +66 -0
  108. data/lib/rbbt/workflow/refactor/inputs.rb +24 -0
  109. data/lib/rbbt/workflow/refactor/recursive.rb +64 -0
  110. data/lib/rbbt/workflow/refactor/task_info.rb +65 -0
  111. data/lib/rbbt/workflow/refactor.rb +153 -0
  112. data/lib/rbbt/workflow/remote_workflow/driver/ssh.rb +55 -32
  113. data/lib/rbbt/workflow/remote_workflow/remote_step/rest.rb +3 -1
  114. data/lib/rbbt/workflow/remote_workflow/remote_step/ssh.rb +14 -5
  115. data/lib/rbbt/workflow/remote_workflow/remote_step.rb +19 -7
  116. data/lib/rbbt/workflow/remote_workflow.rb +6 -1
  117. data/lib/rbbt/workflow/step/run.rb +766 -766
  118. data/lib/rbbt/workflow/step/save_load_inputs.rb +254 -254
  119. data/lib/rbbt/workflow/step.rb +2 -362
  120. data/lib/rbbt/workflow/task.rb +118 -118
  121. data/lib/rbbt/workflow/usage.rb +289 -287
  122. data/lib/rbbt/workflow/util/archive.rb +6 -5
  123. data/lib/rbbt/workflow/util/data.rb +1 -1
  124. data/lib/rbbt/workflow/util/orchestrator.rb +249 -246
  125. data/lib/rbbt/workflow/util/trace.rb +79 -44
  126. data/lib/rbbt/workflow.rb +4 -882
  127. data/lib/rbbt-util.rb +21 -13
  128. data/lib/rbbt.rb +16 -3
  129. data/python/rbbt/__init__.py +19 -1
  130. data/share/Rlib/plot.R +37 -37
  131. data/share/Rlib/svg.R +22 -5
  132. data/share/install/software/lib/install_helpers +1 -1
  133. data/share/rbbt_commands/hpc/list +2 -3
  134. data/share/rbbt_commands/hpc/orchestrate +4 -4
  135. data/share/rbbt_commands/hpc/tail +2 -0
  136. data/share/rbbt_commands/hpc/task +10 -7
  137. data/share/rbbt_commands/lsf/list +2 -3
  138. data/share/rbbt_commands/lsf/orchestrate +4 -4
  139. data/share/rbbt_commands/lsf/tail +2 -0
  140. data/share/rbbt_commands/lsf/task +10 -7
  141. data/share/rbbt_commands/migrate +1 -1
  142. data/share/rbbt_commands/pbs/list +2 -3
  143. data/share/rbbt_commands/pbs/orchestrate +4 -4
  144. data/share/rbbt_commands/pbs/tail +2 -0
  145. data/share/rbbt_commands/pbs/task +10 -7
  146. data/share/rbbt_commands/resource/produce +8 -1
  147. data/share/rbbt_commands/slurm/list +2 -3
  148. data/share/rbbt_commands/slurm/orchestrate +4 -4
  149. data/share/rbbt_commands/slurm/tail +2 -0
  150. data/share/rbbt_commands/slurm/task +10 -7
  151. data/share/rbbt_commands/system/clean +5 -5
  152. data/share/rbbt_commands/system/status +5 -5
  153. data/share/rbbt_commands/tsv/get +2 -3
  154. data/share/rbbt_commands/tsv/info +10 -13
  155. data/share/rbbt_commands/tsv/keys +18 -14
  156. data/share/rbbt_commands/tsv/slice +2 -2
  157. data/share/rbbt_commands/tsv/transpose +6 -2
  158. data/share/rbbt_commands/workflow/info +20 -24
  159. data/share/rbbt_commands/workflow/list +1 -1
  160. data/share/rbbt_commands/workflow/prov +20 -13
  161. data/share/rbbt_commands/workflow/server +11 -1
  162. data/share/rbbt_commands/workflow/task +76 -71
  163. data/share/rbbt_commands/workflow/write_info +26 -9
  164. data/share/software/opt/ssw/ssw.c +861 -0
  165. data/share/software/opt/ssw/ssw.h +130 -0
  166. data/share/workflow_config.ru +3 -3
  167. metadata +40 -2
@@ -1,857 +1,3 @@
1
- require 'rbbt/util/cmd'
2
- require 'rbbt/util/tmpfile'
3
- require 'rbbt/util/misc'
4
- require 'rbbt/util/misc/bgzf'
5
- require 'pathname'
6
-
7
- require 'zlib'
8
-
9
- module Open
10
- class OpenURLError < StandardError; end
11
- class OpenGzipError < StandardError; end
12
-
13
- REMOTE_CACHEDIR = File.join(ENV["HOME"], "/tmp/open_cache")
14
- #FileUtils.mkdir_p REMOTE_CACHEDIR unless File.exist? REMOTE_CACHEDIR
15
-
16
- GREP_CMD = begin
17
- if ENV["GREP_CMD"]
18
- ENV["GREP_CMD"]
19
- elsif File.exist?('/bin/grep')
20
- "/bin/grep"
21
- elsif File.exist?('/usr/bin/grep')
22
- "/usr/bin/grep"
23
- else
24
- "grep"
25
- end
26
- end
27
-
28
- class << self
29
- attr_accessor :repository_dirs
30
-
31
- def repository_dirs
32
- @repository_dirs ||= begin
33
- File.exist?(Rbbt.etc.repository_dirs.find) ?
34
- File.read(Rbbt.etc.repository_dirs.find).split("\n") :
35
- []
36
- rescue
37
- []
38
- end
39
- end
40
-
41
- end
42
-
43
- def self.cachedir=(cachedir)
44
- REMOTE_CACHEDIR.replace cachedir
45
- FileUtils.mkdir_p REMOTE_CACHEDIR unless File.exist? REMOTE_CACHEDIR
46
- end
47
-
48
- def self.cachedir
49
- REMOTE_CACHEDIR
50
- end
51
-
52
- # Remote WGET
53
- LAST_TIME = {}
54
- def self.wait(lag, key = nil)
55
- time = Time.now
56
-
57
- if LAST_TIME[key] != nil && (time < LAST_TIME[key] + lag)
58
- sleep (LAST_TIME[key] + lag) - time
59
- end
60
-
61
- LAST_TIME[key] = Time.now
62
- end
63
-
64
- def self.wget(url, options = {})
65
- Log.low "WGET:\n -URL: #{ url }\n -OPTIONS: #{options.inspect}"
66
- options = Misc.add_defaults options, "--user-agent=" => 'rbbt', :pipe => true
67
-
68
- wait(options[:nice], options[:nice_key]) if options[:nice]
69
- options.delete(:nice)
70
- options.delete(:nice_key)
71
-
72
- pipe = options.delete(:pipe)
73
- quiet = options.delete(:quiet)
74
- post = options.delete(:post)
75
- cookies = options.delete(:cookies)
76
-
77
- options["--quiet"] = quiet if options["--quiet"].nil?
78
- options["--post-data="] ||= post if post
79
-
80
- if cookies
81
- options["--save-cookies"] = cookies
82
- options["--load-cookies"] = cookies
83
- options["--keep-session-cookies"] = true
84
- end
85
-
86
-
87
- stderr = case
88
- when options['stderr']
89
- options['stderr']
90
- when options['--quiet']
91
- false
92
- else
93
- nil
94
- end
95
-
96
- begin
97
- wget_options = options.dup
98
- wget_options = wget_options.merge( '-O' => '-') unless options.include?('--output-document')
99
- wget_options[:pipe] = pipe unless pipe.nil?
100
- wget_options[:stderr] = stderr unless stderr.nil?
101
-
102
- CMD.cmd("wget '#{ url }'", wget_options)
103
- rescue
104
- raise OpenURLError, "Error reading remote url: #{ url }.\n#{$!.message}"
105
- end
106
- end
107
-
108
- def self.digest_url(url, options = {})
109
- params = [url, options.values_at("--post-data", "--post-data="), (options.include?("--post-file")? Open.read(options["--post-file"]).split("\n").sort * "\n" : "")]
110
- digest = Misc.digest(params.inspect)
111
- end
112
- # Cache
113
- #
114
- def self.in_cache(url, options = {})
115
- filename = File.join(REMOTE_CACHEDIR, digest_url(url, options))
116
- if File.exist? filename
117
- return filename
118
- else
119
- nil
120
- end
121
- end
122
-
123
- def self.remove_from_cache(url, options = {})
124
- digest = Misc.digest([url, options.values_at("--post-data", "--post-data="), (options.include?("--post-file")? Open.read(options["--post-file"]) : "")].inspect)
125
-
126
- filename = File.join(REMOTE_CACHEDIR, digest)
127
- if File.exist? filename
128
- FileUtils.rm filename
129
- else
130
- nil
131
- end
132
- end
133
-
134
- def self.add_cache(url, data, options = {})
135
- file = File.join(REMOTE_CACHEDIR, digest_url(url, options))
136
- Misc.sensiblewrite(file, data, :force => true)
137
- end
138
-
139
- # Grep
140
-
141
- def self.grep(stream, grep, invert = false, fixed = nil)
142
- case
143
- when Array === grep
144
- TmpFile.with_file(grep * "\n", false) do |f|
145
- if FalseClass === fixed
146
- CMD.cmd("#{GREP_CMD} #{invert ? '-v' : ''} -", "-f" => f, :in => stream, :pipe => true, :post => proc{FileUtils.rm f})
147
- else
148
- CMD.cmd("#{GREP_CMD} #{invert ? '-v' : ''} -", "-w" => true, "-F" => true, "-f" => f, :in => stream, :pipe => true, :post => proc{FileUtils.rm f})
149
- end
150
- end
151
- else
152
- CMD.cmd("#{GREP_CMD} #{invert ? '-v ' : ''} '#{grep}' -", :in => stream, :pipe => true, :post => proc{begin stream.force_close; rescue Exception; end if stream.respond_to?(:force_close)})
153
- end
154
- end
155
-
156
- def self.clear_dir_repos
157
- @@repos.clear if defined? @@repos and @@repos
158
- end
159
- def self.get_repo_from_dir(dir)
160
- @@repos ||= {}
161
- @@repos[dir] ||= begin
162
- repo_path = File.join(dir, '.file_repo')
163
- Persist.open_tokyocabinet(repo_path, false, :clean, TokyoCabinet::BDB )
164
- end
165
- end
166
-
167
- def self.get_stream_from_repo(dir, sub_path)
168
- repo = get_repo_from_dir(dir)
169
- repo.read_and_close do
170
- content = repo[sub_path]
171
- content.nil? ? nil : StringIO.new(content).tap{|o| o.binmode }
172
- end
173
- end
174
-
175
- def self.get_time_from_repo(dir, sub_path)
176
- repo = get_repo_from_dir(dir)
177
- time = repo.read_and_close do
178
- Time.at(repo['.time.' + sub_path].to_i)
179
- end
180
- time
181
- end
182
-
183
- def self.get_atime_from_repo(dir, sub_path)
184
- repo = get_repo_from_dir(dir)
185
- File.atime(repo.persistance_path)
186
- end
187
-
188
- def self.writable_repo?(dir, sub_path)
189
- repo = get_repo_from_dir(dir)
190
- begin
191
- repo.write_and_close do
192
- end
193
- true
194
- rescue
195
- false
196
- end
197
- end
198
-
199
- def self.set_time_from_repo(dir, sub_path)
200
- repo = get_repo_from_dir(dir)
201
- repo.read_and_close do
202
- repo['.time.' + sub_path] = Time.now.to_i.to_s
203
- end
204
- end
205
-
206
- def self.save_content_in_repo(dir, sub_path, content)
207
- repo = get_repo_from_dir(dir)
208
- repo.write_and_close do
209
- repo['.time.' + sub_path] = Time.now.to_i.to_s
210
- repo[sub_path] = content
211
- end
212
- end
213
-
214
- def self.remove_from_repo(dir, sub_path, recursive = false)
215
- repo = get_repo_from_dir(dir)
216
- repo.write_and_close do
217
- if recursive
218
- repo.outlist repo.range sub_path, true, sub_path.sub(/.$/,('\1'.ord + 1).chr), false
219
- else
220
- repo.outlist sub_path
221
- end
222
- end
223
- end
224
-
225
- def self.list_repo_files(dir, sub_path = nil)
226
- repo = get_repo_from_dir(dir)
227
- files = repo.keys
228
- files.reject!{|f| f[0] == "."}
229
- return files unless sub_path
230
-
231
- files.select{|file| file.start_with?(sub_path) }
232
- end
233
-
234
- def self.exists_in_repo(dir, sub_path, content)
235
- repo = get_repo_from_dir(dir)
236
- repo.read_and_close do
237
- repo.include?(sub_path) && ! repo[sub_path].nil?
238
- end
239
- end
240
-
241
- def self.find_repo_dir(file)
242
- self.repository_dirs.each do |dir|
243
- dir = dir + '/' unless dir.chars[-1] == "/"
244
-
245
- begin
246
- if file.start_with?(dir) || file == dir[0..-2]
247
- sub_path = file.to_s[dir.length..-1]
248
- return [dir, sub_path]
249
- else
250
- if Path === file and (ffile = file.find).start_with? dir
251
- sub_path = ffile.to_s[dir.length..-1]
252
- return [dir, sub_path]
253
- end
254
- end
255
- end
256
- end
257
- nil
258
- end
259
-
260
- def self.rm(file)
261
- if (dir_sub_path = find_repo_dir(file))
262
- remove_from_repo(*dir_sub_path)
263
- else
264
- FileUtils.rm(file) if File.exist?(file) or Open.broken_link?(file)
265
- end
266
- end
267
-
268
- def self.rm_rf(file)
269
- if (dir_sub_path = find_repo_dir(file))
270
- remove_from_repo(dir_sub_path[0], dir_sub_path[1], true)
271
- else
272
- FileUtils.rm_rf(file)
273
- end
274
- end
275
-
276
- def self.file_open(file, grep, mode = 'r', invert_grep = false)
277
- if (dir_sub_path = find_repo_dir(file))
278
- if mode.include? 'w'
279
- stream = StringIO.new
280
- class << stream
281
- attr_accessor :dir_sub_path
282
- def close
283
- self.rewind
284
- Open.save_content_in_repo(*dir_sub_path, self.read)
285
- end
286
- end
287
- stream.dir_sub_path = dir_sub_path
288
-
289
- else
290
- stream = get_stream_from_repo(*dir_sub_path)
291
- end
292
- else
293
- Open.mkdir File.dirname(file) if mode.include? 'w'
294
-
295
- file = file.find if Path === file
296
- stream = File.open(file, mode)
297
- end
298
-
299
- if grep
300
- grep(stream, grep, invert_grep)
301
- else
302
- stream
303
- end
304
- end
305
-
306
- def self.ssh_open(file)
307
- m = file.match(/ssh:\/\/([^:]+):(.*)/)
308
- server = m[1]
309
- file = m[2]
310
- CMD.cmd("ssh '#{server}' cat '#{file}'", :pipe => true)
311
- end
312
-
313
- def self.file_write(file, content, mode = 'w')
314
- if (dir_sub_path = find_repo_dir(file))
315
- dir_sub_path.push content
316
- save_content_in_repo(*dir_sub_path)
317
- else
318
- File.open(file, mode) do |f|
319
- begin
320
- f.flock(File::LOCK_EX)
321
- f.write content
322
- f.flock(File::LOCK_UN)
323
- ensure
324
- f.close unless f.closed?
325
- end
326
- end
327
- end
328
- end
329
-
330
- def self.mkdir(target)
331
- if (dir_sub_path = find_repo_dir(target))
332
- nil
333
- else
334
- target = target.find if Path === target
335
- if ! File.exist?(target)
336
- FileUtils.mkdir_p target
337
- end
338
- end
339
- end
340
-
341
- def self.ln_s(source, target, options = {})
342
- source = source.find if Path === source
343
- target = target.find if Path === target
344
-
345
- target = File.join(target, File.basename(source)) if File.directory? target
346
- FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target))
347
- FileUtils.rm target if File.exist?(target)
348
- FileUtils.rm target if File.symlink?(target)
349
- FileUtils.ln_s source, target
350
- end
351
-
352
- def self.ln(source, target, options = {})
353
- source = source.find if Path === source
354
- target = target.find if Path === target
355
- source = File.realpath(source) if File.symlink?(source)
356
-
357
- FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target))
358
- FileUtils.rm target if File.exist?(target)
359
- FileUtils.rm target if File.symlink?(target)
360
- FileUtils.ln source, target
361
- end
362
-
363
- def self.ln_h(source, target, options = {})
364
- source = source.find if Path === source
365
- target = target.find if Path === target
366
-
367
- FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target))
368
- FileUtils.rm target if File.exist?(target)
369
- begin
370
- CMD.cmd("ln -L '#{ source }' '#{ target }'")
371
- rescue ProcessFailed
372
- Log.debug "Could not hard link #{source} and #{target}: #{$!.message.gsub("\n", '. ')}"
373
- CMD.cmd("cp -L '#{ source }' '#{ target }'")
374
- end
375
- end
376
-
377
- def self.link(source, target, options = {})
378
- begin
379
- Open.ln(source, target, options)
380
- rescue
381
- Open.ln_s(source, target, options)
382
- end
383
- nil
384
- end
385
-
386
- #def self.cp(source, target, options = {})
387
- # source = source.find if Path === source
388
- # target = target.find if Path === target
389
-
390
- # FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target))
391
- # FileUtils.rm target if File.exist?(target)
392
- # FileUtils.cp source, target
393
- #end
394
-
395
- def self.cp(source, target, options = {})
396
- dir_sub_path_source = find_repo_dir(source)
397
- dir_sub_path_target = find_repo_dir(target)
398
-
399
- if dir_sub_path_source.nil? and dir_sub_path_target.nil?
400
- FileUtils.mkdir_p File.dirname(target) unless File.exist? File.dirname(target)
401
- tmp_target = File.join(File.dirname(target), '.tmp_mv.' + File.basename(target))
402
- FileUtils.cp_r source, tmp_target
403
- FileUtils.mv tmp_target, target
404
- return
405
- end
406
-
407
- if dir_sub_path_source.nil?
408
- save_content_in_repo(dir_sub_path_target[0], dir_sub_path_target[1], Open.read(source, :mode => 'rb', :nofix => true))
409
- return nil
410
- end
411
-
412
- if dir_sub_path_target.nil?
413
- Open.write(target, get_stream_from_repo(dir_sub_path_source))
414
- return nil
415
- end
416
-
417
- repo_source = get_repo_from_dir(dir_sub_path_source[0])
418
- repo_target = get_repo_from_dir(dir_sub_path_target[0])
419
-
420
- content = repo_source.read_and_close do
421
- repo_source[dir_sub_path_source[1]]
422
- end
423
-
424
- repo_target.write_and_close do
425
- repo_target[dir_sub_path_target[1]] = content
426
- end
427
-
428
- return nil
429
- end
430
-
431
- def self.mv(source, target, options = {})
432
- dir_sub_path_source = find_repo_dir(source)
433
- dir_sub_path_target = find_repo_dir(target)
434
-
435
- if dir_sub_path_source.nil? and dir_sub_path_target.nil?
436
- FileUtils.mkdir_p File.dirname(target) unless File.exist? File.dirname(target)
437
- tmp_target = File.join(File.dirname(target), '.tmp_mv.' + File.basename(target))
438
- FileUtils.mv source, tmp_target
439
- FileUtils.mv tmp_target, target
440
- return nil
441
- end
442
-
443
- if dir_sub_path_source.nil?
444
- save_content_in_repo(dir_sub_path_target[0], dir_sub_path_target[1], Open.read(source, :mode => 'rb', :nofix => true))
445
- return nil
446
- end
447
-
448
- if dir_sub_path_target.nil?
449
- Open.write(target, get_stream_from_repo(dir_sub_path_source))
450
- return nil
451
- end
452
-
453
- repo_source = get_repo_from_dir(dir_sub_path_source[0])
454
- repo_target = get_repo_from_dir(dir_sub_path_target[0])
455
-
456
- content = repo_source.read_and_close do
457
- repo_source[dir_sub_path_target[1]]
458
- end
459
-
460
- repo_target.write_and_close do
461
- repo_target[dir_sub_path_source[1]] = content
462
- end
463
-
464
- repo_source.write_and_close do
465
- repo_source.delete dir_sub_path_source[1]
466
- end
467
-
468
- return nil
469
- end
470
-
471
- def self.exists?(file)
472
- if (dir_sub_path = find_repo_dir(file))
473
- dir_sub_path.push file
474
- exists_in_repo(*dir_sub_path)
475
- else
476
- file = file.find if Path === file
477
- File.exist?(file) #|| File.symlink?(file)
478
- end
479
- end
480
-
481
- class << self
482
- alias exist? exists?
483
- end
484
-
485
- def self.lock(file, options = {}, &block)
486
- if file and (dir_sub_path = find_repo_dir(file))
487
- dir, sub_path = dir_sub_path
488
- repo = get_repo_from_dir(dir)
489
- Misc.lock_in_repo(repo, sub_path, &block)
490
- else
491
- Misc.lock(file, options, &block)
492
- end
493
- end
494
-
495
-
496
- # Decompression
497
-
498
- def self.bgunzip(stream)
499
- Bgzf.setup stream
500
- end
501
-
502
- def self.gunzip(stream)
503
- CMD.cmd('zcat', :in => stream, :pipe => true, :no_fail => true, :no_wait => true)
504
- end
505
-
506
- def self.gzip(stream)
507
- CMD.cmd('gzip', :in => stream, :pipe => true, :no_fail => true, :no_wait => true)
508
- end
509
-
510
- def self.bgzip(stream)
511
- CMD.cmd('bgzip', :in => stream, :pipe => true, :no_fail => true, :no_wait => true)
512
- end
513
-
514
- def self.unzip(stream)
515
- TmpFile.with_file(stream.read) do |filename|
516
- StringIO.new(CMD.cmd("unzip '{opt}' #{filename}", "-p" => true, :pipe => true).read)
517
- end
518
- end
519
-
520
- # Questions
521
-
522
- def self.remote?(file)
523
- !! (file =~ /^(?:https?|ftp|ssh):\/\//)
524
- end
525
-
526
- def self.ssh?(file)
527
- !! (file =~ /^ssh:\/\//)
528
- end
529
-
530
- def self.gzip?(file)
531
- file = file.find if Path === file
532
- file = file.filename if File === file
533
- return false unless String === file
534
- !! (file =~ /\.gz$/)
535
- end
536
-
537
- def self.bgzip?(file)
538
- file = file.find if Path === file
539
- file = file.filename if File === file
540
- return false unless String === file
541
- !! (file =~ /\.bgz$/)
542
- end
543
-
544
- def self.zip?(file)
545
- file = file.find if Path === file
546
- file = file.filename if File === file
547
- return false unless String === file
548
- !! (file =~ /\.zip$/)
549
- end
550
-
551
-
552
- # Open Read Write
553
-
554
- def self.clean_cache(url, options = {})
555
- options = Misc.add_defaults options, :noz => false, :mode => 'r'
556
-
557
- wget_options = options[:wget_options] || {}
558
- wget_options[:nice] = options.delete(:nice)
559
- wget_options[:nice_key] = options.delete(:nice_key)
560
- wget_options[:quiet] = options.delete(:quiet)
561
- wget_options["--post-data="] = options.delete(:post) if options.include? :post
562
- wget_options["--post-file"] = options.delete("--post-file") if options.include? "--post-file"
563
- wget_options["--post-file="] = options.delete("--post-file=") if options.include? "--post-file="
564
- wget_options[:cookies] = options.delete(:cookies)
565
-
566
- cache_file = in_cache(url, wget_options)
567
- Misc.lock(cache_file) do
568
- FileUtils.rm(cache_file)
569
- end if cache_file
570
- end
571
-
572
- def self.open(url, options = {})
573
- if IO === url
574
- if block_given?
575
- res = yield url
576
- url.close
577
- return res
578
- else
579
- return url
580
- end
581
- end
582
- options = Misc.add_defaults options, :noz => false, :mode => 'r'
583
-
584
- mode = Misc.process_options options, :mode
585
-
586
- options[:noz] = true if mode.include? "w"
587
-
588
- wget_options = options[:wget_options] || {}
589
- wget_options[:nice] = options.delete(:nice)
590
- wget_options[:nice_key] = options.delete(:nice_key)
591
- wget_options[:quiet] = options.delete(:quiet)
592
- wget_options["--post-data="] = options.delete(:post) if options.include? :post
593
- wget_options["--post-file"] = options.delete("--post-file") if options.include? "--post-file"
594
- wget_options["--post-file="] = options.delete("--post-file=") if options.include? "--post-file="
595
- wget_options[:cookies] = options.delete(:cookies)
596
-
597
- io = case
598
- when (IO === url or StringIO === url)
599
- url
600
- when (not remote?(url) and not ssh?(url))
601
- file_open(url, options[:grep], mode, options[:invert_grep])
602
- when (options[:nocache] and options[:nocache] != :update)
603
- # What about grep?
604
- if ssh?(url)
605
- ssh_open(url)
606
- else
607
- wget(url, wget_options)
608
- end
609
- when (options[:nocache] != :update and in_cache(url, wget_options))
610
- file_open(in_cache(url, wget_options), options[:grep], mode, options[:invert_grep])
611
- else
612
- io = if ssh?(url)
613
- ssh_open(url)
614
- else
615
- wget(url, wget_options)
616
- end
617
- add_cache(url, io, wget_options)
618
- file_open(in_cache(url, wget_options), options[:grep], mode, options[:invert_grep])
619
- end
620
- io = unzip(io) if ((String === url and zip?(url)) and not options[:noz]) or options[:zip]
621
- io = gunzip(io) if ((String === url and gzip?(url)) and not options[:noz]) or options[:gzip]
622
- io = bgunzip(io) if ((String === url and bgzip?(url)) and not options[:noz]) or options[:bgzip]
623
-
624
- class << io;
625
- attr_accessor :filename
626
- end
627
-
628
- io.filename = url.to_s
629
-
630
- if block_given?
631
- begin
632
- return yield(io)
633
- rescue DontClose
634
- rescue Exception
635
- io.abort if io.respond_to? :abort
636
- io.join if io.respond_to? :join
637
- raise $!
638
- ensure
639
- io.close if io.respond_to? :close and not io.closed?
640
- io.join if io.respond_to? :join
641
- end
642
- end
643
-
644
- io
645
- end
646
-
647
- def self.download_old(url, file)
648
- Open.open(url, :mode => 'rb', :noz => true) do |sin|
649
- Open.open(file, :mode => 'wb') do |sout|
650
- Misc.consume_stream(sin, false, sout)
651
- end
652
- end
653
- end
654
-
655
- def self.download(url, path)
656
- begin
657
- Open.wget(url, "--output-document" => path, :pipe => false)
658
- rescue Exception
659
- Open.rm(path) if Open.exist?(path)
660
- raise $!
661
- end
662
- end
663
-
664
- def self.can_open?(file)
665
- String === file and (Open.exist?(file) or remote?(file))
666
- end
667
-
668
- def self.read(file, options = {}, &block)
669
- open(file, options) do |f|
670
- if block_given?
671
- res = []
672
- while not f.eof?
673
- l = f.gets
674
- l = Misc.fixutf8(l) unless options[:nofix]
675
- res << yield(l)
676
- end
677
- res
678
- else
679
- if options[:nofix]
680
- f.read
681
- else
682
- Misc.fixutf8(f.read)
683
- end
684
- end
685
- end
686
- end
687
-
688
- def self.notify_write(file)
689
- begin
690
- notification_file = file + '.notify'
691
- if Open.exists? notification_file
692
- key = Open.read(notification_file).strip
693
- key = nil if key.empty?
694
- if key && key.include?("@")
695
- to = from = key
696
- subject = "Wrote " << file
697
- message = "Content attached"
698
- Misc.send_email(from, to, subject, message, :files => [file])
699
- else
700
- Misc.notify("Wrote " << file, nil, key)
701
- end
702
- Open.rm notification_file
703
- end
704
- rescue
705
- Log.exception $!
706
- Log.warn "Error notifying write of #{ file }"
707
- end
708
- end
709
-
710
- def self.write(file, content = nil, options = {})
711
- options = Misc.add_defaults options, :mode => 'w'
712
-
713
- file = file.find(options[:where]) if Path === file
714
- mode = Misc.process_options options, :mode
715
-
716
- if (dir_sub_path = find_repo_dir(file))
717
- content = case content
718
- when String
719
- content
720
- when nil
721
- if block_given?
722
- yield
723
- else
724
- ""
725
- end
726
- else
727
- content.read
728
- end
729
- dir_sub_path.push content
730
- save_content_in_repo(*dir_sub_path)
731
- else
732
- FileUtils.mkdir_p File.dirname(file) unless File.directory?(file)
733
- case
734
- when block_given?
735
- begin
736
- f = File.open(file, mode)
737
- begin
738
- yield f
739
- ensure
740
- f.close unless f.closed?
741
- end
742
- rescue Exception
743
- FileUtils.rm file if File.exist? file
744
- raise $!
745
- end
746
- when content.nil?
747
- File.open(file, mode){|f| f.write "" }
748
- when String === content
749
- file_write(file, content, mode)
750
- else
751
- begin
752
- File.open(file, mode) do |f|
753
- f.flock(File::LOCK_EX)
754
- while block = content.read(Misc::BLOCK_SIZE)
755
- f.write block
756
- end
757
- f.flock(File::LOCK_UN)
758
- end
759
- rescue Exception
760
- FileUtils.rm_rf file if File.exist? file
761
- raise $!
762
- end
763
- content.close
764
- content.join if content.respond_to? :join
765
- end
766
- end
767
-
768
- notify_write(file)
769
- end
770
-
771
- def self.writable?(path)
772
- path = path.find if Path === path
773
- if (dir_sub_path = find_repo_dir(path))
774
- writable_repo?(*dir_sub_path)
775
- else
776
- if File.symlink?(path)
777
- File.writable?(File.dirname(path))
778
- elsif File.exist?(path)
779
- File.writable?(path)
780
- else
781
- File.writable?(File.dirname(File.expand_path(path)))
782
- end
783
- end
784
- end
785
-
786
- def self.ctime(file)
787
- if (dir_sub_path = find_repo_dir(file))
788
- get_time_from_repo(*dir_sub_path)
789
- else
790
- file = file.find if Path === file
791
- File.ctime(file)
792
- end
793
- end
794
-
795
- def self.realpath(file)
796
- file = file.find if Path === file
797
- Pathname.new(File.expand_path(file)).realpath.to_s
798
- end
799
-
800
- def self.mtime(file)
801
- if (dir_sub_path = find_repo_dir(file))
802
- get_time_from_repo(*dir_sub_path)
803
- else
804
- file = file.find if Path === file
805
- begin
806
- if File.symlink?(file) || File.stat(file).nlink > 1
807
- if File.exist?(file + '.info') && defined?(Step)
808
- done = Step::INFO_SERIALIZER.load(Open.open(file + '.info'))[:done]
809
- return done if done
810
- end
811
-
812
- file = Pathname.new(file).realpath.to_s
813
- end
814
- return nil unless File.exist?(file)
815
- File.mtime(file)
816
- rescue
817
- nil
818
- end
819
- end
820
- end
821
-
822
- def self.update_mtime(path, target)
823
- if File.symlink?(target) || File.stat(target).nlink > 1
824
- if File.exist?(target + '.info')
825
- target = target + '.info'
826
- else
827
- target = Pathname.new(target).realpath.to_s
828
- end
829
- end
830
-
831
- CMD.cmd("touch -r '#{path}' '#{target}'")
832
- CMD.cmd("touch -r '#{path}.info' '#{target}'") if File.exist?(path + '.info')
833
- end
834
-
835
- def self.atime(file)
836
- if (dir_sub_path = find_repo_dir(file))
837
- get_atime_from_repo(*dir_sub_path)
838
- else
839
- file = file.find if Path === file
840
- File.atime(file)
841
- end
842
- end
843
-
844
- def self.touch(file)
845
- if (dir_sub_path = find_repo_dir(file))
846
- set_time_from_repo(*dir_sub_path)
847
- else
848
- file = file.find if Path === file
849
- FileUtils.touch(file)
850
- end
851
- end
852
-
853
- def self.broken_link?(path)
854
- File.symlink?(path) && ! File.exist?(File.readlink(path))
855
- end
856
-
857
- end
1
+ require_relative '../refactor'
2
+ Rbbt.require_instead 'scout/open'
3
+ require_relative 'open/refactor'