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