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")
- GREP_CMD =
FileUtils.mkdir_p REMOTE_CACHEDIR unless File.exist? REMOTE_CACHEDIR
begin if ENV["GREP_CMD"] ENV["GREP_CMD"] elsif File.exist?('/bin/grep') "/bin/grep" elsif File.exist?('/usr/bin/grep') "/usr/bin/grep" else "grep" end end
- LAST_TIME =
Remote WGET
{}
Class Attribute Summary collapse
-
.repository_dirs ⇒ Object
Returns the value of attribute repository_dirs.
Class Method Summary collapse
- .add_cache(url, data, options = {}) ⇒ Object
- .atime(file) ⇒ Object
-
.bgunzip(stream) ⇒ Object
Decompression.
- .bgzip(stream) ⇒ Object
- .bgzip?(file) ⇒ Boolean
- .broken_link?(path) ⇒ Boolean
- .cachedir ⇒ Object
- .cachedir=(cachedir) ⇒ Object
- .can_open?(file) ⇒ Boolean
-
.clean_cache(url, options = {}) ⇒ Object
Open Read Write.
- .clear_dir_repos ⇒ Object
-
.cp(source, target, options = {}) ⇒ Object
FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target)) FileUtils.rm target if File.exist?(target) FileUtils.cp source, target end.
- .ctime(file) ⇒ Object
- .digest_url(url, options = {}) ⇒ Object
- .download(url, path) ⇒ Object
- .download_old(url, file) ⇒ Object
- .exists?(file) ⇒ Boolean (also: exist?)
- .exists_in_repo(dir, sub_path, content) ⇒ Object
- .file_open(file, grep, mode = 'r', invert_grep = false) ⇒ Object
- .file_write(file, content, mode = 'w') ⇒ Object
- .find_repo_dir(file) ⇒ Object
- .get_atime_from_repo(dir, sub_path) ⇒ Object
- .get_repo_from_dir(dir) ⇒ Object
- .get_stream_from_repo(dir, sub_path) ⇒ Object
- .get_time_from_repo(dir, sub_path) ⇒ Object
-
.grep(stream, grep, invert = false, fixed = nil) ⇒ Object
Grep.
- .gunzip(stream) ⇒ Object
- .gzip(stream) ⇒ Object
- .gzip?(file) ⇒ Boolean
-
.in_cache(url, options = {}) ⇒ Object
Cache.
- .link(source, target, options = {}) ⇒ Object
- .list_repo_files(dir, sub_path = nil) ⇒ Object
- .ln(source, target, options = {}) ⇒ Object
- .ln_h(source, target, options = {}) ⇒ Object
- .ln_s(source, target, options = {}) ⇒ Object
- .lock(file, options = {}, &block) ⇒ Object
- .mkdir(target) ⇒ Object
- .mtime(file) ⇒ Object
- .mv(source, target, options = {}) ⇒ Object
- .notify_write(file) ⇒ Object
- .open(url, options = {}) ⇒ Object
- .read(file, options = {}, &block) ⇒ Object
- .realpath(file) ⇒ Object
-
.remote?(file) ⇒ Boolean
Questions.
- .remove_from_cache(url, options = {}) ⇒ Object
- .remove_from_repo(dir, sub_path, recursive = false) ⇒ Object
- .rm(file) ⇒ Object
- .rm_rf(file) ⇒ Object
- .save_content_in_repo(dir, sub_path, content) ⇒ Object
- .set_time_from_repo(dir, sub_path) ⇒ Object
- .ssh?(file) ⇒ Boolean
- .ssh_open(file) ⇒ Object
- .touch(file) ⇒ Object
- .unzip(stream) ⇒ Object
- .update_mtime(path, target) ⇒ Object
- .wait(lag, key = nil) ⇒ Object
- .wget(url, options = {}) ⇒ Object
- .writable?(path) ⇒ Boolean
- .writable_repo?(dir, sub_path) ⇒ Boolean
- .write(file, content = nil, options = {}) ⇒ Object
- .zip?(file) ⇒ Boolean
Class Attribute Details
.repository_dirs ⇒ Object
Returns the value of attribute repository_dirs.
29 30 31 |
# File 'lib/rbbt/util/open.rb', line 29 def repository_dirs @repository_dirs end |
Class Method Details
.add_cache(url, data, options = {}) ⇒ Object
134 135 136 137 |
# File 'lib/rbbt/util/open.rb', line 134 def self.add_cache(url, data, = {}) file = File.join(REMOTE_CACHEDIR, digest_url(url, )) Misc.sensiblewrite(file, data, :force => true) end |
.atime(file) ⇒ Object
835 836 837 838 839 840 841 842 |
# File 'lib/rbbt/util/open.rb', line 835 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
498 499 500 |
# File 'lib/rbbt/util/open.rb', line 498 def self.bgunzip(stream) Bgzf.setup stream end |
.bgzip(stream) ⇒ Object
510 511 512 |
# File 'lib/rbbt/util/open.rb', line 510 def self.bgzip(stream) CMD.cmd('bgzip', :in => stream, :pipe => true, :no_fail => true, :no_wait => true) end |
.bgzip?(file) ⇒ Boolean
537 538 539 540 541 542 |
# File 'lib/rbbt/util/open.rb', line 537 def self.bgzip?(file) file = file.find if Path === file file = file.filename if File === file return false unless String === file !! (file =~ /\.bgz$/) end |
.broken_link?(path) ⇒ Boolean
853 854 855 |
# File 'lib/rbbt/util/open.rb', line 853 def self.broken_link?(path) File.symlink?(path) && ! File.exist?(File.readlink(path)) end |
.cachedir ⇒ Object
48 49 50 |
# File 'lib/rbbt/util/open.rb', line 48 def self.cachedir REMOTE_CACHEDIR end |
.cachedir=(cachedir) ⇒ Object
43 44 45 46 |
# File 'lib/rbbt/util/open.rb', line 43 def self.cachedir=(cachedir) REMOTE_CACHEDIR.replace cachedir FileUtils.mkdir_p REMOTE_CACHEDIR unless File.exist? REMOTE_CACHEDIR end |
.can_open?(file) ⇒ Boolean
664 665 666 |
# File 'lib/rbbt/util/open.rb', line 664 def self.can_open?(file) String === file and (Open.exist?(file) or remote?(file)) end |
.clean_cache(url, options = {}) ⇒ Object
Open Read Write
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
# File 'lib/rbbt/util/open.rb', line 554 def self.clean_cache(url, = {}) = Misc.add_defaults , :noz => false, :mode => 'r' = [:wget_options] || {} [:nice] = .delete(:nice) [:nice_key] = .delete(:nice_key) [:quiet] = .delete(:quiet) ["--post-data="] = .delete(:post) if .include? :post ["--post-file"] = .delete("--post-file") if .include? "--post-file" ["--post-file="] = .delete("--post-file=") if .include? "--post-file=" [:cookies] = .delete(:cookies) cache_file = in_cache(url, ) Misc.lock(cache_file) do FileUtils.rm(cache_file) end if cache_file end |
.clear_dir_repos ⇒ Object
156 157 158 |
# File 'lib/rbbt/util/open.rb', line 156 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.exist?(File.dirname(target))
FileUtils.rm target if File.exist?(target)
FileUtils.cp source, target
end
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 422 423 424 425 426 427 428 429 |
# File 'lib/rbbt/util/open.rb', line 395 def self.cp(source, target, = {}) 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_r 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]) content = repo_source.read_and_close do repo_source[dir_sub_path_source[1]] end repo_target.write_and_close do repo_target[dir_sub_path_target[1]] = content end return nil end |
.ctime(file) ⇒ Object
786 787 788 789 790 791 792 793 |
# File 'lib/rbbt/util/open.rb', line 786 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
108 109 110 111 |
# File 'lib/rbbt/util/open.rb', line 108 def self.digest_url(url, = {}) params = [url, .values_at("--post-data", "--post-data="), (.include?("--post-file")? Open.read(["--post-file"]).split("\n").sort * "\n" : "")] digest = Misc.digest(params.inspect) end |
.download(url, path) ⇒ Object
655 656 657 658 659 660 661 662 |
# File 'lib/rbbt/util/open.rb', line 655 def self.download(url, path) begin Open.wget(url, "--output-document" => path, :pipe => false) rescue Exception Open.rm(path) if Open.exist?(path) raise $! end end |
.download_old(url, file) ⇒ Object
647 648 649 650 651 652 653 |
# File 'lib/rbbt/util/open.rb', line 647 def self.download_old(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?
471 472 473 474 475 476 477 478 479 |
# File 'lib/rbbt/util/open.rb', line 471 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
234 235 236 237 238 239 |
# File 'lib/rbbt/util/open.rb', line 234 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
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/rbbt/util/open.rb', line 276 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 Open.mkdir File.dirname(file) if mode.include? 'w' 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
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/rbbt/util/open.rb', line 313 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
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/rbbt/util/open.rb', line 241 def self.find_repo_dir(file) self.repository_dirs.each do |dir| dir = dir + '/' unless dir.chars[-1] == "/" begin if file.start_with?(dir) || file == dir[0..-2] 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
183 184 185 186 |
# File 'lib/rbbt/util/open.rb', line 183 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
159 160 161 162 163 164 165 |
# File 'lib/rbbt/util/open.rb', line 159 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
167 168 169 170 171 172 173 |
# File 'lib/rbbt/util/open.rb', line 167 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
175 176 177 178 179 180 181 |
# File 'lib/rbbt/util/open.rb', line 175 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, fixed = nil) ⇒ Object
Grep
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rbbt/util/open.rb', line 141 def self.grep(stream, grep, invert = false, fixed = nil) case when Array === grep TmpFile.with_file(grep * "\n", false) do |f| if FalseClass === fixed CMD.cmd("#{GREP_CMD} #{invert ? '-v' : ''} -", "-f" => f, :in => stream, :pipe => true, :post => proc{FileUtils.rm f}) else CMD.cmd("#{GREP_CMD} #{invert ? '-v' : ''} -", "-w" => true, "-F" => true, "-f" => f, :in => stream, :pipe => true, :post => proc{FileUtils.rm f}) end 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
502 503 504 |
# File 'lib/rbbt/util/open.rb', line 502 def self.gunzip(stream) CMD.cmd('zcat', :in => stream, :pipe => true, :no_fail => true, :no_wait => true) end |
.gzip(stream) ⇒ Object
506 507 508 |
# File 'lib/rbbt/util/open.rb', line 506 def self.gzip(stream) CMD.cmd('gzip', :in => stream, :pipe => true, :no_fail => true, :no_wait => true) end |
.gzip?(file) ⇒ Boolean
530 531 532 533 534 535 |
# File 'lib/rbbt/util/open.rb', line 530 def self.gzip?(file) file = file.find if Path === file file = file.filename if File === file return false unless String === file !! (file =~ /\.gz$/) end |
.in_cache(url, options = {}) ⇒ Object
Cache
114 115 116 117 118 119 120 121 |
# File 'lib/rbbt/util/open.rb', line 114 def self.in_cache(url, = {}) filename = File.join(REMOTE_CACHEDIR, digest_url(url, )) if File.exist? filename return filename else nil end end |
.link(source, target, options = {}) ⇒ Object
377 378 379 380 381 382 383 384 |
# File 'lib/rbbt/util/open.rb', line 377 def self.link(source, target, = {}) begin Open.ln(source, target, ) rescue Open.ln_s(source, target, ) end nil end |
.list_repo_files(dir, sub_path = nil) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/rbbt/util/open.rb', line 225 def self.list_repo_files(dir, sub_path = nil) repo = get_repo_from_dir(dir) files = repo.keys files.reject!{|f| f[0] == "."} return files unless sub_path files.select{|file| file.start_with?(sub_path) } end |
.ln(source, target, options = {}) ⇒ Object
352 353 354 355 356 357 358 359 360 361 |
# File 'lib/rbbt/util/open.rb', line 352 def self.ln(source, target, = {}) source = source.find if Path === source target = target.find if Path === target source = File.realpath(source) if File.symlink?(source) FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target)) FileUtils.rm target if File.exist?(target) FileUtils.rm target if File.symlink?(target) FileUtils.ln source, target end |
.ln_h(source, target, options = {}) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/rbbt/util/open.rb', line 363 def self.ln_h(source, target, = {}) source = source.find if Path === source target = target.find if Path === target FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target)) FileUtils.rm target if File.exist?(target) begin CMD.cmd("ln -L '#{ source }' '#{ target }'") rescue ProcessFailed Log.debug "Could not hard link #{source} and #{target}: #{$!..gsub("\n", '. ')}" CMD.cmd("cp -L '#{ source }' '#{ target }'") end end |
.ln_s(source, target, options = {}) ⇒ Object
341 342 343 344 345 346 347 348 349 350 |
# File 'lib/rbbt/util/open.rb', line 341 def self.ln_s(source, target, = {}) source = source.find if Path === source target = target.find if Path === target target = File.join(target, File.basename(source)) if File.directory? target FileUtils.mkdir_p File.dirname(target) unless File.exist?(File.dirname(target)) FileUtils.rm target if File.exist?(target) FileUtils.rm target if File.symlink?(target) FileUtils.ln_s source, target end |
.lock(file, options = {}, &block) ⇒ Object
485 486 487 488 489 490 491 492 493 |
# File 'lib/rbbt/util/open.rb', line 485 def self.lock(file, = {}, &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, , &block) end end |
.mkdir(target) ⇒ Object
330 331 332 333 334 335 336 337 338 339 |
# File 'lib/rbbt/util/open.rb', line 330 def self.mkdir(target) if (dir_sub_path = find_repo_dir(target)) nil else target = target.find if Path === target if ! File.exist?(target) FileUtils.mkdir_p target end end end |
.mtime(file) ⇒ Object
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
# File 'lib/rbbt/util/open.rb', line 800 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 if File.symlink?(file) || File.stat(file).nlink > 1 if File.exist?(file + '.info') && defined?(Step) done = Step::INFO_SERIALIZER.load(Open.open(file + '.info'))[:done] return done if done end file = Pathname.new(file).realpath.to_s end return nil unless File.exist?(file) File.mtime(file) rescue nil end end end |
.mv(source, target, options = {}) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/rbbt/util/open.rb', line 431 def self.mv(source, target, = {}) 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 nil 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]) content = repo_source.read_and_close do repo_source[dir_sub_path_target[1]] end repo_target.write_and_close do repo_target[dir_sub_path_source[1]] = content end repo_source.write_and_close do repo_source.delete dir_sub_path_source[1] end return nil end |
.notify_write(file) ⇒ Object
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
# File 'lib/rbbt/util/open.rb', line 688 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? if key && key.include?("@") to = from = key subject = "Wrote " << file = "Content attached" Misc.send_email(from, to, subject, , :files => [file]) else Misc.notify("Wrote " << file, nil, key) end Open.rm notification_file end rescue Log.exception $! Log.warn "Error notifying write of #{ file }" end end |
.open(url, options = {}) ⇒ Object
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/rbbt/util/open.rb', line 572 def self.open(url, = {}) if IO === url if block_given? res = yield url url.close return res else return url end end = Misc.add_defaults , :noz => false, :mode => 'r' mode = Misc. , :mode [:noz] = true if mode.include? "w" = [:wget_options] || {} [:nice] = .delete(:nice) [:nice_key] = .delete(:nice_key) [:quiet] = .delete(:quiet) ["--post-data="] = .delete(:post) if .include? :post ["--post-file"] = .delete("--post-file") if .include? "--post-file" ["--post-file="] = .delete("--post-file=") if .include? "--post-file=" [:cookies] = .delete(:cookies) io = case when (IO === url or StringIO === url) url when (not remote?(url) and not ssh?(url)) file_open(url, [:grep], mode, [:invert_grep]) when ([:nocache] and [:nocache] != :update) # What about grep? if ssh?(url) ssh_open(url) else wget(url, ) end when ([:nocache] != :update and in_cache(url, )) file_open(in_cache(url, ), [:grep], mode, [:invert_grep]) else io = if ssh?(url) ssh_open(url) else wget(url, ) end add_cache(url, io, ) file_open(in_cache(url, ), [:grep], mode, [:invert_grep]) end io = unzip(io) if ((String === url and zip?(url)) and not [:noz]) or [:zip] io = gunzip(io) if ((String === url and gzip?(url)) and not [:noz]) or [:gzip] io = bgunzip(io) if ((String === url and bgzip?(url)) and not [:noz]) or [: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
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/rbbt/util/open.rb', line 668 def self.read(file, = {}, &block) open(file, ) do |f| if block_given? res = [] while not f.eof? l = f.gets l = Misc.fixutf8(l) unless [:nofix] res << yield(l) end res else if [:nofix] f.read else Misc.fixutf8(f.read) end end end end |
.realpath(file) ⇒ Object
795 796 797 798 |
# File 'lib/rbbt/util/open.rb', line 795 def self.realpath(file) file = file.find if Path === file Pathname.new(File.(file)).realpath.to_s end |
.remote?(file) ⇒ Boolean
Questions
522 523 524 |
# File 'lib/rbbt/util/open.rb', line 522 def self.remote?(file) !! (file =~ /^(?:https?|ftp|ssh):\/\//) end |
.remove_from_cache(url, options = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rbbt/util/open.rb', line 123 def self.remove_from_cache(url, = {}) digest = Misc.digest([url, .values_at("--post-data", "--post-data="), (.include?("--post-file")? Open.read(["--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
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/rbbt/util/open.rb', line 214 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
260 261 262 263 264 265 266 |
# File 'lib/rbbt/util/open.rb', line 260 def self.rm(file) if (dir_sub_path = find_repo_dir(file)) remove_from_repo(*dir_sub_path) else FileUtils.rm(file) if File.exist?(file) or Open.broken_link?(file) end end |
.rm_rf(file) ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/rbbt/util/open.rb', line 268 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
206 207 208 209 210 211 212 |
# File 'lib/rbbt/util/open.rb', line 206 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
199 200 201 202 203 204 |
# File 'lib/rbbt/util/open.rb', line 199 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 |
.ssh?(file) ⇒ Boolean
526 527 528 |
# File 'lib/rbbt/util/open.rb', line 526 def self.ssh?(file) !! (file =~ /^ssh:\/\//) end |
.ssh_open(file) ⇒ Object
306 307 308 309 310 311 |
# File 'lib/rbbt/util/open.rb', line 306 def self.ssh_open(file) m = file.match(/ssh:\/\/([^:]+):(.*)/) server = m[1] file = m[2] CMD.cmd("ssh '#{server}' cat '#{file}'", :pipe => true) end |
.touch(file) ⇒ Object
844 845 846 847 848 849 850 851 |
# File 'lib/rbbt/util/open.rb', line 844 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
514 515 516 517 518 |
# File 'lib/rbbt/util/open.rb', line 514 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 |
.update_mtime(path, target) ⇒ Object
822 823 824 825 826 827 828 829 830 831 832 833 |
# File 'lib/rbbt/util/open.rb', line 822 def self.update_mtime(path, target) if File.symlink?(target) || File.stat(target).nlink > 1 if File.exist?(target + '.info') target = target + '.info' else target = Pathname.new(target).realpath.to_s end end CMD.cmd("touch -r '#{path}' '#{target}'") CMD.cmd("touch -r '#{path}.info' '#{target}'") if File.exist?(path + '.info') end |
.wait(lag, key = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/rbbt/util/open.rb', line 54 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
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 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rbbt/util/open.rb', line 64 def self.wget(url, = {}) Log.low "WGET:\n -URL: #{ url }\n -OPTIONS: #{.inspect}" = Misc.add_defaults , "--user-agent=" => 'rbbt', :pipe => true wait([:nice], [:nice_key]) if [:nice] .delete(:nice) .delete(:nice_key) pipe = .delete(:pipe) quiet = .delete(:quiet) post = .delete(:post) = .delete(:cookies) ["--quiet"] = quiet if ["--quiet"].nil? ["--post-data="] ||= post if post if ["--save-cookies"] = ["--load-cookies"] = ["--keep-session-cookies"] = true end stderr = case when ['stderr'] ['stderr'] when ['--quiet'] false else nil end begin = .dup = .merge( '-O' => '-') unless .include?('--output-document') [:pipe] = pipe unless pipe.nil? [:stderr] = stderr unless stderr.nil? CMD.cmd("wget '#{ url }'", ) rescue raise OpenURLError, "Error reading remote url: #{ url }.\n#{$!.}" end end |
.writable?(path) ⇒ Boolean
771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
# File 'lib/rbbt/util/open.rb', line 771 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.symlink?(path) File.writable?(File.dirname(path)) elsif File.exist?(path) File.writable?(path) else File.writable?(File.dirname(File.(path))) end end end |
.writable_repo?(dir, sub_path) ⇒ Boolean
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rbbt/util/open.rb', line 188 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
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
# File 'lib/rbbt/util/open.rb', line 710 def self.write(file, content = nil, = {}) = Misc.add_defaults , :mode => 'w' file = file.find([:where]) if Path === file mode = Misc. , :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) unless File.directory?(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 content.join if content.respond_to? :join end end notify_write(file) end |