Module: FileOperations

Included in:
Installer, ToplevelInstallerMulti
Defined in:
lib/priority-queue/setup.rb

Overview

This module requires: #verbose?, #no_harm?

Constant Summary collapse

DIR_REJECT =
%w( . .. CVS SCCS RCS CVS.adm .svn )

Instance Method Summary collapse

Instance Method Details

#command(*args) ⇒ Object



654
655
656
657
658
# File 'lib/priority-queue/setup.rb', line 654

def command(*args)
  $stderr.puts args.join(' ') if verbose?
  system(*args) or raise RuntimeError,
      "system(#{args.map{|a| a.inspect }.join(' ')}) failed"
end

#diff?(new_content, path) ⇒ Boolean



649
650
651
652
# File 'lib/priority-queue/setup.rb', line 649

def diff?(new_content, path)
  return true unless File.exist?(path)
  new_content != File.binread(path)
end

#directories_of(dir) ⇒ Object



680
681
682
683
684
# File 'lib/priority-queue/setup.rb', line 680

def directories_of(dir)
  Dir.open(dir) {|d|
    return d.select {|ent| File.dir?("#{dir}/#{ent}") } - DIR_REJECT
  }
end

#extdir?(dir) ⇒ Boolean



668
669
670
# File 'lib/priority-queue/setup.rb', line 668

def extdir?(dir)
  File.exist?("#{dir}/MANIFEST") or File.exist?("#{dir}/extconf.rb")
end

#files_of(dir) ⇒ Object



672
673
674
675
676
# File 'lib/priority-queue/setup.rb', line 672

def files_of(dir)
  Dir.open(dir) {|d|
    return d.select {|ent| File.file?("#{dir}/#{ent}") }
  }
end

#force_remove_file(path) ⇒ Object



611
612
613
614
615
616
# File 'lib/priority-queue/setup.rb', line 611

def force_remove_file(path)
  begin
    remove_file path
  rescue
  end
end

#install(from, dest, mode, prefix = nil) ⇒ Object



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
# File 'lib/priority-queue/setup.rb', line 623

def install(from, dest, mode, prefix = nil)
  $stderr.puts "install #{from} #{dest}" if verbose?
  return if no_harm?

  realdest = prefix ? prefix + File.expand_path(dest) : dest
  realdest = File.join(realdest, File.basename(from)) if File.dir?(realdest)
  str = File.binread(from)
  if diff?(str, realdest)
    verbose_off {
      rm_f realdest if File.exist?(realdest)
    }
    File.open(realdest, 'wb') {|f|
      f.write str
    }
    File.chmod mode, realdest

    File.open("#{objdir_root()}/InstalledFiles", 'a') {|f|
      if prefix
        f.puts realdest.sub(prefix, '')
      else
        f.puts realdest
      end
    }
  end
end

#make(task = nil) ⇒ Object



664
665
666
# File 'lib/priority-queue/setup.rb', line 664

def make(task = nil)
  command(*[config('makeprog'), task].compact)
end

#mkdir_p(dirname, prefix = nil) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/priority-queue/setup.rb', line 539

def mkdir_p(dirname, prefix = nil)
  dirname = prefix + File.expand_path(dirname) if prefix
  $stderr.puts "mkdir -p #{dirname}" if verbose?
  return if no_harm?

  # Does not check '/', it's too abnormal.
  dirs = File.expand_path(dirname).split(%r<(?=/)>)
  if /\A[a-z]:\z/i =~ dirs[0]
    disk = dirs.shift
    dirs[0] = disk + dirs[0]
  end
  dirs.each_index do |idx|
    path = dirs[0..idx].join('')
    Dir.mkdir path unless File.dir?(path)
  end
end

#move_file(src, dest) ⇒ Object



598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/priority-queue/setup.rb', line 598

def move_file(src, dest)
  force_remove_file dest
  begin
    File.rename src, dest
  rescue
    File.open(dest, 'wb') {|f|
      f.write File.binread(src)
    }
    File.chmod File.stat(src).mode, dest
    File.unlink src
  end
end

#remove_file(path) ⇒ Object



618
619
620
621
# File 'lib/priority-queue/setup.rb', line 618

def remove_file(path)
  File.chmod 0777, path
  File.unlink path
end

#remove_tree(path) ⇒ Object



568
569
570
571
572
573
574
575
576
# File 'lib/priority-queue/setup.rb', line 568

def remove_tree(path)
  if File.symlink?(path)
    remove_file path
  elsif File.dir?(path)
    remove_tree0 path
  else
    force_remove_file path
  end
end

#remove_tree0(path) ⇒ Object



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/priority-queue/setup.rb', line 578

def remove_tree0(path)
  Dir.foreach(path) do |ent|
    next if ent == '.'
    next if ent == '..'
    entpath = "#{path}/#{ent}"
    if File.symlink?(entpath)
      remove_file entpath
    elsif File.dir?(entpath)
      remove_tree0 entpath
    else
      force_remove_file entpath
    end
  end
  begin
    Dir.rmdir path
  rescue Errno::ENOTEMPTY
    # directory may not be empty
  end
end

#rm_f(path) ⇒ Object



556
557
558
559
560
# File 'lib/priority-queue/setup.rb', line 556

def rm_f(path)
  $stderr.puts "rm -f #{path}" if verbose?
  return if no_harm?
  force_remove_file path
end

#rm_rf(path) ⇒ Object



562
563
564
565
566
# File 'lib/priority-queue/setup.rb', line 562

def rm_rf(path)
  $stderr.puts "rm -rf #{path}" if verbose?
  return if no_harm?
  remove_tree path
end

#ruby(*args) ⇒ Object



660
661
662
# File 'lib/priority-queue/setup.rb', line 660

def ruby(*args)
  command config('rubyprog'), *args
end