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 | |
| 5 | # created 2000/04/03, Greg Ward (extracted from util.py) |
| 6 | |
| 7 | __revision__ = "$Id$" |
| 8 | |
| 9 | import os |
Greg Ward | 2d238c5 | 2000-05-27 01:35:27 +0000 | [diff] [blame^] | 10 | from types import * |
| 11 | from distutils.errors import DistutilsFileError, DistutilsInternalError |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | # cache for by mkpath() -- in addition to cheapening redundant calls, |
| 15 | # eliminates redundant "creating /foo/bar/baz" messages in dry-run mode |
| 16 | PATH_CREATED = {} |
| 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 | |
| 31 | global PATH_CREATED |
| 32 | |
Greg Ward | 2d238c5 | 2000-05-27 01:35:27 +0000 | [diff] [blame^] | 33 | # Detect a common bug -- name is None |
| 34 | if type(name) is not StringType: |
| 35 | raise DistutilsInternalError, \ |
| 36 | "mkpath: 'name' must be a string (got %s)" % `name` |
| 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 | |
| 43 | name = os.path.normpath (name) |
| 44 | created_dirs = [] |
| 45 | if os.path.isdir (name) or name == '': |
| 46 | return created_dirs |
| 47 | if PATH_CREATED.get (name): |
| 48 | return created_dirs |
| 49 | |
| 50 | (head, tail) = os.path.split (name) |
| 51 | tails = [tail] # stack of lone dirs to create |
| 52 | |
| 53 | while head and tail and not os.path.isdir (head): |
| 54 | #print "splitting '%s': " % head, |
| 55 | (head, tail) = os.path.split (head) |
| 56 | #print "to ('%s','%s')" % (head, tail) |
| 57 | tails.insert (0, tail) # push next higher dir onto stack |
| 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), |
| 66 | head = os.path.join (head, d) |
| 67 | if PATH_CREATED.get (head): |
| 68 | continue |
| 69 | |
| 70 | if verbose: |
| 71 | print "creating", head |
| 72 | |
| 73 | if not dry_run: |
| 74 | try: |
| 75 | os.mkdir (head) |
| 76 | created_dirs.append(head) |
| 77 | except OSError, exc: |
| 78 | raise DistutilsFileError, \ |
| 79 | "could not create '%s': %s" % (head, exc[-1]) |
| 80 | |
| 81 | PATH_CREATED[head] = 1 |
| 82 | return created_dirs |
| 83 | |
| 84 | # mkpath () |
| 85 | |
| 86 | |
| 87 | def create_tree (base_dir, files, mode=0777, verbose=0, dry_run=0): |
| 88 | |
| 89 | """Create all the empty directories under 'base_dir' needed to |
| 90 | put 'files' there. 'base_dir' is just the a name of a directory |
| 91 | which doesn't necessarily exist yet; 'files' is a list of filenames |
| 92 | to be interpreted relative to 'base_dir'. 'base_dir' + the |
| 93 | directory portion of every file in 'files' will be created if it |
| 94 | doesn't already exist. 'mode', 'verbose' and 'dry_run' flags are as |
| 95 | for 'mkpath()'.""" |
| 96 | |
| 97 | # First get the list of directories to create |
| 98 | need_dir = {} |
| 99 | for file in files: |
| 100 | need_dir[os.path.join (base_dir, os.path.dirname (file))] = 1 |
| 101 | need_dirs = need_dir.keys() |
| 102 | need_dirs.sort() |
| 103 | |
| 104 | # Now create them |
| 105 | for dir in need_dirs: |
| 106 | mkpath (dir, mode, verbose, dry_run) |
| 107 | |
| 108 | # create_tree () |
| 109 | |
| 110 | |
| 111 | def copy_tree (src, dst, |
| 112 | preserve_mode=1, |
| 113 | preserve_times=1, |
| 114 | preserve_symlinks=0, |
| 115 | update=0, |
| 116 | verbose=0, |
| 117 | dry_run=0): |
| 118 | |
| 119 | """Copy an entire directory tree 'src' to a new location 'dst'. Both |
| 120 | 'src' and 'dst' must be directory names. If 'src' is not a |
| 121 | directory, raise DistutilsFileError. If 'dst' does not exist, it is |
| 122 | created with 'mkpath()'. The end result of the copy is that every |
| 123 | file in 'src' is copied to 'dst', and directories under 'src' are |
| 124 | recursively copied to 'dst'. Return the list of files that were |
| 125 | copied or might have been copied, using their output name. The |
| 126 | return value is unaffected by 'update' or 'dry_run': it is simply |
| 127 | the list of all files under 'src', with the names changed to be |
| 128 | under 'dst'. |
| 129 | |
| 130 | 'preserve_mode' and 'preserve_times' are the same as for |
| 131 | 'copy_file'; note that they only apply to regular files, not to |
| 132 | directories. If 'preserve_symlinks' is true, symlinks will be |
| 133 | copied as symlinks (on platforms that support them!); otherwise |
| 134 | (the default), the destination of the symlink will be copied. |
| 135 | 'update' and 'verbose' are the same as for 'copy_file'.""" |
| 136 | |
| 137 | from distutils.file_util import copy_file |
| 138 | |
| 139 | if not dry_run and not os.path.isdir (src): |
| 140 | raise DistutilsFileError, \ |
| 141 | "cannot copy tree '%s': not a directory" % src |
| 142 | try: |
| 143 | names = os.listdir (src) |
| 144 | except os.error, (errno, errstr): |
| 145 | if dry_run: |
| 146 | names = [] |
| 147 | else: |
| 148 | raise DistutilsFileError, \ |
| 149 | "error listing files in '%s': %s" % (src, errstr) |
| 150 | |
| 151 | if not dry_run: |
| 152 | mkpath (dst, verbose=verbose) |
| 153 | |
| 154 | outputs = [] |
| 155 | |
| 156 | for n in names: |
| 157 | src_name = os.path.join (src, n) |
| 158 | dst_name = os.path.join (dst, n) |
| 159 | |
| 160 | if preserve_symlinks and os.path.islink (src_name): |
| 161 | link_dest = os.readlink (src_name) |
| 162 | if verbose: |
| 163 | print "linking %s -> %s" % (dst_name, link_dest) |
| 164 | if not dry_run: |
| 165 | os.symlink (link_dest, dst_name) |
| 166 | outputs.append (dst_name) |
| 167 | |
| 168 | elif os.path.isdir (src_name): |
| 169 | outputs.extend ( |
| 170 | copy_tree (src_name, dst_name, |
| 171 | preserve_mode, preserve_times, preserve_symlinks, |
| 172 | update, verbose, dry_run)) |
| 173 | else: |
| 174 | copy_file (src_name, dst_name, |
| 175 | preserve_mode, preserve_times, |
| 176 | update, None, verbose, dry_run) |
| 177 | outputs.append (dst_name) |
| 178 | |
| 179 | return outputs |
| 180 | |
| 181 | # copy_tree () |
| 182 | |
| 183 | |
| 184 | def remove_tree (directory, verbose=0, dry_run=0): |
| 185 | """Recursively remove an entire directory tree. Any errors are ignored |
| 186 | (apart from being reported to stdout if 'verbose' is true).""" |
| 187 | |
| 188 | from shutil import rmtree |
| 189 | |
| 190 | if verbose: |
| 191 | print "removing '%s' (and everything under it)" % directory |
| 192 | if dry_run: |
| 193 | return |
| 194 | try: |
| 195 | rmtree(directory,1) |
| 196 | except (IOError, OSError), exc: |
| 197 | if verbose: |
| 198 | if exc.filename: |
| 199 | print "error removing %s: %s (%s)" % \ |
| 200 | (directory, exc.strerror, exc.filename) |
| 201 | else: |
| 202 | print "error removing %s: %s" % (directory, exc.strerror) |