Module: FileOperations

Included in:
Installer, ToplevelInstallerMulti
Defined in:
lib/roebe/setup/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



714
715
716
717
718
# File 'lib/roebe/setup/setup.rb', line 714

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

diff?

Returns:

  • (Boolean)


709
710
711
712
# File 'lib/roebe/setup/setup.rb', line 709

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

#directories_of(dir) ⇒ Object



740
741
742
743
744
# File 'lib/roebe/setup/setup.rb', line 740

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

#extdir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


728
729
730
# File 'lib/roebe/setup/setup.rb', line 728

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

#files_of(dir) ⇒ Object



732
733
734
735
736
# File 'lib/roebe/setup/setup.rb', line 732

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

#force_remove_file(path) ⇒ Object



665
666
667
668
669
670
# File 'lib/roebe/setup/setup.rb', line 665

def force_remove_file(path)
  begin
    remove_file path
  rescue
  end
end

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

install



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/roebe/setup/setup.rb', line 678

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)
    }
    begin
      File.open(realdest, 'wb') {|f|
        f.write(str)
      }
    rescue Errno::ENOENT => error
      pp error
    end
    File.chmod(mode, realdest) if File.exist? 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



724
725
726
# File 'lib/roebe/setup/setup.rb', line 724

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

#mkdir_p(dirname, prefix = nil) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/roebe/setup/setup.rb', line 593

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



652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/roebe/setup/setup.rb', line 652

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



672
673
674
675
# File 'lib/roebe/setup/setup.rb', line 672

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

#remove_tree(path) ⇒ Object



622
623
624
625
626
627
628
629
630
# File 'lib/roebe/setup/setup.rb', line 622

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



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/roebe/setup/setup.rb', line 632

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



610
611
612
613
614
# File 'lib/roebe/setup/setup.rb', line 610

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

#rm_rf(path) ⇒ Object



616
617
618
619
620
# File 'lib/roebe/setup/setup.rb', line 616

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

#ruby(*args) ⇒ Object



720
721
722
# File 'lib/roebe/setup/setup.rb', line 720

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