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