Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 1 | """distutils.dir_util |
| 2 | |
| 3 | Utility functions for manipulating directories and directory trees.""" |
| 4 | |
Martin v. Löwis | 5a6601c | 2004-11-10 22:23:15 +0000 | [diff] [blame] | 5 | # This module should be kept compatible with Python 2.1. |
Andrew M. Kuchling | d448f66 | 2002-11-19 13:12:28 +0000 | [diff] [blame] | 6 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 7 | __revision__ = "$Id$" |
| 8 | |
Andrew M. Kuchling | 40f23e0 | 2002-11-26 17:42:48 +0000 | [diff] [blame] | 9 | import os, sys |
Greg Ward | 2d238c5 | 2000-05-27 01:35:27 +0000 | [diff] [blame] | 10 | from types import * |
| 11 | from distutils.errors import DistutilsFileError, DistutilsInternalError |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 12 | from distutils import log |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 13 | |
| 14 | # cache for by mkpath() -- in addition to cheapening redundant calls, |
| 15 | # eliminates redundant "creating /foo/bar/baz" messages in dry-run mode |
Greg Ward | b248b7f | 2000-06-17 02:19:30 +0000 | [diff] [blame] | 16 | _path_created = {} |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 17 | |
| 18 | # I don't use os.makedirs because a) it's new to Python 1.5.2, and |
| 19 | # b) it blows up if the directory already exists (I want to silently |
| 20 | # succeed in that case). |
| 21 | def mkpath (name, mode=0777, verbose=0, dry_run=0): |
| 22 | """Create a directory and any missing ancestor directories. If the |
| 23 | directory already exists (or if 'name' is the empty string, which |
| 24 | means the current directory, which of course exists), then do |
| 25 | nothing. Raise DistutilsFileError if unable to create some |
| 26 | directory along the way (eg. some sub-path exists, but is a file |
| 27 | rather than a directory). If 'verbose' is true, print a one-line |
| 28 | summary of each mkdir to stdout. Return the list of directories |
| 29 | actually created.""" |
| 30 | |
Greg Ward | b248b7f | 2000-06-17 02:19:30 +0000 | [diff] [blame] | 31 | global _path_created |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 32 | |
Greg Ward | 2d238c5 | 2000-05-27 01:35:27 +0000 | [diff] [blame] | 33 | # Detect a common bug -- name is None |
Tim Peters | 9e34c04 | 2005-08-26 15:20:46 +0000 | [diff] [blame] | 34 | if not isinstance(name, StringTypes): |
Greg Ward | 2d238c5 | 2000-05-27 01:35:27 +0000 | [diff] [blame] | 35 | raise DistutilsInternalError, \ |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 36 | "mkpath: 'name' must be a string (got %r)" % (name,) |
Greg Ward | 2d238c5 | 2000-05-27 01:35:27 +0000 | [diff] [blame] | 37 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 38 | # XXX what's the better way to handle verbosity? print as we create |
| 39 | # each directory in the path (the current behaviour), or only announce |
| 40 | # the creation of the whole path? (quite easy to do the latter since |
| 41 | # we're not using a recursive algorithm) |
| 42 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 43 | name = os.path.normpath(name) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 44 | created_dirs = [] |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 45 | if os.path.isdir(name) or name == '': |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 46 | return created_dirs |
Greg Ward | 963cd2d | 2000-09-30 17:47:17 +0000 | [diff] [blame] | 47 | if _path_created.get(os.path.abspath(name)): |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 48 | return created_dirs |
| 49 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 50 | (head, tail) = os.path.split(name) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 51 | tails = [tail] # stack of lone dirs to create |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 52 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 53 | while head and tail and not os.path.isdir(head): |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 54 | #print "splitting '%s': " % head, |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 55 | (head, tail) = os.path.split(head) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 56 | #print "to ('%s','%s')" % (head, tail) |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 57 | tails.insert(0, tail) # push next higher dir onto stack |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 58 | |
| 59 | #print "stack of tails:", tails |
| 60 | |
| 61 | # now 'head' contains the deepest directory that already exists |
| 62 | # (that is, the child of 'head' in 'name' is the highest directory |
| 63 | # that does *not* exist) |
| 64 | for d in tails: |
| 65 | #print "head = %s, d = %s: " % (head, d), |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 66 | head = os.path.join(head, d) |
Greg Ward | 963cd2d | 2000-09-30 17:47:17 +0000 | [diff] [blame] | 67 | abs_head = os.path.abspath(head) |
| 68 | |
| 69 | if _path_created.get(abs_head): |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 70 | continue |
| 71 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 72 | log.info("creating %s", head) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 73 | |
| 74 | if not dry_run: |
| 75 | try: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 76 | os.mkdir(head) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 77 | created_dirs.append(head) |
| 78 | except OSError, exc: |
| 79 | raise DistutilsFileError, \ |
| 80 | "could not create '%s': %s" % (head, exc[-1]) |
| 81 | |
Greg Ward | 963cd2d | 2000-09-30 17:47:17 +0000 | [diff] [blame] | 82 | _path_created[abs_head] = 1 |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 83 | return created_dirs |
| 84 | |
| 85 | # mkpath () |
| 86 | |
| 87 | |
| 88 | def create_tree (base_dir, files, mode=0777, verbose=0, dry_run=0): |
| 89 | |
| 90 | """Create all the empty directories under 'base_dir' needed to |
| 91 | put 'files' there. 'base_dir' is just the a name of a directory |
| 92 | which doesn't necessarily exist yet; 'files' is a list of filenames |
| 93 | to be interpreted relative to 'base_dir'. 'base_dir' + the |
| 94 | directory portion of every file in 'files' will be created if it |
| 95 | doesn't already exist. 'mode', 'verbose' and 'dry_run' flags are as |
| 96 | for 'mkpath()'.""" |
| 97 | |
| 98 | # First get the list of directories to create |
| 99 | need_dir = {} |
| 100 | for file in files: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 101 | need_dir[os.path.join(base_dir, os.path.dirname(file))] = 1 |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 102 | need_dirs = need_dir.keys() |
| 103 | need_dirs.sort() |
| 104 | |
| 105 | # Now create them |
| 106 | for dir in need_dirs: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 107 | mkpath(dir, mode, dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 108 | |
| 109 | # create_tree () |
| 110 | |
| 111 | |
| 112 | def copy_tree (src, dst, |
| 113 | preserve_mode=1, |
| 114 | preserve_times=1, |
| 115 | preserve_symlinks=0, |
| 116 | update=0, |
| 117 | verbose=0, |
| 118 | dry_run=0): |
| 119 | |
| 120 | """Copy an entire directory tree 'src' to a new location 'dst'. Both |
| 121 | 'src' and 'dst' must be directory names. If 'src' is not a |
| 122 | directory, raise DistutilsFileError. If 'dst' does not exist, it is |
| 123 | created with 'mkpath()'. The end result of the copy is that every |
| 124 | file in 'src' is copied to 'dst', and directories under 'src' are |
| 125 | recursively copied to 'dst'. Return the list of files that were |
| 126 | copied or might have been copied, using their output name. The |
| 127 | return value is unaffected by 'update' or 'dry_run': it is simply |
| 128 | the list of all files under 'src', with the names changed to be |
| 129 | under 'dst'. |
| 130 | |
| 131 | 'preserve_mode' and 'preserve_times' are the same as for |
| 132 | 'copy_file'; note that they only apply to regular files, not to |
| 133 | directories. If 'preserve_symlinks' is true, symlinks will be |
| 134 | copied as symlinks (on platforms that support them!); otherwise |
| 135 | (the default), the destination of the symlink will be copied. |
| 136 | 'update' and 'verbose' are the same as for 'copy_file'.""" |
| 137 | |
| 138 | from distutils.file_util import copy_file |
| 139 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 140 | if not dry_run and not os.path.isdir(src): |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 141 | raise DistutilsFileError, \ |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 142 | "cannot copy tree '%s': not a directory" % src |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 143 | try: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 144 | names = os.listdir(src) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 145 | except os.error, (errno, errstr): |
| 146 | if dry_run: |
| 147 | names = [] |
| 148 | else: |
| 149 | raise DistutilsFileError, \ |
| 150 | "error listing files in '%s': %s" % (src, errstr) |
| 151 | |
| 152 | if not dry_run: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 153 | mkpath(dst) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 154 | |
| 155 | outputs = [] |
| 156 | |
| 157 | for n in names: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 158 | src_name = os.path.join(src, n) |
| 159 | dst_name = os.path.join(dst, n) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 160 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 161 | if preserve_symlinks and os.path.islink(src_name): |
| 162 | link_dest = os.readlink(src_name) |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 163 | log.info("linking %s -> %s", dst_name, link_dest) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 164 | if not dry_run: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 165 | os.symlink(link_dest, dst_name) |
| 166 | outputs.append(dst_name) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 167 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 168 | elif os.path.isdir(src_name): |
| 169 | outputs.extend( |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 170 | copy_tree(src_name, dst_name, preserve_mode, |
| 171 | preserve_times, preserve_symlinks, update, |
| 172 | dry_run=dry_run)) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 173 | else: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 174 | copy_file(src_name, dst_name, preserve_mode, |
| 175 | preserve_times, update, dry_run=dry_run) |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 176 | outputs.append(dst_name) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 177 | |
| 178 | return outputs |
| 179 | |
| 180 | # copy_tree () |
| 181 | |
Greg Ward | 039accf | 2000-06-17 01:58:14 +0000 | [diff] [blame] | 182 | # Helper for remove_tree() |
| 183 | def _build_cmdtuple(path, cmdtuples): |
| 184 | for f in os.listdir(path): |
| 185 | real_f = os.path.join(path,f) |
| 186 | if os.path.isdir(real_f) and not os.path.islink(real_f): |
| 187 | _build_cmdtuple(real_f, cmdtuples) |
| 188 | else: |
| 189 | cmdtuples.append((os.remove, real_f)) |
| 190 | cmdtuples.append((os.rmdir, path)) |
| 191 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 192 | |
| 193 | def remove_tree (directory, verbose=0, dry_run=0): |
| 194 | """Recursively remove an entire directory tree. Any errors are ignored |
Greg Ward | fcd4f87 | 2000-06-17 02:18:19 +0000 | [diff] [blame] | 195 | (apart from being reported to stdout if 'verbose' is true). |
| 196 | """ |
| 197 | from distutils.util import grok_environment_error |
Greg Ward | b248b7f | 2000-06-17 02:19:30 +0000 | [diff] [blame] | 198 | global _path_created |
Greg Ward | fcd4f87 | 2000-06-17 02:18:19 +0000 | [diff] [blame] | 199 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 200 | log.info("removing '%s' (and everything under it)", directory) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 201 | if dry_run: |
| 202 | return |
Greg Ward | 039accf | 2000-06-17 01:58:14 +0000 | [diff] [blame] | 203 | cmdtuples = [] |
| 204 | _build_cmdtuple(directory, cmdtuples) |
| 205 | for cmd in cmdtuples: |
| 206 | try: |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 207 | cmd[0](cmd[1]) |
Greg Ward | 039accf | 2000-06-17 01:58:14 +0000 | [diff] [blame] | 208 | # remove dir from cache if it's already there |
Greg Ward | 963cd2d | 2000-09-30 17:47:17 +0000 | [diff] [blame] | 209 | abspath = os.path.abspath(cmd[1]) |
Neal Norwitz | f1a69c1 | 2006-08-20 16:25:10 +0000 | [diff] [blame^] | 210 | if abspath in _path_created: |
Greg Ward | 963cd2d | 2000-09-30 17:47:17 +0000 | [diff] [blame] | 211 | del _path_created[abspath] |
Greg Ward | 039accf | 2000-06-17 01:58:14 +0000 | [diff] [blame] | 212 | except (IOError, OSError), exc: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 213 | log.warn(grok_environment_error( |
| 214 | exc, "error removing %s: " % directory)) |
Andrew M. Kuchling | 40f23e0 | 2002-11-26 17:42:48 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | def ensure_relative (path): |
| 218 | """Take the full path 'path', and make it a relative path so |
| 219 | it can be the second argument to os.path.join(). |
| 220 | """ |
| 221 | drive, path = os.path.splitdrive(path) |
| 222 | if sys.platform == 'mac': |
| 223 | return os.sep + path |
| 224 | else: |
| 225 | if path[0:1] == os.sep: |
| 226 | path = drive + path[1:] |
| 227 | return path |