Module: Open

Defined in:
lib/rbbt/util/open.rb

Defined Under Namespace

Classes: OpenGzipError, OpenURLError

Constant Summary collapse

REMOTE_CACHEDIR =
File.join(ENV["HOME"], "/tmp/open_cache")
LAST_TIME =

Remote WGET

{}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.repository_dirsObject

Returns the value of attribute repository_dirs.



17
18
19
# File 'lib/rbbt/util/open.rb', line 17

def repository_dirs
  @repository_dirs
end

Class Method Details

.add_cache(url, data, options = {}) ⇒ Object



122
123
124
125
# File 'lib/rbbt/util/open.rb', line 122

def self.add_cache(url, data, options = {})
  file = File.join(REMOTE_CACHEDIR, digest_url(url, options))
  Misc.sensiblewrite(file, data, :force => true)
end

.atime(file) ⇒ Object



723
724
725
726
727
728
729
730
# File 'lib/rbbt/util/open.rb', line 723

def self.atime(file)
  if (dir_sub_path = find_repo_dir(file))
    get_atime_from_repo(*dir_sub_path)
  else
    file = file.find if Path === file
    File.atime(file)
  end
end

.bgunzip(stream) ⇒ Object

Decompression



449
450
451
# File 'lib/rbbt/util/open.rb', line 449

def self.bgunzip(stream)
  Bgzf.setup stream
end

.bgzip(stream) ⇒ Object



461
462
463
# File 'lib/rbbt/util/open.rb', line 461

def self.bgzip(stream)
  CMD.cmd('bgzip', :in => stream, :pipe => true, :no_fail => true, :no_wait => true)
end

.bgzip?(file) ⇒ Boolean

Returns:

  • (Boolean)


482
483
484
485
# File 'lib/rbbt/util/open.rb', line 482

def self.bgzip?(file)
  file = file.find if Path === file
  !! (file =~ /\.bgz$/)
end

.cachedirObject



36
37
38
# File 'lib/rbbt/util/open.rb', line 36

def self.cachedir
  REMOTE_CACHEDIR
end

.cachedir=(cachedir) ⇒ Object



31
32
33
34
# File 'lib/rbbt/util/open.rb', line 31

def self.cachedir=(cachedir)
  REMOTE_CACHEDIR.replace cachedir
  FileUtils.mkdir_p REMOTE_CACHEDIR unless File.exist? REMOTE_CACHEDIR
end

.can_open?(file) ⇒ Boolean

Returns:

  • (Boolean)


588
589
590
# File 'lib/rbbt/util/open.rb', line 588

def self.can_open?(file)
  String === file and (Open.exist?(file) or remote?(file))
end

.clean_cache(url, options = {}) ⇒ Object

Open Read Write



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/rbbt/util/open.rb', line 495

def self.clean_cache(url, options = {})
  options = Misc.add_defaults options, :noz => false, :mode => 'r'

  wget_options = options[:wget_options] || {}
  wget_options[:nice] = options.delete(:nice)
  wget_options[:nice_key] = options.delete(:nice_key)
  wget_options[:quiet] = options.delete(:quiet)
  wget_options["--post-data="] = options.delete(:post) if options.include? :post
  wget_options["--post-file"] = options.delete("--post-file") if options.include? "--post-file"
  wget_options["--post-file="] = options.delete("--post-file=") if options.include? "--post-file="
  wget_options[:cookies] = options.delete(:cookies)

  cache_file = in_cache(url, wget_options)
  Misc.lock(cache_file) do
    FileUtils.rm(cache_file)
  end if cache_file
end

.clear_dir_reposObject



141
142
143
# File 'lib/rbbt/util/open.rb', line 141

def self.clear_dir_repos
  @@repos.clear if defined? @@repos and @@repos
end

.cp(source, target, options = {}) ⇒ Object

FileUtils.mkdir_p File.dirname(target) unless File.exists?(File.dirname(target))

FileUtils.rm target if File.exists?(target)
FileUtils.cp source, target

end



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rbbt/util/open.rb', line 354

def self.cp(source, target, options = {})
  dir_sub_path_source = find_repo_dir(source)
  dir_sub_path_target = find_repo_dir(target)

  if dir_sub_path_source.nil? and dir_sub_path_target.nil?
    FileUtils.mkdir_p File.dirname(target) unless File.exist? File.dirname(target)
    tmp_target = File.join(File.dirname(target), '.tmp_mv.' + File.basename(target))
    FileUtils.cp source, tmp_target
    FileUtils.cp tmp_target, target
    return
  end

  if dir_sub_path_source.nil?
    save_content_in_repo(dir_sub_path_target[0], dir_sub_path_target[1], Open.read(source, :mode => 'rb', :nofix => true))
    return nil
  end

  if dir_sub_path_target.nil?
    Open.write(target, get_stream_from_repo(dir_sub_path_source))
    return nil
  end

  repo_source = get_repo_from_dir(dir_sub_path_source[0])
  repo_target = get_repo_from_dir(dir_sub_path_target[0])

  repo_source.read_and_close do
    repo_target.write_and_close do
      repo_source[dir_sub_path_source[1]] = repo_target[dir_sub_path_target[1]]
    end
  end

  return nil
end

.ctime(file) ⇒ Object



700
701
702
703
704
705
706
707
# File 'lib/rbbt/util/open.rb', line 700

def self.ctime(file)
  if (dir_sub_path = find_repo_dir(file))
    get_time_from_repo(*dir_sub_path)
  else
    file = file.find if Path === file
    File.ctime(file)
  end
end

.digest_url(url, options = {}) ⇒ Object



96
97
98
99
# File 'lib/rbbt/util/open.rb', line 96

def self.digest_url(url, options = {})
  params = [url, options.values_at("--post-data", "--post-data="), (options.include?("--post-file")? Open.read(options["--post-file"]).split("\n").sort * "\n" : "")]
  digest = Misc.digest(params.inspect)
end

.download(url, file) ⇒ Object



580
581
582
583
584
585
586
# File 'lib/rbbt/util/open.rb', line 580

def self.download(url, file)
  Open.open(url, :mode => 'rb', :noz => true) do |sin|
    Open.open(file, :mode => 'wb') do |sout|
      Misc.consume_stream(sin, false, sout)
    end
  end
end

.exists?(file) ⇒ Boolean Also known as: exist?

Returns:

  • (Boolean)


423
424
425
426
427
428
429
430
431
# File 'lib/rbbt/util/open.rb', line 423

def self.exists?(file)
  if (dir_sub_path = find_repo_dir(file))
    dir_sub_path.push file
    exists_in_repo(*dir_sub_path)
  else
    file = file.find if Path === file
    File.exist?(file) #|| File.symlink?(file)
  end
end

.exists_in_repo(dir, sub_path, content) ⇒ Object



210
211
212
213
214
215
# File 'lib/rbbt/util/open.rb', line 210

def self.exists_in_repo(dir, sub_path, content)
  repo = get_repo_from_dir(dir)
  repo.read_and_close do
    repo.include?(sub_path) && ! repo[sub_path].nil?
  end
end

.file_open(file, grep, mode = 'r', invert_grep = false) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rbbt/util/open.rb', line 252

def self.file_open(file, grep, mode = 'r', invert_grep = false)
  if (dir_sub_path = find_repo_dir(file))
    if mode.include? 'w'

      stream = StringIO.new
      class << stream
        attr_accessor :dir_sub_path
        def close
          self.rewind
          Open.save_content_in_repo(*dir_sub_path, self.read)
        end
      end
      stream.dir_sub_path = dir_sub_path

    else
      stream = get_stream_from_repo(*dir_sub_path)
    end
  else
    file = file.find if Path === file
    stream =  File.open(file, mode)
  end

  if grep
    grep(stream, grep, invert_grep)
  else
    stream
  end
end

.file_write(file, content, mode = 'w') ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rbbt/util/open.rb', line 281

def self.file_write(file, content, mode = 'w')
  if (dir_sub_path = find_repo_dir(file))
    dir_sub_path.push content
    save_content_in_repo(*dir_sub_path)
  else
    File.open(file, mode) do |f|
      begin
        f.flock(File::LOCK_EX)
        f.write content 
        f.flock(File::LOCK_UN)
      ensure
        f.close unless f.closed?
      end
    end
  end
end

.find_repo_dir(file) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rbbt/util/open.rb', line 217

def self.find_repo_dir(file)
  self.repository_dirs.each do |dir|
    dir = dir + '/' unless dir[-1] == "/"

    begin
      if file.start_with? dir
        sub_path = file.to_s[dir.length..-1]
        return [dir, sub_path]
      else 
        if Path === file and (ffile = file.find).start_with? dir
          sub_path = ffile.to_s[dir.length..-1]
          return [dir, sub_path]
        end
      end
    end
  end
  nil
end

.get_atime_from_repo(dir, sub_path) ⇒ Object



168
169
170
171
# File 'lib/rbbt/util/open.rb', line 168

def self.get_atime_from_repo(dir, sub_path)
  repo = get_repo_from_dir(dir)
  File.atime(repo.persistance_path)
end

.get_repo_from_dir(dir) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/rbbt/util/open.rb', line 144

def self.get_repo_from_dir(dir)
  @@repos ||= {}
  @@repos[dir] ||= begin
                    repo_path = File.join(dir, '.file_repo')
                    Persist.open_tokyocabinet(repo_path, false, :clean, TokyoCabinet::BDB )
                  end
end

.get_stream_from_repo(dir, sub_path) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/rbbt/util/open.rb', line 152

def self.get_stream_from_repo(dir, sub_path)
  repo = get_repo_from_dir(dir)
  repo.read_and_close do
    content = repo[sub_path]
    content.nil? ? nil : StringIO.new(content).tap{|o| o.binmode }
  end
end

.get_time_from_repo(dir, sub_path) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/rbbt/util/open.rb', line 160

def self.get_time_from_repo(dir, sub_path)
  repo = get_repo_from_dir(dir)
  time = repo.read_and_close do
    Time.at(repo['.time.' + sub_path].to_i)
  end
  time
end

.grep(stream, grep, invert = false) ⇒ Object

Grep



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rbbt/util/open.rb', line 129

def self.grep(stream, grep, invert = false)
  grep_cmd = ENV["GREP_CMD"] || "/bin/grep"
  case 
  when Array === grep
    TmpFile.with_file(grep * "\n", false) do |f|
      CMD.cmd("#{grep_cmd} #{invert ? '-v' : ''} -", "-w" => true, "-F" => true, "-f" => f, :in => stream, :pipe => true, :post => proc{FileUtils.rm f})
    end
  else
    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)})
  end
end

.gunzip(stream) ⇒ Object



453
454
455
# File 'lib/rbbt/util/open.rb', line 453

def self.gunzip(stream)
  CMD.cmd('zcat', :in => stream, :pipe => true, :no_fail => true, :no_wait => true)
end

.gzip(stream) ⇒ Object



457
458
459
# File 'lib/rbbt/util/open.rb', line 457

def self.gzip(stream)
  CMD.cmd('gzip', :in => stream, :pipe => true, :no_fail => true, :no_wait => true)
end

.gzip?(file) ⇒ Boolean

Returns:

  • (Boolean)


477
478
479
480
# File 'lib/rbbt/util/open.rb', line 477

def self.gzip?(file)
  file = file.find if Path === file
  !! (file =~ /\.gz$/)
end

.in_cache(url, options = {}) ⇒ Object

Cache



102
103
104
105
106
107
108
109
# File 'lib/rbbt/util/open.rb', line 102

def self.in_cache(url, options = {})
  filename = File.join(REMOTE_CACHEDIR, digest_url(url, options))
  if File.exist? filename
    return filename 
  else
    nil
  end
end

.ln(source, target, options = {}) ⇒ Object



318
319
320
321
322
323
324
325
# File 'lib/rbbt/util/open.rb', line 318

def self.ln(source, target, options = {})
  source = source.find if Path === source
  target = target.find if Path === target

  FileUtils.mkdir_p File.dirname(target) unless File.exists?(File.dirname(target))
  FileUtils.rm target if File.exists?(target)
  FileUtils.ln source, target
end

.ln_h(source, target, options = {}) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/rbbt/util/open.rb', line 327

def self.ln_h(source, target, options = {})
  source = source.find if Path === source
  target = target.find if Path === target

  FileUtils.mkdir_p File.dirname(target) unless File.exists?(File.dirname(target))
  FileUtils.rm target if File.exists?(target)
  begin
    CMD.cmd("ln -L '#{ source }' '#{ target }'")
  rescue ProcessFailed
    if $!.message.include? "Invalid cross-device link"
      Log.debug "Could not hard link #{source} and #{target}: cross-device link"
      CMD.cmd("cp -L '#{ source }' '#{ target }'")
    else
      raise $!
    end
  end
end

.ln_s(source, target, options = {}) ⇒ Object



309
310
311
312
313
314
315
316
# File 'lib/rbbt/util/open.rb', line 309

def self.ln_s(source, target, options = {})
  source = source.find if Path === source
  target = target.find if Path === target

  FileUtils.mkdir_p File.dirname(target) unless File.exists?(File.dirname(target))
  FileUtils.rm target if File.exists?(target)
  FileUtils.ln_s source, target
end

.lock(file, options = {}, &block) ⇒ Object



436
437
438
439
440
441
442
443
444
# File 'lib/rbbt/util/open.rb', line 436

def self.lock(file, options = {}, &block)
  if file and (dir_sub_path = find_repo_dir(file))
    dir, sub_path = dir_sub_path
    repo = get_repo_from_dir(dir)
    Misc.lock_in_repo(repo, sub_path, &block)
  else
    Misc.lock(file, options, &block)
  end
end

.mkdir(target) ⇒ Object



298
299
300
301
302
303
304
305
306
307
# File 'lib/rbbt/util/open.rb', line 298

def self.mkdir(target)
  if (dir_sub_path = find_repo_dir(target))
    nil
  else
    target = target.find if Path === target
    if ! File.exists?(target)
      FileUtils.mkdir_p target
    end
  end
end

.mtime(file) ⇒ Object



709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/rbbt/util/open.rb', line 709

def self.mtime(file)
  if (dir_sub_path = find_repo_dir(file))
    get_time_from_repo(*dir_sub_path)
  else
    file = file.find if Path === file
    begin
      file = Pathname.new(file).realpath.to_s if File.symlink?(file)
      File.mtime(file)
    rescue
      nil
    end
  end
end

.mv(source, target, options = {}) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/rbbt/util/open.rb', line 388

def self.mv(source, target, options = {})
  dir_sub_path_source = find_repo_dir(source)
  dir_sub_path_target = find_repo_dir(target)

  if dir_sub_path_source.nil? and dir_sub_path_target.nil?
    FileUtils.mkdir_p File.dirname(target) unless File.exist? File.dirname(target)
    tmp_target = File.join(File.dirname(target), '.tmp_mv.' + File.basename(target))
    FileUtils.mv source, tmp_target
    FileUtils.mv tmp_target, target
    return
  end

  if dir_sub_path_source.nil?
    save_content_in_repo(dir_sub_path_target[0], dir_sub_path_target[1], Open.read(source, :mode => 'rb', :nofix => true))
    return nil
  end

  if dir_sub_path_target.nil?
    Open.write(target, get_stream_from_repo(dir_sub_path_source))
    return nil
  end

  repo_source = get_repo_from_dir(dir_sub_path_source[0])
  repo_target = get_repo_from_dir(dir_sub_path_target[0])

  repo_source.write_and_close do
    repo_target.write_and_close do
      repo_source[dir_sub_path_source[1]] = repo_target[dir_sub_path_target[1]]
    end
    repo_source.delete dir_sub_path_source[1]
  end

  return nil
end

.notify_write(file) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/rbbt/util/open.rb', line 612

def self.notify_write(file)
  begin
    notification_file = file + '.notify'
    if Open.exists? notification_file
      key = Open.read(notification_file).strip
      key = nil if key.empty?
      Misc.notify("Wrote " << file, nil, key)
      Open.rm notification_file
    end
  rescue
    Log.exception $!
    Log.warn "Error notifying write of #{ file }"
  end
end

.open(url, options = {}) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/rbbt/util/open.rb', line 513

def self.open(url, options = {})
  if IO === url
    if block_given?
      res = yield url 
      url.close
      return res
    else
      return url 
    end
  end
  options = Misc.add_defaults options, :noz => false, :mode => 'r'

  mode = Misc.process_options options, :mode

  options[:noz] = true if mode.include? "w"

  wget_options = options[:wget_options] || {}
  wget_options[:nice] = options.delete(:nice)
  wget_options[:nice_key] = options.delete(:nice_key)
  wget_options[:quiet] = options.delete(:quiet)
  wget_options["--post-data="] = options.delete(:post) if options.include? :post
  wget_options["--post-file"] = options.delete("--post-file") if options.include? "--post-file"
  wget_options["--post-file="] = options.delete("--post-file=") if options.include? "--post-file="
  wget_options[:cookies] = options.delete(:cookies)

  io = case
       when (IO === url or StringIO === url)
         url
       when (not remote?(url))
         file_open(url, options[:grep], mode, options[:invert_grep])
       when (options[:nocache] and options[:nocache] != :update)
         # What about grep?
         wget(url, wget_options)
       when (options[:nocache] != :update and in_cache(url, wget_options))
         file_open(in_cache(url, wget_options), options[:grep], mode, options[:invert_grep])
       else
         io = wget(url, wget_options)
         add_cache(url, io, wget_options)
         file_open(in_cache(url, wget_options), options[:grep], mode, options[:invert_grep])
       end
  io = unzip(io)  if ((String === url and zip?(url))  and not options[:noz]) or options[:zip]
  io = gunzip(io) if ((String === url and gzip?(url)) and not options[:noz]) or options[:gzip]
  io = bgunzip(io) if ((String === url and bgzip?(url)) and not options[:noz]) or options[:bgzip]

  class << io;
    attr_accessor :filename
  end

  io.filename = url.to_s

  if block_given?
    begin
      return yield(io)
    rescue DontClose
    rescue Exception
      io.abort if io.respond_to? :abort
      io.join if io.respond_to? :join
      raise $!
    ensure
      io.close if io.respond_to? :close and not io.closed?
      io.join if io.respond_to? :join
    end
  end

  io
end

.read(file, options = {}, &block) ⇒ Object



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/rbbt/util/open.rb', line 592

def self.read(file, options = {}, &block)
  open(file, options) do |f|
    if block_given?
      res = []
      while not f.eof?
        l = f.gets
        l = Misc.fixutf8(l) unless options[:nofix]
        res << yield(l)
      end
      res
    else
      if options[:nofix]
        f.read
      else
        Misc.fixutf8(f.read)
      end
    end
  end
end

.remote?(file) ⇒ Boolean

Questions

Returns:

  • (Boolean)


473
474
475
# File 'lib/rbbt/util/open.rb', line 473

def self.remote?(file)
  !! (file =~ /^(?:https?|ftp):\/\//)
end

.remove_from_cache(url, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/rbbt/util/open.rb', line 111

def self.remove_from_cache(url, options = {})
  digest = Misc.digest([url, options.values_at("--post-data", "--post-data="), (options.include?("--post-file")? Open.read(options["--post-file"]) : "")].inspect)

  filename = File.join(REMOTE_CACHEDIR, digest)
  if File.exist? filename
    FileUtils.rm filename 
  else
    nil
  end
end

.remove_from_repo(dir, sub_path, recursive = false) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/rbbt/util/open.rb', line 199

def self.remove_from_repo(dir, sub_path, recursive = false)
  repo = get_repo_from_dir(dir)
  repo.write_and_close do
    if recursive
      repo.outlist repo.range sub_path, true, sub_path.sub(/.$/,('\1'.ord + 1).chr), false
    else
      repo.outlist sub_path
    end
  end
end

.rm(file) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/rbbt/util/open.rb', line 236

def self.rm(file)
  if (dir_sub_path = find_repo_dir(file))
    remove_from_repo(*dir_sub_path)
  else
    FileUtils.rm(file) if File.exists?(file)
  end
end

.rm_rf(file) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/rbbt/util/open.rb', line 244

def self.rm_rf(file)
  if (dir_sub_path = find_repo_dir(file))
    remove_from_repo(dir_sub_path[0], dir_sub_path[1], true)
  else
    FileUtils.rm_rf(file)
  end
end

.save_content_in_repo(dir, sub_path, content) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/rbbt/util/open.rb', line 191

def self.save_content_in_repo(dir, sub_path, content)
  repo = get_repo_from_dir(dir)
  repo.write_and_close do
    repo['.time.' + sub_path] = Time.now.to_i.to_s
    repo[sub_path] = content
  end
end

.set_time_from_repo(dir, sub_path) ⇒ Object



184
185
186
187
188
189
# File 'lib/rbbt/util/open.rb', line 184

def self.set_time_from_repo(dir, sub_path)
  repo = get_repo_from_dir(dir)
  repo.read_and_close do
    repo['.time.' + sub_path] = Time.now.to_i.to_s
  end
end

.touch(file) ⇒ Object



732
733
734
735
736
737
738
739
# File 'lib/rbbt/util/open.rb', line 732

def self.touch(file)
  if (dir_sub_path = find_repo_dir(file))
    set_time_from_repo(*dir_sub_path)
  else
    file = file.find if Path === file
    FileUtils.touch(file)
  end
end

.unzip(stream) ⇒ Object



465
466
467
468
469
# File 'lib/rbbt/util/open.rb', line 465

def self.unzip(stream)
  TmpFile.with_file(stream.read) do |filename|
    StringIO.new(CMD.cmd("unzip '{opt}' #{filename}", "-p" => true, :pipe => true).read)
  end
end

.wait(lag, key = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/rbbt/util/open.rb', line 42

def self.wait(lag, key = nil)
  time = Time.now   

  if LAST_TIME[key] != nil && (time < LAST_TIME[key] + lag)
    sleep (LAST_TIME[key] + lag) - time
  end

  LAST_TIME[key] = Time.now   
end

.wget(url, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rbbt/util/open.rb', line 52

def self.wget(url, options = {})
  Log.low "WGET:\n -URL: #{ url }\n -OPTIONS: #{options.inspect}"
  options = Misc.add_defaults options, "--user-agent=" => 'rbbt', :pipe => true

  wait(options[:nice], options[:nice_key]) if options[:nice]
  options.delete(:nice)
  options.delete(:nice_key)

  pipe  = options.delete(:pipe)
  quiet = options.delete(:quiet)
  post  = options.delete(:post)
  cookies = options.delete(:cookies)

  options["--quiet"]     = quiet if options["--quiet"].nil?
  options["--post-data="] ||= post if post

  if cookies
    options["--save-cookies"] = cookies
    options["--load-cookies"] = cookies
    options["--keep-session-cookies"] = true
  end


  stderr = case
           when options['stderr']
             options['stderr'] 
           when options['--quiet']
             false
           else
             nil
           end

  begin
    wget_options = options.merge( '-O' => '-')
    wget_options[:pipe] = pipe unless pipe.nil?
    wget_options[:stderr] = stderr unless stderr.nil?

    CMD.cmd("wget '#{ url }'", wget_options)
  rescue
   STDERR.puts $!.backtrace.inspect
   raise OpenURLError, "Error reading remote url: #{ url }.\n#{$!.message}"
  end
end

.writable?(path) ⇒ Boolean

Returns:

  • (Boolean)


687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/rbbt/util/open.rb', line 687

def self.writable?(path)
  path = path.find if Path === path
  if (dir_sub_path = find_repo_dir(path))
    writable_repo?(*dir_sub_path)
  else
    if File.exists?(path)
      File.writable?(path)
    else
      File.writable?(File.dirname(File.expand_path(path)))
    end
  end
end

.writable_repo?(dir, sub_path) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
# File 'lib/rbbt/util/open.rb', line 173

def self.writable_repo?(dir, sub_path)
  repo = get_repo_from_dir(dir)
  begin
    repo.write_and_close do
    end
    true
  rescue
    false
  end
end

.write(file, content = nil, options = {}) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/rbbt/util/open.rb', line 627

def self.write(file, content = nil, options = {})
  options = Misc.add_defaults options, :mode => 'w'

  file = file.find(options[:where]) if Path === file
  mode = Misc.process_options options, :mode

  if (dir_sub_path = find_repo_dir(file))
    content = case content
              when String
                content
              when nil 
                if block_given?
                  yield
                else
                  ""
                end
              else
                content.read
              end
    dir_sub_path.push content
    save_content_in_repo(*dir_sub_path)
  else
    FileUtils.mkdir_p File.dirname(file)
    case
    when block_given?
      begin
        f = File.open(file, mode)
        begin
          yield f
        ensure
          f.close unless f.closed?
        end
      rescue Exception
        FileUtils.rm file if File.exist? file
        raise $!
      end
    when content.nil?
      File.open(file, mode){|f| f.write "" }
    when String === content
      file_write(file, content, mode)
    else
      begin
        File.open(file, mode) do |f| 
          f.flock(File::LOCK_EX)
          while block = content.read(Misc::BLOCK_SIZE)
            f.write block
          end
          f.flock(File::LOCK_UN)
        end
      rescue Exception
        FileUtils.rm_rf file if File.exist? file
        raise $!
      end
      content.close
    end
  end

  notify_write(file) 
end

.zip?(file) ⇒ Boolean

Returns:

  • (Boolean)


487
488
489
490
# File 'lib/rbbt/util/open.rb', line 487

def self.zip?(file)
  file = file.find if Path === file
  !! (file =~ /\.zip$/)
end