lib/pa/cmd.rb in pa-1.1.4 vs lib/pa/cmd.rb in pa-1.2.0
- old
+ new
@@ -1,5 +1,7 @@
+require "securerandom"
+
=begin
rm family
* rm _rm file only_
* rmdir _rm directory only_
* rm_r _rm recurive, both file and directory_
@@ -17,98 +19,105 @@
# @overload ln(src, dest)
# @overload ln([src,..], directory)
#
# @param [Array<String>, String] src_s support globbing
# @param [String,Pa] dest
- # @param [Hash] o option
- # @option o [Boolean] :force overwrite if exists.
# @return [nil]
def ln(src_s, dest, o={})
- _ln(File.method(:link), src_s, dest, o)
+ _ln(:link, src_s, dest, o)
end
# ln force
#
# @see ln
# @return [nil]
- def ln_f(src_s, dest, o)
+ def ln_f(src_s, dest, o={})
o[:force]=true
- _ln(File.method(:link), src_s, dest, o)
+ _ln(:link, src_s, dest, o)
end
# symbol link
#
# @see ln
# @return [nil]
- def symln(src_s, dest, o)
- _ln(File.method(:symlink), src_s, dest, o)
+ def symln(src_s, dest, o={})
+ _ln(:symlink, src_s, dest, o)
end
alias symlink ln
# symln force
#
# @see ln
# @return [nil]
- def symln_f(src_s, dest, o)
+ def symln_f(src_s, dest, o={})
o[:force]=true
- _ln(File.method(:symlink), src_s, dest, o)
+ _ln(:symlink, src_s, dest, o)
end
-
# @see File.readlink
def readlink(path)
File.readlink(get(path))
end
# change directory
#
# @param [String,Pa] path
- def cd(path=ENV["HOME"], &blk)
- Dir.chdir(get(path), &blk)
+ # @param [Hash] o
+ # @option o [Boolean] :verbose verbose mode
+ def cd(path=ENV["HOME"], o={}, &blk)
+ p = get(path)
+ puts "cd #{p}" if o[:verbose]
+ Dir.chdir(p, &blk)
end
# chroot
# @see {Dir.chroot}
#
# @param [String] path
+ # @param [Hash] o
+ # @option o [Boolean] :verbose verbose mode
# @return [nil]
- def chroot(path)
- Dir.chroot(get(path))
+ def chroot(path, o={})
+ p = get(path)
+ puts "chdroot #{p}" if o[:verbose]
+ Dir.chroot(p)
end
# touch a blank file
#
# @overload touch(*paths, o={})
# @param [String] *paths
# @param [Hash] o option
# @option o [Fixnum,String] :mode
# @option o [Boolean] :mkdir auto mkdir if path contained directory not exists.
# @option o [Boolean] :force
+ # @option o [Boolean] :verbose
# @return [nil]
def touch(*args)
paths, o = Util.extract_options(args)
- _touch(*paths, o)
+ _touch(paths, o)
end
# touch force
# @see touch
#
# @overload touch_f(*paths, o={})
# @return [nil]
def touch_f(*args)
paths, o = Util.extract_options(args)
- o[:force]=true
- _touch(*paths, o)
+ o[:force] = true
+ _touch(paths, o)
end
# make a directory
#
# @overload mkdir(*paths, o={})
# @param [String, Pa] *paths
# @param [Hash] o option
# @option o [Fixnum] :mode
# @option o [Boolean] :force
+ # @option o [Boolean] :verbose
# @return [nil]
def mkdir(*args)
paths, o = Util.extract_options(args)
_mkdir(paths, o)
end
@@ -124,49 +133,88 @@
_mkdir(paths, o)
end
# make temp directory
#
- # @param [Hash] o options
- # @option o [Symbol] :prefix ("")
- # @option o [Symbol] :suffix ("")
- # @option o [Symbol] :tmpdir (ENV["TEMP"])
- # @return [String] path
- def mktmpdir(o={}, &blk)
- p = _mktmpname(o)
- File.mkdir(p)
- begin blk.call(p) ensure Dir.delete(p) end if blk
+ # @overload mktmpdir(name, o={}, &blk)
+ # @param [Hash] o options
+ # @option o [String] :tmpdir (ENV["TEMP"])
+ # @option o [Symbol] :verbose
+ # @return [String] path
+ # @overload mktmpdir(o={}, &blk) # name=$$
+ def mktmpdir(*args, &blk)
+ (name,), o = Util.extract_options(args)
+
+ p = _mktmpname(name, o)
+ puts "mktmpdir #{p}" if o[:verbose]
+
+ Dir.mkdir(p)
+
+ begin
+ blk.call(p)
+ ensure
+ Dir.delete(p)
+ end if blk
+
p
end # def mktmpdir
- def home(user=nil)
+ def home
Dir.home
end
# make temp file
# @see mktmpdir
#
- # @param [Hash] o options
- # @return [String] path
- def mktmpfile(o={}, &blk)
- p = _mktmpname(o)
+ # @overload mktmpfile2(name=$$, o={}, &blk)
+ # @param [Hash] o options
+ # @option o [Boolean] :verbose
+ # @option o [String] :tmpdir
+ # @return [String] path
+ def mktmpfile2(*args, &blk)
+ (name,), o = Util.extract_options(args)
+
+ p = _mktmpname(name, o)
+ puts "mktmpfile #{p}" if o[:verbose]
+
begin
blk.call(p)
ensure
File.delete(p)
end if blk
+
p
- end # mktmpfile
+ end
+ # @return [Pa] path
+ def mktmpfile(*args, &blk)
+ (name,), o = Util.extract_options(args)
+ p = _mktmpname(name, o)
+ puts "mktmpfile #{p}" if o[:verbose]
+
+ begin
+ blk.call(Pa(p))
+ ensure
+ File.delete(p)
+ end if blk
+
+ Pa(p)
+ end
+
# rm file only
#
- # @param [String] *paths support globbing
+ # @overload rm(*paths, o={})
+ # @param [String] *paths support globbing
+ # @param o [Boolean] :verbose
# @return [nil]
def rm(*paths)
paths, o = Util.extract_options(paths)
glob(*paths) { |pa|
+ extra_doc = o[:force] ? "-f " : nil
+ puts "rm #{extra_doc}#{pd.p}" if o[:verbose]
+
if File.directory?(pa.p)
if o[:force]
next
else
raise Errno::EISDIR, "is a directory -- #{pa.p}"
@@ -188,10 +236,13 @@
# @param [String] *paths support globbing
# @return [nil]
def rmdir(*paths)
paths, o = Util.extract_options(paths)
glob(*paths) { |pa|
+ extra_doc = o[:force] ? "-f " : nil
+ puts "rmdir #{extra_doc}#{pa.p}" if o[:verbose]
+
if not File.directory?(pa.p)
if o[:force]
next
else
raise Errno::ENOTDIR, "not a directory -- #{pa.p}"
@@ -210,11 +261,13 @@
# rm recusive, rm both file and directory
#
# @see rm
# @return [nil]
def rm_r(*paths)
+ paths, o = Util.extract_options(paths)
glob(*paths){ |pa|
+ puts "rm -r #{pa.p}" if o[:verbose]
File.directory?(pa.p) ? _rmdir(pa) : File.delete(pa.p)
}
end
alias rm_rf rm_r
@@ -229,12 +282,13 @@
# @yield [path]
# @yieldparam [Pa] path
# @yieldreturn [Boolean] rm_r path if true
# @return [nil]
def rm_if(*paths, &blk)
+ paths, o = Util.extract_options(paths)
glob(*paths) do |pa|
- rm_r pa if blk.call(pa)
+ rm_r pa, o if blk.call(pa)
end
end
# copy
#
@@ -293,27 +347,10 @@
def cp_f(src_s, dest, o={}, &blk)
o[:force] = true
cp src_s, dest, o, &blk
end
-
- # a rename util
- #
- # @example
- #
- # Pa.rename2('/home/guten.jpg') {|pa| pa.name+'_1'+pa.fext} # => '/home/guten_1.jpg'
- #
- # @param [String,Pa] src
- # @yieldparam [Pa] pa
- # @yieldreturn [String] fname
- # @return [String,Pa] # Pa.rename return String. Pa#rename return Pa.
- def rename2(src, &blk)
- src = Pa(src)
- fname = blk.call(src)
- src.dir.join(fname).path
- end
-
# move, use rename for same device. and cp for cross device.
# @see cp
#
# @param [Hash] o option
# @option o [Boolean] :verbose
@@ -381,13 +418,16 @@
end # def _move
def _touch(paths, o)
o[:mode] ||= 0644
paths.map!{|v|get(v)}
- paths.each {|path|
- if File.exists?(path)
- o[:force] ? next : raise(Errno::EEXIST, "File exist -- #{path}")
+ paths.each {|p|
+ extra_doc = o[:force] ? "-f " : nil
+ puts "touch #{extra_doc}#{p}" if o[:verbose]
+
+ if File.exists?(p)
+ o[:force] ? next : raise(Errno::EEXIST, "File exist -- #{p}")
end
mkdir(File.dirname(p)) if o[:mkdir]
if win32?
@@ -397,26 +437,33 @@
File.open(p, "w"){|f| f.chmod(o[:mode])}
end
}
end
- # @param [Array,String,#path] src_s
- # @param [String,#path] dest
- def _ln(method, src_s, dest, o={})
- dest = get(dest)
- glob(*Util.wrap_array(src_s)) {|src|
- src = get(src)
- dest = File.join(dest, File.basename(src)) if File.directory?(dest)
- Pa.rm_r(dest) if o[:force] and File.exists?(dest)
- method.call(src, dest)
- }
- end
+ # @param [Array,String,#path] src_s
+ # @param [String,#path] dest
+ def _ln(method, src_s, dest, o={})
+ dest = get(dest)
+ glob(*Util.wrap_array(src_s)) {|src|
+ src = get(src)
+ dest = File.join(dest, File.basename(src)) if File.directory?(dest)
+ Pa.rm_r(dest) if o[:force] and File.exists?(dest)
+ extra_doc = ""
+ extra_doc << (method==:symlink ? "-s " : "")
+ extra_doc << (o[:force] ? "-f " : "")
+ puts "ln #{extra_doc}#{src} #{dest}" if o[:verbose]
+ File.send(method, src, dest)
+ }
+ end
+
def _mkdir(paths, o)
o[:mode] ||= 0744
paths.map!{|v|get(v)}
paths.each {|p|
+ puts "mkdir #{p}" if o[:verbose]
+
if File.exists?(p)
o[:force] ? next : raise(Errno::EEXIST, "File exist -- #{p}")
end
stack = []
@@ -431,36 +478,28 @@
File.chmod(o[:mode], path)
end
}
end
- def _mktmpname(o={})
- # :prefix :suffix :tmpdir
- # $$-(time*100_000).to_i.to_s(36)
- # parse o
- o[:dir] ||= ENV["TEMP"]
- o[:prefix] ||= ""
- o[:suffix] ||= ""
+ # <name>.JNBNZG
+ def _mktmpname(name=nil, o={})
+ o[:tmpdir] ||= ENV["TEMP"]
+ name ||= $$
- # begin
- collision = 0
- path = "#{o[:dir]}/#{o[:prefix]}#{$$}-#{(Time.time*100_000).to_i.to_s(36)}"
- orgi_path = path.dup
- while File.exists?(path)
- path = orgi_path+ collision.to_s
- collision +=1
- end
- path << o[:suffix]
+ begin
+ random = SecureRandom.hex(3).upcase
+ path = "#{o[:tmpdir]}/#{name}.#{random}"
+ end while File.exists?(path)
path
end # def mktmpname
# I'm recusive
# param@ [Pa] path
def _rmdir(pa, o={})
return if not File.exists?(pa.p)
- pa.each {|pa1|
+ Pa.each(pa) {|pa1|
File.directory?(pa1.p) ? _rmdir(pa1, o) : File.delete(pa1.p)
}
File.directory?(pa.p) ? Dir.rmdir(pa.p) : File.delete(pa.p)
end
@@ -477,12 +516,11 @@
puts "cp #{src} #{dest}" if o[:verbose]
File.copy_stream(src, dest)
when "directory"
begin
- Pa.mkdir dest
- puts "mkdir #{dest}" if o[:verbose]
+ Pa.mkdir dest, :verbose => o[:verbose]
rescue Errno::EEXIST
end
return if o[:special]
@@ -492,11 +530,10 @@
when "link" # symbol link
if o[:folsymlink]
_copy(Pa.readlink(src), dest)
else
- Pa.symln(Pa.readlink(src), dest, force: true)
- puts "symlink #{src} #{dest}" if o[:verbose]
+ Pa.symln(Pa.readlink(src), dest, :force => true, :verbose => o[:verbose])
end
when "unknow"
raise EUnKnownType, "Can't handle unknow type(#{:type}) -- #{src}"
end