Remove Barry's love of deprecated syntax to silence warnings in the email
[python.git] / Lib / shutil.py
blob3af280dd42ee528b328737f47d46c3ce49155ccc
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
10 from os.path import abspath
11 import fnmatch
13 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
14 "copytree","move","rmtree","Error"]
16 class Error(EnvironmentError):
17 pass
19 def copyfileobj(fsrc, fdst, length=16*1024):
20 """copy data from file-like object fsrc to file-like object fdst"""
21 while 1:
22 buf = fsrc.read(length)
23 if not buf:
24 break
25 fdst.write(buf)
27 def _samefile(src, dst):
28 # Macintosh, Unix.
29 if hasattr(os.path,'samefile'):
30 try:
31 return os.path.samefile(src, dst)
32 except OSError:
33 return False
35 # All other platforms: check for same pathname.
36 return (os.path.normcase(os.path.abspath(src)) ==
37 os.path.normcase(os.path.abspath(dst)))
39 def copyfile(src, dst):
40 """Copy data from src to dst"""
41 if _samefile(src, dst):
42 raise Error, "`%s` and `%s` are the same file" % (src, dst)
44 fsrc = None
45 fdst = None
46 try:
47 fsrc = open(src, 'rb')
48 fdst = open(dst, 'wb')
49 copyfileobj(fsrc, fdst)
50 finally:
51 if fdst:
52 fdst.close()
53 if fsrc:
54 fsrc.close()
56 def copymode(src, dst):
57 """Copy mode bits from src to dst"""
58 if hasattr(os, 'chmod'):
59 st = os.stat(src)
60 mode = stat.S_IMODE(st.st_mode)
61 os.chmod(dst, mode)
63 def copystat(src, dst):
64 """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
65 st = os.stat(src)
66 mode = stat.S_IMODE(st.st_mode)
67 if hasattr(os, 'utime'):
68 os.utime(dst, (st.st_atime, st.st_mtime))
69 if hasattr(os, 'chmod'):
70 os.chmod(dst, mode)
71 if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
72 os.chflags(dst, st.st_flags)
75 def copy(src, dst):
76 """Copy data and mode bits ("cp src dst").
78 The destination may be a directory.
80 """
81 if os.path.isdir(dst):
82 dst = os.path.join(dst, os.path.basename(src))
83 copyfile(src, dst)
84 copymode(src, dst)
86 def copy2(src, dst):
87 """Copy data and all stat info ("cp -p src dst").
89 The destination may be a directory.
91 """
92 if os.path.isdir(dst):
93 dst = os.path.join(dst, os.path.basename(src))
94 copyfile(src, dst)
95 copystat(src, dst)
97 def ignore_patterns(*patterns):
98 """Function that can be used as copytree() ignore parameter.
100 Patterns is a sequence of glob-style patterns
101 that are used to exclude files"""
102 def _ignore_patterns(path, names):
103 ignored_names = []
104 for pattern in patterns:
105 ignored_names.extend(fnmatch.filter(names, pattern))
106 return set(ignored_names)
107 return _ignore_patterns
109 def copytree(src, dst, symlinks=False, ignore=None):
110 """Recursively copy a directory tree using copy2().
112 The destination directory must not already exist.
113 If exception(s) occur, an Error is raised with a list of reasons.
115 If the optional symlinks flag is true, symbolic links in the
116 source tree result in symbolic links in the destination tree; if
117 it is false, the contents of the files pointed to by symbolic
118 links are copied.
120 The optional ignore argument is a callable. If given, it
121 is called with the `src` parameter, which is the directory
122 being visited by copytree(), and `names` which is the list of
123 `src` contents, as returned by os.listdir():
125 callable(src, names) -> ignored_names
127 Since copytree() is called recursively, the callable will be
128 called once for each directory that is copied. It returns a
129 list of names relative to the `src` directory that should
130 not be copied.
132 XXX Consider this example code rather than the ultimate tool.
135 names = os.listdir(src)
136 if ignore is not None:
137 ignored_names = ignore(src, names)
138 else:
139 ignored_names = set()
141 os.makedirs(dst)
142 errors = []
143 for name in names:
144 if name in ignored_names:
145 continue
146 srcname = os.path.join(src, name)
147 dstname = os.path.join(dst, name)
148 try:
149 if symlinks and os.path.islink(srcname):
150 linkto = os.readlink(srcname)
151 os.symlink(linkto, dstname)
152 elif os.path.isdir(srcname):
153 copytree(srcname, dstname, symlinks, ignore)
154 else:
155 copy2(srcname, dstname)
156 # XXX What about devices, sockets etc.?
157 except (IOError, os.error), why:
158 errors.append((srcname, dstname, str(why)))
159 # catch the Error from the recursive copytree so that we can
160 # continue with other files
161 except Error, err:
162 errors.extend(err.args[0])
163 try:
164 copystat(src, dst)
165 except WindowsError:
166 # can't copy file access times on Windows
167 pass
168 except OSError, why:
169 errors.extend((src, dst, str(why)))
170 if errors:
171 raise Error, errors
173 def rmtree(path, ignore_errors=False, onerror=None):
174 """Recursively delete a directory tree.
176 If ignore_errors is set, errors are ignored; otherwise, if onerror
177 is set, it is called to handle the error with arguments (func,
178 path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
179 path is the argument to that function that caused it to fail; and
180 exc_info is a tuple returned by sys.exc_info(). If ignore_errors
181 is false and onerror is None, an exception is raised.
184 if ignore_errors:
185 def onerror(*args):
186 pass
187 elif onerror is None:
188 def onerror(*args):
189 raise
190 try:
191 if os.path.islink(path):
192 # symlinks to directories are forbidden, see bug #1669
193 raise OSError("Cannot call rmtree on a symbolic link")
194 except OSError:
195 onerror(os.path.islink, path, sys.exc_info())
196 # can't continue even if onerror hook returns
197 return
198 names = []
199 try:
200 names = os.listdir(path)
201 except os.error, err:
202 onerror(os.listdir, path, sys.exc_info())
203 for name in names:
204 fullname = os.path.join(path, name)
205 try:
206 mode = os.lstat(fullname).st_mode
207 except os.error:
208 mode = 0
209 if stat.S_ISDIR(mode):
210 rmtree(fullname, ignore_errors, onerror)
211 else:
212 try:
213 os.remove(fullname)
214 except os.error, err:
215 onerror(os.remove, fullname, sys.exc_info())
216 try:
217 os.rmdir(path)
218 except os.error:
219 onerror(os.rmdir, path, sys.exc_info())
222 def _basename(path):
223 # A basename() variant which first strips the trailing slash, if present.
224 # Thus we always get the last component of the path, even for directories.
225 return os.path.basename(path.rstrip(os.path.sep))
227 def move(src, dst):
228 """Recursively move a file or directory to another location. This is
229 similar to the Unix "mv" command.
231 If the destination is a directory or a symlink to a directory, the source
232 is moved inside the directory. The destination path must not already
233 exist.
235 If the destination already exists but is not a directory, it may be
236 overwritten depending on os.rename() semantics.
238 If the destination is on our current filesystem, then rename() is used.
239 Otherwise, src is copied to the destination and then removed.
240 A lot more could be done here... A look at a mv.c shows a lot of
241 the issues this implementation glosses over.
244 real_dst = dst
245 if os.path.isdir(dst):
246 real_dst = os.path.join(dst, _basename(src))
247 if os.path.exists(real_dst):
248 raise Error, "Destination path '%s' already exists" % real_dst
249 try:
250 os.rename(src, real_dst)
251 except OSError:
252 if os.path.isdir(src):
253 if destinsrc(src, dst):
254 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
255 copytree(src, real_dst, symlinks=True)
256 rmtree(src)
257 else:
258 copy2(src, real_dst)
259 os.unlink(src)
261 def destinsrc(src, dst):
262 return abspath(dst).startswith(abspath(src))