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 |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 10 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 11 | __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", |
| 12 | "copytree","rmtree"] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 13 | |
Greg Stein | 42bb8b3 | 2000-07-12 09:55:30 +0000 | [diff] [blame] | 14 | def copyfileobj(fsrc, fdst, length=16*1024): |
| 15 | """copy data from file-like object fsrc to file-like object fdst""" |
| 16 | while 1: |
| 17 | buf = fsrc.read(length) |
| 18 | if not buf: |
| 19 | break |
| 20 | fdst.write(buf) |
| 21 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 23 | def copyfile(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 24 | """Copy data from src to dst""" |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 25 | fsrc = None |
| 26 | fdst = None |
| 27 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 28 | fsrc = open(src, 'rb') |
| 29 | fdst = open(dst, 'wb') |
Greg Stein | 42bb8b3 | 2000-07-12 09:55:30 +0000 | [diff] [blame] | 30 | copyfileobj(fsrc, fdst) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 31 | finally: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 32 | if fdst: |
| 33 | fdst.close() |
| 34 | if fsrc: |
| 35 | fsrc.close() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 37 | def copymode(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 38 | """Copy mode bits from src to dst""" |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 39 | if hasattr(os, 'chmod'): |
| 40 | st = os.stat(src) |
| 41 | mode = stat.S_IMODE(st[stat.ST_MODE]) |
| 42 | os.chmod(dst, mode) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 44 | def copystat(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 45 | """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] | 46 | st = os.stat(src) |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 47 | mode = stat.S_IMODE(st[stat.ST_MODE]) |
Tim Peters | 0c94724 | 2001-01-21 20:00:00 +0000 | [diff] [blame] | 48 | if hasattr(os, 'utime'): |
| 49 | os.utime(dst, (st[stat.ST_ATIME], st[stat.ST_MTIME])) |
| 50 | if hasattr(os, 'chmod'): |
| 51 | os.chmod(dst, mode) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 54 | def copy(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 55 | """Copy data and mode bits ("cp src dst"). |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 57 | The destination may be a directory. |
| 58 | |
| 59 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 60 | if os.path.isdir(dst): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 61 | dst = os.path.join(dst, os.path.basename(src)) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 62 | copyfile(src, dst) |
| 63 | copymode(src, dst) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 65 | def copy2(src, dst): |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 66 | """Copy data and all stat info ("cp -p src dst"). |
| 67 | |
| 68 | The destination may be a directory. |
| 69 | |
| 70 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 71 | if os.path.isdir(dst): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 72 | dst = os.path.join(dst, os.path.basename(src)) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 73 | copyfile(src, dst) |
| 74 | copystat(src, dst) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 76 | |
| 77 | def copytree(src, dst, symlinks=0): |
| 78 | """Recursively copy a directory tree using copy2(). |
| 79 | |
| 80 | The destination directory must not already exist. |
| 81 | Error are reported to standard output. |
| 82 | |
| 83 | If the optional symlinks flag is true, symbolic links in the |
| 84 | source tree result in symbolic links in the destination tree; if |
| 85 | it is false, the contents of the files pointed to by symbolic |
| 86 | links are copied. |
| 87 | |
| 88 | XXX Consider this example code rather than the ultimate tool. |
| 89 | |
| 90 | """ |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 91 | names = os.listdir(src) |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 92 | os.mkdir(dst) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 93 | for name in names: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | srcname = os.path.join(src, name) |
| 95 | dstname = os.path.join(dst, name) |
| 96 | try: |
| 97 | if symlinks and os.path.islink(srcname): |
| 98 | linkto = os.readlink(srcname) |
| 99 | os.symlink(linkto, dstname) |
| 100 | elif os.path.isdir(srcname): |
Fred Drake | 5fa3886 | 2000-04-07 14:34:50 +0000 | [diff] [blame] | 101 | copytree(srcname, dstname, symlinks) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 102 | else: |
| 103 | copy2(srcname, dstname) |
| 104 | # XXX What about devices, sockets etc.? |
| 105 | except (IOError, os.error), why: |
| 106 | print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why)) |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 107 | |
| 108 | def rmtree(path, ignore_errors=0, onerror=None): |
| 109 | """Recursively delete a directory tree. |
| 110 | |
| 111 | If ignore_errors is set, errors are ignored; otherwise, if |
| 112 | onerror is set, it is called to handle the error; otherwise, an |
| 113 | exception is raised. |
| 114 | |
| 115 | """ |
| 116 | cmdtuples = [] |
| 117 | _build_cmdtuple(path, cmdtuples) |
| 118 | for cmd in cmdtuples: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 119 | try: |
| 120 | apply(cmd[0], (cmd[1],)) |
| 121 | except: |
| 122 | exc = sys.exc_info() |
| 123 | if ignore_errors: |
| 124 | pass |
| 125 | elif onerror: |
| 126 | onerror(cmd[0], cmd[1], exc) |
| 127 | else: |
| 128 | raise exc[0], (exc[1][0], exc[1][1] + ' removing '+cmd[1]) |
Guido van Rossum | d767329 | 1998-02-06 21:38:09 +0000 | [diff] [blame] | 129 | |
| 130 | # Helper for rmtree() |
| 131 | def _build_cmdtuple(path, cmdtuples): |
| 132 | for f in os.listdir(path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | real_f = os.path.join(path,f) |
| 134 | if os.path.isdir(real_f) and not os.path.islink(real_f): |
| 135 | _build_cmdtuple(real_f, cmdtuples) |
| 136 | else: |
Guido van Rossum | d832f9e | 1998-10-07 13:18:17 +0000 | [diff] [blame] | 137 | cmdtuples.append((os.remove, real_f)) |
| 138 | cmdtuples.append((os.rmdir, path)) |