You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.7 KiB

Merged revisions 56020-56124 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ................ r56037 | georg.brandl | 2007-06-19 05:33:20 -0700 (Tue, 19 Jun 2007) | 2 lines Patch #1739659: don't slice dict.keys() in pydoc. ................ r56060 | martin.v.loewis | 2007-06-21 13:00:02 -0700 (Thu, 21 Jun 2007) | 2 lines Regenerate to add True, False, None. ................ r56069 | neal.norwitz | 2007-06-21 22:31:56 -0700 (Thu, 21 Jun 2007) | 1 line Get the doctest working again after adding None, True, and False as kewyords. ................ r56070 | neal.norwitz | 2007-06-21 23:25:33 -0700 (Thu, 21 Jun 2007) | 1 line Add space to error message. ................ r56071 | neal.norwitz | 2007-06-21 23:40:04 -0700 (Thu, 21 Jun 2007) | 6 lines Get pybench working, primarily * Use print function * Stop using string module * Use sorted instead of assuming dict methods return lists * Convert range result to a list ................ r56089 | collin.winter | 2007-06-26 10:31:48 -0700 (Tue, 26 Jun 2007) | 1 line Fix AttributeError in distutils/dir_util.py. ................ r56124 | guido.van.rossum | 2007-06-29 18:04:31 -0700 (Fri, 29 Jun 2007) | 30 lines Merged revisions 56014-56123 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r56019 | lars.gustaebel | 2007-06-18 04:42:11 -0700 (Mon, 18 Jun 2007) | 2 lines Added exclude keyword argument to the TarFile.add() method. ........ r56023 | lars.gustaebel | 2007-06-18 13:05:55 -0700 (Mon, 18 Jun 2007) | 3 lines Added missing \versionchanged tag for the new exclude parameter. ........ r56038 | georg.brandl | 2007-06-19 05:36:00 -0700 (Tue, 19 Jun 2007) | 2 lines Bug #1737864: allow empty message in logging format routines. ........ r56040 | georg.brandl | 2007-06-19 05:38:20 -0700 (Tue, 19 Jun 2007) | 2 lines Bug #1739115: make shutil.rmtree docs clear wrt. file deletion. ........ r56084 | georg.brandl | 2007-06-25 08:21:23 -0700 (Mon, 25 Jun 2007) | 2 lines Bug #1742901: document None behavior of shlex.split. ........ r56091 | georg.brandl | 2007-06-27 07:09:56 -0700 (Wed, 27 Jun 2007) | 2 lines Fix a variable name in winreg docs. ........ ................
19 years ago
Merged revisions 56020-56124 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ................ r56037 | georg.brandl | 2007-06-19 05:33:20 -0700 (Tue, 19 Jun 2007) | 2 lines Patch #1739659: don't slice dict.keys() in pydoc. ................ r56060 | martin.v.loewis | 2007-06-21 13:00:02 -0700 (Thu, 21 Jun 2007) | 2 lines Regenerate to add True, False, None. ................ r56069 | neal.norwitz | 2007-06-21 22:31:56 -0700 (Thu, 21 Jun 2007) | 1 line Get the doctest working again after adding None, True, and False as kewyords. ................ r56070 | neal.norwitz | 2007-06-21 23:25:33 -0700 (Thu, 21 Jun 2007) | 1 line Add space to error message. ................ r56071 | neal.norwitz | 2007-06-21 23:40:04 -0700 (Thu, 21 Jun 2007) | 6 lines Get pybench working, primarily * Use print function * Stop using string module * Use sorted instead of assuming dict methods return lists * Convert range result to a list ................ r56089 | collin.winter | 2007-06-26 10:31:48 -0700 (Tue, 26 Jun 2007) | 1 line Fix AttributeError in distutils/dir_util.py. ................ r56124 | guido.van.rossum | 2007-06-29 18:04:31 -0700 (Fri, 29 Jun 2007) | 30 lines Merged revisions 56014-56123 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r56019 | lars.gustaebel | 2007-06-18 04:42:11 -0700 (Mon, 18 Jun 2007) | 2 lines Added exclude keyword argument to the TarFile.add() method. ........ r56023 | lars.gustaebel | 2007-06-18 13:05:55 -0700 (Mon, 18 Jun 2007) | 3 lines Added missing \versionchanged tag for the new exclude parameter. ........ r56038 | georg.brandl | 2007-06-19 05:36:00 -0700 (Tue, 19 Jun 2007) | 2 lines Bug #1737864: allow empty message in logging format routines. ........ r56040 | georg.brandl | 2007-06-19 05:38:20 -0700 (Tue, 19 Jun 2007) | 2 lines Bug #1739115: make shutil.rmtree docs clear wrt. file deletion. ........ r56084 | georg.brandl | 2007-06-25 08:21:23 -0700 (Mon, 25 Jun 2007) | 2 lines Bug #1742901: document None behavior of shlex.split. ........ r56091 | georg.brandl | 2007-06-27 07:09:56 -0700 (Wed, 27 Jun 2007) | 2 lines Fix a variable name in winreg docs. ........ ................
19 years ago
Merged revisions 56020-56124 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ................ r56037 | georg.brandl | 2007-06-19 05:33:20 -0700 (Tue, 19 Jun 2007) | 2 lines Patch #1739659: don't slice dict.keys() in pydoc. ................ r56060 | martin.v.loewis | 2007-06-21 13:00:02 -0700 (Thu, 21 Jun 2007) | 2 lines Regenerate to add True, False, None. ................ r56069 | neal.norwitz | 2007-06-21 22:31:56 -0700 (Thu, 21 Jun 2007) | 1 line Get the doctest working again after adding None, True, and False as kewyords. ................ r56070 | neal.norwitz | 2007-06-21 23:25:33 -0700 (Thu, 21 Jun 2007) | 1 line Add space to error message. ................ r56071 | neal.norwitz | 2007-06-21 23:40:04 -0700 (Thu, 21 Jun 2007) | 6 lines Get pybench working, primarily * Use print function * Stop using string module * Use sorted instead of assuming dict methods return lists * Convert range result to a list ................ r56089 | collin.winter | 2007-06-26 10:31:48 -0700 (Tue, 26 Jun 2007) | 1 line Fix AttributeError in distutils/dir_util.py. ................ r56124 | guido.van.rossum | 2007-06-29 18:04:31 -0700 (Fri, 29 Jun 2007) | 30 lines Merged revisions 56014-56123 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r56019 | lars.gustaebel | 2007-06-18 04:42:11 -0700 (Mon, 18 Jun 2007) | 2 lines Added exclude keyword argument to the TarFile.add() method. ........ r56023 | lars.gustaebel | 2007-06-18 13:05:55 -0700 (Mon, 18 Jun 2007) | 3 lines Added missing \versionchanged tag for the new exclude parameter. ........ r56038 | georg.brandl | 2007-06-19 05:36:00 -0700 (Tue, 19 Jun 2007) | 2 lines Bug #1737864: allow empty message in logging format routines. ........ r56040 | georg.brandl | 2007-06-19 05:38:20 -0700 (Tue, 19 Jun 2007) | 2 lines Bug #1739115: make shutil.rmtree docs clear wrt. file deletion. ........ r56084 | georg.brandl | 2007-06-25 08:21:23 -0700 (Mon, 25 Jun 2007) | 2 lines Bug #1742901: document None behavior of shlex.split. ........ r56091 | georg.brandl | 2007-06-27 07:09:56 -0700 (Wed, 27 Jun 2007) | 2 lines Fix a variable name in winreg docs. ........ ................
19 years ago
20 years ago
  1. """distutils.dir_util
  2. Utility functions for manipulating directories and directory trees."""
  3. __revision__ = "$Id$"
  4. import os, sys
  5. import errno
  6. from distutils.errors import DistutilsFileError, DistutilsInternalError
  7. from distutils import log
  8. # cache for by mkpath() -- in addition to cheapening redundant calls,
  9. # eliminates redundant "creating /foo/bar/baz" messages in dry-run mode
  10. _path_created = {}
  11. # I don't use os.makedirs because a) it's new to Python 1.5.2, and
  12. # b) it blows up if the directory already exists (I want to silently
  13. # succeed in that case).
  14. def mkpath(name, mode=0o777, verbose=1, dry_run=0):
  15. """Create a directory and any missing ancestor directories.
  16. If the directory already exists (or if 'name' is the empty string, which
  17. means the current directory, which of course exists), then do nothing.
  18. Raise DistutilsFileError if unable to create some directory along the way
  19. (eg. some sub-path exists, but is a file rather than a directory).
  20. If 'verbose' is true, print a one-line summary of each mkdir to stdout.
  21. Return the list of directories actually created.
  22. """
  23. global _path_created
  24. # Detect a common bug -- name is None
  25. if not isinstance(name, str):
  26. raise DistutilsInternalError(
  27. "mkpath: 'name' must be a string (got %r)" % (name,))
  28. # XXX what's the better way to handle verbosity? print as we create
  29. # each directory in the path (the current behaviour), or only announce
  30. # the creation of the whole path? (quite easy to do the latter since
  31. # we're not using a recursive algorithm)
  32. name = os.path.normpath(name)
  33. created_dirs = []
  34. if os.path.isdir(name) or name == '':
  35. return created_dirs
  36. if _path_created.get(os.path.abspath(name)):
  37. return created_dirs
  38. (head, tail) = os.path.split(name)
  39. tails = [tail] # stack of lone dirs to create
  40. while head and tail and not os.path.isdir(head):
  41. (head, tail) = os.path.split(head)
  42. tails.insert(0, tail) # push next higher dir onto stack
  43. # now 'head' contains the deepest directory that already exists
  44. # (that is, the child of 'head' in 'name' is the highest directory
  45. # that does *not* exist)
  46. for d in tails:
  47. #print "head = %s, d = %s: " % (head, d),
  48. head = os.path.join(head, d)
  49. abs_head = os.path.abspath(head)
  50. if _path_created.get(abs_head):
  51. continue
  52. if verbose >= 1:
  53. log.info("creating %s", head)
  54. if not dry_run:
  55. try:
  56. os.mkdir(head, mode)
  57. except OSError as exc:
  58. if not (exc.errno == errno.EEXIST and os.path.isdir(head)):
  59. raise DistutilsFileError(
  60. "could not create '%s': %s" % (head, exc.args[-1]))
  61. created_dirs.append(head)
  62. _path_created[abs_head] = 1
  63. return created_dirs
  64. def create_tree(base_dir, files, mode=0o777, verbose=1, dry_run=0):
  65. """Create all the empty directories under 'base_dir' needed to put 'files'
  66. there.
  67. 'base_dir' is just the a name of a directory which doesn't necessarily
  68. exist yet; 'files' is a list of filenames to be interpreted relative to
  69. 'base_dir'. 'base_dir' + the directory portion of every file in 'files'
  70. will be created if it doesn't already exist. 'mode', 'verbose' and
  71. 'dry_run' flags are as for 'mkpath()'.
  72. """
  73. # First get the list of directories to create
  74. need_dir = set()
  75. for file in files:
  76. need_dir.add(os.path.join(base_dir, os.path.dirname(file)))
  77. # Now create them
  78. for dir in sorted(need_dir):
  79. mkpath(dir, mode, verbose=verbose, dry_run=dry_run)
  80. def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
  81. preserve_symlinks=0, update=0, verbose=1, dry_run=0):
  82. """Copy an entire directory tree 'src' to a new location 'dst'.
  83. Both 'src' and 'dst' must be directory names. If 'src' is not a
  84. directory, raise DistutilsFileError. If 'dst' does not exist, it is
  85. created with 'mkpath()'. The end result of the copy is that every
  86. file in 'src' is copied to 'dst', and directories under 'src' are
  87. recursively copied to 'dst'. Return the list of files that were
  88. copied or might have been copied, using their output name. The
  89. return value is unaffected by 'update' or 'dry_run': it is simply
  90. the list of all files under 'src', with the names changed to be
  91. under 'dst'.
  92. 'preserve_mode' and 'preserve_times' are the same as for
  93. 'copy_file'; note that they only apply to regular files, not to
  94. directories. If 'preserve_symlinks' is true, symlinks will be
  95. copied as symlinks (on platforms that support them!); otherwise
  96. (the default), the destination of the symlink will be copied.
  97. 'update' and 'verbose' are the same as for 'copy_file'.
  98. """
  99. from distutils.file_util import copy_file
  100. if not dry_run and not os.path.isdir(src):
  101. raise DistutilsFileError(
  102. "cannot copy tree '%s': not a directory" % src)
  103. try:
  104. names = os.listdir(src)
  105. except os.error as e:
  106. (errno, errstr) = e
  107. if dry_run:
  108. names = []
  109. else:
  110. raise DistutilsFileError(
  111. "error listing files in '%s': %s" % (src, errstr))
  112. if not dry_run:
  113. mkpath(dst, verbose=verbose)
  114. outputs = []
  115. for n in names:
  116. src_name = os.path.join(src, n)
  117. dst_name = os.path.join(dst, n)
  118. if preserve_symlinks and os.path.islink(src_name):
  119. link_dest = os.readlink(src_name)
  120. if verbose >= 1:
  121. log.info("linking %s -> %s", dst_name, link_dest)
  122. if not dry_run:
  123. os.symlink(link_dest, dst_name)
  124. outputs.append(dst_name)
  125. elif os.path.isdir(src_name):
  126. outputs.extend(
  127. copy_tree(src_name, dst_name, preserve_mode,
  128. preserve_times, preserve_symlinks, update,
  129. verbose=verbose, dry_run=dry_run))
  130. else:
  131. copy_file(src_name, dst_name, preserve_mode,
  132. preserve_times, update, verbose=verbose,
  133. dry_run=dry_run)
  134. outputs.append(dst_name)
  135. return outputs
  136. def _build_cmdtuple(path, cmdtuples):
  137. """Helper for remove_tree()."""
  138. for f in os.listdir(path):
  139. real_f = os.path.join(path,f)
  140. if os.path.isdir(real_f) and not os.path.islink(real_f):
  141. _build_cmdtuple(real_f, cmdtuples)
  142. else:
  143. cmdtuples.append((os.remove, real_f))
  144. cmdtuples.append((os.rmdir, path))
  145. def remove_tree(directory, verbose=1, dry_run=0):
  146. """Recursively remove an entire directory tree.
  147. Any errors are ignored (apart from being reported to stdout if 'verbose'
  148. is true).
  149. """
  150. from distutils.util import grok_environment_error
  151. global _path_created
  152. if verbose >= 1:
  153. log.info("removing '%s' (and everything under it)", directory)
  154. if dry_run:
  155. return
  156. cmdtuples = []
  157. _build_cmdtuple(directory, cmdtuples)
  158. for cmd in cmdtuples:
  159. try:
  160. cmd[0](cmd[1])
  161. # remove dir from cache if it's already there
  162. abspath = os.path.abspath(cmd[1])
  163. if abspath in _path_created:
  164. del _path_created[abspath]
  165. except (IOError, OSError) as exc:
  166. log.warn(grok_environment_error(
  167. exc, "error removing %s: " % directory))
  168. def ensure_relative(path):
  169. """Take the full path 'path', and make it a relative path.
  170. This is useful to make 'path' the second argument to os.path.join().
  171. """
  172. drive, path = os.path.splitdrive(path)
  173. if path[0:1] == os.sep:
  174. path = drive + path[1:]
  175. return path