2003-01-24 19:13:33 +03:00
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# = fileutils.rb
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Copyright (c) 2000-2002 Minero Aoki <aamine@loveruby.net>
|
|
|
|
#
|
|
|
|
# This program is free software.
|
|
|
|
# You can distribute/modify this program under the same terms of ruby.
|
|
|
|
#
|
|
|
|
# == module FileUtils
|
|
|
|
#
|
|
|
|
# Namespace for several file utility methods for copying, moving, removing, etc.
|
|
|
|
#
|
|
|
|
# === Module Functions
|
|
|
|
#
|
|
|
|
# cd( dir, *options )
|
|
|
|
# cd( dir, *options ) {|dir| .... }
|
2003-02-21 14:57:31 +03:00
|
|
|
# pwd()
|
2003-01-24 19:13:33 +03:00
|
|
|
# mkdir( dir, *options )
|
|
|
|
# mkdir_p( dir, *options )
|
|
|
|
# rmdir( dir, *options )
|
|
|
|
# ln( old, new, *options )
|
|
|
|
# ln( list, destdir, *options )
|
|
|
|
# ln_s( old, new, *options )
|
|
|
|
# ln_s( list, destdir, *options )
|
|
|
|
# ln_sf( src, dest, *options )
|
|
|
|
# cp( src, dest, *options )
|
|
|
|
# cp( list, dir, *options )
|
|
|
|
# cp_r( src, dest, *options )
|
|
|
|
# cp_r( list, dir, *options )
|
|
|
|
# mv( src, dest, *options )
|
|
|
|
# mv( list, dir, *options )
|
|
|
|
# rm( list, *options )
|
|
|
|
# rm_r( list, *options )
|
|
|
|
# rm_rf( list, *options )
|
|
|
|
# install( src, dest, mode = <src's>, *options )
|
|
|
|
# chmod( mode, list, *options )
|
|
|
|
# touch( list, *options )
|
|
|
|
#
|
|
|
|
# The <tt>*options</tt> parameter is a list of 0-3 options, taken from the list
|
|
|
|
# +:force+, +:noop+, +:preserve+, and +:verbose+. +:noop+ means that no changes
|
|
|
|
# are made. The other two are obvious. Each method documents the options that
|
|
|
|
# it honours.
|
|
|
|
#
|
|
|
|
# All methods that have the concept of a "source" file or directory can take
|
|
|
|
# either one file or a list of files in that argument. See the method
|
|
|
|
# documentation for examples.
|
|
|
|
#
|
2003-02-21 14:57:31 +03:00
|
|
|
# There are some `low level' methods, which does not accept any option:
|
|
|
|
#
|
|
|
|
# uptodate?( file, cmp_list )
|
|
|
|
# copy_file( srcfilename, destfilename )
|
|
|
|
# copy_stream( srcstream, deststream )
|
|
|
|
# compare_file( file_a, file_b )
|
|
|
|
# compare_stream( stream_a, stream_b )
|
|
|
|
#
|
2003-01-24 19:13:33 +03:00
|
|
|
# == module FileUtils::Verbose
|
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# This module has all methods of FileUtils module, but it outputs messages
|
|
|
|
# before acting. This equates to passing the +:verbose+ flag to methods in
|
|
|
|
# FileUtils.
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# == module FileUtils::NoWrite
|
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# This module has all methods of FileUtils module, but never changes
|
2003-01-24 19:13:33 +03:00
|
|
|
# files/directories. This equates to passing the +:noop+ flag to methods in
|
|
|
|
# FileUtils.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2002-03-26 05:03:04 +03:00
|
|
|
module FileUtils
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
# All methods are module_function.
|
|
|
|
|
2003-02-09 01:01:53 +03:00
|
|
|
#
|
|
|
|
# Options: (none)
|
|
|
|
#
|
|
|
|
# Returns the name of the current directory.
|
|
|
|
#
|
|
|
|
def pwd
|
|
|
|
Dir.pwd
|
|
|
|
end
|
|
|
|
|
|
|
|
alias getwd pwd
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Changes the current directory to the directory +dir+.
|
|
|
|
#
|
|
|
|
# If this method is called with block, resumes to the old
|
|
|
|
# working directory after the block execution finished.
|
|
|
|
#
|
|
|
|
# FileUtils.cd '/', :verbose # chdir and report it
|
|
|
|
#
|
|
|
|
def cd( dir, *options, &block ) # :yield: dir
|
2002-03-26 05:03:04 +03:00
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
fu_output_message "cd #{dir}" if verbose
|
2003-02-21 14:57:31 +03:00
|
|
|
Dir.chdir(dir, &block) unless noop
|
2002-03-26 05:03:04 +03:00
|
|
|
fu_output_message 'cd -' if verbose and block
|
|
|
|
end
|
|
|
|
|
|
|
|
alias chdir cd
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2003-02-09 01:01:53 +03:00
|
|
|
# Options: (none)
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Returns true if +newer+ is newer than all +old_list+.
|
|
|
|
# Non-existent files are older than any file.
|
|
|
|
#
|
2003-02-10 13:48:38 +03:00
|
|
|
# FileUtils.uptodate? 'hello.o', %w(hello.c hello.h) or system 'make hello.o'
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2002-12-27 07:21:27 +03:00
|
|
|
def uptodate?( new, old_list, *options )
|
2003-02-09 01:01:53 +03:00
|
|
|
raise ArgumentError, 'uptodate? does not accept any option' unless options.empty?
|
2002-03-26 05:03:04 +03:00
|
|
|
|
|
|
|
return false unless FileTest.exist? new
|
2003-02-10 13:48:38 +03:00
|
|
|
new_time = File.mtime(new)
|
2002-12-27 07:21:27 +03:00
|
|
|
old_list.each do |old|
|
2002-10-17 07:54:25 +04:00
|
|
|
if FileTest.exist? old
|
2002-03-26 05:03:04 +03:00
|
|
|
return false unless new_time > File.mtime(old)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Creates one or more directories.
|
|
|
|
#
|
|
|
|
# FileUtils.mkdir 'test'
|
|
|
|
# FileUtils.mkdir %w( tmp data )
|
|
|
|
# FileUtils.mkdir 'notexist', :noop # Does not really create.
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def mkdir( list, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
list = fu_list(list)
|
|
|
|
fu_output_message "mkdir #{list.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
|
|
|
list.each do |dir|
|
|
|
|
Dir.mkdir dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Creates a directory and all its parent directories.
|
|
|
|
# For example,
|
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# FileUtils.mkdir_p '/usr/local/lib/ruby'
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# causes to make following directories, if it does not exist.
|
2003-01-24 19:13:33 +03:00
|
|
|
# * /usr
|
|
|
|
# * /usr/local
|
2003-02-06 12:56:13 +03:00
|
|
|
# * /usr/local/lib
|
|
|
|
# * /usr/local/lib/ruby
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# You can pass several directories at a time in a list.
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def mkdir_p( list, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
list = fu_list(list)
|
|
|
|
fu_output_message "mkdir -p #{list.join ' '}" if verbose
|
|
|
|
return *list if noop
|
|
|
|
|
2002-10-17 07:54:25 +04:00
|
|
|
list.map {|n| File.expand_path(n) }.each do |dir|
|
2002-03-26 05:03:04 +03:00
|
|
|
stack = []
|
2002-10-17 07:54:25 +04:00
|
|
|
until FileTest.directory? dir
|
2002-03-26 05:03:04 +03:00
|
|
|
stack.push dir
|
|
|
|
dir = File.dirname(dir)
|
|
|
|
end
|
2002-10-17 07:54:25 +04:00
|
|
|
stack.reverse_each do |n|
|
|
|
|
Dir.mkdir n
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return *list
|
|
|
|
end
|
|
|
|
|
|
|
|
alias mkpath mkdir_p
|
|
|
|
alias makedirs mkdir_p
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop, verbose
|
|
|
|
#
|
|
|
|
# Removes one or more directories.
|
|
|
|
#
|
|
|
|
# FileUtils.rmdir 'somedir'
|
|
|
|
# FileUtils.rmdir %w(somedir anydir otherdir)
|
|
|
|
# # Does not really remove directory; outputs message.
|
|
|
|
# FileUtils.rmdir 'somedir', :verbose, :noop
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def rmdir( list, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
list = fu_list(list)
|
|
|
|
fu_output_message "rmdir #{list.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
|
|
|
list.each do |dir|
|
|
|
|
Dir.rmdir dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: force noop verbose
|
|
|
|
#
|
|
|
|
# <b><tt>ln( old, new, *options )</tt></b>
|
|
|
|
#
|
|
|
|
# Creates a hard link +new+ which points to +old+.
|
|
|
|
# If +new+ already exists and it is a directory, creates a symbolic link +new/old+.
|
|
|
|
# If +new+ already exists and it is not a directory, raises Errno::EEXIST.
|
|
|
|
# But if :force option is set, overwrite +new+.
|
|
|
|
#
|
|
|
|
# FileUtils.ln 'gcc', 'cc', :verbose
|
|
|
|
# FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
|
|
|
|
#
|
|
|
|
# <b><tt>ln( list, destdir, *options )</tt></b>
|
|
|
|
#
|
|
|
|
# Creates several hard links in a directory, with each one pointing to the
|
|
|
|
# item in +list+. If +destdir+ is not a directory, raises Errno::ENOTDIR.
|
|
|
|
#
|
|
|
|
# include FileUtils
|
|
|
|
# cd '/bin'
|
|
|
|
# ln %w(cp mv mkdir), '/usr/bin' # Now /usr/bin/cp and /bin/cp are linked.
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def ln( src, dest, *options )
|
|
|
|
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
2003-01-06 12:34:16 +03:00
|
|
|
fu_output_message "ln#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
2002-03-26 05:03:04 +03:00
|
|
|
return if noop
|
|
|
|
|
2002-10-17 07:54:25 +04:00
|
|
|
fu_each_src_dest(src, dest) do |s,d|
|
2002-03-26 05:03:04 +03:00
|
|
|
remove_file d, true if force
|
|
|
|
File.link s, d
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias link ln
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: force noop verbose
|
|
|
|
#
|
|
|
|
# <b><tt>ln_s( old, new, *options )</tt></b>
|
|
|
|
#
|
|
|
|
# Creates a symbolic link +new+ which points to +old+. If +new+ already
|
|
|
|
# exists and it is a directory, creates a symbolic link +new/old+. If +new+
|
|
|
|
# already exists and it is not a directory, raises Errno::EEXIST. But if
|
|
|
|
# :force option is set, overwrite +new+.
|
|
|
|
#
|
|
|
|
# FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
|
|
|
|
# FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force
|
|
|
|
#
|
|
|
|
# <b><tt>ln_s( list, destdir, *options )</tt></b>
|
|
|
|
#
|
|
|
|
# Creates several symbolic links in a directory, with each one pointing to the
|
|
|
|
# item in +list+. If +destdir+ is not a directory, raises Errno::ENOTDIR.
|
|
|
|
#
|
|
|
|
# If +destdir+ is not a directory, raises Errno::ENOTDIR.
|
|
|
|
#
|
|
|
|
# FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def ln_s( src, dest, *options )
|
|
|
|
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
|
|
|
fu_output_message "ln -s#{force ? 'f' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
2002-10-17 07:54:25 +04:00
|
|
|
fu_each_src_dest(src, dest) do |s,d|
|
2002-03-26 05:03:04 +03:00
|
|
|
remove_file d, true if force
|
|
|
|
File.symlink s, d
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias symlink ln_s
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Same as
|
|
|
|
# #ln_s(src, dest, :force)
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def ln_sf( src, dest, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
ln_s src, dest, :force, *options
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: preserve noop verbose
|
|
|
|
#
|
|
|
|
# Copies a file +src+ to +dest+. If +dest+ is a directory, copies
|
|
|
|
# +src+ to +dest/src+.
|
|
|
|
#
|
|
|
|
# If +src+ is a list of files, then +dest+ must be a directory.
|
|
|
|
#
|
|
|
|
# FileUtils.cp 'eval.c', 'eval.c.org'
|
2003-02-06 12:56:13 +03:00
|
|
|
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'
|
|
|
|
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def cp( src, dest, *options )
|
|
|
|
preserve, noop, verbose, = fu_parseargs(options, :preserve, :noop, :verbose)
|
|
|
|
fu_output_message "cp#{preserve ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
2002-10-17 07:54:25 +04:00
|
|
|
fu_each_src_dest(src, dest) do |s,d|
|
2002-03-26 05:03:04 +03:00
|
|
|
fu_preserve_attr(preserve, s, d) {
|
|
|
|
copy_file s, d
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias copy cp
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: preserve noop verbose
|
|
|
|
#
|
|
|
|
# Copies +src+ to +dest+. If +src+ is a directory, this method copies
|
|
|
|
# all its contents recursively. If +dest+ is a directory, copies
|
|
|
|
# +src+ to +dest/src+.
|
|
|
|
#
|
|
|
|
# +src+ can be a list of files.
|
|
|
|
#
|
|
|
|
# # Installing ruby library "mylib" under the site_ruby
|
|
|
|
# FileUtils.rm_r site_ruby + '/mylib', :force
|
|
|
|
# FileUtils.cp_r 'lib/', site_ruby + '/mylib'
|
|
|
|
#
|
|
|
|
# # Examples of copying several files to target directory.
|
|
|
|
# FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
|
|
|
|
# FileUtils.cp_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop, :verbose
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def cp_r( src, dest, *options )
|
|
|
|
preserve, noop, verbose, = fu_parseargs(options, :preserve, :noop, :verbose)
|
2003-01-06 12:34:16 +03:00
|
|
|
fu_output_message "cp -r#{preserve ? 'p' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
2002-03-26 05:03:04 +03:00
|
|
|
return if noop
|
|
|
|
|
2002-10-17 07:54:25 +04:00
|
|
|
fu_each_src_dest(src, dest) do |s,d|
|
|
|
|
if FileTest.directory? s
|
2002-03-26 05:03:04 +03:00
|
|
|
fu_copy_dir s, d, '.', preserve
|
|
|
|
else
|
|
|
|
fu_p_copy s, d, preserve
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
def fu_copy_dir( src, dest, rel, preserve ) #:nodoc:
|
2002-12-27 07:21:27 +03:00
|
|
|
fu_preserve_attr(preserve, "#{src}/#{rel}", "#{dest}/#{rel}") {|s,d|
|
2002-03-26 05:03:04 +03:00
|
|
|
dir = File.expand_path(d) # to remove '/./'
|
|
|
|
Dir.mkdir dir unless FileTest.directory? dir
|
|
|
|
}
|
2002-12-27 07:21:27 +03:00
|
|
|
Dir.entries("#{src}/#{rel}").each do |fname|
|
|
|
|
if FileTest.directory? File.join(src,rel,fname)
|
|
|
|
next if /\A\.\.?\z/ === fname
|
|
|
|
fu_copy_dir src, dest, "#{rel}/#{fname}", preserve
|
2002-03-26 05:03:04 +03:00
|
|
|
else
|
2002-12-27 07:21:27 +03:00
|
|
|
fu_p_copy File.join(src,rel,fname), File.join(dest,rel,fname), preserve
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :fu_copy_dir
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
def fu_p_copy( src, dest, really ) #:nodoc:
|
2002-12-27 07:21:27 +03:00
|
|
|
fu_preserve_attr(really, src, dest) {
|
2002-03-26 05:03:04 +03:00
|
|
|
copy_file src, dest
|
|
|
|
}
|
|
|
|
end
|
|
|
|
private :fu_p_copy
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
def fu_preserve_attr( really, src, dest ) #:nodoc:
|
2002-10-17 07:54:25 +04:00
|
|
|
unless really
|
2002-03-26 05:03:04 +03:00
|
|
|
yield src, dest
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
st = File.stat(src)
|
|
|
|
yield src, dest
|
|
|
|
File.utime st.atime, st.mtime, dest
|
|
|
|
begin
|
2002-10-17 07:54:25 +04:00
|
|
|
File.chown st.uid, st.gid, dest
|
2002-03-26 05:03:04 +03:00
|
|
|
rescue Errno::EPERM
|
|
|
|
File.chmod st.mode & 01777, dest # clear setuid/setgid
|
|
|
|
else
|
|
|
|
File.chmod st.mode, dest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :fu_preserve_attr
|
|
|
|
|
2003-02-21 14:57:31 +03:00
|
|
|
#
|
|
|
|
# Copies file +src+ to +dest+.
|
|
|
|
# Both of +src+ and +dest+ must be a filename.
|
|
|
|
#
|
|
|
|
def copy_file( src, dest )
|
2002-10-17 07:54:25 +04:00
|
|
|
File.open(src, 'rb') {|r|
|
|
|
|
File.open(dest, 'wb') {|w|
|
2003-02-21 14:57:31 +03:00
|
|
|
copy_stream r, w
|
2002-03-26 05:03:04 +03:00
|
|
|
} }
|
|
|
|
end
|
|
|
|
|
2003-02-21 14:57:31 +03:00
|
|
|
#
|
|
|
|
# Copies stream +src+ to +dest+.
|
|
|
|
# Both of +src+ and +dest+ must be a IO.
|
|
|
|
#
|
|
|
|
def copy_stream( src, dest )
|
|
|
|
bsize = fu_stream_blksize(src, dest)
|
|
|
|
begin
|
|
|
|
while true
|
|
|
|
dest.syswrite src.sysread(bsize)
|
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
end
|
|
|
|
end
|
2002-03-26 05:03:04 +03:00
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Moves file(s) +src+ to +dest+. If +file+ and +dest+ exist on the different
|
|
|
|
# disk partition, the file is copied instead.
|
|
|
|
#
|
|
|
|
# FileUtils.mv 'badname.rb', 'goodname.rb'
|
|
|
|
# FileUtils.mv 'stuff.rb', 'lib/ruby', :force
|
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
|
|
|
|
# FileUtils.mv Dir.glob('test*.rb'), 'test', :noop, :verbose
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def mv( src, dest, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
fu_output_message "mv #{[src,dest].flatten.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
2002-10-17 07:54:25 +04:00
|
|
|
fu_each_src_dest(src, dest) do |s,d|
|
|
|
|
if cannot_overwrite_file? and FileTest.file? d
|
|
|
|
File.unlink d
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
File.rename s, d
|
|
|
|
rescue
|
2002-10-17 07:54:25 +04:00
|
|
|
if FileTest.symlink? s
|
2002-03-26 05:03:04 +03:00
|
|
|
File.symlink File.readlink(s), dest
|
|
|
|
File.unlink s
|
|
|
|
else
|
|
|
|
st = File.stat(s)
|
|
|
|
copy_file s, d
|
|
|
|
File.unlink s
|
|
|
|
File.utime st.atime, st.mtime, d
|
|
|
|
begin
|
|
|
|
File.chown st.uid, st.gid, d
|
|
|
|
rescue
|
|
|
|
# ignore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias move mv
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
def cannot_overwrite_file? #:nodoc:
|
2002-10-17 07:54:25 +04:00
|
|
|
/djgpp|cygwin|mswin32/ === RUBY_PLATFORM
|
|
|
|
end
|
2003-02-06 12:56:13 +03:00
|
|
|
private :cannot_overwrite_file?
|
2002-10-17 07:54:25 +04:00
|
|
|
|
2002-03-26 05:03:04 +03:00
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: force noop verbose
|
|
|
|
#
|
|
|
|
# Remove file(s) specified in +list+. This method cannot remove directories.
|
|
|
|
# All errors are ignored when the :force option is set.
|
|
|
|
#
|
|
|
|
# FileUtils.rm %w( junk.txt dust.txt )
|
2003-02-06 12:56:13 +03:00
|
|
|
# FileUtils.rm Dir.glob('*.so')
|
2003-01-24 19:13:33 +03:00
|
|
|
# FileUtils.rm 'NotExistFile', :force # never raises exception
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def rm( list, *options )
|
|
|
|
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
|
|
|
list = fu_list(list)
|
|
|
|
fu_output_message "rm#{force ? ' -f' : ''} #{list.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
|
|
|
list.each do |fname|
|
|
|
|
remove_file fname, force
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias remove rm
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Same as
|
|
|
|
# #rm(list, :force)
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def rm_f( list, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
rm list, :force, *options
|
|
|
|
end
|
|
|
|
|
|
|
|
alias safe_unlink rm_f
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: force noop verbose
|
|
|
|
#
|
|
|
|
# remove files +list+[0] +list+[1]... If +list+[n] is a directory,
|
|
|
|
# removes its all contents recursively. This method ignores
|
|
|
|
# StandardError when :force option is set.
|
|
|
|
#
|
|
|
|
# FileUtils.rm_r Dir.glob('/tmp/*')
|
|
|
|
# FileUtils.rm_r '/', :force # :-)
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def rm_r( list, *options )
|
|
|
|
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
|
|
|
list = fu_list(list)
|
|
|
|
fu_output_message "rm -r#{force ? 'f' : ''} #{list.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
|
|
|
list.each do |fname|
|
|
|
|
begin
|
|
|
|
st = File.lstat(fname)
|
|
|
|
rescue
|
|
|
|
next if force
|
|
|
|
end
|
|
|
|
if st.symlink? then remove_file fname, force
|
|
|
|
elsif st.directory? then remove_dir fname, force
|
|
|
|
else remove_file fname, force
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Same as
|
|
|
|
# #rm_r(list, :force)
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def rm_rf( list, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
rm_r list, :force, *options
|
|
|
|
end
|
|
|
|
|
2003-02-21 14:57:31 +03:00
|
|
|
alias rmtree rm_rf
|
|
|
|
|
2003-02-06 12:56:13 +03:00
|
|
|
def remove_file( fname, force = false ) #:nodoc:
|
2002-12-27 07:21:27 +03:00
|
|
|
first_time_p = true
|
2002-03-26 05:03:04 +03:00
|
|
|
begin
|
2002-12-27 07:21:27 +03:00
|
|
|
File.unlink fname
|
2002-03-26 05:03:04 +03:00
|
|
|
rescue Errno::ENOENT
|
2002-12-27 07:21:27 +03:00
|
|
|
raise unless force
|
2002-03-26 05:03:04 +03:00
|
|
|
rescue
|
2002-12-27 07:21:27 +03:00
|
|
|
if first_time_p
|
|
|
|
# try once more for Windows
|
|
|
|
first_time_p = false
|
|
|
|
File.chmod 0777, fname
|
|
|
|
retry
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
2002-12-27 07:21:27 +03:00
|
|
|
raise
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-06 12:56:13 +03:00
|
|
|
def remove_dir( dir, force = false ) #:nodoc:
|
2002-10-17 07:54:25 +04:00
|
|
|
Dir.foreach(dir) do |file|
|
2002-03-26 05:03:04 +03:00
|
|
|
next if /\A\.\.?\z/ === file
|
|
|
|
path = "#{dir}/#{file}"
|
2002-12-27 07:21:27 +03:00
|
|
|
if FileTest.directory? path
|
|
|
|
remove_dir path, force
|
|
|
|
else
|
|
|
|
remove_file path, force
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
Dir.rmdir dir
|
|
|
|
rescue Errno::ENOENT
|
2002-12-27 07:21:27 +03:00
|
|
|
raise unless force
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Returns true if the contents of a file A and a file B are identical.
|
|
|
|
#
|
2003-02-21 14:57:31 +03:00
|
|
|
# FileUtils.compare_file('somefile', 'somefile') #=> true
|
|
|
|
# FileUtils.compare_file('/bin/cp', '/bin/mv') #=> maybe false
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2003-02-21 14:57:31 +03:00
|
|
|
def compare_file( a, b )
|
|
|
|
return false unless File.size(a) == File.size(b)
|
|
|
|
File.open(a, 'rb') {|fa|
|
|
|
|
File.open(b, 'rb') {|fb|
|
|
|
|
return compare_stream(fa, fb)
|
|
|
|
} }
|
|
|
|
end
|
2002-03-26 05:03:04 +03:00
|
|
|
|
2003-02-21 14:57:31 +03:00
|
|
|
alias identical? compare_file
|
|
|
|
alias cmp compare_file
|
2002-03-26 05:03:04 +03:00
|
|
|
|
2003-02-21 14:57:31 +03:00
|
|
|
#
|
|
|
|
# Returns true if the contents of a stream +a+ and +b+ are identical.
|
|
|
|
#
|
|
|
|
def compare_stream( a, b )
|
|
|
|
bsize = fu_stream_blksize(a, b)
|
|
|
|
sa = sb = nil
|
|
|
|
while sa == sb
|
|
|
|
sa = a.read(bsize)
|
|
|
|
sb = b.read(bsize)
|
|
|
|
unless sa and sb
|
|
|
|
if sa.nil? and sb.nil?
|
|
|
|
return true
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
end
|
2003-02-21 14:57:31 +03:00
|
|
|
end
|
2002-03-26 05:03:04 +03:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# If +src+ is not same as +dest+, copies it and changes the permission
|
2003-02-06 12:56:13 +03:00
|
|
|
# mode to +mode+. If +dest+ is a directory, destination is +dest+/+src+.
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# FileUtils.install 'ruby', '/usr/local/bin/ruby', 0755, :verbose
|
2003-02-06 12:56:13 +03:00
|
|
|
# FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def install( src, dest, mode, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
2003-01-05 10:04:10 +03:00
|
|
|
fu_output_message "install -c#{mode ? ' -m 0%o'%mode : ''} #{[src,dest].flatten.join ' '}" if verbose
|
2002-03-26 05:03:04 +03:00
|
|
|
return if noop
|
|
|
|
|
2002-12-27 07:21:27 +03:00
|
|
|
fu_each_src_dest(src, dest) do |s,d|
|
2002-10-17 07:54:25 +04:00
|
|
|
unless FileTest.exist? d and cmp(s,d)
|
2002-03-26 05:03:04 +03:00
|
|
|
remove_file d, true
|
|
|
|
copy_file s, d
|
|
|
|
File.chmod mode, d if mode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Changes permission bits on the named files (in +list+) to the bit pattern
|
|
|
|
# represented by +mode+.
|
|
|
|
#
|
|
|
|
# FileUtils.chmod 0755, 'somecommand'
|
2003-02-06 12:56:13 +03:00
|
|
|
# FileUtils.chmod 0644, %w(my.rb your.rb)
|
2003-01-24 19:13:33 +03:00
|
|
|
# FileUtils.chmod 0755, '/usr/bin/ruby', :verbose
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def chmod( mode, list, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
list = fu_list(list)
|
|
|
|
fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if verbose
|
|
|
|
return if noop
|
|
|
|
File.chmod mode, *list
|
|
|
|
end
|
|
|
|
|
2003-02-21 14:57:31 +03:00
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
|
|
|
# Options: noop verbose
|
|
|
|
#
|
|
|
|
# Updates modification time (mtime) and access time (atime) of file(s) in
|
|
|
|
# +list+. Files are created if they don't exist.
|
|
|
|
#
|
|
|
|
# FileUtils.touch 'timestamp'
|
|
|
|
# FileUtils.touch Dir.glob('*.c'); system 'make'
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
def touch( list, *options )
|
|
|
|
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
|
|
|
list = fu_list(list)
|
|
|
|
fu_output_message "touch #{list.join ' '}" if verbose
|
|
|
|
return if noop
|
|
|
|
|
|
|
|
t = Time.now
|
|
|
|
list.each do |fname|
|
|
|
|
begin
|
|
|
|
File.utime(t, t, fname)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
File.open(fname, 'a') { }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2002-12-27 07:21:27 +03:00
|
|
|
def fu_parseargs( optargs, *optdecl )
|
|
|
|
table = Hash.new(false)
|
|
|
|
optargs.each do |opt|
|
|
|
|
table[opt] = true
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
|
2002-12-27 07:21:27 +03:00
|
|
|
option_list = optdecl.map {|s| table.delete(s) }
|
|
|
|
raise ArgumentError, "wrong option #{table.keys.map {|o|o.inspect}.join(' ')}" unless table.empty?
|
2002-03-26 05:03:04 +03:00
|
|
|
|
2002-12-27 07:21:27 +03:00
|
|
|
option_list
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def fu_list( arg )
|
|
|
|
Array === arg ? arg : [arg]
|
|
|
|
end
|
|
|
|
|
|
|
|
def fu_each_src_dest( src, dest )
|
2002-10-17 07:54:25 +04:00
|
|
|
unless Array === src
|
2002-03-26 05:03:04 +03:00
|
|
|
yield src, fu_dest_filename(src, dest)
|
|
|
|
else
|
|
|
|
dir = dest
|
|
|
|
# FileTest.directory? dir or raise ArgumentError, "must be dir: #{dir}"
|
|
|
|
dir += (dir[-1,1] == '/') ? '' : '/'
|
2002-12-27 07:21:27 +03:00
|
|
|
src.each do |fname|
|
|
|
|
yield fname, dir + File.basename(fname)
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fu_dest_filename( src, dest )
|
2002-10-17 07:54:25 +04:00
|
|
|
if FileTest.directory? dest
|
2002-03-26 05:03:04 +03:00
|
|
|
(dest[-1,1] == '/' ? dest : dest + '/') + File.basename(src)
|
|
|
|
else
|
|
|
|
dest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-02-21 14:57:31 +03:00
|
|
|
def fu_stream_blksize( *streams )
|
|
|
|
streams.each do |s|
|
|
|
|
next unless s.respond_to?(:stat)
|
|
|
|
size = s.stat.blksize
|
|
|
|
return size unless size.nil? and size.zero?
|
2002-10-17 07:54:25 +04:00
|
|
|
end
|
2003-02-21 14:57:31 +03:00
|
|
|
1024
|
2002-10-17 07:54:25 +04:00
|
|
|
end
|
|
|
|
|
2002-03-26 05:03:04 +03:00
|
|
|
|
|
|
|
@fileutils_output = $stderr
|
2003-02-06 12:56:13 +03:00
|
|
|
@fileutils_label = ''
|
2002-03-26 05:03:04 +03:00
|
|
|
|
|
|
|
def fu_output_message( msg )
|
|
|
|
@fileutils_output ||= $stderr
|
2003-02-06 12:56:13 +03:00
|
|
|
@fileutils_label ||= ''
|
2002-03-26 05:03:04 +03:00
|
|
|
@fileutils_output.puts @fileutils_label + msg
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
extend self
|
|
|
|
|
|
|
|
|
|
|
|
OPT_TABLE = {
|
2003-02-09 01:01:53 +03:00
|
|
|
'pwd' => %w(),
|
2002-03-26 05:03:04 +03:00
|
|
|
'cd' => %w( noop verbose ),
|
|
|
|
'chdir' => %w( noop verbose ),
|
|
|
|
'chmod' => %w( noop verbose ),
|
|
|
|
'copy' => %w( preserve noop verbose ),
|
|
|
|
'cp' => %w( preserve noop verbose ),
|
|
|
|
'cp_r' => %w( preserve noop verbose ),
|
|
|
|
'install' => %w( noop verbose ),
|
|
|
|
'link' => %w( force noop verbose ),
|
|
|
|
'ln' => %w( force noop verbose ),
|
|
|
|
'ln_s' => %w( force noop verbose ),
|
|
|
|
'ln_sf' => %w( noop verbose ),
|
|
|
|
'makedirs' => %w( noop verbose ),
|
|
|
|
'mkdir' => %w( noop verbose ),
|
|
|
|
'mkdir_p' => %w( noop verbose ),
|
|
|
|
'mkpath' => %w( noop verbose ),
|
|
|
|
'move' => %w( noop verbose ),
|
|
|
|
'mv' => %w( noop verbose ),
|
|
|
|
'remove' => %w( force noop verbose ),
|
|
|
|
'rm' => %w( force noop verbose ),
|
|
|
|
'rm_f' => %w( noop verbose ),
|
|
|
|
'rm_r' => %w( force noop verbose ),
|
|
|
|
'rm_rf' => %w( noop verbose ),
|
2003-02-21 14:57:31 +03:00
|
|
|
'rmtree' => %w( noop verbose ),
|
2002-03-26 05:03:04 +03:00
|
|
|
'rmdir' => %w( noop verbose ),
|
|
|
|
'safe_unlink' => %w( noop verbose ),
|
|
|
|
'symlink' => %w( force noop verbose ),
|
2003-02-21 14:57:31 +03:00
|
|
|
'touch' => %w( noop verbose )
|
2002-03-26 05:03:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# This module has all methods of FileUtils module, but it outputs messages
|
|
|
|
# before acting. This equates to passing the +:verbose+ flag to methods in
|
|
|
|
# FileUtils.
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
module Verbose
|
|
|
|
|
|
|
|
include FileUtils
|
|
|
|
|
|
|
|
@fileutils_output = $stderr
|
2003-02-06 12:56:13 +03:00
|
|
|
@fileutils_label = ''
|
2002-03-26 05:03:04 +03:00
|
|
|
@fileutils_verbose = true
|
|
|
|
|
|
|
|
FileUtils::OPT_TABLE.each do |name, opts|
|
|
|
|
next unless opts.include? 'verbose'
|
2003-02-06 12:56:13 +03:00
|
|
|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
|
|
|
def #{name}( *args )
|
|
|
|
unless defined? @fileutils_verbose
|
|
|
|
@fileutils_verbose = true
|
|
|
|
end
|
|
|
|
args.push :verbose if @fileutils_verbose
|
|
|
|
super(*args)
|
|
|
|
end
|
|
|
|
EOS
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
extend self
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-01-24 19:13:33 +03:00
|
|
|
#
|
2003-02-06 12:56:13 +03:00
|
|
|
# This module has all methods of FileUtils module, but never changes
|
2003-01-24 19:13:33 +03:00
|
|
|
# files/directories. This equates to passing the +:noop+ flag to methods in
|
|
|
|
# FileUtils.
|
|
|
|
#
|
2002-03-26 05:03:04 +03:00
|
|
|
module NoWrite
|
|
|
|
|
|
|
|
include FileUtils
|
|
|
|
|
|
|
|
@fileutils_output = $stderr
|
2003-02-06 12:56:13 +03:00
|
|
|
@fileutils_label = ''
|
2002-03-26 05:03:04 +03:00
|
|
|
@fileutils_nowrite = true
|
|
|
|
|
|
|
|
FileUtils::OPT_TABLE.each do |name, opts|
|
|
|
|
next unless opts.include? 'noop'
|
2003-02-06 12:56:13 +03:00
|
|
|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
|
|
|
def #{name}( *args )
|
|
|
|
unless defined? @fileutils_nowrite
|
|
|
|
@fileutils_nowrite ||= true
|
|
|
|
end
|
|
|
|
args.push :noop if @fileutils_nowrite
|
|
|
|
super(*args)
|
|
|
|
end
|
|
|
|
EOS
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
extend self
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2003-02-06 12:56:13 +03:00
|
|
|
class Operator #:nodoc:
|
2002-03-26 05:03:04 +03:00
|
|
|
|
|
|
|
include FileUtils
|
|
|
|
|
|
|
|
def initialize( v = false )
|
|
|
|
@verbose = v
|
|
|
|
@noop = false
|
|
|
|
@force = false
|
|
|
|
@preserve = false
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :verbose
|
|
|
|
attr_accessor :noop
|
|
|
|
attr_accessor :force
|
|
|
|
attr_accessor :preserve
|
|
|
|
|
|
|
|
FileUtils::OPT_TABLE.each do |name, opts|
|
2002-12-27 07:21:27 +03:00
|
|
|
s = opts.map {|i| "args.unshift :#{i} if @#{i}" }.join(' '*10+"\n")
|
|
|
|
module_eval(<<-End, __FILE__, __LINE__ + 1)
|
2002-03-26 05:03:04 +03:00
|
|
|
def #{name}( *args )
|
|
|
|
#{s}
|
2002-10-17 07:54:25 +04:00
|
|
|
super(*args)
|
2002-03-26 05:03:04 +03:00
|
|
|
end
|
|
|
|
End
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2003-01-24 19:13:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Documentation comments:
|
|
|
|
# - Some RDoc markup used here doesn't work (namely, +file1+, +:noop+,
|
|
|
|
# +dir/file+). I consider this a bug and expect that these will be valid in
|
|
|
|
# the near future.
|