Ratch Shell object provides a limited file system shell in code. It is similar to having a shell prompt available to you in Ruby.
(Not documented)
# File lib/path/shell.rb, line 742 def self.[](path) new(path) end
New Shell object.
# File lib/path/shell.rb, line 23 def initialize(*path_opts) opts = (Hash===path_opts.last ? path_opts.pop : {}) path = path_opts @quiet = opts[:quiet] @noop = opts[:noop] @verbose = opts[:verbose] @debug = opts[:debug] if path.empty? path = Dir.pwd else path = File.join(*path) end raise FileNotFound, "#{path}" unless ::File.exist?(path) raise FileNotFound, "#{path}" unless ::File.directory?(path) @work = dir(path) end
Join paths. TODO: Should this return a new directory object? Or should it change directories?
# File lib/path/shell.rb, line 210 def /(path) #@work += dir # did not work, why? @work = dir(localize(path)) self end
# File lib/path/shell.rb, line 79 def ==(other) return false unless self.class===other return true if @work == other.work false end
Get the FileObject for the given file name.
# File lib/path/shell.rb, line 119 def [](name) #FileObject[localize(name)] #Pathname.new(localize(path)) Pathlist.new(localize(path)) end
(Not documented)
# File lib/path/shell.rb, line 327 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/path/shell.rb, line 526 def amass(include_globs, exclude_globs=[], ignore_globs=[]) locally do fileutils.amass(include_globs, exclude_globs, ignore_globs) end end
Append to file.
# File lib/path/shell.rb, line 292 def append(path, text) puts "append #{path}" if verbose? File.open(localize(path), 'a'){ |f| f << text } unless noop? end
(Not documented)
# File lib/path/shell.rb, line 315 def blockdev?(path) ; FileTest.blockdev?(localize(path)) ; end
Look up a cache file.
# File lib/path/shell/xdg.rb, line 22 def cache(path) file(XDG.xdg_cache_file(path)) end
Change working directory.
TODO: Make thread safe.
# File lib/path/shell.rb, line 259 def cd(path, &block) if block work_old = @work begin @work = dir(localize(path)) locally(&block) #mutex.synchronize do # Dir.chdir(@work){ block.call } #end ensure @work = work_old end else @work = dir(localize(path)) end end
(Not documented)
# File lib/path/shell.rb, line 308 def chardev?(path) ; FileTest.chardev?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 481 def chmod(mode, list, options={}) list = localize(list) fileutils.chmod(mode, list, options) end
(Not documented)
# File lib/path/shell.rb, line 486 def chmod_r(mode, list, options={}) list = localize(list) fileutils.chmod_r(mode, list, options) end
alias_method :chmod_R, :chmod_r
# File lib/path/shell.rb, line 492 def chown(user, group, list, options={}) list = localize(list) fileutils.chown(user, group, list, options) end
(Not documented)
# File lib/path/shell.rb, line 497 def chown_r(user, group, list, options={}) list = localize(list) fileutils.chown_r(user, group, list, options) end
Same as identical?
# File lib/path/shell.rb, line 388 def cmp(a,b) fileutils.compare_file(a,b) end
Look up a config file.
# File lib/path/shell/xdg.rb, line 12 def config(path) file(XDG.xdg_config_file(path)) end
cp(list, dir, options={})
# File lib/path/shell.rb, line 432 def cp(src, dest, options={}) src = localize(src) dest = localize(dest) fileutils.cp(src, dest, options) end
cp_r(list, dir, options={})
# File lib/path/shell.rb, line 440 def cp_r(src, dest, options={}) src = localize(src) dest = localize(dest) fileutils.cp_r(src, dest, options) end
Look up a data file.
# File lib/path/shell/xdg.rb, line 17 def data(path) file(XDG.xdg_data_file(path)) end
(Not documented)
# File lib/path/shell.rb, line 67 def debug? ; @debug ; end
def doc(name)
Document.new(name)
end
# File lib/path/shell.rb, line 136 def dir(path) #Directory.new(name) raise unless File.directory?(path) Pathname.new(localize(path)) end
Returns list of documents.
# File lib/path/shell.rb, line 170 def directories ; work.directories ; end
(Not documented)
# File lib/path/shell.rb, line 305 def directory?(path) ; FileTest.directory?(localize(path)) ; end
Lists directory entries.
# File lib/path/shell.rb, line 154 def directory_entries work.entries.select{ |d| d.directory? } end
(Not documented)
# File lib/path/shell.rb, line 72 def dryrun? ; @noop && @verbose ; end
Lists all entries.
# File lib/path/shell.rb, line 148 def entries work.entries end
(Not documented)
# File lib/path/shell.rb, line 322 def executable?(path) ; FileTest.executable?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 330 def executable_real?(path) ; FileTest.executable_real?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 309 def exist?(path) ; FileTest.exist?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 310 def exists?(path) ; FileTest.exists?(localize(path)) ; end
should file and this be the same?
# File lib/path/shell.rb, line 126 def file(path) #FileObject[name] raise unless File.file?(path) Pathname.new(localize(path)) end
(Not documented)
# File lib/path/shell.rb, line 313 def file?(path) ; FileTest.file?(localize(path)) ; end
Lists directory entries.
# File lib/path/shell.rb, line 159 def file_entries work.entries.select{ |f| f.file? } end
Put together the list of files to copy.
# File lib/path/shell/ftp.rb, line 78 def files( dir, copy ) Dir.chdir(dir) do del, add = copy.partition{ |f| /^[-]/ =~ f } # remove - and + prefixes del.collect!{ |f| f.sub(/^[-]/,'') } add.collect!{ |f| f.sub(/^[+]/,'') } #del.concat(must_exclude) files = [] add.each{ |g| files += Dir.glob(g) } del.each{ |g| files -= Dir.glob(g) } files.collect!{ |f| f.sub(/^\//,'') } return files end end
Use ftp to upload files.
# File lib/path/shell/ftp.rb, line 7 def ftp( keys ) keys = upload_parameters(keys) # set transfer rules if keys.stage trans = stage_transfer(keys.stage) else files(keys.dir, keys.copy).each do |from| trans << [from,from] end end # append location of publication dir to from dir = keys.dir trans.collect!{ |from,to| [File.join(dir,from), to] } if keys.dryrun puts "ftp open #{keys.user}@#{keys.host}:#{keys.root}/" keys.trans.each do |f, t| puts "ftp put #{f} #{t}" end else require 'net/ftp' Net::FTP.open(keys.host) do |ftp| ftp.login(keys.user) #password? ftp.chdir(keys.root) keys.trans.each do |f, t| puts "ftp #{f} #{t}" unless keys.quiet ftp.putbinaryfile( f, t, 1024 ) end end end end
Glob pattern. Returns matches as strings.
# File lib/path/shell.rb, line 173 def glob(*patterns, &block) opts = (::Integer===patterns.last ? patterns.pop : 0) matches = [] locally do matches = patterns.map{ |pattern| ::Dir.glob(pattern, opts) }.flatten end if block_given? matches.each(&block) else matches end end
(Not documented)
# File lib/path/shell.rb, line 316 def grpowned?(path) ; FileTest.grpowned?(localize(path)) ; end
Create a gzip file.
# File lib/path/shell/gzip.rb, line 7 def gzip(file, tofile=nil, options={}) require 'zlib' noop, verbose = *util_options(options) tofile ||= File.basename(file) + '.gz' puts "gzip #{file}" if verbose file = localize(file) tofile = localize(tofile) Zlib::GzipWriter.open(tofile) do |gz| gz.write(File.read(file)) end unless noop return tofile end
Current home path.
# File lib/path/shell.rb, line 96 def home(*args) dir('~', *args) end
Return the home cache directory.
# File lib/path/shell/xdg.rb, line 47 def home_cache dir(XDG.xdg_cache_home) end
Return the home config directory.
# File lib/path/shell/xdg.rb, line 37 def home_config dir(XDG.xdg_config_home) end
Return the home data directory.
# File lib/path/shell/xdg.rb, line 42 def home_data dir(XDG.xdg_data_home) end
(Not documented)
# File lib/path/shell.rb, line 333 def identical?(path, other) FileTest.identical?(localize(path), localize(other)) end
(Not documented)
# File lib/path/shell.rb, line 475 def install(src, dest, mode, options={}) src = localize(src) dest = localize(dest) fileutils.install(src, dest, mode, options) end
ln(list, destdir, options={})
# File lib/path/shell.rb, line 410 def ln(old, new, options={}) old = localize(old) new = localize(new) fileutils.ln(old, new, options) end
ln_s(list, destdir, options={})
# File lib/path/shell.rb, line 418 def ln_s(old, new, options={}) old = localize(old) new = localize(new) fileutils.ln_s(old, new, options) end
(Not documented)
# File lib/path/shell.rb, line 425 def ln_sf(old, new, options={}) old = localize(old) new = localize(new) fileutils.ln_sf(old, new, options) end
Returns a path local to the current working path.
# File lib/path/shell.rb, line 670 def localize(local_path) # some path arguments are optional return local_path unless local_path # case local_path when Array local_path.collect do |lp| if absolute?(lp) lp else File.expand_path(File.join(work.to_s, lp)) end end else # do not localize an absolute path return local_path if absolute?(local_path) File.expand_path(File.join(work.to_s, local_path)) end end
# File lib/path/shell.rb, line 691 def locally(&block) if work.to_s == Dir.pwd block.call else mutex.synchronize do #work.chdir(&block) Dir.chdir(work, &block) end end end
Match pattern. Like glob but returns file objects.
# File lib/path/shell.rb, line 196 def match(*patterns, &block) opts = (::Integer===patterns.last ? patterns.pop : 0) patterns = localize(patterns) matches = patterns.map{ |pattern| ::Dir.glob(pattern, opts) }.flatten matches = matches.map{ |f| FileObject[f] } if block_given? matches.each(&block) else matches end end
# File lib/path/shell.rb, line 393 def mkdir(dir, options={}) dir = localize(dir) fileutils.mkdir(dir, options) end
(Not documented)
# File lib/path/shell.rb, line 398 def mkdir_p(dir, options={}) dir = localize(dir) fileutils.mkdir_p(dir, options) end
(Not documented)
# File lib/path/shell.rb, line 191 def multiglob_r(*a) Dir.multiglob_r(*a) end
mv(list, dir, options={})
# File lib/path/shell.rb, line 447 def mv(src, dest, options={}) src = localize(src) dest = localize(dest) fileutils.mv(src, dest, options) end
(Not documented)
# File lib/path/shell.rb, line 69 def noop? ; @noop ; end
# File lib/path/shell.rb, line 533 def outofdate?(path, *sources) #fileutils.outofdate?(localize(path), localize(sources)) # DIDN'T WORK, why? locally do fileutils.outofdate?(path, sources.flatten) end end
(Not documented)
# File lib/path/shell.rb, line 320 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/path/shell.rb, line 113 def parent dir('..') end
(Not documented)
# File lib/path/shell.rb, line 142 def path(path) Pathname.new(localize(path)) end
(Not documented)
# File lib/path/shell.rb, line 312 def pipe?(path) ; FileTest.pipe?(localize(path)) ; end
Present working directory.
# File lib/path/shell.rb, line 383 def pwd work.to_s 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/path/shell.rb, line 66 def quiet? ; @quiet ; end
Read file.
# File lib/path/shell.rb, line 281 def read(path) File.read(localize(path)) end
(Not documented)
# File lib/path/shell.rb, line 307 def readable?(path) ; FileTest.readable?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 331 def readable_real?(path) ; FileTest.readable_real?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 326 def relative?(path) ; FileTest.relative?(path) ; end
(Not documented)
# File lib/path/shell.rb, line 454 def rm(list, options={}) list = localize(list) fileutils.rm(list, options) end
(Not documented)
# File lib/path/shell.rb, line 465 def rm_f(list, options={}) list = localize(list) fileutils.rm_f(list, options) end
(Not documented)
# File lib/path/shell.rb, line 460 def rm_r(list, options={}) list = localize(list) fileutils.rm_r(list, options) end
(Not documented)
# File lib/path/shell.rb, line 470 def rm_rf(list, options={}) list = localize(list) fileutils.rm_rf(list, options) end
(Not documented)
# File lib/path/shell.rb, line 404 def rmdir(dir, options={}) dir = localize(dir) fileutils.rmdir(dir, options) end
Root location.
# File lib/path/shell.rb, line 91 def root(*args) dir('/', *args) end
Return a enumertor of system config directories.
# File lib/path/shell/xdg.rb, line 27 def root_config() #_directories XDG.xdg_config_dirs.to_enum(:each){ |f| dir(f) } end
Return a enumertor of system data directories.
# File lib/path/shell/xdg.rb, line 32 def root_data() #_directories XDG.xdg_data_dirs.to_enum(:each){ |f| dir(f) } end
(Not documented)
# File lib/path/shell.rb, line 324 def safe?(path) ; FileTest.safe?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 317 def setgid?(path) ; FileTest.setgid?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 318 def setuid?(path) ; FileTest.setuid?(localize(path)) ; end
Use sftp to upload files.
# File lib/path/shell/ftp.rb, line 43 def sftp( keys ) keys = upload_parameters(keys) # set transfer rules if keys.stage trans = stage_transfer(keys.stage) else files(keys.dir, keys.copy).each do |from| trans << [from,from] end end # append location of publication dir to from dir = keys.dir trans.collect!{ |from,to| [File.join(dir,from), to] } if keys.dryrun puts "sftp open #{keys.user}@#{keys.host}:#{keys.root}/" keys.trans.each do |f,t| puts "sftp put #{f} #{t}" end else require 'net/sftp' Net::SFTP.start(keys.host, keys.user, keys.pass) do |sftp| #sftp.login( user ) sftp.chdir(keys.root) keys.trans.each do |f,t| puts "sftp #{f} #{t}" unless keys.quiet sftp.put_file(f,t) #, 1024 ) end end end end
Shell runner.
# File lib/path/shell.rb, line 227 def sh(cmd) #puts "--> system call: #{cmd}" if verbose? puts cmd if verbose? return true if noop? #locally do if quiet? silently{ system(cmd) } else system(cmd) end #end end
# File lib/path/shell.rb, line 303 def size(path) ; FileTest.size(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 304 def size?(path) ; FileTest.size?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 319 def socket?(path) ; FileTest.socket?(localize(path)) ; end
TODO: should this have SOURCE diectory?
stage(directory, source_dir, files)
# File lib/path/shell.rb, line 512 def stage(stage_dir, files) #dir = localize(directory) #files = localize(files) locally do fileutils.stage(stage_dir, work, files) end end
Combine three part stage list into a two part from->to list.
Using the stage list of three space separated fields.
fromdir file todir
This is used to generate a from -> to list of the form:
fromdir/file todir/file
# File lib/path/shell/ftp.rb, line 108 def stage_transfer( list ) trans = [] list.each do |line| trans << Shellwords.shellwords(line) end trans.collect! do |from, base, to| file = File.join(from,base) to = File.join(to,base) [from, to] end end
(Not documented)
# File lib/path/shell.rb, line 314 def sticky?(path) ; FileTest.sticky?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 306 def symlink?(path) ; FileTest.symlink?(localize(path)) ; end
# File lib/path/shell.rb, line 220 def system(cmd) locally do super(cmd) end end
Tar
# File lib/path/shell/tar.rb, line 21 def tar(folder, file=nil, options={}) Shell.require_minitar noop, verbose = *util_options(options) file ||= File.basename(File.expand_path(folder)) + '.tar' cmd = "tar -cf #{file} #{folder}" puts cmd if verbose unless noop locally do gzIO = File.open(file, 'wb') Archive::Tar::Minitar.pack(folder, gzIO) end end path(file) end
Tar Gzip
# File lib/path/shell/tar.rb, line 55 def tar_gzip(folder, file=nil, options={}) Shell.require_minitar noop, verbose = *util_options(options) file ||= File.basename(File.expand_path(folder)) + '.tar.gz' # '.tgz' which ? cmd = "tar --gzip -czf #{file} #{folder}" puts cmd if verbose unless noop locally do #folder, file = localize(folder), localize(file) gzIO = Zlib::GzipWriter.new(File.open(file, 'wb')) Archive::Tar::Minitar.pack(folder, gzIO) end end path(file) end
String representation is work directory path.
# File lib/path/shell.rb, line 76 def to_s ; work.to_s ; end
alias_method :chown_R, :chown_r
# File lib/path/shell.rb, line 503 def touch(list, options={}) list = localize(list) fileutils.touch(list, options) end
(Not documented)
# File lib/path/shell.rb, line 73 def trace? ; @debug && @verbose ; end
Unpack a gzip file.
# File lib/path/shell/gzip.rb, line 28 def ungzip(file, options={}) require 'zlib' noop, verbose = *util_options(options) fname = File.basename(file).chomp(File.extname(file)) puts "ungzip #{file}" if verbose fname = localize(fname) file = localize(file) Zlib::GzipReader.open(file) do |gz| File.open(fname, 'wb'){ |f| f << gz.read } end unless noop return fname end
Untar
# File lib/path/shell/tar.rb, line 38 def untar(file, options={}) Shell.require_minitar noop, verbose = *util_options(options) #file ||= File.basename(File.expand_path(folder)) + '.tar' cmd = "untar #{file}" puts cmd if verbose unless noop locally do gzIO = File.open(file, 'wb') Archive::Tar::Minitar.unpack(gzIO) end end path(file) end
Untar Gzip
FIXME: Write unified untar_gzip function.
# File lib/path/shell/tar.rb, line 75 def untar_gzip(file, options={}) Shell.require_minitar untar(ungzip(file, options), options) end
Unzip
# File lib/path/shell/zip.rb, line 31 def unzip(file, options={}) noop, verbose = *util_options(options) file = localize(file) cmd = "unzip #{file}" puts cmd if verbose system cmd if !noop end
# File lib/path/shell.rb, line 541 def uptodate?(path, *sources) locally do fileutils.uptodate?(path, sources.flatten) end end
(Not documented)
# File lib/path/shell.rb, line 70 def verbose? ; @verbose ; end
Current working path.
# File lib/path/shell.rb, line 101 def work(*args) return @work if args.empty? return dir(@work, *args) end
Return the work cache directory.
# File lib/path/shell/xdg.rb, line 57 def work_cache dir(XDG.xdg_cache_work) end
Return the work config directory.
# File lib/path/shell/xdg.rb, line 52 def work_config dir(XDG.xdg_config_work) end
(Not documented)
# File lib/path/shell.rb, line 321 def writable?(path) ; FileTest.writable?(localize(path)) ; end
(Not documented)
# File lib/path/shell.rb, line 329 def writable_real?(path) ; FileTest.writable_real?(localize(path)) ; end
Write file.
# File lib/path/shell.rb, line 286 def write(path, text) puts "write #{path}" if verbose? File.open(localize(path), 'w'){ |f| f << text } unless noop? end
(Not documented)
# File lib/path/shell.rb, line 311 def zero?(path) ; FileTest.zero?(localize(path)) ; end
Zip
TODO: replace with a pure ruby zip library
# File lib/path/shell/zip.rb, line 12 def zip(folder, file=nil, options={}) noop, verbose = *util_options(options) raise ArgumentError if folder == '.*' file ||= File.basename(File.expand_path(folder)) + '.zip' folder = localize(folder) file = localize(file) cmd = "zip -rqu #{file} #{folder}" puts cmd if verbose system cmd if !noop return file end
Returns FileUtils module based on mode.
# File lib/path/shell.rb, line 705 def fileutils if dryrun? ::FileUtils::DryRun elsif noop? ::FileUtils::Noop elsif verbose? ::FileUtils::Verbose else ::FileUtils end end
This may be used by script commands to allow for per command noop and verbose options. Global options have precedence.
# File lib/path/shell.rb, line 734 def util_options(options) noop = noop? || options[:noop] || options[:dryrun] verbose = verbose? || options[:verbose] || options[:dryrun] return noop, verbose end
Disabled; run with --debug to generate this.