Module: Pa::Cmd::ClassMethods

Defined in:
lib/pa/cmd.rb

Constant Summary collapse

DELEGATE_METHODS =
[ :home ]

Instance Method Summary collapse

Instance Method Details

#_copy(src, dest, o = {}) ⇒ Object

I’m recursive

Parameters:

  • src (String)
  • dest (String)

Raises:

  • (Errno::EEXIST)


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
646
647
648
# File 'lib/pa/cmd.rb', line 608

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

Parameters:

  • src_s (Array, String, #path)
  • dest (String, #path)


525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/pa/cmd.rb', line 525

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]

  Pa.glob(*srcs) {|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] 

    File.send(method, src, dest)
  }	
end

#_mkdir(paths, o) ⇒ Object



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/pa/cmd.rb', line 551

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



582
583
584
585
586
587
588
589
590
591
592
# File 'lib/pa/cmd.rb', line 582

def _mktmpname(name=nil, o={})
  o[:tmpdir] ||= ENV["TEMP"]
  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”

Parameters:

  • src (String)
  • dest (String)

Raises:

  • (Errno::EEXIST)


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/pa/cmd.rb', line 474

def _move(src, dest, o)
  raise Errno::EEXIST, "dest exists -- #{dest}" if File.exists?(dest) and (not o[:force])

  # :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



596
597
598
599
600
601
602
# File 'lib/pa/cmd.rb', line 596

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

Parameters:

  • paths (Array<String,Pa>)


499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/pa/cmd.rb', line 499

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.win32?
      # win32 BUG. must f.write("") then file can be deleted.
      File.open(p, "w"){|f| f.chmod(o[:mode]); f.write("")} 
    else
      File.open(p, "w"){|f| f.chmod(o[:mode])}
    end
  }
end

#cd(path = , o = {}, &blk) ⇒ Object

change directory

Parameters:

  • path (String, Pa) (defaults to: )
  • o (Hash) (defaults to: {})

Options Hash (o):

  • :verbose (Boolean)

    verbose mode

  • :show_cmd (Boolean)

    puts cmd



93
94
95
96
97
# File 'lib/pa/cmd.rb', line 93

def cd(path=ENV["HOME"], o={}, &blk)
  p = get(path)
  puts "cd #{p}" if (o[:verbose] or o[:show_cmd])
  Dir.chdir(p, &blk) 
end

#chroot(path, o = {}) ⇒ nil

chroot

Parameters:

  • path (String)
  • o (Hash) (defaults to: {})

Options Hash (o):

  • :verbose (Boolean)

    verbose mode

  • :show_cmd (Boolean)

    puts cmd

Returns:

  • (nil)

See Also:

  • Pa::Cmd::ClassMethods.{Dir{Dir.chroot}


107
108
109
110
111
# File 'lib/pa/cmd.rb', line 107

def chroot(path, o={})
  p = get(path)
  puts "chdroot #{p}" 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.

Examples:

cp '*', 'dir' do |src, dest, o|
  skip if src.name=~'.o$'
  dest.replace 'dirc' if src.name=="foo"
  yield  # use yield to do the actuactal cp work
end

Overloads:

  • #cp(src_s, dest, o) ⇒ nil

    Parameters:

    • src_s (Array<String>, String)

      support globbing

    • dest (String, Pa)
    • o (Hash)

      option

    Options Hash (o):

    • :mkdir (Boolean)

      mkdir(dest) if dest not exists.

    • :verbose (Boolean)

      puts cmd when execute

    • :show_cmd (Boolean)

      puts cmd

    • :folsymlink (Boolean)

      follow symlink

    • :force (Boolean)

      force dest file if dest is a file

    • :special (Boolean)

      special copy, when cp a directory, only mkdir, not cp the directory’s content, usefull in Pa.each_r

    Returns:

    • (nil)
  • #cp(src_s, dest, o) {|src, dest, o| ... } ⇒ nil

    Yields:

    • (src, dest, o)

    Returns:

    • (nil)


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
# File 'lib/pa/cmd.rb', line 395

def cp(src_s, dest, o={}, &blk)
  srcs = Pa.glob(*Util.wrap_array(src_s)).map{|v| v.path}
  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



421
422
423
424
# File 'lib/pa/cmd.rb', line 421

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.

Examples:


empty_dir("foo")

Parameters:

  • *dirs (String)
  • o (Hash)

    options

Returns:

  • (nil)


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/pa/cmd.rb', line 307

def empty_dir(*dirs)
  dirs, o = Util.extract_options(dirs)
  extra_doc = o[:force] ? "-f " : nil
  puts "empty_dir #{extra_doc}#{dirs.join(" ")}" 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



324
325
326
327
328
# File 'lib/pa/cmd.rb', line 324

def empty_dir_f(*dirs)
  dirs, o = Util.extract_options(dirs)
  o[:force] = true
  empty_dir *dirs, o
end

#home2Object



36
37
38
# File 'lib/pa/cmd.rb', line 36

def home2
  Dir.home 
end

#ln(src, dest) ⇒ nil #ln([src,..], directory) ⇒ nil Also known as: symlink

link

Parameters:

  • src_s (Array<String>, String)

    support globbing

  • dest (String, Pa)
  • o (Hash) (defaults to: {})

Options Hash (o):

  • :show_cmd (Boolean)

    puts cmd

  • :verbose (Boolean)

    verbose mode

Returns:

  • (nil)


51
52
53
# File 'lib/pa/cmd.rb', line 51

def ln(src_s, dest, o={})
  _ln(:link, src_s, dest, o) 
end

#ln_f(src_s, dest, o = {}) ⇒ nil

ln force

Returns:

  • (nil)

See Also:



59
60
61
62
# File 'lib/pa/cmd.rb', line 59

def ln_f(src_s, dest, o={})
  o[:force] = true
  _ln(:link, src_s, dest, o) 
end

#mkdir(*paths, o = {}) ⇒ nil

make a directory

Parameters:

  • *paths (String, Pa)
  • o (Hash) (defaults to: {})

    option

Options Hash (o):

  • :mode (Fixnum) — default: 0775
  • :force (Boolean)
  • :verbose (Boolean)

    verbose mode

  • :show_cmd (Boolean)

    puts cmd

Returns:

  • (nil)


150
151
152
153
# File 'lib/pa/cmd.rb', line 150

def mkdir(*args)
  paths, o = Util.extract_options(args)
  _mkdir(paths, o) 
end

#mkdir_f(*paths, o = {}) ⇒ nil

mkdir force

Returns:

  • (nil)

See Also:



160
161
162
163
164
# File 'lib/pa/cmd.rb', line 160

def mkdir_f(*args)
  paths, o = Util.extract_options(args)
  o[:force]=true
  _mkdir(paths, o)
end

#mktmpdir(name, o = {}, &blk) ⇒ String #mktmpdir(o = {}, &blk) ⇒ Object

make temp directory

Overloads:

  • #mktmpdir(name, o = {}, &blk) ⇒ String

    Returns path.

    Parameters:

    • o (Hash) (defaults to: {})

      options

    Options Hash (o):

    • :tmpdir (String) — default: ENV["TEMP"]
    • :verbose (Symbol)

      verbose mode

    • :show_cmd (Symbol)

      puts cmd

    Returns:

    • (String)

      path



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pa/cmd.rb', line 175

def mktmpdir(*args, &blk)
  (name,), o = Util.extract_options(args)

  p = _mktmpname(name, o)
  puts "mktmpdir #{p}" 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.

Returns:

  • (Pa)

    path



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/pa/cmd.rb', line 217

def mktmpfile(*args, &blk)
  (name,), o = Util.extract_options(args)

  p = _mktmpname(name, o) 
  puts "mktmpfile #{p}" 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

Returns path.

Parameters:

  • o (Hash) (defaults to: {})

    options

Options Hash (o):

  • :verbose (Boolean)

    verbose mode

  • :show_cmd (Boolean)

    puts cmd

  • :tmpdir (String)

Returns:

  • (String)

    path

See Also:



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/pa/cmd.rb', line 201

def mktmpfile2(*args, &blk) 
  (name,), o = Util.extract_options(args)

  p = _mktmpname(name, o) 
  puts "mktmpfile #{p}" 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.

Parameters:

  • o (Hash) (defaults to: {})

    option

Options Hash (o):

  • :verbose (Boolean)

    verbose mode

  • :show_cmd (Boolean)

    puts cmd

  • :mkdir (Boolean)
  • :fore (Boolean)

Returns:

  • (nil)

See Also:



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
# File 'lib/pa/cmd.rb', line 435

def mv(src_s, dest, o={}, &blk)
  srcs = Pa.glob(*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



463
464
465
466
# File 'lib/pa/cmd.rb', line 463

def mv_f(src_s, dest, o={}, &blk)
  o[:force] = true
  mv src_s, dest, o, &blk
end

See Also:

  • File.readlink


83
84
85
# File 'lib/pa/cmd.rb', line 83

def readlink(path)
  File.readlink(get(path)) 
end

#rm(*paths, o = {}) ⇒ nil

rm file only

Parameters:

  • *paths (String)

    support globbing

  • o (Boolean) (defaults to: {})

    :verbose verbose mode

  • o (Boolean) (defaults to: {})

    :show_cmd puts cmd

Returns:

  • (nil)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/pa/cmd.rb', line 239

def rm(*paths)
  paths, o = Util.extract_options(paths)
  extra_doc = o[:force] ? "-f " : nil
  puts "rm #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]

  Pa.glob(*paths) { |pa|
    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



259
260
261
262
263
# File 'lib/pa/cmd.rb', line 259

def rm_f(*paths)
  paths, o = Util.extract_options(paths)
  o[:force] = true
  rm *paths, o
end

#rm_if(*paths) {|path| ... } ⇒ nil

rm_if(path) if condition is true

Examples:

Pa.rm_if '/tmp/**/*.rb' do |pa|
  pa.name == 'old'
end

Parameters:

  • *paths (String)

    support globbing

Yields:

  • (path)

Yield Parameters:

  • path (Pa)

Yield Returns:

  • (Boolean)

    rm_r path if true

Returns:

  • (nil)


356
357
358
359
360
361
# File 'lib/pa/cmd.rb', line 356

def rm_if(*paths, &blk)
  paths, o = Util.extract_options(paths)
  Pa.glob(*paths) do |pa|
    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

Returns:

  • (nil)

See Also:



334
335
336
337
338
339
340
341
# File 'lib/pa/cmd.rb', line 334

def rm_r(*paths)
  paths, o = Util.extract_options(paths)
  puts "rm -r #{path.join(" ")}" if o[:show_cmd]
  Pa.glob(*paths){ |pa|
    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.

Parameters:

  • *paths (String)

    support globbing

  • o (Hash)

    options

Returns:

  • (nil)


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/pa/cmd.rb', line 272

def rmdir(*paths)
  paths, o = Util.extract_options(paths)
  extra_doc = o[:force] ? "-f " : nil
  puts "rmdir #{extra_doc}#{paths.join(" ")}" if o[:show_cmd]
  Pa.glob(*paths) { |pa|
    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



290
291
292
293
294
# File 'lib/pa/cmd.rb', line 290

def rmdir_f(*paths)
  paths, o = Util.extract_options(paths)
  o[:force] = true
  rmdir *paths, o
end

#symln(src_s, dest, o = {}) ⇒ nil

symbol link

Returns:

  • (nil)

See Also:



68
69
70
# File 'lib/pa/cmd.rb', line 68

def symln(src_s, dest, o={})
  _ln(:symlink, src_s, dest, o) 
end

#symln_f(src_s, dest, o = {}) ⇒ nil

symln force

Returns:

  • (nil)

See Also:



77
78
79
80
# File 'lib/pa/cmd.rb', line 77

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

Parameters:

  • *paths (String)
  • o (Hash) (defaults to: {})

    option

Options Hash (o):

  • :mode (Fixnum, String) — default: 0664
  • :mkdir (Boolean)

    auto mkdir if path contained directory not exists.

  • :force (Boolean)
  • :verbose (Boolean)

    verbose mode

  • :show_cmd (Boolean)

    puts cmd

Returns:

  • (nil)


124
125
126
127
# File 'lib/pa/cmd.rb', line 124

def touch(*args)
  paths, o = Util.extract_options(args)
  _touch(paths, o) 
end

#touch_f(*paths, o = {}) ⇒ nil

touch force

Returns:

  • (nil)

See Also:



134
135
136
137
138
# File 'lib/pa/cmd.rb', line 134

def touch_f(*args)
  paths, o = Util.extract_options(args)
  o[:force] = true
  _touch(paths, o) 
end