* lib/fileutils.rb: use hashes to pass options.
* lib/fileutils.rb: new option mkdir(:mode), mkdir_p(:mode). * instruby.rb: follow fileutils.rb feature change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
fa0108a9a3
commit
f701792f6f
@ -1,3 +1,11 @@
|
|||||||
|
Fri May 2 15:10:41 2003 Minero Aoki <aamine@loveruby.net>
|
||||||
|
|
||||||
|
* lib/fileutils.rb: use hashes to pass options.
|
||||||
|
|
||||||
|
* lib/fileutils.rb: new option mkdir(:mode), mkdir_p(:mode).
|
||||||
|
|
||||||
|
* instruby.rb: follow fileutils.rb feature change.
|
||||||
|
|
||||||
Thu May 1 08:24:00 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Thu May 1 08:24:00 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* regex.c (re_match_exec): $ _always_ matches at the end of string.
|
* regex.c (re_match_exec): $ _always_ matches at the end of string.
|
||||||
|
34
instruby.rb
34
instruby.rb
@ -9,7 +9,7 @@ require 'shellwords'
|
|||||||
require 'getopts'
|
require 'getopts'
|
||||||
require 'tempfile'
|
require 'tempfile'
|
||||||
|
|
||||||
File.umask(0022)
|
File.umask(0)
|
||||||
|
|
||||||
def parse_args()
|
def parse_args()
|
||||||
getopts('n', 'dest-dir:',
|
getopts('n', 'dest-dir:',
|
||||||
@ -55,7 +55,7 @@ include FileUtils::NoWrite if $dryrun
|
|||||||
@fileutils_label = ''
|
@fileutils_label = ''
|
||||||
alias makelink ln_sf
|
alias makelink ln_sf
|
||||||
class << self
|
class << self
|
||||||
body = proc {|*args|super(*args<<:verbose)}
|
body = proc {|*args|super(*fu_update_option(args,:verbose=>true))}
|
||||||
for func in [:install, :makelink]
|
for func in [:install, :makelink]
|
||||||
define_method(func, body)
|
define_method(func, body)
|
||||||
end
|
end
|
||||||
@ -69,7 +69,7 @@ def makedirs(dirs)
|
|||||||
File.directory?(dir)
|
File.directory?(dir)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
super(dirs, :verbose) unless dirs.empty?
|
super(dirs, :mode => 0755, :verbose => true) unless dirs.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
exeext = CONFIG["EXEEXT"]
|
exeext = CONFIG["EXEEXT"]
|
||||||
@ -95,18 +95,18 @@ makedirs [bindir, libdir, rubylibdir, archlibdir, sitelibdir, sitearchlibdir]
|
|||||||
|
|
||||||
ruby_bin = File.join(bindir, ruby_install_name)
|
ruby_bin = File.join(bindir, ruby_install_name)
|
||||||
|
|
||||||
install ruby_install_name+exeext, ruby_bin+exeext, 0755
|
install ruby_install_name+exeext, ruby_bin+exeext, :mode => 0755
|
||||||
if rubyw_install_name and !rubyw_install_name.empty?
|
if rubyw_install_name and !rubyw_install_name.empty?
|
||||||
install rubyw_install_name+exeext, bindir, 0755
|
install rubyw_install_name+exeext, bindir, :mode => 0755
|
||||||
end
|
end
|
||||||
install dll, bindir, 0755 if enable_shared and dll != lib
|
install dll, bindir, :mode => 0755 if enable_shared and dll != lib
|
||||||
install lib, libdir, 0555 unless lib == arc
|
install lib, libdir, :mode => 0555 unless lib == arc
|
||||||
install arc, libdir, 0644
|
install arc, libdir, :mode => 0644
|
||||||
install "config.h", archlibdir, 0644
|
install "config.h", archlibdir, :mode => 0644
|
||||||
install "rbconfig.rb", archlibdir, 0644
|
install "rbconfig.rb", archlibdir, :mode => 0644
|
||||||
if CONFIG["ARCHFILE"]
|
if CONFIG["ARCHFILE"]
|
||||||
for file in CONFIG["ARCHFILE"].split
|
for file in CONFIG["ARCHFILE"].split
|
||||||
install file, archlibdir, 0644
|
install file, archlibdir, :mode => 0644
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -126,7 +126,7 @@ for src in Dir["bin/*"]
|
|||||||
name = ruby_install_name.sub(/ruby/, File.basename(src))
|
name = ruby_install_name.sub(/ruby/, File.basename(src))
|
||||||
dest = File.join(bindir, name)
|
dest = File.join(bindir, name)
|
||||||
|
|
||||||
install src, dest, 0755
|
install src, dest, :mode => 0755
|
||||||
|
|
||||||
next if $dryrun
|
next if $dryrun
|
||||||
|
|
||||||
@ -166,16 +166,16 @@ end
|
|||||||
Dir.glob("lib/**/*{.rb,help-message}") do |f|
|
Dir.glob("lib/**/*{.rb,help-message}") do |f|
|
||||||
dir = File.dirname(f).sub!(/\Alib/, rubylibdir) || rubylibdir
|
dir = File.dirname(f).sub!(/\Alib/, rubylibdir) || rubylibdir
|
||||||
makedirs dir
|
makedirs dir
|
||||||
install f, dir, 0644
|
install f, dir, :mode => 0644
|
||||||
end
|
end
|
||||||
|
|
||||||
Dir.glob("*.h") do |f|
|
Dir.glob("*.h") do |f|
|
||||||
install f, archlibdir, 0644
|
install f, archlibdir, :mode => 0644
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_PLATFORM =~ /mswin32|mingw|bccwin32/
|
if RUBY_PLATFORM =~ /mswin32|mingw|bccwin32/
|
||||||
makedirs File.join(archlibdir, "win32")
|
makedirs File.join(archlibdir, "win32")
|
||||||
install "win32/win32.h", File.join(archlibdir, "win32"), 0644
|
install "win32/win32.h", File.join(archlibdir, "win32"), :mode => 0644
|
||||||
end
|
end
|
||||||
|
|
||||||
Dir.glob("*.[1-9]") do |mdoc|
|
Dir.glob("*.[1-9]") do |mdoc|
|
||||||
@ -187,7 +187,7 @@ Dir.glob("*.[1-9]") do |mdoc|
|
|||||||
makedirs destdir
|
makedirs destdir
|
||||||
|
|
||||||
if $mantype == "doc"
|
if $mantype == "doc"
|
||||||
install mdoc, destfile, 0644
|
install mdoc, destfile, :mode => 0644
|
||||||
else
|
else
|
||||||
require 'mdoc2man.rb'
|
require 'mdoc2man.rb'
|
||||||
|
|
||||||
@ -199,7 +199,7 @@ Dir.glob("*.[1-9]") do |mdoc|
|
|||||||
|
|
||||||
w.close
|
w.close
|
||||||
|
|
||||||
install w.path, destfile, 0644
|
install w.path, destfile, :mode => 0644
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
359
lib/fileutils.rb
359
lib/fileutils.rb
@ -1,7 +1,7 @@
|
|||||||
#
|
#
|
||||||
# = fileutils.rb
|
# = fileutils.rb
|
||||||
#
|
#
|
||||||
# Copyright (c) 2000-2002 Minero Aoki <aamine@loveruby.net>
|
# Copyright (c) 2000-2003 Minero Aoki <aamine@loveruby.net>
|
||||||
#
|
#
|
||||||
# This program is free software.
|
# This program is free software.
|
||||||
# You can distribute/modify this program under the same terms of ruby.
|
# You can distribute/modify this program under the same terms of ruby.
|
||||||
@ -12,31 +12,31 @@
|
|||||||
#
|
#
|
||||||
# === Module Functions
|
# === Module Functions
|
||||||
#
|
#
|
||||||
# cd( dir, *options )
|
# cd( dir, options )
|
||||||
# cd( dir, *options ) {|dir| .... }
|
# cd( dir, options ) {|dir| .... }
|
||||||
# pwd()
|
# pwd()
|
||||||
# mkdir( dir, *options )
|
# mkdir( dir, options )
|
||||||
# mkdir_p( dir, *options )
|
# mkdir_p( dir, options )
|
||||||
# rmdir( dir, *options )
|
# rmdir( dir, options )
|
||||||
# ln( old, new, *options )
|
# ln( old, new, options )
|
||||||
# ln( list, destdir, *options )
|
# ln( list, destdir, options )
|
||||||
# ln_s( old, new, *options )
|
# ln_s( old, new, options )
|
||||||
# ln_s( list, destdir, *options )
|
# ln_s( list, destdir, options )
|
||||||
# ln_sf( src, dest, *options )
|
# ln_sf( src, dest, options )
|
||||||
# cp( src, dest, *options )
|
# cp( src, dest, options )
|
||||||
# cp( list, dir, *options )
|
# cp( list, dir, options )
|
||||||
# cp_r( src, dest, *options )
|
# cp_r( src, dest, options )
|
||||||
# cp_r( list, dir, *options )
|
# cp_r( list, dir, options )
|
||||||
# mv( src, dest, *options )
|
# mv( src, dest, options )
|
||||||
# mv( list, dir, *options )
|
# mv( list, dir, options )
|
||||||
# rm( list, *options )
|
# rm( list, options )
|
||||||
# rm_r( list, *options )
|
# rm_r( list, options )
|
||||||
# rm_rf( list, *options )
|
# rm_rf( list, options )
|
||||||
# install( src, dest, mode = <src's>, *options )
|
# install( src, dest, mode = <src's>, options )
|
||||||
# chmod( mode, list, *options )
|
# chmod( mode, list, options )
|
||||||
# touch( list, *options )
|
# touch( list, options )
|
||||||
#
|
#
|
||||||
# The <tt>*options</tt> parameter is a list of 0-3 options, taken from the list
|
# The <tt>options</tt> parameter is a hash of options, taken from the list
|
||||||
# +:force+, +:noop+, +:preserve+, and +:verbose+. +:noop+ means that no changes
|
# +:force+, +:noop+, +:preserve+, and +:verbose+. +:noop+ means that no changes
|
||||||
# are made. The other two are obvious. Each method documents the options that
|
# are made. The other two are obvious. Each method documents the options that
|
||||||
# it honours.
|
# it honours.
|
||||||
@ -90,13 +90,13 @@ module FileUtils
|
|||||||
# If this method is called with block, resumes to the old
|
# If this method is called with block, resumes to the old
|
||||||
# working directory after the block execution finished.
|
# working directory after the block execution finished.
|
||||||
#
|
#
|
||||||
# FileUtils.cd '/', :verbose # chdir and report it
|
# FileUtils.cd('/', :verbose => true) # chdir and report it
|
||||||
#
|
#
|
||||||
def cd( dir, *options, &block ) # :yield: dir
|
def cd( dir, options = {}, &block ) # :yield: dir
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
fu_output_message "cd #{dir}" if verbose
|
fu_output_message "cd #{dir}" if options[:verbose]
|
||||||
Dir.chdir(dir, &block) unless noop
|
Dir.chdir(dir, &block) unless options[:noop]
|
||||||
fu_output_message 'cd -' if verbose and block
|
fu_output_message 'cd -' if options[:verbose] and block
|
||||||
end
|
end
|
||||||
|
|
||||||
alias chdir cd
|
alias chdir cd
|
||||||
@ -108,15 +108,16 @@ module FileUtils
|
|||||||
# Returns true if +newer+ is newer than all +old_list+.
|
# Returns true if +newer+ is newer than all +old_list+.
|
||||||
# Non-existent files are older than any file.
|
# Non-existent files are older than any file.
|
||||||
#
|
#
|
||||||
# FileUtils.uptodate? 'hello.o', %w(hello.c hello.h) or system 'make hello.o'
|
# FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \
|
||||||
|
# system 'make hello.o'
|
||||||
#
|
#
|
||||||
def uptodate?( new, old_list, *options )
|
def uptodate?( new, old_list, options = nil )
|
||||||
raise ArgumentError, 'uptodate? does not accept any option' unless options.empty?
|
raise ArgumentError, 'uptodate? does not accept any option' if options
|
||||||
|
|
||||||
return false unless FileTest.exist? new
|
return false unless FileTest.exist?(new)
|
||||||
new_time = File.mtime(new)
|
new_time = File.mtime(new)
|
||||||
old_list.each do |old|
|
old_list.each do |old|
|
||||||
if FileTest.exist? old
|
if FileTest.exist?(old)
|
||||||
return false unless new_time > File.mtime(old)
|
return false unless new_time > File.mtime(old)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -125,27 +126,29 @@ module FileUtils
|
|||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Options: noop verbose
|
# Options: mode noop verbose
|
||||||
#
|
#
|
||||||
# Creates one or more directories.
|
# Creates one or more directories.
|
||||||
#
|
#
|
||||||
# FileUtils.mkdir 'test'
|
# FileUtils.mkdir 'test'
|
||||||
# FileUtils.mkdir %w( tmp data )
|
# FileUtils.mkdir %w( tmp data )
|
||||||
# FileUtils.mkdir 'notexist', :noop # Does not really create.
|
# FileUtils.mkdir 'notexist', :noop => true # Does not really create.
|
||||||
|
# FileUtils.mkdir 'tmp', :mode => 0700
|
||||||
#
|
#
|
||||||
def mkdir( list, *options )
|
def mkdir( list, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :mode, :noop, :verbose
|
||||||
list = fu_list(list)
|
list = fu_list(list)
|
||||||
fu_output_message "mkdir #{list.join ' '}" if verbose
|
fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
|
mode = options[:mode] || (0777 & ~File.umask)
|
||||||
list.each do |dir|
|
list.each do |dir|
|
||||||
Dir.mkdir dir
|
Dir.mkdir dir, mode
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Options: noop verbose
|
# Options: mode noop verbose
|
||||||
#
|
#
|
||||||
# Creates a directory and all its parent directories.
|
# Creates a directory and all its parent directories.
|
||||||
# For example,
|
# For example,
|
||||||
@ -160,20 +163,21 @@ module FileUtils
|
|||||||
#
|
#
|
||||||
# You can pass several directories at a time in a list.
|
# You can pass several directories at a time in a list.
|
||||||
#
|
#
|
||||||
def mkdir_p( list, *options )
|
def mkdir_p( list, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :mode, :noop, :verbose
|
||||||
list = fu_list(list)
|
list = fu_list(list)
|
||||||
fu_output_message "mkdir -p #{list.join ' '}" if verbose
|
fu_output_message "mkdir -p #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
|
||||||
return *list if noop
|
return *list if options[:noop]
|
||||||
|
|
||||||
|
mode = options[:mode] || (0777 & ~File.umask)
|
||||||
list.map {|n| File.expand_path(n) }.each do |dir|
|
list.map {|n| File.expand_path(n) }.each do |dir|
|
||||||
stack = []
|
stack = []
|
||||||
until FileTest.directory? dir
|
until FileTest.directory?(dir)
|
||||||
stack.push dir
|
stack.push dir
|
||||||
dir = File.dirname(dir)
|
dir = File.dirname(dir)
|
||||||
end
|
end
|
||||||
stack.reverse_each do |n|
|
stack.reverse_each do |n|
|
||||||
Dir.mkdir n
|
Dir.mkdir n, mode
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -192,13 +196,13 @@ module FileUtils
|
|||||||
# FileUtils.rmdir 'somedir'
|
# FileUtils.rmdir 'somedir'
|
||||||
# FileUtils.rmdir %w(somedir anydir otherdir)
|
# FileUtils.rmdir %w(somedir anydir otherdir)
|
||||||
# # Does not really remove directory; outputs message.
|
# # Does not really remove directory; outputs message.
|
||||||
# FileUtils.rmdir 'somedir', :verbose, :noop
|
# FileUtils.rmdir 'somedir', :verbose => true, :noop => true
|
||||||
#
|
#
|
||||||
def rmdir( list, *options )
|
def rmdir( list, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
list = fu_list(list)
|
list = fu_list(list)
|
||||||
fu_output_message "rmdir #{list.join ' '}" if verbose
|
fu_output_message "rmdir #{list.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
list.each do |dir|
|
list.each do |dir|
|
||||||
Dir.rmdir dir
|
Dir.rmdir dir
|
||||||
@ -209,17 +213,17 @@ module FileUtils
|
|||||||
#
|
#
|
||||||
# Options: force noop verbose
|
# Options: force noop verbose
|
||||||
#
|
#
|
||||||
# <b><tt>ln( old, new, *options )</tt></b>
|
# <b><tt>ln( old, new, options = {} )</tt></b>
|
||||||
#
|
#
|
||||||
# Creates a hard link +new+ which points to +old+.
|
# 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 a directory, creates a symbolic link +new/old+.
|
||||||
# If +new+ already exists and it is not a directory, raises Errno::EEXIST.
|
# If +new+ already exists and it is not a directory, raises Errno::EEXIST.
|
||||||
# But if :force option is set, overwrite +new+.
|
# But if :force option is set, overwrite +new+.
|
||||||
#
|
#
|
||||||
# FileUtils.ln 'gcc', 'cc', :verbose
|
# FileUtils.ln 'gcc', 'cc', :verbose => true
|
||||||
# FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
|
# FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
|
||||||
#
|
#
|
||||||
# <b><tt>ln( list, destdir, *options )</tt></b>
|
# <b><tt>ln( list, destdir, options = {} )</tt></b>
|
||||||
#
|
#
|
||||||
# Creates several hard links in a directory, with each one pointing to the
|
# 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.
|
# item in +list+. If +destdir+ is not a directory, raises Errno::ENOTDIR.
|
||||||
@ -228,13 +232,13 @@ module FileUtils
|
|||||||
# cd '/bin'
|
# cd '/bin'
|
||||||
# ln %w(cp mv mkdir), '/usr/bin' # Now /usr/bin/cp and /bin/cp are linked.
|
# ln %w(cp mv mkdir), '/usr/bin' # Now /usr/bin/cp and /bin/cp are linked.
|
||||||
#
|
#
|
||||||
def ln( src, dest, *options )
|
def ln( src, dest, options = {} )
|
||||||
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
fu_check_options options, :force, :noop, :verbose
|
||||||
fu_output_message "ln#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
fu_output_message "ln#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
fu_each_src_dest(src, dest) do |s,d|
|
fu_each_src_dest(src, dest) do |s,d|
|
||||||
remove_file d, true if force
|
remove_file d, true if options[:force]
|
||||||
File.link s, d
|
File.link s, d
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -244,7 +248,7 @@ module FileUtils
|
|||||||
#
|
#
|
||||||
# Options: force noop verbose
|
# Options: force noop verbose
|
||||||
#
|
#
|
||||||
# <b><tt>ln_s( old, new, *options )</tt></b>
|
# <b><tt>ln_s( old, new, options = {} )</tt></b>
|
||||||
#
|
#
|
||||||
# Creates a symbolic link +new+ which points to +old+. If +new+ already
|
# 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+
|
# exists and it is a directory, creates a symbolic link +new/old+. If +new+
|
||||||
@ -252,9 +256,9 @@ module FileUtils
|
|||||||
# :force option is set, overwrite +new+.
|
# :force option is set, overwrite +new+.
|
||||||
#
|
#
|
||||||
# FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
|
# FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
|
||||||
# FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force
|
# FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force => true
|
||||||
#
|
#
|
||||||
# <b><tt>ln_s( list, destdir, *options )</tt></b>
|
# <b><tt>ln_s( list, destdir, options = {} )</tt></b>
|
||||||
#
|
#
|
||||||
# Creates several symbolic links in a directory, with each one pointing to the
|
# 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.
|
# item in +list+. If +destdir+ is not a directory, raises Errno::ENOTDIR.
|
||||||
@ -263,13 +267,13 @@ module FileUtils
|
|||||||
#
|
#
|
||||||
# FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
|
# FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
|
||||||
#
|
#
|
||||||
def ln_s( src, dest, *options )
|
def ln_s( src, dest, options = {} )
|
||||||
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
fu_check_options options, :force, :noop, :verbose
|
||||||
fu_output_message "ln -s#{force ? 'f' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
fu_output_message "ln -s#{options[:force] ? 'f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
fu_each_src_dest(src, dest) do |s,d|
|
fu_each_src_dest(src, dest) do |s,d|
|
||||||
remove_file d, true if force
|
remove_file d, true if options[:force]
|
||||||
File.symlink s, d
|
File.symlink s, d
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -282,9 +286,11 @@ module FileUtils
|
|||||||
# Same as
|
# Same as
|
||||||
# #ln_s(src, dest, :force)
|
# #ln_s(src, dest, :force)
|
||||||
#
|
#
|
||||||
def ln_sf( src, dest, *options )
|
def ln_sf( src, dest, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
ln_s src, dest, :force, *options
|
options = options.dup
|
||||||
|
options[:force] = true
|
||||||
|
ln_s src, dest, options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -298,15 +304,15 @@ module FileUtils
|
|||||||
#
|
#
|
||||||
# FileUtils.cp 'eval.c', 'eval.c.org'
|
# FileUtils.cp 'eval.c', 'eval.c.org'
|
||||||
# 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'
|
||||||
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose
|
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
|
||||||
#
|
#
|
||||||
def cp( src, dest, *options )
|
def cp( src, dest, options = {} )
|
||||||
preserve, noop, verbose, = fu_parseargs(options, :preserve, :noop, :verbose)
|
fu_check_options options, :preserve, :noop, :verbose
|
||||||
fu_output_message "cp#{preserve ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
fu_output_message "cp#{options[:preserve] ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
fu_each_src_dest(src, dest) do |s,d|
|
fu_each_src_dest(src, dest) do |s,d|
|
||||||
fu_preserve_attr(preserve, s, d) {
|
fu_preserve_attr(options[:preserve], s, d) {
|
||||||
copy_file s, d
|
copy_file s, d
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -331,16 +337,16 @@ module FileUtils
|
|||||||
# FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
|
# FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
|
||||||
# FileUtils.cp_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop, :verbose
|
# FileUtils.cp_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop, :verbose
|
||||||
#
|
#
|
||||||
def cp_r( src, dest, *options )
|
def cp_r( src, dest, options = {} )
|
||||||
preserve, noop, verbose, = fu_parseargs(options, :preserve, :noop, :verbose)
|
fu_check_options options, :preserve, :noop, :verbose
|
||||||
fu_output_message "cp -r#{preserve ? 'p' : ''} #{[src,dest].flatten.join ' '}" if verbose
|
fu_output_message "cp -r#{options[:preserve] ? 'p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
fu_each_src_dest(src, dest) do |s,d|
|
fu_each_src_dest(src, dest) do |s,d|
|
||||||
if FileTest.directory? s
|
if FileTest.directory?(s)
|
||||||
fu_copy_dir s, d, '.', preserve
|
fu_copy_dir s, d, '.', options[:preserve]
|
||||||
else
|
else
|
||||||
fu_p_copy s, d, preserve
|
fu_p_copy s, d, options[:preserve]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -419,25 +425,25 @@ module FileUtils
|
|||||||
# disk partition, the file is copied instead.
|
# disk partition, the file is copied instead.
|
||||||
#
|
#
|
||||||
# FileUtils.mv 'badname.rb', 'goodname.rb'
|
# FileUtils.mv 'badname.rb', 'goodname.rb'
|
||||||
# FileUtils.mv 'stuff.rb', 'lib/ruby', :force
|
# FileUtils.mv 'stuff.rb', 'lib/ruby', :force => true
|
||||||
#
|
#
|
||||||
# FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
|
# FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
|
||||||
# FileUtils.mv Dir.glob('test*.rb'), 'test', :noop, :verbose
|
# FileUtils.mv Dir.glob('test*.rb'), 'test', :noop, :verbose => true
|
||||||
#
|
#
|
||||||
def mv( src, dest, *options )
|
def mv( src, dest, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
fu_output_message "mv #{[src,dest].flatten.join ' '}" if verbose
|
fu_output_message "mv #{[src,dest].flatten.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
fu_each_src_dest(src, dest) do |s,d|
|
fu_each_src_dest(src, dest) do |s,d|
|
||||||
if cannot_overwrite_file? and FileTest.file? d
|
if cannot_overwrite_file? and FileTest.file?(d)
|
||||||
File.unlink d
|
File.unlink d
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
File.rename s, d
|
File.rename s, d
|
||||||
rescue
|
rescue
|
||||||
if FileTest.symlink? s
|
if FileTest.symlink?(s)
|
||||||
File.symlink File.readlink(s), dest
|
File.symlink File.readlink(s), dest
|
||||||
File.unlink s
|
File.unlink s
|
||||||
else
|
else
|
||||||
@ -471,16 +477,16 @@ module FileUtils
|
|||||||
#
|
#
|
||||||
# FileUtils.rm %w( junk.txt dust.txt )
|
# FileUtils.rm %w( junk.txt dust.txt )
|
||||||
# FileUtils.rm Dir.glob('*.so')
|
# FileUtils.rm Dir.glob('*.so')
|
||||||
# FileUtils.rm 'NotExistFile', :force # never raises exception
|
# FileUtils.rm 'NotExistFile', :force => true # never raises exception
|
||||||
#
|
#
|
||||||
def rm( list, *options )
|
def rm( list, options = {} )
|
||||||
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
fu_check_options options, :force, :noop, :verbose
|
||||||
list = fu_list(list)
|
list = fu_list(list)
|
||||||
fu_output_message "rm#{force ? ' -f' : ''} #{list.join ' '}" if verbose
|
fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
list.each do |fname|
|
list.each do |fname|
|
||||||
remove_file fname, force
|
remove_file fname, options[:force]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -492,9 +498,11 @@ module FileUtils
|
|||||||
# Same as
|
# Same as
|
||||||
# #rm(list, :force)
|
# #rm(list, :force)
|
||||||
#
|
#
|
||||||
def rm_f( list, *options )
|
def rm_f( list, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
rm list, :force, *options
|
options = options.dup
|
||||||
|
options[:force] = true
|
||||||
|
rm list, options
|
||||||
end
|
end
|
||||||
|
|
||||||
alias safe_unlink rm_f
|
alias safe_unlink rm_f
|
||||||
@ -507,24 +515,24 @@ module FileUtils
|
|||||||
# StandardError when :force option is set.
|
# StandardError when :force option is set.
|
||||||
#
|
#
|
||||||
# FileUtils.rm_r Dir.glob('/tmp/*')
|
# FileUtils.rm_r Dir.glob('/tmp/*')
|
||||||
# FileUtils.rm_r '/', :force # :-)
|
# FileUtils.rm_r '/', :force => true # :-)
|
||||||
#
|
#
|
||||||
def rm_r( list, *options )
|
def rm_r( list, options = {} )
|
||||||
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
fu_check_options options, :force, :noop, :verbose
|
||||||
list = fu_list(list)
|
list = fu_list(list)
|
||||||
fu_output_message "rm -r#{force ? 'f' : ''} #{list.join ' '}" if verbose
|
fu_output_message "rm -r#{options[:force] ? 'f' : ''} #{list.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
list.each do |fname|
|
list.each do |fname|
|
||||||
begin
|
begin
|
||||||
st = File.lstat(fname)
|
st = File.lstat(fname)
|
||||||
rescue
|
rescue
|
||||||
next if force
|
next if options[:force]
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
if st.symlink? then remove_file fname, force
|
if st.symlink? then remove_file fname, options[:force]
|
||||||
elsif st.directory? then remove_dir fname, force
|
elsif st.directory? then remove_dir fname, options[:force]
|
||||||
else remove_file fname, force
|
else remove_file fname, options[:force]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -533,11 +541,13 @@ module FileUtils
|
|||||||
# Options: noop verbose
|
# Options: noop verbose
|
||||||
#
|
#
|
||||||
# Same as
|
# Same as
|
||||||
# #rm_r(list, :force)
|
# #rm_r(list, :force => true)
|
||||||
#
|
#
|
||||||
def rm_rf( list, *options )
|
def rm_rf( list, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
rm_r list, :force, *options
|
options = options.dup
|
||||||
|
options[:force] = true
|
||||||
|
rm_r list, options
|
||||||
end
|
end
|
||||||
|
|
||||||
alias rmtree rm_rf
|
alias rmtree rm_rf
|
||||||
@ -614,24 +624,24 @@ module FileUtils
|
|||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Options: noop verbose
|
# Options: mode noop verbose
|
||||||
#
|
#
|
||||||
# If +src+ is not same as +dest+, copies it and changes the permission
|
# If +src+ is not same as +dest+, copies it and changes the permission
|
||||||
# mode to +mode+. If +dest+ is a directory, destination is +dest+/+src+.
|
# mode to +mode+. If +dest+ is a directory, destination is +dest+/+src+.
|
||||||
#
|
#
|
||||||
# FileUtils.install 'ruby', '/usr/local/bin/ruby', 0755, :verbose
|
# FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
|
||||||
# FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose
|
# FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
|
||||||
#
|
#
|
||||||
def install( src, dest, mode, *options )
|
def install( src, dest, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :mode, :noop, :verbose
|
||||||
fu_output_message "install -c#{mode ? ' -m 0%o'%mode : ''} #{[src,dest].flatten.join ' '}" if verbose
|
fu_output_message "install -c#{options[:mode] ? (' -m 0%o' % options[:mode]) : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
fu_each_src_dest(src, dest) do |s,d|
|
fu_each_src_dest(src, dest) do |s,d|
|
||||||
unless FileTest.exist? d and cmp(s,d)
|
unless FileTest.exist?(d) and compare_file(s,d)
|
||||||
remove_file d, true
|
remove_file d, true
|
||||||
copy_file s, d
|
copy_file s, d
|
||||||
File.chmod mode, d if mode
|
File.chmod options[:mode], d if options[:mode]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -644,14 +654,14 @@ module FileUtils
|
|||||||
# represented by +mode+.
|
# represented by +mode+.
|
||||||
#
|
#
|
||||||
# FileUtils.chmod 0755, 'somecommand'
|
# FileUtils.chmod 0755, 'somecommand'
|
||||||
# FileUtils.chmod 0644, %w(my.rb your.rb)
|
# FileUtils.chmod 0644, %w(my.rb your.rb his.rb her.rb)
|
||||||
# FileUtils.chmod 0755, '/usr/bin/ruby', :verbose
|
# FileUtils.chmod 0755, '/usr/bin/ruby', :verbose => true
|
||||||
#
|
#
|
||||||
def chmod( mode, list, *options )
|
def chmod( mode, list, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
list = fu_list(list)
|
list = fu_list(list)
|
||||||
fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if verbose
|
fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
File.chmod mode, *list
|
File.chmod mode, *list
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -665,38 +675,34 @@ module FileUtils
|
|||||||
# FileUtils.touch 'timestamp'
|
# FileUtils.touch 'timestamp'
|
||||||
# FileUtils.touch Dir.glob('*.c'); system 'make'
|
# FileUtils.touch Dir.glob('*.c'); system 'make'
|
||||||
#
|
#
|
||||||
def touch( list, *options )
|
def touch( list, options = {} )
|
||||||
noop, verbose, = fu_parseargs(options, :noop, :verbose)
|
fu_check_options options, :noop, :verbose
|
||||||
list = fu_list(list)
|
list = fu_list(list)
|
||||||
fu_output_message "touch #{list.join ' '}" if verbose
|
fu_output_message "touch #{list.join ' '}" if options[:verbose]
|
||||||
return if noop
|
return if options[:noop]
|
||||||
|
|
||||||
t = Time.now
|
t = Time.now
|
||||||
list.each do |fname|
|
list.each do |fname|
|
||||||
begin
|
begin
|
||||||
File.utime(t, t, fname)
|
File.utime(t, t, fname)
|
||||||
rescue Errno::ENOENT
|
rescue Errno::ENOENT
|
||||||
File.open(fname, 'a') { }
|
File.open(fname, 'a') {
|
||||||
|
;
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def fu_parseargs( optargs, *optdecl )
|
def fu_check_options( options, *optdecl )
|
||||||
table = Hash.new(false)
|
h = options.dup
|
||||||
optargs.each do |opt|
|
optdecl.each do |name|
|
||||||
table[opt] = true
|
h.delete name
|
||||||
end
|
end
|
||||||
|
raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
|
||||||
option_list = optdecl.map {|s| table.delete(s) }
|
|
||||||
raise ArgumentError, "wrong option #{table.keys.map {|o|o.inspect}.join(' ')}" unless table.empty?
|
|
||||||
|
|
||||||
option_list
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def fu_list( arg )
|
def fu_list( arg )
|
||||||
Array === arg ? arg : [arg]
|
Array === arg ? arg : [arg]
|
||||||
end
|
end
|
||||||
@ -731,7 +737,6 @@ module FileUtils
|
|||||||
1024
|
1024
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@fileutils_output = $stderr
|
@fileutils_output = $stderr
|
||||||
@fileutils_label = ''
|
@fileutils_label = ''
|
||||||
|
|
||||||
@ -741,6 +746,15 @@ module FileUtils
|
|||||||
@fileutils_output.puts @fileutils_label + msg
|
@fileutils_output.puts @fileutils_label + msg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fu_update_option( args, new )
|
||||||
|
if Hash === args.last
|
||||||
|
args.last.update new
|
||||||
|
else
|
||||||
|
args.push new
|
||||||
|
end
|
||||||
|
args
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
@ -753,14 +767,14 @@ module FileUtils
|
|||||||
'copy' => %w( preserve noop verbose ),
|
'copy' => %w( preserve noop verbose ),
|
||||||
'cp' => %w( preserve noop verbose ),
|
'cp' => %w( preserve noop verbose ),
|
||||||
'cp_r' => %w( preserve noop verbose ),
|
'cp_r' => %w( preserve noop verbose ),
|
||||||
'install' => %w( noop verbose ),
|
'install' => %w( mode noop verbose ),
|
||||||
'link' => %w( force noop verbose ),
|
'link' => %w( force noop verbose ),
|
||||||
'ln' => %w( force noop verbose ),
|
'ln' => %w( force noop verbose ),
|
||||||
'ln_s' => %w( force noop verbose ),
|
'ln_s' => %w( force noop verbose ),
|
||||||
'ln_sf' => %w( noop verbose ),
|
'ln_sf' => %w( noop verbose ),
|
||||||
'makedirs' => %w( noop verbose ),
|
'makedirs' => %w( noop verbose ),
|
||||||
'mkdir' => %w( noop verbose ),
|
'mkdir' => %w( mode noop verbose ),
|
||||||
'mkdir_p' => %w( noop verbose ),
|
'mkdir_p' => %w( mode noop verbose ),
|
||||||
'mkpath' => %w( noop verbose ),
|
'mkpath' => %w( noop verbose ),
|
||||||
'move' => %w( noop verbose ),
|
'move' => %w( noop verbose ),
|
||||||
'mv' => %w( noop verbose ),
|
'mv' => %w( noop verbose ),
|
||||||
@ -791,14 +805,11 @@ module FileUtils
|
|||||||
@fileutils_verbose = true
|
@fileutils_verbose = true
|
||||||
|
|
||||||
FileUtils::OPT_TABLE.each do |name, opts|
|
FileUtils::OPT_TABLE.each do |name, opts|
|
||||||
next unless opts.include? 'verbose'
|
next unless opts.include?('verbose')
|
||||||
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
||||||
def #{name}( *args )
|
def #{name}( *args )
|
||||||
unless defined? @fileutils_verbose
|
@fileutils_verbose = true unless defined?(@fileutils_verbose)
|
||||||
@fileutils_verbose = true
|
super(*fu_update_option(args, :verbose => @fileutils_verbose))
|
||||||
end
|
|
||||||
args.push :verbose if @fileutils_verbose
|
|
||||||
super(*args)
|
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
@ -825,11 +836,10 @@ module FileUtils
|
|||||||
next unless opts.include? 'noop'
|
next unless opts.include? 'noop'
|
||||||
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
||||||
def #{name}( *args )
|
def #{name}( *args )
|
||||||
unless defined? @fileutils_nowrite
|
unless defined?(@fileutils_nowrite)
|
||||||
@fileutils_nowrite ||= true
|
@fileutils_nowrite ||= true
|
||||||
end
|
end
|
||||||
args.push :noop if @fileutils_nowrite
|
super(*fu_update_option(args, :noop => true))
|
||||||
super(*args)
|
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
@ -838,35 +848,6 @@ module FileUtils
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
class Operator #:nodoc:
|
|
||||||
|
|
||||||
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|
|
|
||||||
s = opts.map {|i| "args.unshift :#{i} if @#{i}" }.join(' '*10+"\n")
|
|
||||||
module_eval(<<-End, __FILE__, __LINE__ + 1)
|
|
||||||
def #{name}( *args )
|
|
||||||
#{s}
|
|
||||||
super(*args)
|
|
||||||
end
|
|
||||||
End
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user