Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Utility functions for copying files and directory trees. |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 959fa01 | 1999-08-18 20:03:17 +0000 | [diff] [blame] | 3 | XXX The functions here don't copy the resource fork or other metadata on Mac. |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 4 | |
| 5 | """ |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | c96207a | 1992-03-31 18:55:40 +0000 | [diff] [blame] | 7 | import os |
Guido van Rossum | 83c03e2 | 1999-02-23 23:07:51 +0000 | [diff] [blame] | 8 | import sys |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 9 | import stat |
Brett Cannon | 1c3fa18 | 2004-06-19 21:11:35 +0000 | [diff] [blame] | 10 | from os.path import abspath |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 11 | import fnmatch |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 12 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 13 | __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", |
Antoine Pitrou | 1fc0231 | 2009-05-01 20:55:35 +0000 | [diff] [blame] | 14 | "copytree","move","rmtree","Error", "SpecialFileError"] |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 15 | |
Neal Norwitz | 4ce69a5 | 2005-09-01 00:45:28 +0000 | [diff] [blame] | 16 | class Error(EnvironmentError): |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 17 | pass |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 18 | |
Antoine Pitrou | 1fc0231 | 2009-05-01 20:55:35 +0000 | [diff] [blame] | 19 | class SpecialFileError(EnvironmentError): |
| 20 | """Raised when trying to do a kind of operation (e.g. copying) which is |
| 21 | not supported on a special file (e.g. a named pipe)""" |
| 22 | |
Antoine Pitrou | 9fcd4b3 | 2008-08-11 17:21:36 +0000 | [diff] [blame] | 23 | try: |
| 24 | WindowsError |
| 25 | except NameError: |
| 26 | WindowsError = None |
| 27 | |
Greg Stein | 42bb8b3 | 2000-07-12 09:55:30 +0000 | [diff] [blame] | 28 | def copyfileobj(fsrc, fdst, length=16*1024): |
| 29 | """copy data from file-like object fsrc to file-like object fdst""" |
| 30 | while 1: |
| 31 | buf = fsrc.read(length) |
| 32 | if not buf: |
| 33 | break |
| 34 | fdst.write(buf) |
| 35 | |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 36 | def _samefile(src, dst): |
| 37 | # Macintosh, Unix. |
| 38 | if hasattr(os.path,'samefile'): |
Johannes Gijsbers | f9a098e | 2004-08-14 14:51:01 +0000 | [diff] [blame] | 39 | try: |
| 40 | return os.path.samefile(src, dst) |
| 41 | except OSError: |
| 42 | return False |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 43 | |
| 44 | # All other platforms: check for same pathname. |
| 45 | return (os.path.normcase(os.path.abspath(src)) == |
| 46 | os.path.normcase(os.path.abspath(dst))) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 48 | def copyfile(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 49 | """Copy data from src to dst""" |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 50 | if _samefile(src, dst): |
| 51 | raise Error, "`%s` and `%s` are the same file" % (src, dst) |
| 52 | |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 53 | fsrc = None |
| 54 | fdst = None |
Antoine Pitrou | 1fc0231 | 2009-05-01 20:55:35 +0000 | [diff] [blame] | 55 | for fn in [src, dst]: |
| 56 | try: |
| 57 | st = os.stat(fn) |
| 58 | except OSError: |
| 59 | # File most likely does not exist |
| 60 | pass |
| 61 | # XXX What about other special files? (sockets, devices...) |
| 62 | if stat.S_ISFIFO(st.st_mode): |
| 63 | raise SpecialFileError("`%s` is a named pipe" % fn) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 64 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 65 | fsrc = open(src, 'rb') |
| 66 | fdst = open(dst, 'wb') |
Greg Stein | 42bb8b3 | 2000-07-12 09:55:30 +0000 | [diff] [blame] | 67 | copyfileobj(fsrc, fdst) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 68 | finally: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 69 | if fdst: |
| 70 | fdst.close() |
| 71 | if fsrc: |
| 72 | fsrc.close() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 74 | def copymode(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 75 | """Copy mode bits from src to dst""" |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 76 | if hasattr(os, 'chmod'): |
| 77 | st = os.stat(src) |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 78 | mode = stat.S_IMODE(st.st_mode) |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 79 | os.chmod(dst, mode) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 81 | def copystat(src, dst): |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 82 | """Copy all stat info (mode bits, atime, mtime, flags) from src to dst""" |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 83 | st = os.stat(src) |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 84 | mode = stat.S_IMODE(st.st_mode) |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 85 | if hasattr(os, 'utime'): |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 86 | os.utime(dst, (st.st_atime, st.st_mtime)) |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 87 | if hasattr(os, 'chmod'): |
| 88 | os.chmod(dst, mode) |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 89 | if hasattr(os, 'chflags') and hasattr(st, 'st_flags'): |
| 90 | os.chflags(dst, st.st_flags) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 93 | def copy(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 94 | """Copy data and mode bits ("cp src dst"). |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 96 | The destination may be a directory. |
| 97 | |
| 98 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 99 | if os.path.isdir(dst): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 100 | dst = os.path.join(dst, os.path.basename(src)) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 101 | copyfile(src, dst) |
| 102 | copymode(src, dst) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 104 | def copy2(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 105 | """Copy data and all stat info ("cp -p src dst"). |
| 106 | |
| 107 | The destination may be a directory. |
| 108 | |
| 109 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 110 | if os.path.isdir(dst): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | dst = os.path.join(dst, os.path.basename(src)) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 112 | copyfile(src, dst) |
| 113 | copystat(src, dst) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 114 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 115 | def ignore_patterns(*patterns): |
| 116 | """Function that can be used as copytree() ignore parameter. |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 117 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 118 | Patterns is a sequence of glob-style patterns |
| 119 | that are used to exclude files""" |
| 120 | def _ignore_patterns(path, names): |
| 121 | ignored_names = [] |
| 122 | for pattern in patterns: |
| 123 | ignored_names.extend(fnmatch.filter(names, pattern)) |
| 124 | return set(ignored_names) |
| 125 | return _ignore_patterns |
| 126 | |
| 127 | def copytree(src, dst, symlinks=False, ignore=None): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 128 | """Recursively copy a directory tree using copy2(). |
| 129 | |
| 130 | The destination directory must not already exist. |
Neal Norwitz | a4c93b6 | 2003-02-23 21:36:32 +0000 | [diff] [blame] | 131 | If exception(s) occur, an Error is raised with a list of reasons. |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 132 | |
| 133 | If the optional symlinks flag is true, symbolic links in the |
| 134 | source tree result in symbolic links in the destination tree; if |
| 135 | it is false, the contents of the files pointed to by symbolic |
| 136 | links are copied. |
| 137 | |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 138 | The optional ignore argument is a callable. If given, it |
| 139 | is called with the `src` parameter, which is the directory |
| 140 | being visited by copytree(), and `names` which is the list of |
| 141 | `src` contents, as returned by os.listdir(): |
| 142 | |
| 143 | callable(src, names) -> ignored_names |
| 144 | |
| 145 | Since copytree() is called recursively, the callable will be |
| 146 | called once for each directory that is copied. It returns a |
| 147 | list of names relative to the `src` directory that should |
| 148 | not be copied. |
| 149 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 150 | XXX Consider this example code rather than the ultimate tool. |
| 151 | |
| 152 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 153 | names = os.listdir(src) |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 154 | if ignore is not None: |
| 155 | ignored_names = ignore(src, names) |
| 156 | else: |
| 157 | ignored_names = set() |
| 158 | |
Johannes Gijsbers | e4172ea | 2005-01-08 12:31:29 +0000 | [diff] [blame] | 159 | os.makedirs(dst) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 160 | errors = [] |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 161 | for name in names: |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 162 | if name in ignored_names: |
| 163 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 164 | srcname = os.path.join(src, name) |
| 165 | dstname = os.path.join(dst, name) |
| 166 | try: |
| 167 | if symlinks and os.path.islink(srcname): |
| 168 | linkto = os.readlink(srcname) |
| 169 | os.symlink(linkto, dstname) |
| 170 | elif os.path.isdir(srcname): |
Georg Brandl | e78fbcc | 2008-07-05 10:13:36 +0000 | [diff] [blame] | 171 | copytree(srcname, dstname, symlinks, ignore) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 172 | else: |
Antoine Pitrou | 1fc0231 | 2009-05-01 20:55:35 +0000 | [diff] [blame] | 173 | # Will raise a SpecialFileError for unsupported file types |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 174 | copy2(srcname, dstname) |
Georg Brandl | a1be88e | 2005-08-31 22:48:45 +0000 | [diff] [blame] | 175 | # catch the Error from the recursive copytree so that we can |
| 176 | # continue with other files |
| 177 | except Error, err: |
| 178 | errors.extend(err.args[0]) |
Antoine Pitrou | 1fc0231 | 2009-05-01 20:55:35 +0000 | [diff] [blame] | 179 | except EnvironmentError, why: |
| 180 | errors.append((srcname, dstname, str(why))) |
Martin v. Löwis | 4e67838 | 2006-07-30 13:00:31 +0000 | [diff] [blame] | 181 | try: |
| 182 | copystat(src, dst) |
Martin v. Löwis | 4e67838 | 2006-07-30 13:00:31 +0000 | [diff] [blame] | 183 | except OSError, why: |
Antoine Pitrou | 9fcd4b3 | 2008-08-11 17:21:36 +0000 | [diff] [blame] | 184 | if WindowsError is not None and isinstance(why, WindowsError): |
| 185 | # Copying file access times may fail on Windows |
| 186 | pass |
| 187 | else: |
| 188 | errors.extend((src, dst, str(why))) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 189 | if errors: |
| 190 | raise Error, errors |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 191 | |
Barry Warsaw | 234d9a9 | 2003-01-24 17:36:15 +0000 | [diff] [blame] | 192 | def rmtree(path, ignore_errors=False, onerror=None): |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 193 | """Recursively delete a directory tree. |
| 194 | |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 195 | If ignore_errors is set, errors are ignored; otherwise, if onerror |
| 196 | is set, it is called to handle the error with arguments (func, |
| 197 | path, exc_info) where func is os.listdir, os.remove, or os.rmdir; |
| 198 | path is the argument to that function that caused it to fail; and |
| 199 | exc_info is a tuple returned by sys.exc_info(). If ignore_errors |
| 200 | is false and onerror is None, an exception is raised. |
| 201 | |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 202 | """ |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 203 | if ignore_errors: |
| 204 | def onerror(*args): |
Barry Warsaw | 234d9a9 | 2003-01-24 17:36:15 +0000 | [diff] [blame] | 205 | pass |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 206 | elif onerror is None: |
| 207 | def onerror(*args): |
| 208 | raise |
Georg Brandl | 5235398 | 2008-01-20 14:17:42 +0000 | [diff] [blame] | 209 | try: |
| 210 | if os.path.islink(path): |
| 211 | # symlinks to directories are forbidden, see bug #1669 |
| 212 | raise OSError("Cannot call rmtree on a symbolic link") |
| 213 | except OSError: |
| 214 | onerror(os.path.islink, path, sys.exc_info()) |
| 215 | # can't continue even if onerror hook returns |
| 216 | return |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 217 | names = [] |
| 218 | try: |
| 219 | names = os.listdir(path) |
| 220 | except os.error, err: |
| 221 | onerror(os.listdir, path, sys.exc_info()) |
| 222 | for name in names: |
| 223 | fullname = os.path.join(path, name) |
| 224 | try: |
| 225 | mode = os.lstat(fullname).st_mode |
| 226 | except os.error: |
| 227 | mode = 0 |
| 228 | if stat.S_ISDIR(mode): |
| 229 | rmtree(fullname, ignore_errors, onerror) |
Barry Warsaw | 234d9a9 | 2003-01-24 17:36:15 +0000 | [diff] [blame] | 230 | else: |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 231 | try: |
| 232 | os.remove(fullname) |
| 233 | except os.error, err: |
| 234 | onerror(os.remove, fullname, sys.exc_info()) |
| 235 | try: |
| 236 | os.rmdir(path) |
| 237 | except os.error: |
| 238 | onerror(os.rmdir, path, sys.exc_info()) |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 239 | |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 240 | |
Sean Reifscheider | 493894c | 2008-03-18 17:24:12 +0000 | [diff] [blame] | 241 | def _basename(path): |
| 242 | # A basename() variant which first strips the trailing slash, if present. |
| 243 | # Thus we always get the last component of the path, even for directories. |
| 244 | return os.path.basename(path.rstrip(os.path.sep)) |
| 245 | |
| 246 | def move(src, dst): |
| 247 | """Recursively move a file or directory to another location. This is |
| 248 | similar to the Unix "mv" command. |
| 249 | |
| 250 | If the destination is a directory or a symlink to a directory, the source |
| 251 | is moved inside the directory. The destination path must not already |
| 252 | exist. |
| 253 | |
| 254 | If the destination already exists but is not a directory, it may be |
| 255 | overwritten depending on os.rename() semantics. |
| 256 | |
| 257 | If the destination is on our current filesystem, then rename() is used. |
| 258 | Otherwise, src is copied to the destination and then removed. |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 259 | A lot more could be done here... A look at a mv.c shows a lot of |
| 260 | the issues this implementation glosses over. |
| 261 | |
| 262 | """ |
Sean Reifscheider | 493894c | 2008-03-18 17:24:12 +0000 | [diff] [blame] | 263 | real_dst = dst |
| 264 | if os.path.isdir(dst): |
| 265 | real_dst = os.path.join(dst, _basename(src)) |
| 266 | if os.path.exists(real_dst): |
| 267 | raise Error, "Destination path '%s' already exists" % real_dst |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 268 | try: |
Sean Reifscheider | 493894c | 2008-03-18 17:24:12 +0000 | [diff] [blame] | 269 | os.rename(src, real_dst) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 270 | except OSError: |
| 271 | if os.path.isdir(src): |
Benjamin Peterson | 096c3ad | 2009-02-07 19:08:22 +0000 | [diff] [blame] | 272 | if _destinsrc(src, dst): |
Brett Cannon | 1c3fa18 | 2004-06-19 21:11:35 +0000 | [diff] [blame] | 273 | raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst) |
Sean Reifscheider | 493894c | 2008-03-18 17:24:12 +0000 | [diff] [blame] | 274 | copytree(src, real_dst, symlinks=True) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 275 | rmtree(src) |
| 276 | else: |
Sean Reifscheider | 493894c | 2008-03-18 17:24:12 +0000 | [diff] [blame] | 277 | copy2(src, real_dst) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 278 | os.unlink(src) |
Brett Cannon | 1c3fa18 | 2004-06-19 21:11:35 +0000 | [diff] [blame] | 279 | |
Benjamin Peterson | 096c3ad | 2009-02-07 19:08:22 +0000 | [diff] [blame] | 280 | def _destinsrc(src, dst): |
Antoine Pitrou | 707c593 | 2009-01-29 20:19:34 +0000 | [diff] [blame] | 281 | src = abspath(src) |
| 282 | dst = abspath(dst) |
| 283 | if not src.endswith(os.path.sep): |
| 284 | src += os.path.sep |
| 285 | if not dst.endswith(os.path.sep): |
| 286 | dst += os.path.sep |
| 287 | return dst.startswith(src) |