| 1 | n/a | """distutils.file_util |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Utility functions for operating on single files. |
|---|
| 4 | n/a | """ |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | from distutils.errors import DistutilsFileError |
|---|
| 8 | n/a | from distutils import log |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | # for generating verbose output in 'copy_file()' |
|---|
| 11 | n/a | _copy_action = { None: 'copying', |
|---|
| 12 | n/a | 'hard': 'hard linking', |
|---|
| 13 | n/a | 'sym': 'symbolically linking' } |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | def _copy_file_contents(src, dst, buffer_size=16*1024): |
|---|
| 17 | n/a | """Copy the file 'src' to 'dst'; both must be filenames. Any error |
|---|
| 18 | n/a | opening either file, reading from 'src', or writing to 'dst', raises |
|---|
| 19 | n/a | DistutilsFileError. Data is read/written in chunks of 'buffer_size' |
|---|
| 20 | n/a | bytes (default 16k). No attempt is made to handle anything apart from |
|---|
| 21 | n/a | regular files. |
|---|
| 22 | n/a | """ |
|---|
| 23 | n/a | # Stolen from shutil module in the standard library, but with |
|---|
| 24 | n/a | # custom error-handling added. |
|---|
| 25 | n/a | fsrc = None |
|---|
| 26 | n/a | fdst = None |
|---|
| 27 | n/a | try: |
|---|
| 28 | n/a | try: |
|---|
| 29 | n/a | fsrc = open(src, 'rb') |
|---|
| 30 | n/a | except OSError as e: |
|---|
| 31 | n/a | raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror)) |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | if os.path.exists(dst): |
|---|
| 34 | n/a | try: |
|---|
| 35 | n/a | os.unlink(dst) |
|---|
| 36 | n/a | except OSError as e: |
|---|
| 37 | n/a | raise DistutilsFileError( |
|---|
| 38 | n/a | "could not delete '%s': %s" % (dst, e.strerror)) |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | try: |
|---|
| 41 | n/a | fdst = open(dst, 'wb') |
|---|
| 42 | n/a | except OSError as e: |
|---|
| 43 | n/a | raise DistutilsFileError( |
|---|
| 44 | n/a | "could not create '%s': %s" % (dst, e.strerror)) |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | while True: |
|---|
| 47 | n/a | try: |
|---|
| 48 | n/a | buf = fsrc.read(buffer_size) |
|---|
| 49 | n/a | except OSError as e: |
|---|
| 50 | n/a | raise DistutilsFileError( |
|---|
| 51 | n/a | "could not read from '%s': %s" % (src, e.strerror)) |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | if not buf: |
|---|
| 54 | n/a | break |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | try: |
|---|
| 57 | n/a | fdst.write(buf) |
|---|
| 58 | n/a | except OSError as e: |
|---|
| 59 | n/a | raise DistutilsFileError( |
|---|
| 60 | n/a | "could not write to '%s': %s" % (dst, e.strerror)) |
|---|
| 61 | n/a | finally: |
|---|
| 62 | n/a | if fdst: |
|---|
| 63 | n/a | fdst.close() |
|---|
| 64 | n/a | if fsrc: |
|---|
| 65 | n/a | fsrc.close() |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0, |
|---|
| 68 | n/a | link=None, verbose=1, dry_run=0): |
|---|
| 69 | n/a | """Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is |
|---|
| 70 | n/a | copied there with the same name; otherwise, it must be a filename. (If |
|---|
| 71 | n/a | the file exists, it will be ruthlessly clobbered.) If 'preserve_mode' |
|---|
| 72 | n/a | is true (the default), the file's mode (type and permission bits, or |
|---|
| 73 | n/a | whatever is analogous on the current platform) is copied. If |
|---|
| 74 | n/a | 'preserve_times' is true (the default), the last-modified and |
|---|
| 75 | n/a | last-access times are copied as well. If 'update' is true, 'src' will |
|---|
| 76 | n/a | only be copied if 'dst' does not exist, or if 'dst' does exist but is |
|---|
| 77 | n/a | older than 'src'. |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | 'link' allows you to make hard links (os.link) or symbolic links |
|---|
| 80 | n/a | (os.symlink) instead of copying: set it to "hard" or "sym"; if it is |
|---|
| 81 | n/a | None (the default), files are copied. Don't set 'link' on systems that |
|---|
| 82 | n/a | don't support it: 'copy_file()' doesn't check if hard or symbolic |
|---|
| 83 | n/a | linking is available. If hardlink fails, falls back to |
|---|
| 84 | n/a | _copy_file_contents(). |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | Under Mac OS, uses the native file copy function in macostools; on |
|---|
| 87 | n/a | other systems, uses '_copy_file_contents()' to copy file contents. |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | Return a tuple (dest_name, copied): 'dest_name' is the actual name of |
|---|
| 90 | n/a | the output file, and 'copied' is true if the file was copied (or would |
|---|
| 91 | n/a | have been copied, if 'dry_run' true). |
|---|
| 92 | n/a | """ |
|---|
| 93 | n/a | # XXX if the destination file already exists, we clobber it if |
|---|
| 94 | n/a | # copying, but blow up if linking. Hmmm. And I don't know what |
|---|
| 95 | n/a | # macostools.copyfile() does. Should definitely be consistent, and |
|---|
| 96 | n/a | # should probably blow up if destination exists and we would be |
|---|
| 97 | n/a | # changing it (ie. it's not already a hard/soft link to src OR |
|---|
| 98 | n/a | # (not update) and (src newer than dst). |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | from distutils.dep_util import newer |
|---|
| 101 | n/a | from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | if not os.path.isfile(src): |
|---|
| 104 | n/a | raise DistutilsFileError( |
|---|
| 105 | n/a | "can't copy '%s': doesn't exist or not a regular file" % src) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | if os.path.isdir(dst): |
|---|
| 108 | n/a | dir = dst |
|---|
| 109 | n/a | dst = os.path.join(dst, os.path.basename(src)) |
|---|
| 110 | n/a | else: |
|---|
| 111 | n/a | dir = os.path.dirname(dst) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | if update and not newer(src, dst): |
|---|
| 114 | n/a | if verbose >= 1: |
|---|
| 115 | n/a | log.debug("not copying %s (output up-to-date)", src) |
|---|
| 116 | n/a | return (dst, 0) |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | try: |
|---|
| 119 | n/a | action = _copy_action[link] |
|---|
| 120 | n/a | except KeyError: |
|---|
| 121 | n/a | raise ValueError("invalid value '%s' for 'link' argument" % link) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | if verbose >= 1: |
|---|
| 124 | n/a | if os.path.basename(dst) == os.path.basename(src): |
|---|
| 125 | n/a | log.info("%s %s -> %s", action, src, dir) |
|---|
| 126 | n/a | else: |
|---|
| 127 | n/a | log.info("%s %s -> %s", action, src, dst) |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | if dry_run: |
|---|
| 130 | n/a | return (dst, 1) |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | # If linking (hard or symbolic), use the appropriate system call |
|---|
| 133 | n/a | # (Unix only, of course, but that's the caller's responsibility) |
|---|
| 134 | n/a | elif link == 'hard': |
|---|
| 135 | n/a | if not (os.path.exists(dst) and os.path.samefile(src, dst)): |
|---|
| 136 | n/a | try: |
|---|
| 137 | n/a | os.link(src, dst) |
|---|
| 138 | n/a | return (dst, 1) |
|---|
| 139 | n/a | except OSError: |
|---|
| 140 | n/a | # If hard linking fails, fall back on copying file |
|---|
| 141 | n/a | # (some special filesystems don't support hard linking |
|---|
| 142 | n/a | # even under Unix, see issue #8876). |
|---|
| 143 | n/a | pass |
|---|
| 144 | n/a | elif link == 'sym': |
|---|
| 145 | n/a | if not (os.path.exists(dst) and os.path.samefile(src, dst)): |
|---|
| 146 | n/a | os.symlink(src, dst) |
|---|
| 147 | n/a | return (dst, 1) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | # Otherwise (non-Mac, not linking), copy the file contents and |
|---|
| 150 | n/a | # (optionally) copy the times and mode. |
|---|
| 151 | n/a | _copy_file_contents(src, dst) |
|---|
| 152 | n/a | if preserve_mode or preserve_times: |
|---|
| 153 | n/a | st = os.stat(src) |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | # According to David Ascher <da@ski.org>, utime() should be done |
|---|
| 156 | n/a | # before chmod() (at least under NT). |
|---|
| 157 | n/a | if preserve_times: |
|---|
| 158 | n/a | os.utime(dst, (st[ST_ATIME], st[ST_MTIME])) |
|---|
| 159 | n/a | if preserve_mode: |
|---|
| 160 | n/a | os.chmod(dst, S_IMODE(st[ST_MODE])) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | return (dst, 1) |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | # XXX I suspect this is Unix-specific -- need porting help! |
|---|
| 166 | n/a | def move_file (src, dst, |
|---|
| 167 | n/a | verbose=1, |
|---|
| 168 | n/a | dry_run=0): |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will |
|---|
| 171 | n/a | be moved into it with the same name; otherwise, 'src' is just renamed |
|---|
| 172 | n/a | to 'dst'. Return the new full name of the file. |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | Handles cross-device moves on Unix using 'copy_file()'. What about |
|---|
| 175 | n/a | other systems??? |
|---|
| 176 | n/a | """ |
|---|
| 177 | n/a | from os.path import exists, isfile, isdir, basename, dirname |
|---|
| 178 | n/a | import errno |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | if verbose >= 1: |
|---|
| 181 | n/a | log.info("moving %s -> %s", src, dst) |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | if dry_run: |
|---|
| 184 | n/a | return dst |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | if not isfile(src): |
|---|
| 187 | n/a | raise DistutilsFileError("can't move '%s': not a regular file" % src) |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | if isdir(dst): |
|---|
| 190 | n/a | dst = os.path.join(dst, basename(src)) |
|---|
| 191 | n/a | elif exists(dst): |
|---|
| 192 | n/a | raise DistutilsFileError( |
|---|
| 193 | n/a | "can't move '%s': destination '%s' already exists" % |
|---|
| 194 | n/a | (src, dst)) |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | if not isdir(dirname(dst)): |
|---|
| 197 | n/a | raise DistutilsFileError( |
|---|
| 198 | n/a | "can't move '%s': destination '%s' not a valid path" % |
|---|
| 199 | n/a | (src, dst)) |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | copy_it = False |
|---|
| 202 | n/a | try: |
|---|
| 203 | n/a | os.rename(src, dst) |
|---|
| 204 | n/a | except OSError as e: |
|---|
| 205 | n/a | (num, msg) = e.args |
|---|
| 206 | n/a | if num == errno.EXDEV: |
|---|
| 207 | n/a | copy_it = True |
|---|
| 208 | n/a | else: |
|---|
| 209 | n/a | raise DistutilsFileError( |
|---|
| 210 | n/a | "couldn't move '%s' to '%s': %s" % (src, dst, msg)) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | if copy_it: |
|---|
| 213 | n/a | copy_file(src, dst, verbose=verbose) |
|---|
| 214 | n/a | try: |
|---|
| 215 | n/a | os.unlink(src) |
|---|
| 216 | n/a | except OSError as e: |
|---|
| 217 | n/a | (num, msg) = e.args |
|---|
| 218 | n/a | try: |
|---|
| 219 | n/a | os.unlink(dst) |
|---|
| 220 | n/a | except OSError: |
|---|
| 221 | n/a | pass |
|---|
| 222 | n/a | raise DistutilsFileError( |
|---|
| 223 | n/a | "couldn't move '%s' to '%s' by copy/delete: " |
|---|
| 224 | n/a | "delete '%s' failed: %s" |
|---|
| 225 | n/a | % (src, dst, src, msg)) |
|---|
| 226 | n/a | return dst |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | def write_file (filename, contents): |
|---|
| 230 | n/a | """Create a file with the specified name and write 'contents' (a |
|---|
| 231 | n/a | sequence of strings without line terminators) to it. |
|---|
| 232 | n/a | """ |
|---|
| 233 | n/a | f = open(filename, "w") |
|---|
| 234 | n/a | try: |
|---|
| 235 | n/a | for line in contents: |
|---|
| 236 | n/a | f.write(line + "\n") |
|---|
| 237 | n/a | finally: |
|---|
| 238 | n/a | f.close() |
|---|