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