dir_util.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. """distutils.dir_util
  2. Utility functions for manipulating directories and directory trees."""
  3. import os
  4. import errno
  5. from distutils.errors import DistutilsInternalError, DistutilsFileError
  6. from distutils import log
  7. # cache for by mkpath() -- in addition to cheapening redundant calls,
  8. # eliminates redundant "creating /foo/bar/baz" messages in dry-run mode
  9. _path_created = {}
  10. def mkpath(name, mode=0o777, verbose=1, dry_run=0): # noqa: C901
  11. """Create a directory and any missing ancestor directories.
  12. If the directory already exists (or if 'name' is the empty string, which
  13. means the current directory, which of course exists), then do nothing.
  14. Raise DistutilsFileError if unable to create some directory along the way
  15. (eg. some sub-path exists, but is a file rather than a directory).
  16. If 'verbose' is true, print a one-line summary of each mkdir to stdout.
  17. Return the list of directories actually created.
  18. os.makedirs is not used because:
  19. a) It's new to Python 1.5.2, and
  20. b) it blows up if the directory already exists (in which case it should
  21. silently succeed).
  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})".format(name)
  28. )
  29. # XXX what's the better way to handle verbosity? print as we create
  30. # each directory in the path (the current behaviour), or only announce
  31. # the creation of the whole path? (quite easy to do the latter since
  32. # we're not using a recursive algorithm)
  33. name = os.path.normpath(name)
  34. created_dirs = []
  35. if os.path.isdir(name) or name == '':
  36. return created_dirs
  37. if _path_created.get(os.path.abspath(name)):
  38. return created_dirs
  39. (head, tail) = os.path.split(name)
  40. tails = [tail] # stack of lone dirs to create
  41. while head and tail and not os.path.isdir(head):
  42. (head, tail) = os.path.split(head)
  43. tails.insert(0, tail) # push next higher dir onto stack
  44. # now 'head' contains the deepest directory that already exists
  45. # (that is, the child of 'head' in 'name' is the highest directory
  46. # that does *not* exist)
  47. for d in tails:
  48. # print "head = %s, d = %s: " % (head, d),
  49. head = os.path.join(head, d)
  50. abs_head = os.path.abspath(head)
  51. if _path_created.get(abs_head):
  52. continue
  53. if verbose >= 1:
  54. log.info("creating %s", head)
  55. if not dry_run:
  56. try:
  57. os.mkdir(head, mode)
  58. except OSError as exc:
  59. if not (exc.errno == errno.EEXIST and os.path.isdir(head)):
  60. raise DistutilsFileError(
  61. "could not create '{}': {}".format(head, exc.args[-1])
  62. )
  63. created_dirs.append(head)
  64. _path_created[abs_head] = 1
  65. return created_dirs
  66. def create_tree(base_dir, files, mode=0o777, verbose=1, dry_run=0):
  67. """Create all the empty directories under 'base_dir' needed to put 'files'
  68. there.
  69. 'base_dir' is just the name of a directory which doesn't necessarily
  70. exist yet; 'files' is a list of filenames to be interpreted relative to
  71. 'base_dir'. 'base_dir' + the directory portion of every file in 'files'
  72. will be created if it doesn't already exist. 'mode', 'verbose' and
  73. 'dry_run' flags are as for 'mkpath()'.
  74. """
  75. # First get the list of directories to create
  76. need_dir = set()
  77. for file in files:
  78. need_dir.add(os.path.join(base_dir, os.path.dirname(file)))
  79. # Now create them
  80. for dir in sorted(need_dir):
  81. mkpath(dir, mode, verbose=verbose, dry_run=dry_run)
  82. def copy_tree( # noqa: C901
  83. src,
  84. dst,
  85. preserve_mode=1,
  86. preserve_times=1,
  87. preserve_symlinks=0,
  88. update=0,
  89. verbose=1,
  90. dry_run=0,
  91. ):
  92. """Copy an entire directory tree 'src' to a new location 'dst'.
  93. Both 'src' and 'dst' must be directory names. If 'src' is not a
  94. directory, raise DistutilsFileError. If 'dst' does not exist, it is
  95. created with 'mkpath()'. The end result of the copy is that every
  96. file in 'src' is copied to 'dst', and directories under 'src' are
  97. recursively copied to 'dst'. Return the list of files that were
  98. copied or might have been copied, using their output name. The
  99. return value is unaffected by 'update' or 'dry_run': it is simply
  100. the list of all files under 'src', with the names changed to be
  101. under 'dst'.
  102. 'preserve_mode' and 'preserve_times' are the same as for
  103. 'copy_file'; note that they only apply to regular files, not to
  104. directories. If 'preserve_symlinks' is true, symlinks will be
  105. copied as symlinks (on platforms that support them!); otherwise
  106. (the default), the destination of the symlink will be copied.
  107. 'update' and 'verbose' are the same as for 'copy_file'.
  108. """
  109. from distutils.file_util import copy_file
  110. if not dry_run and not os.path.isdir(src):
  111. raise DistutilsFileError("cannot copy tree '%s': not a directory" % src)
  112. try:
  113. names = os.listdir(src)
  114. except OSError as e:
  115. if dry_run:
  116. names = []
  117. else:
  118. raise DistutilsFileError(
  119. "error listing files in '{}': {}".format(src, e.strerror)
  120. )
  121. if not dry_run:
  122. mkpath(dst, verbose=verbose)
  123. outputs = []
  124. for n in names:
  125. src_name = os.path.join(src, n)
  126. dst_name = os.path.join(dst, n)
  127. if n.startswith('.nfs'):
  128. # skip NFS rename files
  129. continue
  130. if preserve_symlinks and os.path.islink(src_name):
  131. link_dest = os.readlink(src_name)
  132. if verbose >= 1:
  133. log.info("linking %s -> %s", dst_name, link_dest)
  134. if not dry_run:
  135. os.symlink(link_dest, dst_name)
  136. outputs.append(dst_name)
  137. elif os.path.isdir(src_name):
  138. outputs.extend(
  139. copy_tree(
  140. src_name,
  141. dst_name,
  142. preserve_mode,
  143. preserve_times,
  144. preserve_symlinks,
  145. update,
  146. verbose=verbose,
  147. dry_run=dry_run,
  148. )
  149. )
  150. else:
  151. copy_file(
  152. src_name,
  153. dst_name,
  154. preserve_mode,
  155. preserve_times,
  156. update,
  157. verbose=verbose,
  158. dry_run=dry_run,
  159. )
  160. outputs.append(dst_name)
  161. return outputs
  162. def _build_cmdtuple(path, cmdtuples):
  163. """Helper for remove_tree()."""
  164. for f in os.listdir(path):
  165. real_f = os.path.join(path, f)
  166. if os.path.isdir(real_f) and not os.path.islink(real_f):
  167. _build_cmdtuple(real_f, cmdtuples)
  168. else:
  169. cmdtuples.append((os.remove, real_f))
  170. cmdtuples.append((os.rmdir, path))
  171. def remove_tree(directory, verbose=1, dry_run=0):
  172. """Recursively remove an entire directory tree.
  173. Any errors are ignored (apart from being reported to stdout if 'verbose'
  174. is true).
  175. """
  176. global _path_created
  177. if verbose >= 1:
  178. log.info("removing '%s' (and everything under it)", directory)
  179. if dry_run:
  180. return
  181. cmdtuples = []
  182. _build_cmdtuple(directory, cmdtuples)
  183. for cmd in cmdtuples:
  184. try:
  185. cmd[0](cmd[1])
  186. # remove dir from cache if it's already there
  187. abspath = os.path.abspath(cmd[1])
  188. if abspath in _path_created:
  189. del _path_created[abspath]
  190. except OSError as exc:
  191. log.warn("error removing %s: %s", directory, exc)
  192. def ensure_relative(path):
  193. """Take the full path 'path', and make it a relative path.
  194. This is useful to make 'path' the second argument to os.path.join().
  195. """
  196. drive, path = os.path.splitdrive(path)
  197. if path[0:1] == os.sep:
  198. path = drive + path[1:]
  199. return path