Mercurial > cpython-lru_cache-weakref
annotate Lib/shutil.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | f68dbffff927 |
| children | a6a765c7d33b |
| rev | line source |
|---|---|
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11202
diff
changeset
|
1 """Utility functions for copying files and directory trees. |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
2 |
|
11202
7eeeb0d2b0a4
Typo in comment (on Mac, it's the *resource* fork that's not copied,
Guido van Rossum <guido@python.org>
parents:
10289
diff
changeset
|
3 XXX The functions here don't copy the resource fork or other metadata on Mac. |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
4 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
5 """ |
| 5 | 6 |
| 771 | 7 import os |
|
10289
1e766de714de
Add import sys, needed by reference to sys.exc_info() in rmtree().
Guido van Rossum <guido@python.org>
parents:
9872
diff
changeset
|
8 import sys |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
9 import stat |
| 5 | 10 |
| 16613 | 11 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", |
| 12 "copytree","rmtree"] | |
| 5 | 13 |
|
13571
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
14 def copyfileobj(fsrc, fdst, length=16*1024): |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
15 """copy data from file-like object fsrc to file-like object fdst""" |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
16 while 1: |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
17 buf = fsrc.read(length) |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
18 if not buf: |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
19 break |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
20 fdst.write(buf) |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
21 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13571
diff
changeset
|
22 |
| 5 | 23 def copyfile(src, dst): |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
24 """Copy data from src to dst""" |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
25 fsrc = None |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
26 fdst = None |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
27 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
28 fsrc = open(src, 'rb') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
29 fdst = open(dst, 'wb') |
|
13571
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
30 copyfileobj(fsrc, fdst) |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
31 finally: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
32 if fdst: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
33 fdst.close() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
34 if fsrc: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
35 fsrc.close() |
| 5 | 36 |
| 37 def copymode(src, dst): | |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
38 """Copy mode bits from src to dst""" |
|
16084
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
39 if hasattr(os, 'chmod'): |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
40 st = os.stat(src) |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
41 mode = stat.S_IMODE(st[stat.ST_MODE]) |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
42 os.chmod(dst, mode) |
| 5 | 43 |
| 44 def copystat(src, dst): | |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
45 """Copy all stat info (mode bits, atime and mtime) from src to dst""" |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
46 st = os.stat(src) |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
47 mode = stat.S_IMODE(st[stat.ST_MODE]) |
|
16084
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
48 if hasattr(os, 'utime'): |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
49 os.utime(dst, (st[stat.ST_ATIME], st[stat.ST_MTIME])) |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
50 if hasattr(os, 'chmod'): |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
51 os.chmod(dst, mode) |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
52 |
| 5 | 53 |
| 54 def copy(src, dst): | |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
55 """Copy data and mode bits ("cp src dst"). |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13571
diff
changeset
|
56 |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
57 The destination may be a directory. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
58 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
59 """ |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
60 if os.path.isdir(dst): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
61 dst = os.path.join(dst, os.path.basename(src)) |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
62 copyfile(src, dst) |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
63 copymode(src, dst) |
| 5 | 64 |
| 65 def copy2(src, dst): | |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
66 """Copy data and all stat info ("cp -p src dst"). |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
67 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
68 The destination may be a directory. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
69 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
70 """ |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
71 if os.path.isdir(dst): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
72 dst = os.path.join(dst, os.path.basename(src)) |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
73 copyfile(src, dst) |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
74 copystat(src, dst) |
| 5 | 75 |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
76 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
77 def copytree(src, dst, symlinks=0): |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
78 """Recursively copy a directory tree using copy2(). |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
79 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
80 The destination directory must not already exist. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
81 Error are reported to standard output. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
82 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
83 If the optional symlinks flag is true, symbolic links in the |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
84 source tree result in symbolic links in the destination tree; if |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
85 it is false, the contents of the files pointed to by symbolic |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
86 links are copied. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
87 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
88 XXX Consider this example code rather than the ultimate tool. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
89 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
90 """ |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
91 names = os.listdir(src) |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
92 os.mkdir(dst) |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
93 for name in names: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
94 srcname = os.path.join(src, name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
95 dstname = os.path.join(dst, name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
96 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
97 if symlinks and os.path.islink(srcname): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
98 linkto = os.readlink(srcname) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
99 os.symlink(linkto, dstname) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
100 elif os.path.isdir(srcname): |
|
12285
8186145c4a07
Fred Gansevles <gansevle@cs.utwente.nl>:
Fred Drake <fdrake@acm.org>
parents:
11707
diff
changeset
|
101 copytree(srcname, dstname, symlinks) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
102 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
103 copy2(srcname, dstname) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
104 # XXX What about devices, sockets etc.? |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
105 except (IOError, os.error), why: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
106 print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why)) |
|
7203
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
107 |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
108 def rmtree(path, ignore_errors=0, onerror=None): |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
109 """Recursively delete a directory tree. |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
110 |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
111 If ignore_errors is set, errors are ignored; otherwise, if |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
112 onerror is set, it is called to handle the error; otherwise, an |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
113 exception is raised. |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
114 |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
115 """ |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
116 cmdtuples = [] |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
117 _build_cmdtuple(path, cmdtuples) |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
118 for cmd in cmdtuples: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
119 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
120 apply(cmd[0], (cmd[1],)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
121 except: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
122 exc = sys.exc_info() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
123 if ignore_errors: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
124 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
125 elif onerror: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
126 onerror(cmd[0], cmd[1], exc) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
127 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
128 raise exc[0], (exc[1][0], exc[1][1] + ' removing '+cmd[1]) |
|
7203
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
129 |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
130 # Helper for rmtree() |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
131 def _build_cmdtuple(path, cmdtuples): |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
132 for f in os.listdir(path): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
133 real_f = os.path.join(path,f) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
134 if os.path.isdir(real_f) and not os.path.islink(real_f): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
135 _build_cmdtuple(real_f, cmdtuples) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
136 else: |
|
9274
8791455485e5
Fix append() calls with more than 1 argument.
Guido van Rossum <guido@python.org>
parents:
9212
diff
changeset
|
137 cmdtuples.append((os.remove, real_f)) |
|
8791455485e5
Fix append() calls with more than 1 argument.
Guido van Rossum <guido@python.org>
parents:
9212
diff
changeset
|
138 cmdtuples.append((os.rmdir, path)) |
