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 |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 10 | import exceptions |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 11 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 12 | __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", |
Raymond Hettinger | 2b9bfb3 | 2002-10-30 05:44:50 +0000 | [diff] [blame] | 13 | "copytree","move","rmtree","Error"] |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 14 | |
| 15 | class Error(exceptions.EnvironmentError): |
| 16 | pass |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 17 | |
Greg Stein | 42bb8b3 | 2000-07-12 09:55:30 +0000 | [diff] [blame] | 18 | def copyfileobj(fsrc, fdst, length=16*1024): |
| 19 | """copy data from file-like object fsrc to file-like object fdst""" |
| 20 | while 1: |
| 21 | buf = fsrc.read(length) |
| 22 | if not buf: |
| 23 | break |
| 24 | fdst.write(buf) |
| 25 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 27 | def copyfile(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 28 | """Copy data from src to dst""" |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 29 | fsrc = None |
| 30 | fdst = None |
Raymond Hettinger | 57e7945 | 2002-09-08 20:43:59 +0000 | [diff] [blame] | 31 | # check for same pathname; all platforms |
| 32 | _src = os.path.normcase(os.path.abspath(src)) |
| 33 | _dst = os.path.normcase(os.path.abspath(dst)) |
| 34 | if _src == _dst: |
| 35 | return |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 36 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 37 | fsrc = open(src, 'rb') |
| 38 | fdst = open(dst, 'wb') |
Greg Stein | 42bb8b3 | 2000-07-12 09:55:30 +0000 | [diff] [blame] | 39 | copyfileobj(fsrc, fdst) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 40 | finally: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 41 | if fdst: |
| 42 | fdst.close() |
| 43 | if fsrc: |
| 44 | fsrc.close() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 46 | def copymode(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 47 | """Copy mode bits from src to dst""" |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 48 | if hasattr(os, 'chmod'): |
| 49 | st = os.stat(src) |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 50 | mode = stat.S_IMODE(st.st_mode) |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 51 | os.chmod(dst, mode) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 53 | def copystat(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 54 | """Copy all stat info (mode bits, atime and mtime) from src to dst""" |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 55 | st = os.stat(src) |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 56 | mode = stat.S_IMODE(st.st_mode) |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 57 | if hasattr(os, 'utime'): |
Walter Dörwald | 294bbf3 | 2002-06-06 09:48:13 +0000 | [diff] [blame] | 58 | os.utime(dst, (st.st_atime, st.st_mtime)) |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 59 | if hasattr(os, 'chmod'): |
| 60 | os.chmod(dst, mode) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 63 | def copy(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 64 | """Copy data and mode bits ("cp src dst"). |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 66 | The destination may be a directory. |
| 67 | |
| 68 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 69 | if os.path.isdir(dst): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 70 | dst = os.path.join(dst, os.path.basename(src)) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 71 | copyfile(src, dst) |
| 72 | copymode(src, dst) |
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 copy2(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 75 | """Copy data and all stat info ("cp -p src dst"). |
| 76 | |
| 77 | The destination may be a directory. |
| 78 | |
| 79 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 80 | if os.path.isdir(dst): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 81 | dst = os.path.join(dst, os.path.basename(src)) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 82 | copyfile(src, dst) |
| 83 | copystat(src, dst) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 85 | |
Neal Norwitz | a4c93b6 | 2003-02-23 21:36:32 +0000 | [diff] [blame] | 86 | def copytree(src, dst, symlinks=False): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 87 | """Recursively copy a directory tree using copy2(). |
| 88 | |
| 89 | The destination directory must not already exist. |
Neal Norwitz | a4c93b6 | 2003-02-23 21:36:32 +0000 | [diff] [blame] | 90 | 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] | 91 | |
| 92 | If the optional symlinks flag is true, symbolic links in the |
| 93 | source tree result in symbolic links in the destination tree; if |
| 94 | it is false, the contents of the files pointed to by symbolic |
| 95 | links are copied. |
| 96 | |
| 97 | XXX Consider this example code rather than the ultimate tool. |
| 98 | |
| 99 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 100 | names = os.listdir(src) |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 101 | os.mkdir(dst) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 102 | errors = [] |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 103 | for name in names: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 104 | srcname = os.path.join(src, name) |
| 105 | dstname = os.path.join(dst, name) |
| 106 | try: |
| 107 | if symlinks and os.path.islink(srcname): |
| 108 | linkto = os.readlink(srcname) |
| 109 | os.symlink(linkto, dstname) |
| 110 | elif os.path.isdir(srcname): |
Fred Drake | 5fa3886 | 2000-04-07 14:34:50 +0000 | [diff] [blame] | 111 | copytree(srcname, dstname, symlinks) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 112 | else: |
| 113 | copy2(srcname, dstname) |
| 114 | # XXX What about devices, sockets etc.? |
| 115 | except (IOError, os.error), why: |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 116 | errors.append((srcname, dstname, why)) |
| 117 | if errors: |
| 118 | raise Error, errors |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 119 | |
Barry Warsaw | 234d9a9 | 2003-01-24 17:36:15 +0000 | [diff] [blame] | 120 | def rmtree(path, ignore_errors=False, onerror=None): |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 121 | """Recursively delete a directory tree. |
| 122 | |
| 123 | If ignore_errors is set, errors are ignored; otherwise, if |
| 124 | onerror is set, it is called to handle the error; otherwise, an |
| 125 | exception is raised. |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 126 | """ |
| 127 | cmdtuples = [] |
Barry Warsaw | 234d9a9 | 2003-01-24 17:36:15 +0000 | [diff] [blame] | 128 | arg = path |
| 129 | try: |
| 130 | _build_cmdtuple(path, cmdtuples) |
| 131 | for func, arg in cmdtuples: |
Just van Rossum | 66d16ba | 2003-01-05 19:44:11 +0000 | [diff] [blame] | 132 | func(arg) |
Barry Warsaw | 234d9a9 | 2003-01-24 17:36:15 +0000 | [diff] [blame] | 133 | except OSError: |
| 134 | exc = sys.exc_info() |
| 135 | if ignore_errors: |
| 136 | pass |
| 137 | elif onerror is not None: |
| 138 | onerror(func, arg, exc) |
| 139 | else: |
| 140 | raise exc[0], (exc[1][0], exc[1][1] + ' removing '+arg) |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 141 | |
| 142 | # Helper for rmtree() |
| 143 | def _build_cmdtuple(path, cmdtuples): |
| 144 | for f in os.listdir(path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 145 | real_f = os.path.join(path,f) |
| 146 | if os.path.isdir(real_f) and not os.path.islink(real_f): |
| 147 | _build_cmdtuple(real_f, cmdtuples) |
| 148 | else: |
Guido van Rossum | d832f9e | 1998-10-07 13:18:17 +0000 | [diff] [blame] | 149 | cmdtuples.append((os.remove, real_f)) |
| 150 | cmdtuples.append((os.rmdir, path)) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | def move(src, dst): |
| 154 | """Recursively move a file or directory to another location. |
| 155 | |
| 156 | If the destination is on our current filesystem, then simply use |
| 157 | rename. Otherwise, copy src to the dst and then remove src. |
| 158 | A lot more could be done here... A look at a mv.c shows a lot of |
| 159 | the issues this implementation glosses over. |
| 160 | |
| 161 | """ |
| 162 | |
| 163 | try: |
| 164 | os.rename(src, dst) |
| 165 | except OSError: |
| 166 | if os.path.isdir(src): |
Neal Norwitz | a4c93b6 | 2003-02-23 21:36:32 +0000 | [diff] [blame] | 167 | copytree(src, dst, symlinks=True) |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 168 | rmtree(src) |
| 169 | else: |
| 170 | copy2(src,dst) |
| 171 | os.unlink(src) |