Bump version to 0.9.1.
[python/dscho.git] / Lib / shutil.py
blobdfde2361369ff0d7fbdf8f4909c3d13cb4ce6341
1 """Utility functions for copying files and directory trees.
3 XXX The functions here don't copy the resource fork or other metadata on Mac.
5 """
7 import os
8 import sys
9 import stat
12 def copyfileobj(fsrc, fdst, length=16*1024):
13 """copy data from file-like object fsrc to file-like object fdst"""
14 while 1:
15 buf = fsrc.read(length)
16 if not buf:
17 break
18 fdst.write(buf)
21 def copyfile(src, dst):
22 """Copy data from src to dst"""
23 fsrc = None
24 fdst = None
25 try:
26 fsrc = open(src, 'rb')
27 fdst = open(dst, 'wb')
28 copyfileobj(fsrc, fdst)
29 finally:
30 if fdst:
31 fdst.close()
32 if fsrc:
33 fsrc.close()
35 def copymode(src, dst):
36 """Copy mode bits from src to dst"""
37 st = os.stat(src)
38 mode = stat.S_IMODE(st[stat.ST_MODE])
39 os.chmod(dst, mode)
41 def copystat(src, dst):
42 """Copy all stat info (mode bits, atime and mtime) from src to dst"""
43 st = os.stat(src)
44 mode = stat.S_IMODE(st[stat.ST_MODE])
45 os.utime(dst, (st[stat.ST_ATIME], st[stat.ST_MTIME]))
46 os.chmod(dst, mode)
49 def copy(src, dst):
50 """Copy data and mode bits ("cp src dst").
52 The destination may be a directory.
54 """
55 if os.path.isdir(dst):
56 dst = os.path.join(dst, os.path.basename(src))
57 copyfile(src, dst)
58 copymode(src, dst)
60 def copy2(src, dst):
61 """Copy data and all stat info ("cp -p src dst").
63 The destination may be a directory.
65 """
66 if os.path.isdir(dst):
67 dst = os.path.join(dst, os.path.basename(src))
68 copyfile(src, dst)
69 copystat(src, dst)
72 def copytree(src, dst, symlinks=0):
73 """Recursively copy a directory tree using copy2().
75 The destination directory must not already exist.
76 Error are reported to standard output.
78 If the optional symlinks flag is true, symbolic links in the
79 source tree result in symbolic links in the destination tree; if
80 it is false, the contents of the files pointed to by symbolic
81 links are copied.
83 XXX Consider this example code rather than the ultimate tool.
85 """
86 names = os.listdir(src)
87 os.mkdir(dst)
88 for name in names:
89 srcname = os.path.join(src, name)
90 dstname = os.path.join(dst, name)
91 try:
92 if symlinks and os.path.islink(srcname):
93 linkto = os.readlink(srcname)
94 os.symlink(linkto, dstname)
95 elif os.path.isdir(srcname):
96 copytree(srcname, dstname, symlinks)
97 else:
98 copy2(srcname, dstname)
99 # XXX What about devices, sockets etc.?
100 except (IOError, os.error), why:
101 print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
103 def rmtree(path, ignore_errors=0, onerror=None):
104 """Recursively delete a directory tree.
106 If ignore_errors is set, errors are ignored; otherwise, if
107 onerror is set, it is called to handle the error; otherwise, an
108 exception is raised.
111 cmdtuples = []
112 _build_cmdtuple(path, cmdtuples)
113 for cmd in cmdtuples:
114 try:
115 apply(cmd[0], (cmd[1],))
116 except:
117 exc = sys.exc_info()
118 if ignore_errors:
119 pass
120 elif onerror:
121 onerror(cmd[0], cmd[1], exc)
122 else:
123 raise exc[0], (exc[1][0], exc[1][1] + ' removing '+cmd[1])
125 # Helper for rmtree()
126 def _build_cmdtuple(path, cmdtuples):
127 for f in os.listdir(path):
128 real_f = os.path.join(path,f)
129 if os.path.isdir(real_f) and not os.path.islink(real_f):
130 _build_cmdtuple(real_f, cmdtuples)
131 else:
132 cmdtuples.append((os.remove, real_f))
133 cmdtuples.append((os.rmdir, path))