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