mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Added 'dry_run' flag to most functions (to support the "shadow methods"
that wrap them in the Command class). Fixed 'copy_file()' to use '_copy_file_contents()', not 'copyfile()' from shutil module -- no reference to shutil anymore. Added "not copying" announcement in 'copy_file()'. Wee comment fix.
This commit is contained in:
parent
4070f50537
commit
e765a3bb61
1 changed files with 28 additions and 15 deletions
|
@ -18,7 +18,7 @@ from distutils.errors import *
|
||||||
# I don't use os.makedirs because a) it's new to Python 1.5.2, and
|
# I don't use os.makedirs because a) it's new to Python 1.5.2, and
|
||||||
# b) it blows up if the directory already exists (I want to silently
|
# b) it blows up if the directory already exists (I want to silently
|
||||||
# succeed in that case).
|
# succeed in that case).
|
||||||
def mkpath (name, mode=0777, verbose=0):
|
def mkpath (name, mode=0777, verbose=0, dry_run=0):
|
||||||
"""Create a directory and any missing ancestor directories. If the
|
"""Create a directory and any missing ancestor directories. If the
|
||||||
directory already exists, return silently. Raise
|
directory already exists, return silently. Raise
|
||||||
DistutilsFileError if unable to create some directory along the
|
DistutilsFileError if unable to create some directory along the
|
||||||
|
@ -44,16 +44,20 @@ def mkpath (name, mode=0777, verbose=0):
|
||||||
|
|
||||||
#print "stack of tails:", tails
|
#print "stack of tails:", tails
|
||||||
|
|
||||||
# now 'head' contains the highest directory that already exists
|
# now 'head' contains the deepest directory that already exists
|
||||||
|
# (that is, the child of 'head' in 'name' is the highest directory
|
||||||
|
# that does *not* exist)
|
||||||
for d in tails:
|
for d in tails:
|
||||||
#print "head = %s, d = %s: " % (head, d),
|
#print "head = %s, d = %s: " % (head, d),
|
||||||
head = os.path.join (head, d)
|
head = os.path.join (head, d)
|
||||||
if verbose:
|
if verbose:
|
||||||
print "creating", head
|
print "creating", head
|
||||||
try:
|
|
||||||
os.mkdir (head)
|
if not dry_run:
|
||||||
except os.error, (errno, errstr):
|
try:
|
||||||
raise DistutilsFileError, "%s: %s" % (head, errstr)
|
os.mkdir (head)
|
||||||
|
except os.error, (errno, errstr):
|
||||||
|
raise DistutilsFileError, "%s: %s" % (head, errstr)
|
||||||
|
|
||||||
# mkpath ()
|
# mkpath ()
|
||||||
|
|
||||||
|
@ -147,7 +151,8 @@ def copy_file (src, dst,
|
||||||
preserve_mode=1,
|
preserve_mode=1,
|
||||||
preserve_times=1,
|
preserve_times=1,
|
||||||
update=0,
|
update=0,
|
||||||
verbose=0):
|
verbose=0,
|
||||||
|
dry_run=0):
|
||||||
|
|
||||||
"""Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src'
|
"""Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src'
|
||||||
is copied there with the same name; otherwise, it must be a
|
is copied there with the same name; otherwise, it must be a
|
||||||
|
@ -163,7 +168,6 @@ def copy_file (src, dst,
|
||||||
|
|
||||||
# XXX doesn't copy Mac-specific metadata
|
# XXX doesn't copy Mac-specific metadata
|
||||||
|
|
||||||
from shutil import copyfile
|
|
||||||
from stat import *
|
from stat import *
|
||||||
|
|
||||||
if not os.path.isfile (src):
|
if not os.path.isfile (src):
|
||||||
|
@ -177,12 +181,16 @@ def copy_file (src, dst,
|
||||||
dir = os.path.dirname (dst)
|
dir = os.path.dirname (dst)
|
||||||
|
|
||||||
if update and not newer (src, dst):
|
if update and not newer (src, dst):
|
||||||
|
print "not copying %s (output up-to-date)" % src
|
||||||
return
|
return
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print "copying %s -> %s" % (src, dir)
|
print "copying %s -> %s" % (src, dir)
|
||||||
|
|
||||||
copyfile (src, dst)
|
if dry_run:
|
||||||
|
return
|
||||||
|
|
||||||
|
_copy_file_contents (src, dst)
|
||||||
if preserve_mode or preserve_times:
|
if preserve_mode or preserve_times:
|
||||||
st = os.stat (src)
|
st = os.stat (src)
|
||||||
if preserve_mode:
|
if preserve_mode:
|
||||||
|
@ -198,7 +206,9 @@ def copy_tree (src, dst,
|
||||||
preserve_times=1,
|
preserve_times=1,
|
||||||
preserve_symlinks=0,
|
preserve_symlinks=0,
|
||||||
update=0,
|
update=0,
|
||||||
verbose=0):
|
verbose=0,
|
||||||
|
dry_run=0):
|
||||||
|
|
||||||
|
|
||||||
"""Copy an entire directory tree 'src' to a new location 'dst'. Both
|
"""Copy an entire directory tree 'src' to a new location 'dst'. Both
|
||||||
'src' and 'dst' must be directory names. If 'src' is not a
|
'src' and 'dst' must be directory names. If 'src' is not a
|
||||||
|
@ -223,8 +233,8 @@ def copy_tree (src, dst,
|
||||||
raise DistutilsFileError, \
|
raise DistutilsFileError, \
|
||||||
"error listing files in %s: %s" % (src, errstr)
|
"error listing files in %s: %s" % (src, errstr)
|
||||||
|
|
||||||
|
if not dry_run:
|
||||||
mkpath (dst, verbose=verbose)
|
mkpath (dst, verbose=verbose)
|
||||||
|
|
||||||
for n in names:
|
for n in names:
|
||||||
src_name = os.path.join (src, n)
|
src_name = os.path.join (src, n)
|
||||||
|
@ -232,14 +242,17 @@ def copy_tree (src, dst,
|
||||||
|
|
||||||
if preserve_symlinks and os.path.islink (src_name):
|
if preserve_symlinks and os.path.islink (src_name):
|
||||||
link_dest = os.readlink (src_name)
|
link_dest = os.readlink (src_name)
|
||||||
os.symlink (link_dest, dst_name)
|
if verbose:
|
||||||
|
print "linking %s -> %s" % (dst_name, link_dest)
|
||||||
|
if not dry_run:
|
||||||
|
os.symlink (link_dest, dst_name)
|
||||||
elif os.path.isdir (src_name):
|
elif os.path.isdir (src_name):
|
||||||
copy_tree (src_name, dst_name,
|
copy_tree (src_name, dst_name,
|
||||||
preserve_mode, preserve_times, preserve_symlinks,
|
preserve_mode, preserve_times, preserve_symlinks,
|
||||||
update, verbose)
|
update, verbose, dry_run)
|
||||||
else:
|
else:
|
||||||
copy_file (src_name, dst_name,
|
copy_file (src_name, dst_name,
|
||||||
preserve_mode, preserve_times,
|
preserve_mode, preserve_times,
|
||||||
update, verbose)
|
update, verbose, dry_run)
|
||||||
|
|
||||||
# copy_tree ()
|
# copy_tree ()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue