Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/dir_util.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 | 7a7f557b4d54 |
| children | ca87119255eb |
| rev | line source |
|---|---|
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
1 """distutils.dir_util |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
2 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
3 Utility functions for manipulating directories and directory trees.""" |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
4 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
5 # created 2000/04/03, Greg Ward (extracted from util.py) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
6 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
7 __revision__ = "$Id$" |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
8 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
9 import os |
|
12779
0570de9c8202
'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
10 from types import * |
|
0570de9c8202
'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
11 from distutils.errors import DistutilsFileError, DistutilsInternalError |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
12 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
13 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
14 # cache for by mkpath() -- in addition to cheapening redundant calls, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
15 # eliminates redundant "creating /foo/bar/baz" messages in dry-run mode |
|
12938
7ab5b1eeebde
Renamed PATH_CREATED to _path_created, on the grounds that it's private and
Greg Ward <gward@python.net>
parents:
12937
diff
changeset
|
16 _path_created = {} |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
17 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
18 # I don't use os.makedirs because a) it's new to Python 1.5.2, and |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
19 # b) it blows up if the directory already exists (I want to silently |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
20 # succeed in that case). |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
21 def mkpath (name, mode=0777, verbose=0, dry_run=0): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
22 """Create a directory and any missing ancestor directories. If the |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
23 directory already exists (or if 'name' is the empty string, which |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
24 means the current directory, which of course exists), then do |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
25 nothing. Raise DistutilsFileError if unable to create some |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
26 directory along the way (eg. some sub-path exists, but is a file |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
27 rather than a directory). If 'verbose' is true, print a one-line |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
28 summary of each mkdir to stdout. Return the list of directories |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
29 actually created.""" |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
30 |
|
12938
7ab5b1eeebde
Renamed PATH_CREATED to _path_created, on the grounds that it's private and
Greg Ward <gward@python.net>
parents:
12937
diff
changeset
|
31 global _path_created |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
32 |
|
12779
0570de9c8202
'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
33 # Detect a common bug -- name is None |
|
0570de9c8202
'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
34 if type(name) is not StringType: |
|
0570de9c8202
'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
35 raise DistutilsInternalError, \ |
|
0570de9c8202
'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
36 "mkpath: 'name' must be a string (got %s)" % `name` |
|
0570de9c8202
'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
37 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
38 # XXX what's the better way to handle verbosity? print as we create |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
39 # each directory in the path (the current behaviour), or only announce |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
40 # the creation of the whole path? (quite easy to do the latter since |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
41 # we're not using a recursive algorithm) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
42 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
43 name = os.path.normpath(name) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
44 created_dirs = [] |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
45 if os.path.isdir(name) or name == '': |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
46 return created_dirs |
|
14947
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
47 if _path_created.get(os.path.abspath(name)): |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
48 return created_dirs |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
49 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
50 (head, tail) = os.path.split(name) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
51 tails = [tail] # stack of lone dirs to create |
| 21267 | 52 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
53 while head and tail and not os.path.isdir(head): |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
54 #print "splitting '%s': " % head, |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
55 (head, tail) = os.path.split(head) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
56 #print "to ('%s','%s')" % (head, tail) |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
57 tails.insert(0, tail) # push next higher dir onto stack |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
58 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
59 #print "stack of tails:", tails |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
60 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
61 # now 'head' contains the deepest directory that already exists |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
62 # (that is, the child of 'head' in 'name' is the highest directory |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
63 # that does *not* exist) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
64 for d in tails: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
65 #print "head = %s, d = %s: " % (head, d), |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
66 head = os.path.join(head, d) |
|
14947
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
67 abs_head = os.path.abspath(head) |
|
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
68 |
|
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
69 if _path_created.get(abs_head): |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
70 continue |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
71 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
72 if verbose: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
73 print "creating", head |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
74 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
75 if not dry_run: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
76 try: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
77 os.mkdir(head) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
78 created_dirs.append(head) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
79 except OSError, exc: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
80 raise DistutilsFileError, \ |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
81 "could not create '%s': %s" % (head, exc[-1]) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
82 |
|
14947
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
83 _path_created[abs_head] = 1 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
84 return created_dirs |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
85 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
86 # mkpath () |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
87 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
88 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
89 def create_tree (base_dir, files, mode=0777, verbose=0, dry_run=0): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
90 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
91 """Create all the empty directories under 'base_dir' needed to |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
92 put 'files' there. 'base_dir' is just the a name of a directory |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
93 which doesn't necessarily exist yet; 'files' is a list of filenames |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
94 to be interpreted relative to 'base_dir'. 'base_dir' + the |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
95 directory portion of every file in 'files' will be created if it |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
96 doesn't already exist. 'mode', 'verbose' and 'dry_run' flags are as |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
97 for 'mkpath()'.""" |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
98 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
99 # First get the list of directories to create |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
100 need_dir = {} |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
101 for file in files: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
102 need_dir[os.path.join(base_dir, os.path.dirname(file))] = 1 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
103 need_dirs = need_dir.keys() |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
104 need_dirs.sort() |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
105 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
106 # Now create them |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
107 for dir in need_dirs: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
108 mkpath(dir, mode, verbose, dry_run) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
109 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
110 # create_tree () |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
111 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
112 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
113 def copy_tree (src, dst, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
114 preserve_mode=1, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
115 preserve_times=1, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
116 preserve_symlinks=0, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
117 update=0, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
118 verbose=0, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
119 dry_run=0): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
120 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
121 """Copy an entire directory tree 'src' to a new location 'dst'. Both |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
122 'src' and 'dst' must be directory names. If 'src' is not a |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
123 directory, raise DistutilsFileError. If 'dst' does not exist, it is |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
124 created with 'mkpath()'. The end result of the copy is that every |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
125 file in 'src' is copied to 'dst', and directories under 'src' are |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
126 recursively copied to 'dst'. Return the list of files that were |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
127 copied or might have been copied, using their output name. The |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
128 return value is unaffected by 'update' or 'dry_run': it is simply |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
129 the list of all files under 'src', with the names changed to be |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
130 under 'dst'. |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
131 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
132 'preserve_mode' and 'preserve_times' are the same as for |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
133 'copy_file'; note that they only apply to regular files, not to |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
134 directories. If 'preserve_symlinks' is true, symlinks will be |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
135 copied as symlinks (on platforms that support them!); otherwise |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
136 (the default), the destination of the symlink will be copied. |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
137 'update' and 'verbose' are the same as for 'copy_file'.""" |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
138 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
139 from distutils.file_util import copy_file |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
140 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
141 if not dry_run and not os.path.isdir(src): |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
142 raise DistutilsFileError, \ |
| 21267 | 143 "cannot copy tree '%s': not a directory" % src |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
144 try: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
145 names = os.listdir(src) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
146 except os.error, (errno, errstr): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
147 if dry_run: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
148 names = [] |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
149 else: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
150 raise DistutilsFileError, \ |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
151 "error listing files in '%s': %s" % (src, errstr) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
152 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
153 if not dry_run: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
154 mkpath(dst, verbose=verbose) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
155 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
156 outputs = [] |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
157 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
158 for n in names: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
159 src_name = os.path.join(src, n) |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
160 dst_name = os.path.join(dst, n) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
161 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
162 if preserve_symlinks and os.path.islink(src_name): |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
163 link_dest = os.readlink(src_name) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
164 if verbose: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
165 print "linking %s -> %s" % (dst_name, link_dest) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
166 if not dry_run: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
167 os.symlink(link_dest, dst_name) |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
168 outputs.append(dst_name) |
| 21267 | 169 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
170 elif os.path.isdir(src_name): |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
171 outputs.extend( |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
172 copy_tree(src_name, dst_name, |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
173 preserve_mode, preserve_times, preserve_symlinks, |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
174 update, verbose, dry_run)) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
175 else: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
176 copy_file(src_name, dst_name, |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
177 preserve_mode, preserve_times, |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
178 update, None, verbose, dry_run) |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
179 outputs.append(dst_name) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
180 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
181 return outputs |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
182 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
183 # copy_tree () |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
184 |
|
12934
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
185 # Helper for remove_tree() |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
186 def _build_cmdtuple(path, cmdtuples): |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
187 for f in os.listdir(path): |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
188 real_f = os.path.join(path,f) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
189 if os.path.isdir(real_f) and not os.path.islink(real_f): |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
190 _build_cmdtuple(real_f, cmdtuples) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
191 else: |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
192 cmdtuples.append((os.remove, real_f)) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
193 cmdtuples.append((os.rmdir, path)) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
194 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
195 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
196 def remove_tree (directory, verbose=0, dry_run=0): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
197 """Recursively remove an entire directory tree. Any errors are ignored |
|
12937
abbceb6a2532
Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents:
12934
diff
changeset
|
198 (apart from being reported to stdout if 'verbose' is true). |
|
abbceb6a2532
Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents:
12934
diff
changeset
|
199 """ |
|
abbceb6a2532
Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents:
12934
diff
changeset
|
200 from distutils.util import grok_environment_error |
|
12938
7ab5b1eeebde
Renamed PATH_CREATED to _path_created, on the grounds that it's private and
Greg Ward <gward@python.net>
parents:
12937
diff
changeset
|
201 global _path_created |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
202 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
203 if verbose: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
204 print "removing '%s' (and everything under it)" % directory |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
205 if dry_run: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
206 return |
|
12934
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
207 cmdtuples = [] |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
208 _build_cmdtuple(directory, cmdtuples) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
209 for cmd in cmdtuples: |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
210 try: |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
211 apply(cmd[0], (cmd[1],)) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
212 # remove dir from cache if it's already there |
|
14947
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
213 abspath = os.path.abspath(cmd[1]) |
|
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
214 if _path_created.has_key(abspath): |
|
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
215 del _path_created[abspath] |
|
12934
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
216 except (IOError, OSError), exc: |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
217 if verbose: |
|
12937
abbceb6a2532
Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents:
12934
diff
changeset
|
218 print grok_environment_error( |
|
abbceb6a2532
Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents:
12934
diff
changeset
|
219 exc, "error removing %s: " % directory) |
