Module: Pa::Cmd::ClassMethods
- Defined in:
- lib/pa/cmd.rb
Instance Method Summary collapse
-
#_copy(src, dest, o = {}) ⇒ Object
I’m recursive.
- #_ln(method, src_s, dest, o = {}) ⇒ Object
- #_mkdir(paths, o) ⇒ Object
-
#_mktmpname(name = nil, o = {}) ⇒ Object
<name>.JNBNZG.
-
#_move(src, dest, o) ⇒ Object
I’m recusive.
-
#_rmdir(pa, o = {}) ⇒ Object
I’m recusive param@ [Pa] path.
- #_touch(paths, o) ⇒ Object
- #_wrap_cmd(cmd, pretty) ⇒ Object
-
#cd(path = , o = {}, &blk) ⇒ Object
change directory.
-
#chroot(path, o = {}) ⇒ nil
chroot.
-
#cp(src_s, dest, o = {}, &blk) ⇒ Object
copy.
- #cp_f(src_s, dest, o = {}, &blk) ⇒ Object
-
#empty_dir(*dirs) ⇒ nil
empty a directory.
- #empty_dir_f(*dirs) ⇒ Object
- #home2 ⇒ Object
-
#ln(src_s, dest, o = {}) ⇒ nil
(also: #symlink)
link.
-
#ln_f(src_s, dest, o = {}) ⇒ nil
ln force.
-
#mkdir(*paths, o = {}) ⇒ nil
make a directory.
-
#mkdir_f(*paths, o = {}) ⇒ nil
mkdir force.
-
#mktmpdir(*args, &blk) ⇒ Object
make temp directory.
-
#mktmpfile(*args, &blk) ⇒ Pa
Path.
-
#mktmpfile2(name = $$, o = {}, &blk) ⇒ String
make temp file.
-
#mv(src_s, dest, o = {}, &blk) ⇒ nil
move, use rename for same device.
- #mv_f(src_s, dest, o = {}, &blk) ⇒ Object
- #readlink(path) ⇒ Object
-
#rm(*paths, o = {}) ⇒ nil
rm file only.
- #rm_f(*paths) ⇒ Object
-
#rm_if(*paths) {|path| ... } ⇒ nil
rm_if(path) if condition is true.
-
#rm_r(*paths) ⇒ nil
(also: #rm_rf)
rm recusive, rm both file and directory.
-
#rmdir(*paths) ⇒ nil
rm directory only.
- #rmdir_f(*paths) ⇒ Object
-
#symln(src_s, dest, o = {}) ⇒ nil
symbol link.
-
#symln_f(src_s, dest, o = {}) ⇒ nil
symln force.
-
#touch(*paths, o = {}) ⇒ nil
touch a blank file.
-
#touch_f(*paths, o = {}) ⇒ nil
touch force.
Instance Method Details
#_copy(src, dest, o = {}) ⇒ Object
I’m recursive
624 625 626 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 |
# File 'lib/pa/cmd.rb', line 624 def _copy(src, dest, o={}) raise Errno::EEXIST, "dest exists -- #{dest}" if File.exists?(dest) and (not o[:force]) case type=File.ftype(src) when "file", "socket" puts "cp #{src} #{dest}" if o[:verbose] File.copy_stream(src, dest) when "directory" begin Pa.mkdir dest, :verbose => o[:verbose] rescue Errno::EEXIST end return if o[:special] each(src) { |pa| _copy(pa.p, File.join(dest, File.basename(pa.p)), o) } when "link" # symbol link if o[:folsymlink] _copy(Pa.readlink(src), dest) else Pa.symln(Pa.readlink(src), dest, :force => true, :verbose => o[:verbose]) end when "unknow" raise EUnKnownType, "Can't handle unknow type(#{:type}) -- #{src}" end # chmod chown utime src_stat = o[:folsymlink] ? File.stat(src) : File.lstat(src) begin File.chmod(src_stat.mode, dest) #File.chown(src_stat.uid, src_stat.gid, dest) File.utime(src_stat.atime, src_stat.mtime, dest) rescue Errno::ENOENT end end |
#_ln(method, src_s, dest, o = {}) ⇒ Object
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 |
# File 'lib/pa/cmd.rb', line 536 def _ln(method, src_s, dest, o={}) srcs = Util.wrap_array(src_s) dest = get(dest) extra_doc = "" extra_doc << (method==:symlink ? "-s " : "") extra_doc << (o[:force] ? "-f " : "") puts "ln #{extra_doc}#{srcs.join(" ")} #{dest}" if o[:show_cmd] srcs.each {|src| src = get(src) dest = File.join(dest, File.basename(src)) if File.directory?(dest) if File.exists?(dest) if o[:force] Pa.rm_r(dest) else raise Errno::EEXIST, "dest exists -- #{dest}" end end puts "ln #{extra_doc}#{src} #{dest}" if o[:verbose] # jruby need absolute_path if method == :link then src, dest = File.absolute_path(src, "."), File.absolute_path(dest, ".") # rbx end File.send(method, src, dest) } end |
#_mkdir(paths, o) ⇒ Object
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'lib/pa/cmd.rb', line 567 def _mkdir(paths, o) puts "mkdir #{paths.join(" ")}" if o[:show_cmd] o[:mode] ||= 0775 paths.map!{|v|get(v)} paths.each {|p| puts "mkdir #{p}" if o[:verbose] if File.exists?(p) if o[:force] next else raise Errno::EEXIST, "File exist -- #{p}" end end stack = [] until p == stack.last break if File.exists?(p) stack << p p = File.dirname(p) end stack.reverse.each do |path| Dir.mkdir(path) File.chmod(o[:mode], path) end } end |
#_mktmpname(name = nil, o = {}) ⇒ Object
<name>.JNBNZG
598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/pa/cmd.rb', line 598 def _mktmpname(name=nil, o={}) o[:tmpdir] ||= Dir.tmpdir name ||= $$ begin random = SecureRandom.hex(3).upcase path = "#{o[:tmpdir]}/#{name}.#{random}" end while File.exists?(path) path end |
#_move(src, dest, o) ⇒ Object
I’m recusive
_move “file”, “dir/file”
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/pa/cmd.rb', line 480 def _move(src, dest, o) raise Errno::EEXIST, "dest exists -- #{dest}" if File.exists?(dest) and (not o[:force]) # move same file. return if File.absolute_path(src) == File.absolute_path(dest) # :force. mv "dir", "dira" and 'dira' exists and is a directory. if File.exists?(dest) and File.directory?(dest) ls(src) { |pa| dest1 = File.join(dest, File.basename(pa.p)) _move pa.p, dest1, o } Pa.rm_r src else begin Pa.rm_r dest if o[:force] and File.exists?(dest) puts "rename #{src} #{dest}" if o[:verbose] File.rename(src, dest) rescue Errno::EXDEV # cross-device _copy(src, dest, o) Pa.rm_r src end end end |
#_rmdir(pa, o = {}) ⇒ Object
I’m recusive param@ [Pa] path
612 613 614 615 616 617 618 |
# File 'lib/pa/cmd.rb', line 612 def _rmdir(pa, o={}) return if not File.exists?(pa.p) 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 |
#_touch(paths, o) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/pa/cmd.rb', line 508 def _touch(paths, o) o[:mode] ||= 0644 paths.map!{|v|get(v)} extra_doc = o[:force] ? "-f " : nil puts "touch #{extra_doc}#{paths.join(" ")}" if o[:show_cmd] paths.each {|p| 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 Util.windows? # windows BUG. must f.write("") then file can be deleted. File.open(p, "w"){|f| f.write("")} # windows need f.write so that it can be delete. File.chmod(o[:mode], p) # jruby can't use File#chmod else File.open(p, "w"){} File.chmod(o[:mode], p) end } end |
#_wrap_cmd(cmd, pretty) ⇒ Object
675 676 677 678 679 680 681 682 |
# File 'lib/pa/cmd.rb', line 675 def _wrap_cmd(cmd, pretty) case pretty when "$", "#" "#{pretty} #{cmd}" else cmd end end |
#cd(path = , o = {}, &blk) ⇒ Object
change directory
95 96 97 98 99 |
# File 'lib/pa/cmd.rb', line 95 def cd(path=ENV["HOME"], o={}, &blk) p = get(path) puts _wrap_cmd("cd #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd]) Dir.chdir(p, &blk) end |
#chroot(path, o = {}) ⇒ nil
chroot
109 110 111 112 113 |
# File 'lib/pa/cmd.rb', line 109 def chroot(path, o={}) p = get(path) puts _wrap_cmd("chdroot #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd]) Dir.chroot(p) end |
#cp(src_s, dest, o) ⇒ nil #cp(src_s, dest, o) {|src, dest, o| ... } ⇒ nil
copy
cp file dir
cp 'a', 'dir' #=> dir/a
cp 'a', 'dir/a' #=> dir/a
cp file1 file2 .. dir
cp ['a','b'], 'dir' #=> dir/a dir/b
default: preverse mode, not owner.
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 |
# File 'lib/pa/cmd.rb', line 401 def cp(src_s, dest, o={}, &blk) srcs = Util.wrap_array(src_s).map{|v| Pa.get(v)} dest = Pa.get(dest) puts "cp #{srcs.join(" ")} #{dest}" if o[:show_cmd] if o[:mkdir] and (not File.exists?(dest)) Pa.mkdir dest end # cp file1 file2 .. dir if srcs.size>1 and (not File.directory?(dest)) raise Errno::ENOTDIR, "dest not a directory when cp more than one src -- #{dest}" end srcs.each do |src| dest1 = File.directory?(dest) ? File.join(dest, File.basename(src)) : dest if blk blk.call src, dest1, o, proc{_copy(src, dest1, o)} else _copy src, dest1, o end end end |
#cp_f(src_s, dest, o = {}, &blk) ⇒ Object
427 428 429 430 |
# File 'lib/pa/cmd.rb', line 427 def cp_f(src_s, dest, o={}, &blk) o[:force] = true cp src_s, dest, o, &blk end |
#empty_dir(*dirs) ⇒ nil
empty a directory.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/pa/cmd.rb', line 311 def empty_dir(*dirs) dirs, o = Util.(dirs) extra_doc = o[:force] ? "-f " : nil puts _wrap_cmd("empty_dir #{extra_doc}#{dirs.join(" ")}", o[:show_cmd]) if o[:show_cmd] dirs.each {|dir| dir = Pa(dir) if not File.exists?(dir.p) raise Errno::ENOENT, "not exists -- #{dir}" unless o[:force] elsif not File.directory?(dir.p) raise Errno::ENOTDIR, "not a directory -- #{dir}" unless o[:force] else rm_r *Pa.glob2("#{dir}/*", :dotmatch => true) end } end |
#empty_dir_f(*dirs) ⇒ Object
328 329 330 331 332 |
# File 'lib/pa/cmd.rb', line 328 def empty_dir_f(*dirs) dirs, o = Util.(dirs) o[:force] = true empty_dir *dirs, o end |
#home2 ⇒ Object
38 39 40 |
# File 'lib/pa/cmd.rb', line 38 def home2 Dir.home end |
#ln(src, dest) ⇒ nil #ln([src,..], directory) ⇒ nil Also known as: symlink
link
53 54 55 |
# File 'lib/pa/cmd.rb', line 53 def ln(src_s, dest, o={}) _ln(:link, src_s, dest, o) end |
#ln_f(src_s, dest, o = {}) ⇒ nil
ln force
61 62 63 64 |
# File 'lib/pa/cmd.rb', line 61 def ln_f(src_s, dest, o={}) o[:force] = true _ln(:link, src_s, dest, o) end |
#mkdir(*paths, o = {}) ⇒ nil
make a directory
152 153 154 155 |
# File 'lib/pa/cmd.rb', line 152 def mkdir(*args) paths, o = Util.(args) _mkdir(paths, o) end |
#mkdir_f(*paths, o = {}) ⇒ nil
mkdir force
162 163 164 165 166 |
# File 'lib/pa/cmd.rb', line 162 def mkdir_f(*args) paths, o = Util.(args) o[:force]=true _mkdir(paths, o) end |
#mktmpdir(name, o = {}, &blk) ⇒ String #mktmpdir(o = {}, &blk) ⇒ Object
make temp directory
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/pa/cmd.rb', line 177 def mktmpdir(*args, &blk) (name,), o = Util.(args) p = _mktmpname(name, o) puts _wrap_cmd("mktmpdir #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd]) Dir.mkdir(p) begin blk.call(p) ensure Dir.delete(p) end if blk p end |
#mktmpfile(*args, &blk) ⇒ Pa
Returns path.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/pa/cmd.rb', line 219 def mktmpfile(*args, &blk) (name,), o = Util.(args) p = _mktmpname(name, o) puts _wrap_cmd("mktmpfile #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd]) begin blk.call(Pa(p)) ensure File.delete(p) end if blk Pa(p) end |
#mktmpfile2(name = $$, o = {}, &blk) ⇒ String
make temp file
203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/pa/cmd.rb', line 203 def mktmpfile2(*args, &blk) (name,), o = Util.(args) p = _mktmpname(name, o) puts _wrap_cmd("mktmpfile #{p}", o[:show_cmd]) if (o[:verbose] or o[:show_cmd]) begin blk.call(p) ensure File.delete(p) end if blk p end |
#mv(src_s, dest, o = {}, &blk) ⇒ nil
move, use rename for same device. and cp for cross device.
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 |
# File 'lib/pa/cmd.rb', line 441 def mv(src_s, dest, o={}, &blk) srcs = Util.wrap_array(src_s).map{|v| get(v)} dest = get(dest) extra_doc = o[:force] ? "-f " : nil puts "mv #{extra_doc}#{srcs.join(" ")} #{dest}" if o[:show_cmd] if o[:mkdir] and (not File.exists?(dest)) mkdir dest end # mv file1 file2 .. dir if srcs.size>1 and (not File.directory?(dest)) raise Errno::ENOTDIR, "dest not a directory when mv more than one src -- #{dest}" end srcs.each do |src| dest1 = File.directory?(dest) ? File.join(dest, File.basename(src)) : dest if blk blk.call src, dest1, o, proc{_move(src, dest1, o)} else _move src, dest1, o end end end |
#mv_f(src_s, dest, o = {}, &blk) ⇒ Object
469 470 471 472 |
# File 'lib/pa/cmd.rb', line 469 def mv_f(src_s, dest, o={}, &blk) o[:force] = true mv src_s, dest, o, &blk end |
#readlink(path) ⇒ Object
85 86 87 |
# File 'lib/pa/cmd.rb', line 85 def readlink(path) File.readlink(File.absolute_path(get(path), ".")) # jruby rbx end |
#rm(*paths, o = {}) ⇒ nil
rm file only
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/pa/cmd.rb', line 241 def rm(*paths) paths, o = Util.(paths) extra_doc = o[:force] ? "-f " : nil puts _wrap_cmd("rm #{extra_doc}#{paths.join(' ')}", o[:show_cmd]) if o[:show_cmd] paths.each { |path| pa = Pa(path) puts "rm #{extra_doc}#{pa.p}" if o[:verbose] if File.directory?(pa.p) if o[:force] next else raise Errno::EISDIR, "is a directory -- #{pa.p}" end end next if pa.directory? File.delete(pa.p) } end |
#rm_f(*paths) ⇒ Object
262 263 264 265 266 |
# File 'lib/pa/cmd.rb', line 262 def rm_f(*paths) paths, o = Util.(paths) o[:force] = true rm *paths, o end |
#rm_if(*paths) {|path| ... } ⇒ nil
rm_if(path) if condition is true
361 362 363 364 365 366 367 |
# File 'lib/pa/cmd.rb', line 361 def rm_if(*paths, &blk) paths, o = Util.(paths) paths.each do |path| pa = Pa(path) rm_r pa, o if blk.call(pa) end end |
#rm_r(*paths) ⇒ nil Also known as: rm_rf
rm recusive, rm both file and directory
338 339 340 341 342 343 344 345 346 |
# File 'lib/pa/cmd.rb', line 338 def rm_r(*paths) paths, o = Util.(paths) puts _wrap_cmd("rm -r #{path.join(' ')}", o[:show_cmd]) if o[:show_cmd] paths.each { |path| pa = Pa(path) puts "rm -r #{pa.p}" if o[:verbose] File.directory?(pa.p) ? _rmdir(pa) : File.delete(pa.p) } end |
#rmdir(*paths) ⇒ nil
rm directory only. still remove if directory is not empty.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/pa/cmd.rb', line 275 def rmdir(*paths) paths, o = Util.(paths) extra_doc = o[:force] ? "-f " : nil puts _wrap_cmd("rmdir #{extra_doc}#{paths.join(" ")}", o[:show_cmd]) if o[:show_cmd] paths.each { |path| pa = Pa(path) 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}" end end _rmdir(pa) } end |
#rmdir_f(*paths) ⇒ Object
294 295 296 297 298 |
# File 'lib/pa/cmd.rb', line 294 def rmdir_f(*paths) paths, o = Util.(paths) o[:force] = true rmdir *paths, o end |
#symln(src_s, dest, o = {}) ⇒ nil
symbol link
70 71 72 |
# File 'lib/pa/cmd.rb', line 70 def symln(src_s, dest, o={}) _ln(:symlink, src_s, dest, o) end |
#symln_f(src_s, dest, o = {}) ⇒ nil
symln force
79 80 81 82 |
# File 'lib/pa/cmd.rb', line 79 def symln_f(src_s, dest, o={}) o[:force]=true _ln(:symlink, src_s, dest, o) end |
#touch(*paths, o = {}) ⇒ nil
touch a blank file
126 127 128 129 |
# File 'lib/pa/cmd.rb', line 126 def touch(*args) paths, o = Util.(args) _touch(paths, o) end |
#touch_f(*paths, o = {}) ⇒ nil
touch force
136 137 138 139 140 |
# File 'lib/pa/cmd.rb', line 136 def touch_f(*args) paths, o = Util.(args) o[:force] = true _touch(paths, o) end |