Changeset 391 for python/trunk/Lib/distutils/file_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/file_util.py
r2 r391 4 4 """ 5 5 6 # This module should be kept compatible with Python 2.1. 7 8 __revision__ = "$Id: file_util.py 37828 2004-11-10 22:23:15Z loewis $" 6 __revision__ = "$Id$" 9 7 10 8 import os … … 13 11 14 12 # for generating verbose output in 'copy_file()' 15 _copy_action = { None: 'copying', 16 'hard': 'hard linking', 17 'sym': 'symbolically linking' } 18 19 20 def _copy_file_contents (src, dst, buffer_size=16*1024): 21 """Copy the file 'src' to 'dst'; both must be filenames. Any error 22 opening either file, reading from 'src', or writing to 'dst', raises 23 DistutilsFileError. Data is read/written in chunks of 'buffer_size' 24 bytes (default 16k). No attempt is made to handle anything apart from 25 regular files. 13 _copy_action = {None: 'copying', 14 'hard': 'hard linking', 15 'sym': 'symbolically linking'} 16 17 18 def _copy_file_contents(src, dst, buffer_size=16*1024): 19 """Copy the file 'src' to 'dst'. 20 21 Both must be filenames. Any error opening either file, reading from 22 'src', or writing to 'dst', raises DistutilsFileError. Data is 23 read/written in chunks of 'buffer_size' bytes (default 16k). No attempt 24 is made to handle anything apart from regular files. 26 25 """ 27 26 # Stolen from shutil module in the standard library, but with 28 27 # custom error-handling added. 29 30 28 fsrc = None 31 29 fdst = None … … 34 32 fsrc = open(src, 'rb') 35 33 except os.error, (errno, errstr): 36 raise DistutilsFileError, \ 37 "could not open '%s': %s" % (src, errstr) 34 raise DistutilsFileError("could not open '%s': %s" % (src, errstr)) 38 35 39 36 if os.path.exists(dst): … … 41 38 os.unlink(dst) 42 39 except os.error, (errno, errstr): 43 raise DistutilsFileError , \44 "could not delete '%s': %s" % (dst, errstr) 40 raise DistutilsFileError( 41 "could not delete '%s': %s" % (dst, errstr)) 45 42 46 43 try: 47 44 fdst = open(dst, 'wb') 48 45 except os.error, (errno, errstr): 49 raise DistutilsFileError , \50 "could not create '%s': %s" % (dst, errstr) 46 raise DistutilsFileError( 47 "could not create '%s': %s" % (dst, errstr)) 51 48 52 49 while 1: … … 54 51 buf = fsrc.read(buffer_size) 55 52 except os.error, (errno, errstr): 56 raise DistutilsFileError , \57 "could not read from '%s': %s" % (src, errstr) 53 raise DistutilsFileError( 54 "could not read from '%s': %s" % (src, errstr)) 58 55 59 56 if not buf: … … 63 60 fdst.write(buf) 64 61 except os.error, (errno, errstr): 65 raise DistutilsFileError , \66 "could not write to '%s': %s" % (dst, errstr) 62 raise DistutilsFileError( 63 "could not write to '%s': %s" % (dst, errstr)) 67 64 68 65 finally: … … 72 69 fsrc.close() 73 70 74 # _copy_file_contents() 75 76 def copy_file (src, dst, 77 preserve_mode=1, 78 preserve_times=1, 79 update=0, 80 link=None, 81 verbose=0, 82 dry_run=0): 83 84 """Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is 85 copied there with the same name; otherwise, it must be a filename. (If 86 the file exists, it will be ruthlessly clobbered.) If 'preserve_mode' 87 is true (the default), the file's mode (type and permission bits, or 88 whatever is analogous on the current platform) is copied. If 89 'preserve_times' is true (the default), the last-modified and 90 last-access times are copied as well. If 'update' is true, 'src' will 91 only be copied if 'dst' does not exist, or if 'dst' does exist but is 92 older than 'src'. 71 def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0, 72 link=None, verbose=1, dry_run=0): 73 """Copy a file 'src' to 'dst'. 74 75 If 'dst' is a directory, then 'src' is copied there with the same name; 76 otherwise, it must be a filename. (If the file exists, it will be 77 ruthlessly clobbered.) If 'preserve_mode' is true (the default), 78 the file's mode (type and permission bits, or whatever is analogous on 79 the current platform) is copied. If 'preserve_times' is true (the 80 default), the last-modified and last-access times are copied as well. 81 If 'update' is true, 'src' will only be copied if 'dst' does not exist, 82 or if 'dst' does exist but is older than 'src'. 93 83 94 84 'link' allows you to make hard links (os.link) or symbolic links … … 116 106 117 107 if not os.path.isfile(src): 118 raise DistutilsFileError , \119 "can't copy '%s': doesn't exist or not a regular file" % src 108 raise DistutilsFileError( 109 "can't copy '%s': doesn't exist or not a regular file" % src) 120 110 121 111 if os.path.isdir(dst): … … 126 116 127 117 if update and not newer(src, dst): 128 log.debug("not copying %s (output up-to-date)", src) 118 if verbose >= 1: 119 log.debug("not copying %s (output up-to-date)", src) 129 120 return dst, 0 130 121 … … 132 123 action = _copy_action[link] 133 124 except KeyError: 134 raise ValueError, \ 135 "invalid value '%s' for 'link' argument" % link 136 if os.path.basename(dst) == os.path.basename(src): 137 log.info("%s %s -> %s", action, src, dir) 138 else: 139 log.info("%s %s -> %s", action, src, dst) 125 raise ValueError("invalid value '%s' for 'link' argument" % link) 126 127 if verbose >= 1: 128 if os.path.basename(dst) == os.path.basename(src): 129 log.info("%s %s -> %s", action, src, dir) 130 else: 131 log.info("%s %s -> %s", action, src, dst) 140 132 141 133 if dry_run: 142 134 return (dst, 1) 143 135 144 # On Mac OS, use the native file copy routine145 if os.name == 'mac':146 import macostools147 try:148 macostools.copy(src, dst, 0, preserve_times)149 except os.error, exc:150 raise DistutilsFileError, \151 "could not copy '%s' to '%s': %s" % (src, dst, exc[-1])152 153 136 # If linking (hard or symbolic), use the appropriate system call 154 137 # (Unix only, of course, but that's the caller's responsibility) 155 elif link == 'hard':138 if link == 'hard': 156 139 if not (os.path.exists(dst) and os.path.samefile(src, dst)): 157 140 os.link(src, dst) … … 176 159 return (dst, 1) 177 160 178 # copy_file ()179 180 181 161 # XXX I suspect this is Unix-specific -- need porting help! 182 def move_file (src, dst, 183 verbose=0, 184 dry_run=0): 185 186 """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will 187 be moved into it with the same name; otherwise, 'src' is just renamed 188 to 'dst'. Return the new full name of the file. 162 def move_file (src, dst, verbose=1, dry_run=0): 163 """Move a file 'src' to 'dst'. 164 165 If 'dst' is a directory, the file will be moved into it with the same 166 name; otherwise, 'src' is just renamed to 'dst'. Return the new 167 full name of the file. 189 168 190 169 Handles cross-device moves on Unix using 'copy_file()'. What about … … 194 173 import errno 195 174 196 log.info("moving %s -> %s", src, dst) 175 if verbose >= 1: 176 log.info("moving %s -> %s", src, dst) 197 177 198 178 if dry_run: … … 200 180 201 181 if not isfile(src): 202 raise DistutilsFileError, \ 203 "can't move '%s': not a regular file" % src 182 raise DistutilsFileError("can't move '%s': not a regular file" % src) 204 183 205 184 if isdir(dst): 206 185 dst = os.path.join(dst, basename(src)) 207 186 elif exists(dst): 208 raise DistutilsFileError , \209 "can't move '%s': destination '%s' already exists" % \210 (src, dst) 187 raise DistutilsFileError( 188 "can't move '%s': destination '%s' already exists" % 189 (src, dst)) 211 190 212 191 if not isdir(dirname(dst)): 213 raise DistutilsFileError , \192 raise DistutilsFileError( 214 193 "can't move '%s': destination '%s' not a valid path" % \ 215 (src, dst) 194 (src, dst)) 216 195 217 196 copy_it = 0 … … 222 201 copy_it = 1 223 202 else: 224 raise DistutilsFileError , \225 "couldn't move '%s' to '%s': %s" % (src, dst, msg) 203 raise DistutilsFileError( 204 "couldn't move '%s' to '%s': %s" % (src, dst, msg)) 226 205 227 206 if copy_it: 228 copy_file(src, dst )207 copy_file(src, dst, verbose=verbose) 229 208 try: 230 209 os.unlink(src) … … 234 213 except os.error: 235 214 pass 236 raise DistutilsFileError , \215 raise DistutilsFileError( 237 216 ("couldn't move '%s' to '%s' by copy/delete: " + 238 "delete '%s' failed: %s") % \ 239 (src, dst, src, msg) 240 217 "delete '%s' failed: %s") % 218 (src, dst, src, msg)) 241 219 return dst 242 243 # move_file ()244 220 245 221 … … 249 225 """ 250 226 f = open(filename, "w") 251 for line in contents: 252 f.write(line + "\n") 253 f.close() 227 try: 228 for line in contents: 229 f.write(line + "\n") 230 finally: 231 f.close()
Note:
See TracChangeset
for help on using the changeset viewer.