Mercurial > cpython-lru_cache-weakref
annotate Lib/shutil.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | e2dcad56e40e |
| children | e3fdfdb1a8ff 0ab36796dd79 |
| 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 |
|
32386
686e5d38be42
shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents:
28025
diff
changeset
|
10 from os.path import abspath |
|
47888
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
11 import fnmatch |
| 5 | 12 |
| 16613 | 13 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", |
|
25952
949ed8365d58
Added new move() function to __all__.
Raymond Hettinger <python@rcn.com>
parents:
25641
diff
changeset
|
14 "copytree","move","rmtree","Error"] |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
15 |
|
35505
26ba3d4f4fc4
No need to import exceptions, they are builtins
Neal Norwitz <nnorwitz@gmail.com>
parents:
35501
diff
changeset
|
16 class Error(EnvironmentError): |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
17 pass |
| 5 | 18 |
|
48214
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
19 try: |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
20 WindowsError |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
21 except NameError: |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
22 WindowsError = None |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
23 |
|
13571
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
24 def copyfileobj(fsrc, fdst, length=16*1024): |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
25 """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
|
26 while 1: |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
27 buf = fsrc.read(length) |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
28 if not buf: |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
29 break |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
30 fdst.write(buf) |
|
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
31 |
|
33124
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
32 def _samefile(src, dst): |
|
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
33 # Macintosh, Unix. |
|
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
34 if hasattr(os.path,'samefile'): |
|
33128
c2384601a241
Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents:
33124
diff
changeset
|
35 try: |
|
c2384601a241
Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents:
33124
diff
changeset
|
36 return os.path.samefile(src, dst) |
|
c2384601a241
Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents:
33124
diff
changeset
|
37 except OSError: |
|
c2384601a241
Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents:
33124
diff
changeset
|
38 return False |
|
33124
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
39 |
|
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
40 # All other platforms: check for same pathname. |
|
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
41 return (os.path.normcase(os.path.abspath(src)) == |
|
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
42 os.path.normcase(os.path.abspath(dst))) |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13571
diff
changeset
|
43 |
| 5 | 44 def copyfile(src, dst): |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
45 """Copy data from src to dst""" |
|
33124
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
46 if _samefile(src, dst): |
|
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
47 raise Error, "`%s` and `%s` are the same file" % (src, dst) |
|
dc647df53921
Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents:
32652
diff
changeset
|
48 |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
49 fsrc = None |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
50 fdst = None |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
51 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
52 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
|
53 fdst = open(dst, 'wb') |
|
13571
3a8c670309a2
apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents:
12285
diff
changeset
|
54 copyfileobj(fsrc, fdst) |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
55 finally: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
56 if fdst: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
57 fdst.close() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
58 if fsrc: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
59 fsrc.close() |
| 5 | 60 |
| 61 def copymode(src, dst): | |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
62 """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
|
63 if hasattr(os, 'chmod'): |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
64 st = os.stat(src) |
|
23697
c0e8c07ae1d6
Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents:
23627
diff
changeset
|
65 mode = stat.S_IMODE(st.st_mode) |
|
16084
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
66 os.chmod(dst, mode) |
| 5 | 67 |
| 68 def copystat(src, dst): | |
|
40872
53fd144a3f1a
Patch #1490190: posixmodule now includes os.chflags() and os.lchflags()
Martin v. Löwis <martin@v.loewis.de>
parents:
39307
diff
changeset
|
69 """Copy all stat info (mode bits, atime, mtime, flags) from src to dst""" |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
70 st = os.stat(src) |
|
23697
c0e8c07ae1d6
Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents:
23627
diff
changeset
|
71 mode = stat.S_IMODE(st.st_mode) |
|
16084
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
72 if hasattr(os, 'utime'): |
|
23697
c0e8c07ae1d6
Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents:
23627
diff
changeset
|
73 os.utime(dst, (st.st_atime, st.st_mtime)) |
|
16084
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
74 if hasattr(os, 'chmod'): |
|
4c307a12dcb9
Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
75 os.chmod(dst, mode) |
|
40872
53fd144a3f1a
Patch #1490190: posixmodule now includes os.chflags() and os.lchflags()
Martin v. Löwis <martin@v.loewis.de>
parents:
39307
diff
changeset
|
76 if hasattr(os, 'chflags') and hasattr(st, 'st_flags'): |
|
53fd144a3f1a
Patch #1490190: posixmodule now includes os.chflags() and os.lchflags()
Martin v. Löwis <martin@v.loewis.de>
parents:
39307
diff
changeset
|
77 os.chflags(dst, st.st_flags) |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
78 |
| 5 | 79 |
| 80 def copy(src, dst): | |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
81 """Copy data and mode bits ("cp src dst"). |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13571
diff
changeset
|
82 |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
83 The destination may be a directory. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
84 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
85 """ |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
86 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
|
87 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
|
88 copyfile(src, dst) |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
89 copymode(src, dst) |
| 5 | 90 |
| 91 def copy2(src, dst): | |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
92 """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
|
93 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
94 The destination may be a directory. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
95 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
96 """ |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
97 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
|
98 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
|
99 copyfile(src, dst) |
|
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
100 copystat(src, dst) |
| 5 | 101 |
|
47888
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
102 def ignore_patterns(*patterns): |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
103 """Function that can be used as copytree() ignore parameter. |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
104 |
|
47888
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
105 Patterns is a sequence of glob-style patterns |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
106 that are used to exclude files""" |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
107 def _ignore_patterns(path, names): |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
108 ignored_names = [] |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
109 for pattern in patterns: |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
110 ignored_names.extend(fnmatch.filter(names, pattern)) |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
111 return set(ignored_names) |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
112 return _ignore_patterns |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
113 |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
114 def copytree(src, dst, symlinks=False, ignore=None): |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
115 """Recursively copy a directory tree using copy2(). |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
116 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
117 The destination directory must not already exist. |
|
28025
e63f1eccd250
Fix SF bug #691276, shutil.copytree documentation bug
Neal Norwitz <nnorwitz@gmail.com>
parents:
27361
diff
changeset
|
118 If exception(s) occur, an Error is raised with a list of reasons. |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
119 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 links are copied. |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
124 |
|
47888
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
125 The optional ignore argument is a callable. If given, it |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
126 is called with the `src` parameter, which is the directory |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
127 being visited by copytree(), and `names` which is the list of |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
128 `src` contents, as returned by os.listdir(): |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
129 |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
130 callable(src, names) -> ignored_names |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
131 |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
132 Since copytree() is called recursively, the callable will be |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
133 called once for each directory that is copied. It returns a |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
134 list of names relative to the `src` directory that should |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
135 not be copied. |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
136 |
|
5349
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
137 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
|
138 |
|
6ab392d7a52f
Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents:
5348
diff
changeset
|
139 """ |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
140 names = os.listdir(src) |
|
47888
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
141 if ignore is not None: |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
142 ignored_names = ignore(src, names) |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
143 else: |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
144 ignored_names = set() |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
145 |
| 34364 | 146 os.makedirs(dst) |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
147 errors = [] |
|
5348
ae02ed2aac3f
Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents:
5347
diff
changeset
|
148 for name in names: |
|
47888
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
149 if name in ignored_names: |
|
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
150 continue |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
151 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
|
152 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
|
153 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
154 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
|
155 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
|
156 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
|
157 elif os.path.isdir(srcname): |
|
47888
d7526670ff0a
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents:
45858
diff
changeset
|
158 copytree(srcname, dstname, symlinks, ignore) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
159 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
160 copy2(srcname, dstname) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7203
diff
changeset
|
161 # 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
|
162 except (IOError, os.error), why: |
|
38032
565bffde8bf1
Bug #1472949: stringify IOErrors in shutil.copytree when appending
Georg Brandl <georg@python.org>
parents:
35505
diff
changeset
|
163 errors.append((srcname, dstname, str(why))) |
|
35501
ed65e0ad2215
patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents:
34485
diff
changeset
|
164 # catch the Error from the recursive copytree so that we can |
|
ed65e0ad2215
patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents:
34485
diff
changeset
|
165 # continue with other files |
|
ed65e0ad2215
patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents:
34485
diff
changeset
|
166 except Error, err: |
|
ed65e0ad2215
patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents:
34485
diff
changeset
|
167 errors.extend(err.args[0]) |
|
39307
9377c6604542
Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents:
38032
diff
changeset
|
168 try: |
|
9377c6604542
Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents:
38032
diff
changeset
|
169 copystat(src, dst) |
|
9377c6604542
Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents:
38032
diff
changeset
|
170 except OSError, why: |
|
48214
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
171 if WindowsError is not None and isinstance(why, WindowsError): |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
172 # Copying file access times may fail on Windows |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
173 pass |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
174 else: |
|
0aae72f8dda9
#3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents:
47888
diff
changeset
|
175 errors.extend((src, dst, str(why))) |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
176 if errors: |
|
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
177 raise Error, errors |
|
7203
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
178 |
|
27361
891ec4d266d1
rmtree(): Make implementation agree with documentation (both latex and
Barry Warsaw <barry@python.org>
parents:
27096
diff
changeset
|
179 def rmtree(path, ignore_errors=False, onerror=None): |
|
7203
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
180 """Recursively delete a directory tree. |
|
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
181 |
|
33865
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
182 If ignore_errors is set, errors are ignored; otherwise, if onerror |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
183 is set, it is called to handle the error with arguments (func, |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
184 path, exc_info) where func is os.listdir, os.remove, or os.rmdir; |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
185 path is the argument to that function that caused it to fail; and |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
186 exc_info is a tuple returned by sys.exc_info(). If ignore_errors |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
187 is false and onerror is None, an exception is raised. |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
188 |
|
7203
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
189 """ |
|
33865
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
190 if ignore_errors: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
191 def onerror(*args): |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
192 pass |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
193 elif onerror is None: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
194 def onerror(*args): |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
195 raise |
|
44823
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
196 try: |
|
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
197 if os.path.islink(path): |
|
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
198 # symlinks to directories are forbidden, see bug #1669 |
|
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
199 raise OSError("Cannot call rmtree on a symbolic link") |
|
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
200 except OSError: |
|
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
201 onerror(os.path.islink, path, sys.exc_info()) |
|
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
202 # can't continue even if onerror hook returns |
|
eaca2367ecc9
#1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents:
40872
diff
changeset
|
203 return |
|
33865
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
204 names = [] |
|
27361
891ec4d266d1
rmtree(): Make implementation agree with documentation (both latex and
Barry Warsaw <barry@python.org>
parents:
27096
diff
changeset
|
205 try: |
|
33865
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
206 names = os.listdir(path) |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
207 except os.error, err: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
208 onerror(os.listdir, path, sys.exc_info()) |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
209 for name in names: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
210 fullname = os.path.join(path, name) |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
211 try: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
212 mode = os.lstat(fullname).st_mode |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
213 except os.error: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
214 mode = 0 |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
215 if stat.S_ISDIR(mode): |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
216 rmtree(fullname, ignore_errors, onerror) |
|
27361
891ec4d266d1
rmtree(): Make implementation agree with documentation (both latex and
Barry Warsaw <barry@python.org>
parents:
27096
diff
changeset
|
217 else: |
|
33865
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
218 try: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
219 os.remove(fullname) |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
220 except os.error, err: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
221 onerror(os.remove, fullname, sys.exc_info()) |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
222 try: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
223 os.rmdir(path) |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
224 except os.error: |
|
f196716ea0db
Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents:
33687
diff
changeset
|
225 onerror(os.rmdir, path, sys.exc_info()) |
|
7203
8c129a567867
Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents:
5349
diff
changeset
|
226 |
|
45858
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
227 |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
228 def _basename(path): |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
229 # A basename() variant which first strips the trailing slash, if present. |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
230 # Thus we always get the last component of the path, even for directories. |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
231 return os.path.basename(path.rstrip(os.path.sep)) |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
232 |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
233 def move(src, dst): |
|
45858
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
234 """Recursively move a file or directory to another location. This is |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
235 similar to the Unix "mv" command. |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
236 |
|
45858
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
237 If the destination is a directory or a symlink to a directory, the source |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
238 is moved inside the directory. The destination path must not already |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
239 exist. |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
240 |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
241 If the destination already exists but is not a directory, it may be |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
242 overwritten depending on os.rename() semantics. |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
243 |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
244 If the destination is on our current filesystem, then rename() is used. |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
245 Otherwise, src is copied to the destination and then removed. |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
246 A lot more could be done here... A look at a mv.c shows a lot of |
|
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
247 the issues this implementation glosses over. |
|
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
248 |
|
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
249 """ |
|
45858
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
250 real_dst = dst |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
251 if os.path.isdir(dst): |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
252 real_dst = os.path.join(dst, _basename(src)) |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
253 if os.path.exists(real_dst): |
|
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
254 raise Error, "Destination path '%s' already exists" % real_dst |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
255 try: |
|
45858
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
256 os.rename(src, real_dst) |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
257 except OSError: |
|
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
258 if os.path.isdir(src): |
|
32386
686e5d38be42
shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents:
28025
diff
changeset
|
259 if destinsrc(src, dst): |
|
686e5d38be42
shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents:
28025
diff
changeset
|
260 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst) |
|
45858
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
261 copytree(src, real_dst, symlinks=True) |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
262 rmtree(src) |
|
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
263 else: |
|
45858
923380acd1cd
Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents:
44823
diff
changeset
|
264 copy2(src, real_dst) |
|
25641
13438586daa6
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents:
25170
diff
changeset
|
265 os.unlink(src) |
|
32386
686e5d38be42
shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents:
28025
diff
changeset
|
266 |
|
686e5d38be42
shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents:
28025
diff
changeset
|
267 def destinsrc(src, dst): |
|
51179
e2dcad56e40e
Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48214
diff
changeset
|
268 src = abspath(src) |
|
e2dcad56e40e
Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48214
diff
changeset
|
269 dst = abspath(dst) |
|
e2dcad56e40e
Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48214
diff
changeset
|
270 if not src.endswith(os.path.sep): |
|
e2dcad56e40e
Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48214
diff
changeset
|
271 src += os.path.sep |
|
e2dcad56e40e
Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48214
diff
changeset
|
272 if not dst.endswith(os.path.sep): |
|
e2dcad56e40e
Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48214
diff
changeset
|
273 dst += os.path.sep |
|
e2dcad56e40e
Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48214
diff
changeset
|
274 return dst.startswith(src) |
