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: |
| 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) |
| 23 | finally: |
| 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 | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 40 | os.utime(dst, (st[stat.ST_ATIME], st[stat.ST_MODE])) |
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): |
| 50 | dst = os.path.join(dst, os.path.basename(src)) |
| 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): |
| 61 | dst = os.path.join(dst, os.path.basename(src)) |
| 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: |
| 83 | srcname = os.path.join(src, name) |
| 84 | dstname = os.path.join(dst, name) |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 85 | try: |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 86 | if symlinks and os.path.islink(srcname): |
| 87 | linkto = os.readlink(srcname) |
| 88 | os.symlink(linkto, dstname) |
| 89 | elif os.path.isdir(srcname): |
Guido van Rossum | a2baf46 | 1997-04-29 14:06:46 +0000 | [diff] [blame] | 90 | copytree(srcname, dstname) |
| 91 | else: |
| 92 | copy2(srcname, dstname) |
| 93 | # XXX What about devices, sockets etc.? |
Guido van Rossum | 9d0a3df | 1997-04-29 14:45:19 +0000 | [diff] [blame] | 94 | except (IOError, os.error), why: |
| 95 | print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why)) |