In Files
Parent
Methods
- ::[]
- ::new
- #/
- #==
- #absolute?
- #amass
- #append
- #apply_naming_policy
- #batch
- #batch_all
- #blockdev?
- #cd
- #chardev?
- #chdir
- #chmod
- #chmod_r
- #chown
- #chown_r
- #cmp
- #compare_file
- #copy
- #cp
- #cp_r
- #dir
- #dir_entries
- #directories
- #directory?
- #directory_entries
- #dirs
- #dryrun?
- #entries
- #eql?
- #executable?
- #executable_real?
- #exist?
- #exists?
- #file
- #file?
- #file_entries
- #files
- #fileutils
- #folders
- #glob
- #grpowned?
- #home
- #identical?
- #install
- #link
- #ln
- #ln_s
- #ln_sf
- #localize
- #locally
- #mkdir
- #mkdir_p
- #mkpath
- #move
- #multiglob
- #multiglob_r
- #mutex
- #mv
- #naming_policy
- #noop?
- #outofdate?
- #owned?
- #parent
- #parse_arguments
- #path
- #pathname
- #pathnames
- #pipe?
- #pwd
- #quiet?
- #read
- #readable?
- #readable_real?
- #relative?
- #remove
- #rm
- #rm_f
- #rm_r
- #rm_rf
- #rmdir
- #root
- #safe?
- #set_options
- #setgid?
- #setuid?
- #sh
- #size
- #size?
- #socket?
- #stage
- #sticky?
- #symlink
- #symlink?
- #system
- #to_s
- #touch
- #trace?
- #uptodate?
- #util_options
- #work
- #writable?
- #writable_real?
- #write
- #zero?
Information
Class Index
- Ratch
- Ratch::Batch
- Ratch::CLI
- Ratch::ConfigUtils
- Ratch::Console
- Ratch::EmailUtils
- Ratch::Emailer
- Ratch::FTPUtils
- Ratch::FileList
- Ratch::FileNotFound
- Ratch::Help
- Ratch::POMUtils
- Ratch::RDocUtils
- Ratch::Script
- Ratch::Shell
- Ratch::System
- Ratch::TarUtils
- Ratch::XDGUtils
- Ratch::ZlibUtils
- FileTest
- Hash
- NilClass
- Object
- String
Shell Prompt class
Ratch Shell object provides a limited file system shell in code. It is similar to having a shell prompt available to you in Ruby.
NOTE: We have used the term trace in place of verbose for command line options. Even though Ruby itself uses the term verbose with respect to FileUtils, the term is commonly used for command specific needs, so we want to leave it open for such cases.
Public Class Methods
# File lib/ratch/shell.rb, line 776 776: def self.[](path) 777: new(path) 778: end
New Shell object.
Shell.new(:noop=>true) Shell.new('..', :quiet=>true)
# File lib/ratch/shell.rb, line 28 28: def initialize(*args) 29: path, opts = parse_arguments(*args) 30: 31: opts.rekey!(&:to_sym) 32: 33: set_options(opts) 34: 35: if path.empty? 36: path = Dir.pwd 37: else 38: path = File.join(*path) 39: end 40: 41: raise FileNotFound, "#{path}" unless ::File.exist?(path) 42: raise FileNotFound, "#{path}" unless ::File.directory?(path) 43: 44: @_work = Pathname.new(path).expand_path 45: end
Public Instance Methods
Join paths. TODO: Should this return a new directory object? Or should it change directories?
# File lib/ratch/shell.rb, line 290 290: def /(path) 291: #@_work += dir # did not work, why? 292: @_work = dir(localize(path)) 293: self 294: end
Two Shell’s are equal if they have the same working path.
# File lib/ratch/shell.rb, line 98 98: def ==(other) 99: return false unless other.is_a?(self.class) 100: return false unless work == other.work 101: true 102: end
# File lib/ratch/shell.rb, line 414 414: def absolute?(path) ; FileTest.absolute?(path) ; end
An intergrated glob like method that takes a set of include globs, exclude globs and ignore globs to produce a collection of paths.
Ignore_globs differ from exclude_globs in that they match by the basename of the path rather than the whole pathname.
# File lib/ratch/shell.rb, line 615 615: def amass(include_globs, exclude_globs=[], ignore_globs=[]) 616: locally do 617: fileutils.amass(include_globs, exclude_globs, ignore_globs) 618: end 619: end
Append to file.
# File lib/ratch/shell.rb, line 379 379: def append(path, text) 380: $stderr.puts "append #{path}" if trace? 381: File.open(localize(path), 'a'){ |f| f << text } unless noop? 382: end
# File lib/ratch/shell.rb, line 735 735: def apply_naming_policy(name, ext) 736: naming_policy.each do |policy| 737: case policy.to_s 738: when /^low/, /^down/ 739: name = name.downcase 740: when /^up/ 741: name = name.upcase 742: when /^cap/ 743: name = name.capitalize 744: when /^ext/ 745: name = name + ".#{ext}" 746: end 747: end 748: name 749: end
Returns a Batch of file patterns.
# File lib/ratch/shell.rb, line 172 172: def batch(*patterns) 173: Batch.new patterns.map{|pattern| localize(pattern)} 174: end
Returns a Batch of file patterns, without any exclusions.
# File lib/ratch/shell.rb, line 177 177: def batch_all(*patterns) 178: Batch.all patterns.map{|pattern| localize(pattern)} 179: end
# File lib/ratch/shell.rb, line 402 402: def blockdev?(path) ; FileTest.blockdev?(localize(path)) ; end
Change working directory.
TODO: Make thread safe.
# File lib/ratch/shell.rb, line 339 339: def cd(path, &block) 340: if block 341: work_old = @_work 342: begin 343: @_work = dir(localize(path)) 344: locally(&block) 345: #mutex.synchronize do 346: # Dir.chdir(@_work){ block.call } 347: #end 348: ensure 349: @_work = work_old 350: end 351: else 352: @_work = dir(localize(path)) 353: end 354: end
# File lib/ratch/shell.rb, line 395 395: def chardev?(path) ; FileTest.chardev?(localize(path)) ; end
# File lib/ratch/shell.rb, line 570 570: def chmod(mode, list, options={}) 571: list = localize(list) 572: fileutils.chmod(mode, list, options) 573: end
# File lib/ratch/shell.rb, line 575 575: def chmod_r(mode, list, options={}) 576: list = localize(list) 577: fileutils.chmod_r(mode, list, options) 578: end
alias_method :chmod_R, :chmod_r
# File lib/ratch/shell.rb, line 581 581: def chown(user, group, list, options={}) 582: list = localize(list) 583: fileutils.chown(user, group, list, options) 584: end
# File lib/ratch/shell.rb, line 586 586: def chown_r(user, group, list, options={}) 587: list = localize(list) 588: fileutils.chown_r(user, group, list, options) 589: end
Same as identical?
# File lib/ratch/shell.rb, line 475 475: def cmp(a,b) 476: fileutils.compare_file(a,b) 477: end
cp(list, dir, options={})
# File lib/ratch/shell.rb, line 521 521: def cp(src, dest, options={}) 522: src = localize(src) 523: dest = localize(dest) 524: fileutils.cp(src, dest, options) 525: end
cp_r(list, dir, options={})
# File lib/ratch/shell.rb, line 529 529: def cp_r(src, dest, options={}) 530: src = localize(src) 531: dest = localize(dest) 532: fileutils.cp_r(src, dest, options) 533: end
# File lib/ratch/shell.rb, line 200 200: def dir(path) 201: #Directory.new(name) 202: path = localize(path) 203: raise FileNotFound unless File.directory?(path) 204: Pathname.new(path) 205: end
Returns list of directories.
# File lib/ratch/shell.rb, line 233 233: def directories 234: pathnames.select{ |f| f.directory? } 235: end
# File lib/ratch/shell.rb, line 392 392: def directory?(path) ; FileTest.directory?(localize(path)) ; end
Lists directory entries.
# File lib/ratch/shell.rb, line 215 215: def directory_entries 216: entries.select{ |d| d.directory? } 217: end
def force? ; @_force ; end
# File lib/ratch/shell.rb, line 92 92: def dryrun? ; noop? && trace? ; end
Lists all entries.
# File lib/ratch/shell.rb, line 208 208: def entries 209: work.entries 210: end
Same as #== except that noop? must also be the same.
# File lib/ratch/shell.rb, line 105 105: def eql?(other) 106: return false unless other.is_a?(self.class) 107: return false unless work == other.work 108: return false unless noop? == other.noop? 109: true 110: end
# File lib/ratch/shell.rb, line 409 409: def executable?(path) ; FileTest.executable?(localize(path)) ; end
# File lib/ratch/shell.rb, line 417 417: def executable_real?(path) ; FileTest.executable_real?(localize(path)) ; end
# File lib/ratch/shell.rb, line 396 396: def exist?(path) ; FileTest.exist?(localize(path)) ; end
# File lib/ratch/shell.rb, line 397 397: def exists?(path) ; FileTest.exists?(localize(path)) ; end
# File lib/ratch/shell.rb, line 188 188: def file(path) 189: #FileObject[name] 190: path = localize(path) 191: raise FileNotFound unless File.file?(path) 192: Pathname.new(path) 193: end
# File lib/ratch/shell.rb, line 400 400: def file?(path) ; FileTest.file?(localize(path)) ; end
Lists file entries.
# File lib/ratch/shell.rb, line 223 223: def file_entries 224: entries.select{ |f| f.file? } 225: end
Returns list of files.
# File lib/ratch/shell.rb, line 240 240: def files 241: pathnames.select{ |f| f.file? } 242: end
Glob pattern. Returns matches as strings.
# File lib/ratch/shell.rb, line 245 245: def glob(*patterns, &block) 246: opts = (::Integer===patterns.last ? patterns.pop : 0) 247: matches = [] 248: locally do 249: matches = patterns.map{ |pattern| ::Dir.glob(pattern, opts) }.flatten 250: end 251: if block_given? 252: matches.each(&block) 253: else 254: matches 255: end 256: end
# File lib/ratch/shell.rb, line 403 403: def grpowned?(path) ; FileTest.grpowned?(localize(path)) ; end
Current home path.
# File lib/ratch/shell.rb, line 143 143: def home(*args) 144: dir(File.expand_path('~'), *args) 145: end
# File lib/ratch/shell.rb, line 420 420: def identical?(path, other) 421: FileTest.identical?(localize(path), localize(other)) 422: end
# File lib/ratch/shell.rb, line 564 564: def install(src, dest, mode, options={}) 565: src = localize(src) 566: dest = localize(dest) 567: fileutils.install(src, dest, mode, options) 568: end
ln(list, destdir, options={})
# File lib/ratch/shell.rb, line 499 499: def ln(old, new, options={}) 500: old = localize(old) 501: new = localize(new) 502: fileutils.ln(old, new, options) 503: end
ln_s(list, destdir, options={})
# File lib/ratch/shell.rb, line 507 507: def ln_s(old, new, options={}) 508: old = localize(old) 509: new = localize(new) 510: fileutils.ln_s(old, new, options) 511: end
# File lib/ratch/shell.rb, line 514 514: def ln_sf(old, new, options={}) 515: old = localize(old) 516: new = localize(new) 517: fileutils.ln_sf(old, new, options) 518: end
Returns a path local to the current working path.
# File lib/ratch/shell.rb, line 687 687: def localize(local_path) 688: # some path arguments are optional 689: return local_path unless local_path 690: # 691: case local_path 692: when Array 693: local_path.collect do |lp| 694: if absolute?(lp) 695: lp 696: else 697: File.expand_path(File.join(work.to_s, lp)) 698: end 699: end 700: else 701: # do not localize an absolute path 702: return local_path if absolute?(local_path) 703: File.expand_path(File.join(work.to_s, local_path)) 704: #(work + local_path).expand_path.to_s 705: end 706: end
Change directory to the shell’s work directory, process the block and then return to user directory.
# File lib/ratch/shell.rb, line 710 710: def locally(&block) 711: if work.to_s == Dir.pwd 712: block.call 713: else 714: mutex.synchronize do 715: #work.chdir(&block) 716: Dir.chdir(work, &block) 717: end 718: end 719: end
# File lib/ratch/shell.rb, line 480 480: def mkdir(dir, options={}) 481: dir = localize(dir) 482: fileutils.mkdir(dir, options) 483: end
# File lib/ratch/shell.rb, line 485 485: def mkdir_p(dir, options={}) 486: dir = localize(dir) 487: unless File.directory?(dir) 488: fileutils.mkdir_p(dir, options) 489: end 490: end
# File lib/ratch/shell.rb, line 268 268: def multiglob_r(*args, &blk) 269: Dir.multiglob_r(*args, &blk) 270: end
mv(list, dir, options={})
# File lib/ratch/shell.rb, line 536 536: def mv(src, dest, options={}) 537: src = localize(src) 538: dest = localize(dest) 539: fileutils.mv(src, dest, options) 540: end
# File lib/ratch/shell.rb, line 725 725: def naming_policy(*policies) 726: if policies.empty? 727: @naming_policy ||= ['down', 'ext'] 728: else 729: @naming_policy = policies 730: end 731: end
# File lib/ratch/shell.rb, line 622 622: def outofdate?(path, *sources) 623: #fileutils.outofdate?(localize(path), localize(sources)) # DIDN'T WORK, why? 624: locally do 625: fileutils.outofdate?(path, sources.flatten) 626: end 627: end
# File lib/ratch/shell.rb, line 407 407: def owned?(path) ; FileTest.owned?(localize(path)) ; end
Return a new prompt with the same location. NOTE: Use #dup or #clone ?
def new ; Shell.new(work) ; end
# File lib/ratch/shell.rb, line 160 160: def parent 161: dir('..') 162: end
# File lib/ratch/shell.rb, line 182 182: def path(path) 183: Pathname.new(localize(path)) 184: end
Likes entries but omits ’.’ and ’..’ paths.
# File lib/ratch/shell.rb, line 228 228: def pathnames 229: work.entries - %{. ..}.map{|f|Pathname.new(f)} 230: end
# File lib/ratch/shell.rb, line 399 399: def pipe?(path) ; FileTest.pipe?(localize(path)) ; end
Present working directory.
# File lib/ratch/shell.rb, line 470 470: def pwd 471: work.to_s 472: end
Opertaton mode. This can be :noop, :verbose or :dryrun. The later is the same as the first two combined.
def mode(opts=nil)
return @mode unless opts opts.each do |key, val| next unless val case key when :noop @mode = (@mode == :verbose ? :dryrun : :noop) when :verbose @mode = (@mode == :noop ? :dryrun : :verbose) when :dryrun @mode = :dryrun end end
end
# File lib/ratch/shell.rb, line 87 87: def quiet? ; @_quiet ; end
Read file.
# File lib/ratch/shell.rb, line 368 368: def read(path) 369: File.read(localize(path)) 370: end
# File lib/ratch/shell.rb, line 394 394: def readable?(path) ; FileTest.readable?(localize(path)) ; end
# File lib/ratch/shell.rb, line 418 418: def readable_real?(path) ; FileTest.readable_real?(localize(path)) ; end
# File lib/ratch/shell.rb, line 413 413: def relative?(path) ; FileTest.relative?(path) ; end
# File lib/ratch/shell.rb, line 543 543: def rm(list, options={}) 544: list = localize(list) 545: fileutils.rm(list, options) 546: end
# File lib/ratch/shell.rb, line 554 554: def rm_f(list, options={}) 555: list = localize(list) 556: fileutils.rm_f(list, options) 557: end
# File lib/ratch/shell.rb, line 549 549: def rm_r(list, options={}) 550: list = localize(list) 551: fileutils.rm_r(list, options) 552: end
# File lib/ratch/shell.rb, line 559 559: def rm_rf(list, options={}) 560: list = localize(list) 561: fileutils.rm_rf(list, options) 562: end
# File lib/ratch/shell.rb, line 493 493: def rmdir(dir, options={}) 494: dir = localize(dir) 495: fileutils.rmdir(dir, options) 496: end
Root location.
# File lib/ratch/shell.rb, line 138 138: def root(*args) 139: dir('/', *args) 140: end
# File lib/ratch/shell.rb, line 411 411: def safe?(path) ; FileTest.safe?(localize(path)) ; end
# File lib/ratch/shell.rb, line 404 404: def setgid?(path) ; FileTest.setgid?(localize(path)) ; end
# File lib/ratch/shell.rb, line 405 405: def setuid?(path) ; FileTest.setuid?(localize(path)) ; end
Shell runner.
# File lib/ratch/shell.rb, line 307 307: def sh(cmd) 308: #puts "--> system call: #{cmd}" if trace? 309: puts cmd if trace? 310: return true if noop? 311: #locally do 312: if quiet? 313: silently{ system(cmd) } 314: else 315: system(cmd) 316: end 317: #end 318: end
# File lib/ratch/shell.rb, line 390 390: def size(path) ; FileTest.size(localize(path)) ; end
# File lib/ratch/shell.rb, line 391 391: def size?(path) ; FileTest.size?(localize(path)) ; end
# File lib/ratch/shell.rb, line 406 406: def socket?(path) ; FileTest.socket?(localize(path)) ; end
TODO: should this have SOURCE diectory?
stage(directory, source_dir, files)
# File lib/ratch/shell.rb, line 601 601: def stage(stage_dir, files) 602: #dir = localize(directory) 603: #files = localize(files) 604: locally do 605: fileutils.stage(stage_dir, work, files) 606: end 607: end
# File lib/ratch/shell.rb, line 401 401: def sticky?(path) ; FileTest.sticky?(localize(path)) ; end
# File lib/ratch/shell.rb, line 393 393: def symlink?(path) ; FileTest.symlink?(localize(path)) ; end
TODO: Tie this into the System class.
# File lib/ratch/shell.rb, line 300 300: def system(cmd) 301: locally do 302: super(cmd) 303: end 304: end
String representation is work directory path.
# File lib/ratch/shell.rb, line 95 95: def to_s ; work.to_s ; end
alias_method :chown_R, :chown_r
# File lib/ratch/shell.rb, line 592 592: def touch(list, options={}) 593: list = localize(list) 594: fileutils.touch(list, options) 595: end
# File lib/ratch/shell.rb, line 647 647: def uptodate?(path, *sources) 648: locally do 649: fileutils.uptodate?(path, sources.flatten) 650: end 651: end
Current working path.
# File lib/ratch/shell.rb, line 148 148: def work(*args) 149: return @_work if args.empty? 150: return dir(@_work, *args) 151: end
# File lib/ratch/shell.rb, line 408 408: def writable?(path) ; FileTest.writable?(localize(path)) ; end
# File lib/ratch/shell.rb, line 416 416: def writable_real?(path) ; FileTest.writable_real?(localize(path)) ; end
Private Instance Methods
Returns FileUtils module based on mode.
# File lib/ratch/shell.rb, line 754 754: def fileutils 755: if dryrun? 756: ::FileUtils::DryRun 757: elsif noop? 758: ::FileUtils::Noop 759: elsif trace? 760: ::FileUtils::Verbose 761: else 762: ::FileUtils 763: end 764: end
# File lib/ratch/shell.rb, line 64 64: def mutex 65: @mutex ||= Mutex.new 66: end
# File lib/ratch/shell.rb, line 50 50: def parse_arguments(*args) 51: opts = (Hash===args.last ? args.pop : {}) 52: return args, opts 53: end
# File lib/ratch/shell.rb, line 56 56: def set_options(opts) 57: @_quiet = opts[:quiet] 58: @_noop = opts[:noop] || opts[:dryrun] 59: @_trace = opts[:trace] || opts[:dryrun] 60: #@_force = opts[:force] 61: end
This may be used by script commands to allow for per command noop and trace options. Global options have precedence.
# File lib/ratch/shell.rb, line 768 768: def util_options(options) 769: noop = noop? || options[:noop] || options[:dryrun] 770: trace = trace? || options[:trace] || options[:dryrun] 771: return noop, trace 772: end
Disabled; run with --debug to generate this.