mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
Merged revisions 69324 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r69324 | tarek.ziade | 2009-02-06 01:31:59 +0100 (Fri, 06 Feb 2009) | 1 line Fixed #1276768: verbose option was not used in the code. ........
This commit is contained in:
parent
4d491a535c
commit
70a74eb2c4
5 changed files with 188 additions and 24 deletions
|
@ -15,7 +15,7 @@ _path_created = {}
|
|||
# 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
|
||||
# succeed in that case).
|
||||
def mkpath (name, mode=0o777, verbose=0, dry_run=0):
|
||||
def mkpath (name, mode=0o777, verbose=1, dry_run=0):
|
||||
"""Create a directory and any missing ancestor directories. If the
|
||||
directory already exists (or if 'name' is the empty string, which
|
||||
means the current directory, which of course exists), then do
|
||||
|
@ -48,13 +48,9 @@ def mkpath (name, mode=0o777, verbose=0, dry_run=0):
|
|||
tails = [tail] # stack of lone dirs to create
|
||||
|
||||
while head and tail and not os.path.isdir(head):
|
||||
#print "splitting '%s': " % head,
|
||||
(head, tail) = os.path.split(head)
|
||||
#print "to ('%s','%s')" % (head, tail)
|
||||
tails.insert(0, tail) # push next higher dir onto stack
|
||||
|
||||
#print "stack of tails:", tails
|
||||
|
||||
# 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)
|
||||
|
@ -66,6 +62,7 @@ def mkpath (name, mode=0o777, verbose=0, dry_run=0):
|
|||
if _path_created.get(abs_head):
|
||||
continue
|
||||
|
||||
if verbose == 1:
|
||||
log.info("creating %s", head)
|
||||
|
||||
if not dry_run:
|
||||
|
@ -82,7 +79,7 @@ def mkpath (name, mode=0o777, verbose=0, dry_run=0):
|
|||
# mkpath ()
|
||||
|
||||
|
||||
def create_tree (base_dir, files, mode=0o777, verbose=0, dry_run=0):
|
||||
def create_tree (base_dir, files, mode=0o777, verbose=1, dry_run=0):
|
||||
|
||||
"""Create all the empty directories under 'base_dir' needed to
|
||||
put 'files' there. 'base_dir' is just the a name of a directory
|
||||
|
@ -99,7 +96,7 @@ def create_tree (base_dir, files, mode=0o777, verbose=0, dry_run=0):
|
|||
|
||||
# Now create them
|
||||
for dir in sorted(need_dir):
|
||||
mkpath(dir, mode, dry_run=dry_run)
|
||||
mkpath(dir, mode, verbose=verbose, dry_run=dry_run)
|
||||
|
||||
# create_tree ()
|
||||
|
||||
|
@ -109,7 +106,7 @@ def copy_tree (src, dst,
|
|||
preserve_times=1,
|
||||
preserve_symlinks=0,
|
||||
update=0,
|
||||
verbose=0,
|
||||
verbose=1,
|
||||
dry_run=0):
|
||||
|
||||
"""Copy an entire directory tree 'src' to a new location 'dst'. Both
|
||||
|
@ -146,7 +143,7 @@ def copy_tree (src, dst,
|
|||
"error listing files in '%s': %s" % (src, errstr))
|
||||
|
||||
if not dry_run:
|
||||
mkpath(dst)
|
||||
mkpath(dst, verbose=verbose)
|
||||
|
||||
outputs = []
|
||||
|
||||
|
@ -156,6 +153,7 @@ def copy_tree (src, dst,
|
|||
|
||||
if preserve_symlinks and os.path.islink(src_name):
|
||||
link_dest = os.readlink(src_name)
|
||||
if verbose == 1:
|
||||
log.info("linking %s -> %s", dst_name, link_dest)
|
||||
if not dry_run:
|
||||
os.symlink(link_dest, dst_name)
|
||||
|
@ -165,10 +163,11 @@ def copy_tree (src, dst,
|
|||
outputs.extend(
|
||||
copy_tree(src_name, dst_name, preserve_mode,
|
||||
preserve_times, preserve_symlinks, update,
|
||||
dry_run=dry_run))
|
||||
verbose=verbose, dry_run=dry_run))
|
||||
else:
|
||||
copy_file(src_name, dst_name, preserve_mode,
|
||||
preserve_times, update, dry_run=dry_run)
|
||||
preserve_times, update, verbose=verbose,
|
||||
dry_run=dry_run)
|
||||
outputs.append(dst_name)
|
||||
|
||||
return outputs
|
||||
|
@ -184,13 +183,14 @@ def _build_cmdtuple(path, cmdtuples):
|
|||
cmdtuples.append((os.rmdir, path))
|
||||
|
||||
|
||||
def remove_tree (directory, verbose=0, dry_run=0):
|
||||
def remove_tree (directory, verbose=1, dry_run=0):
|
||||
"""Recursively remove an entire directory tree. Any errors are ignored
|
||||
(apart from being reported to stdout if 'verbose' is true).
|
||||
"""
|
||||
from distutils.util import grok_environment_error
|
||||
global _path_created
|
||||
|
||||
if verbose == 1:
|
||||
log.info("removing '%s' (and everything under it)", directory)
|
||||
if dry_run:
|
||||
return
|
||||
|
|
|
@ -67,7 +67,7 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
|
|||
fsrc.close()
|
||||
|
||||
def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
||||
link=None, verbose=0, dry_run=0):
|
||||
link=None, verbose=1, dry_run=0):
|
||||
"""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 filename. (If
|
||||
the file exists, it will be ruthlessly clobbered.) If 'preserve_mode'
|
||||
|
@ -112,6 +112,7 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
|||
dir = os.path.dirname(dst)
|
||||
|
||||
if update and not newer(src, dst):
|
||||
if verbose == 1:
|
||||
log.debug("not copying %s (output up-to-date)", src)
|
||||
return (dst, 0)
|
||||
|
||||
|
@ -119,6 +120,8 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
|||
action = _copy_action[link]
|
||||
except KeyError:
|
||||
raise ValueError("invalid value '%s' for 'link' argument" % link)
|
||||
|
||||
if verbose == 1:
|
||||
if os.path.basename(dst) == os.path.basename(src):
|
||||
log.info("%s %s -> %s", action, src, dir)
|
||||
else:
|
||||
|
@ -164,7 +167,7 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
|||
|
||||
# XXX I suspect this is Unix-specific -- need porting help!
|
||||
def move_file (src, dst,
|
||||
verbose=0,
|
||||
verbose=1,
|
||||
dry_run=0):
|
||||
|
||||
"""Move a file 'src' to 'dst'. If 'dst' is a directory, the file will
|
||||
|
@ -177,6 +180,7 @@ def move_file (src, dst,
|
|||
from os.path import exists, isfile, isdir, basename, dirname
|
||||
import errno
|
||||
|
||||
if verbose == 1:
|
||||
log.info("moving %s -> %s", src, dst)
|
||||
|
||||
if dry_run:
|
||||
|
@ -209,7 +213,7 @@ def move_file (src, dst,
|
|||
"couldn't move '%s' to '%s': %s" % (src, dst, msg))
|
||||
|
||||
if copy_it:
|
||||
copy_file(src, dst)
|
||||
copy_file(src, dst, verbose=verbose)
|
||||
try:
|
||||
os.unlink(src)
|
||||
except os.error as e:
|
||||
|
|
91
Lib/distutils/tests/test_dir_util.py
Normal file
91
Lib/distutils/tests/test_dir_util.py
Normal file
|
@ -0,0 +1,91 @@
|
|||
"""Tests for distutils.dir_util."""
|
||||
import unittest
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from distutils.dir_util import mkpath
|
||||
from distutils.dir_util import remove_tree
|
||||
from distutils.dir_util import create_tree
|
||||
from distutils.dir_util import copy_tree
|
||||
|
||||
from distutils import log
|
||||
|
||||
class DirUtilTestCase(unittest.TestCase):
|
||||
|
||||
def _log(self, msg, *args):
|
||||
if len(args) > 0:
|
||||
self._logs.append(msg % args)
|
||||
else:
|
||||
self._logs.append(msg)
|
||||
|
||||
def setUp(self):
|
||||
self._logs = []
|
||||
self.root_target = os.path.join(os.path.dirname(__file__), 'deep')
|
||||
self.target = os.path.join(self.root_target, 'here')
|
||||
self.target2 = os.path.join(os.path.dirname(__file__), 'deep2')
|
||||
self.old_log = log.info
|
||||
log.info = self._log
|
||||
|
||||
def tearDown(self):
|
||||
for target in (self.target, self.target2):
|
||||
if os.path.exists(target):
|
||||
shutil.rmtree(target)
|
||||
log.info = self.old_log
|
||||
|
||||
def test_mkpath_remove_tree_verbosity(self):
|
||||
|
||||
mkpath(self.target, verbose=0)
|
||||
wanted = []
|
||||
self.assertEquals(self._logs, wanted)
|
||||
remove_tree(self.root_target, verbose=0)
|
||||
|
||||
mkpath(self.target, verbose=1)
|
||||
wanted = ['creating %s' % self.root_target,
|
||||
'creating %s' % self.target]
|
||||
self.assertEquals(self._logs, wanted)
|
||||
self._logs = []
|
||||
|
||||
remove_tree(self.root_target, verbose=1)
|
||||
wanted = ["removing '%s' (and everything under it)" % self.root_target]
|
||||
self.assertEquals(self._logs, wanted)
|
||||
|
||||
def test_create_tree_verbosity(self):
|
||||
|
||||
create_tree(self.root_target, ['one', 'two', 'three'], verbose=0)
|
||||
self.assertEquals(self._logs, [])
|
||||
remove_tree(self.root_target, verbose=0)
|
||||
|
||||
wanted = ['creating %s' % self.root_target]
|
||||
create_tree(self.root_target, ['one', 'two', 'three'], verbose=1)
|
||||
self.assertEquals(self._logs, wanted)
|
||||
|
||||
remove_tree(self.root_target, verbose=0)
|
||||
|
||||
|
||||
def test_copy_tree_verbosity(self):
|
||||
|
||||
mkpath(self.target, verbose=0)
|
||||
|
||||
copy_tree(self.target, self.target2, verbose=0)
|
||||
self.assertEquals(self._logs, [])
|
||||
|
||||
remove_tree(self.root_target, verbose=0)
|
||||
|
||||
mkpath(self.target, verbose=0)
|
||||
a_file = os.path.join(self.target, 'ok.txt')
|
||||
f = open(a_file, 'w')
|
||||
f.write('some content')
|
||||
f.close()
|
||||
|
||||
wanted = ['copying %s -> %s' % (a_file, self.target2)]
|
||||
copy_tree(self.target, self.target2, verbose=1)
|
||||
self.assertEquals(self._logs, wanted)
|
||||
|
||||
remove_tree(self.root_target, verbose=0)
|
||||
remove_tree(self.target2, verbose=0)
|
||||
|
||||
def test_suite():
|
||||
return unittest.makeSuite(DirUtilTestCase)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(defaultTest="test_suite")
|
66
Lib/distutils/tests/test_file_util.py
Normal file
66
Lib/distutils/tests/test_file_util.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
"""Tests for distutils.file_util."""
|
||||
import unittest
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from distutils.file_util import move_file
|
||||
from distutils import log
|
||||
|
||||
class FileUtilTestCase(unittest.TestCase):
|
||||
|
||||
def _log(self, msg, *args):
|
||||
if len(args) > 0:
|
||||
self._logs.append(msg % args)
|
||||
else:
|
||||
self._logs.append(msg)
|
||||
|
||||
def setUp(self):
|
||||
self._logs = []
|
||||
self.old_log = log.info
|
||||
log.info = self._log
|
||||
self.source = os.path.join(os.path.dirname(__file__), 'f1')
|
||||
self.target = os.path.join(os.path.dirname(__file__), 'f2')
|
||||
self.target_dir = os.path.join(os.path.dirname(__file__), 'd1')
|
||||
|
||||
def tearDown(self):
|
||||
log.info = self.old_log
|
||||
for f in (self.source, self.target, self.target_dir):
|
||||
if os.path.exists(f):
|
||||
if os.path.isfile(f):
|
||||
os.remove(f)
|
||||
else:
|
||||
shutil.rmtree(f)
|
||||
|
||||
def test_move_file_verbosity(self):
|
||||
|
||||
f = open(self.source, 'w')
|
||||
f.write('some content')
|
||||
f.close()
|
||||
|
||||
move_file(self.source, self.target, verbose=0)
|
||||
wanted = []
|
||||
self.assertEquals(self._logs, wanted)
|
||||
|
||||
# back to original state
|
||||
move_file(self.target, self.source, verbose=0)
|
||||
|
||||
move_file(self.source, self.target, verbose=1)
|
||||
wanted = ['moving %s -> %s' % (self.source, self.target)]
|
||||
self.assertEquals(self._logs, wanted)
|
||||
|
||||
# back to original state
|
||||
move_file(self.target, self.source, verbose=0)
|
||||
|
||||
self._logs = []
|
||||
# now the target is a dir
|
||||
os.mkdir(self.target_dir)
|
||||
move_file(self.source, self.target_dir, verbose=1)
|
||||
wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
|
||||
self.assertEquals(self._logs, wanted)
|
||||
|
||||
|
||||
def test_suite():
|
||||
return unittest.makeSuite(FileUtilTestCase)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(defaultTest="test_suite")
|
|
@ -155,6 +155,9 @@ Core and Builtins
|
|||
Library
|
||||
-------
|
||||
|
||||
- Issue #1276768: The verbose option was not used in the code of
|
||||
distutils.file_util and distutils.dir_util.
|
||||
|
||||
- Issue #5132: Fixed trouble building extensions under Solaris with
|
||||
--enabled-shared activated. Initial patch by Dave Peterson.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue