Changeset 391 for python/trunk/Lib/distutils/dir_util.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/distutils/dir_util.py
r2 r391 3 3 Utility functions for manipulating directories and directory trees.""" 4 4 5 # This module should be kept compatible with Python 2.1. 6 7 __revision__ = "$Id: dir_util.py 60923 2008-02-21 18:18:37Z guido.van.rossum $" 8 9 import os, sys 10 from types import * 5 __revision__ = "$Id$" 6 7 import os 8 import errno 11 9 from distutils.errors import DistutilsFileError, DistutilsInternalError 12 10 from distutils import log … … 19 17 # b) it blows up if the directory already exists (I want to silently 20 18 # 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.""" 19 def mkpath(name, mode=0777, 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 """ 30 29 31 30 global _path_created 32 31 33 32 # Detect a common bug -- name is None 34 if not isinstance(name, StringTypes):33 if not isinstance(name, basestring): 35 34 raise DistutilsInternalError, \ 36 35 "mkpath: 'name' must be a string (got %r)" % (name,) … … 52 51 53 52 while head and tail and not os.path.isdir(head): 54 #print "splitting '%s': " % head,55 53 (head, tail) = os.path.split(head) 56 #print "to ('%s','%s')" % (head, tail)57 54 tails.insert(0, tail) # push next higher dir onto stack 58 59 #print "stack of tails:", tails60 55 61 56 # now 'head' contains the deepest directory that already exists … … 70 65 continue 71 66 72 log.info("creating %s", head) 67 if verbose >= 1: 68 log.info("creating %s", head) 73 69 74 70 if not dry_run: 75 71 try: 76 os.mkdir(head) 77 created_dirs.append(head) 72 os.mkdir(head, mode) 78 73 except OSError, exc: 79 raise DistutilsFileError, \ 80 "could not create '%s': %s" % (head, exc[-1]) 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) 81 78 82 79 _path_created[abs_head] = 1 83 80 return created_dirs 84 81 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 82 def create_tree(base_dir, files, mode=0777, verbose=1, dry_run=0): 83 """Create all the empty directories under 'base_dir' needed to put 'files' 84 there. 85 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 """ 98 92 # First get the list of directories to create 99 93 need_dir = {} … … 105 99 # Now create them 106 100 for dir in need_dirs: 107 mkpath(dir, mode, dry_run=dry_run) 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 101 mkpath(dir, mode, verbose=verbose, dry_run=dry_run) 102 103 def copy_tree(src, dst, preserve_mode=1, preserve_times=1, 104 preserve_symlinks=0, update=0, verbose=1, dry_run=0): 105 """Copy an entire directory tree 'src' to a new location 'dst'. 106 107 Both 'src' and 'dst' must be directory names. If 'src' is not a 108 directory, raise DistutilsFileError. If 'dst' does not exist, it is 109 created with 'mkpath()'. The end result of the copy is that every 110 file in 'src' is copied to 'dst', and directories under 'src' are 111 recursively copied to 'dst'. Return the list of files that were 112 copied or might have been copied, using their output name. The 113 return value is unaffected by 'update' or 'dry_run': it is simply 114 the list of all files under 'src', with the names changed to be 115 under 'dst'. 116 117 'preserve_mode' and 'preserve_times' are the same as for 118 'copy_file'; note that they only apply to regular files, not to 119 directories. If 'preserve_symlinks' is true, symlinks will be 120 copied as symlinks (on platforms that support them!); otherwise 121 (the default), the destination of the symlink will be copied. 122 'update' and 'verbose' are the same as for 'copy_file'. 123 """ 138 124 from distutils.file_util import copy_file 139 125 … … 151 137 152 138 if not dry_run: 153 mkpath(dst )139 mkpath(dst, verbose=verbose) 154 140 155 141 outputs = [] … … 159 145 dst_name = os.path.join(dst, n) 160 146 147 if n.startswith('.nfs'): 148 # skip NFS rename files 149 continue 150 161 151 if preserve_symlinks and os.path.islink(src_name): 162 152 link_dest = os.readlink(src_name) 163 log.info("linking %s -> %s", dst_name, link_dest) 153 if verbose >= 1: 154 log.info("linking %s -> %s", dst_name, link_dest) 164 155 if not dry_run: 165 156 os.symlink(link_dest, dst_name) … … 170 161 copy_tree(src_name, dst_name, preserve_mode, 171 162 preserve_times, preserve_symlinks, update, 172 dry_run=dry_run))163 verbose=verbose, dry_run=dry_run)) 173 164 else: 174 165 copy_file(src_name, dst_name, preserve_mode, 175 preserve_times, update, dry_run=dry_run) 166 preserve_times, update, verbose=verbose, 167 dry_run=dry_run) 176 168 outputs.append(dst_name) 177 169 178 170 return outputs 179 171 180 # copy_tree ()181 182 # Helper for remove_tree()183 172 def _build_cmdtuple(path, cmdtuples): 173 """Helper for remove_tree().""" 184 174 for f in os.listdir(path): 185 175 real_f = os.path.join(path,f) … … 190 180 cmdtuples.append((os.rmdir, path)) 191 181 192 193 def remove_tree (directory, verbose=0, dry_run=0): 194 """Recursively remove an entire directory tree. Any errors are ignored 195 (apart from being reported to stdout if 'verbose' is true). 182 def remove_tree(directory, verbose=1, dry_run=0): 183 """Recursively remove an entire directory tree. 184 185 Any errors are ignored (apart from being reported to stdout if 'verbose' 186 is true). 196 187 """ 197 188 from distutils.util import grok_environment_error 198 189 global _path_created 199 190 200 log.info("removing '%s' (and everything under it)", directory) 191 if verbose >= 1: 192 log.info("removing '%s' (and everything under it)", directory) 201 193 if dry_run: 202 194 return … … 205 197 for cmd in cmdtuples: 206 198 try: 207 apply(cmd[0], (cmd[1],))199 cmd[0](cmd[1]) 208 200 # remove dir from cache if it's already there 209 201 abspath = os.path.abspath(cmd[1]) … … 214 206 exc, "error removing %s: " % directory)) 215 207 216 217 def ensure_relative (path): 218 """Take the full path 'path', and make it a relative path so 219 it can bethe second argument to os.path.join().208 def ensure_relative(path): 209 """Take the full path 'path', and make it a relative path. 210 211 This is useful to make 'path' the second argument to os.path.join(). 220 212 """ 221 213 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 214 if path[0:1] == os.sep: 215 path = drive + path[1:] 216 return path
Note:
See TracChangeset
for help on using the changeset viewer.