Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/dir_util.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 | 8a187f562959 |
| children | cdf6a3a50407 a5229237eb50 |
| 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 |
|
33965
893d46576737
Update compatibility comments to 2.1, corresponding to PEP 291 1.13.
Martin v. Löwis <martin@v.loewis.de>
parents:
32736
diff
changeset
|
5 # This module should be kept compatible with Python 2.1. |
|
26285
42bfc98d295b
Add comment to Distutil files about requiring 1.5.2 compatibility, as
Andrew M. Kuchling <amk@amk.ca>
parents:
26210
diff
changeset
|
6 |
|
12226
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 |
|
26423
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
9 import os, sys |
|
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 |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
12 from distutils import log |
|
12226
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 |
|
35465
3e5c07faab11
Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents:
35423
diff
changeset
|
34 if not isinstance(name, StringTypes): |
|
12779
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, \ |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
26423
diff
changeset
|
36 "mkpath: 'name' must be a string (got %r)" % (name,) |
|
12779
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 |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
72 log.info("creating %s", head) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
73 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
74 if not dry_run: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
75 try: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
76 os.mkdir(head) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
77 created_dirs.append(head) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
78 except OSError, exc: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
79 raise DistutilsFileError, \ |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
80 "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
|
81 |
|
14947
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
82 _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
|
83 return created_dirs |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
84 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
85 # mkpath () |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
86 |
|
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 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
|
89 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
90 """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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 for 'mkpath()'.""" |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
97 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
98 # 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
|
99 need_dir = {} |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
100 for file in files: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
101 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
|
102 need_dirs = need_dir.keys() |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
103 need_dirs.sort() |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
104 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
105 # Now create them |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
106 for dir in need_dirs: |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
107 mkpath(dir, mode, dry_run=dry_run) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
108 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
109 # create_tree () |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
110 |
|
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 def copy_tree (src, dst, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
113 preserve_mode=1, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
114 preserve_times=1, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
115 preserve_symlinks=0, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
116 update=0, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
117 verbose=0, |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
118 dry_run=0): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
119 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
120 """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
|
121 '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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 under 'dst'. |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
130 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
131 '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
|
132 '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
|
133 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
|
134 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
|
135 (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
|
136 '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
|
137 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
138 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
|
139 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
140 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
|
141 raise DistutilsFileError, \ |
| 21267 | 142 "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
|
143 try: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
144 names = os.listdir(src) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
145 except os.error, (errno, errstr): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
146 if dry_run: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
147 names = [] |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
148 else: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
149 raise DistutilsFileError, \ |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
150 "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
|
151 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
152 if not dry_run: |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
153 mkpath(dst) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
154 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
155 outputs = [] |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
156 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
157 for n in names: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
158 src_name = os.path.join(src, n) |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
159 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
|
160 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
161 if preserve_symlinks and os.path.islink(src_name): |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
162 link_dest = os.readlink(src_name) |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
163 log.info("linking %s -> %s", dst_name, link_dest) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
164 if not dry_run: |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
165 os.symlink(link_dest, dst_name) |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
166 outputs.append(dst_name) |
| 21267 | 167 |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
168 elif os.path.isdir(src_name): |
|
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
169 outputs.extend( |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
170 copy_tree(src_name, dst_name, preserve_mode, |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
171 preserve_times, preserve_symlinks, update, |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
172 dry_run=dry_run)) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
173 else: |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
174 copy_file(src_name, dst_name, preserve_mode, |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
175 preserve_times, update, dry_run=dry_run) |
|
14863
c3f0d6f19f7b
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
12938
diff
changeset
|
176 outputs.append(dst_name) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
177 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
178 return outputs |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
179 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
180 # copy_tree () |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
181 |
|
12934
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
182 # Helper for remove_tree() |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
183 def _build_cmdtuple(path, cmdtuples): |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
184 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
|
185 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
|
186 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
|
187 _build_cmdtuple(real_f, cmdtuples) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
188 else: |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
189 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
|
190 cmdtuples.append((os.rmdir, path)) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
191 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
192 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
193 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
|
194 """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
|
195 (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
|
196 """ |
|
abbceb6a2532
Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents:
12934
diff
changeset
|
197 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
|
198 global _path_created |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
199 |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
200 log.info("removing '%s' (and everything under it)", directory) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
201 if dry_run: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
202 return |
|
12934
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
203 cmdtuples = [] |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
204 _build_cmdtuple(directory, cmdtuples) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
205 for cmd in cmdtuples: |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
206 try: |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
207 apply(cmd[0], (cmd[1],)) |
|
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
208 # 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
|
209 abspath = os.path.abspath(cmd[1]) |
|
45413
8a187f562959
Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents:
35465
diff
changeset
|
210 if abspath in _path_created: |
|
14947
73cea59657ba
Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents:
14863
diff
changeset
|
211 del _path_created[abspath] |
|
12934
753f9163df0b
Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents:
12779
diff
changeset
|
212 except (IOError, OSError), exc: |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
213 log.warn(grok_environment_error( |
|
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21267
diff
changeset
|
214 exc, "error removing %s: " % directory)) |
|
26423
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
215 |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
216 |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
217 def ensure_relative (path): |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
218 """Take the full path 'path', and make it a relative path so |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
219 it can be the second argument to os.path.join(). |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
220 """ |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
221 drive, path = os.path.splitdrive(path) |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
222 if sys.platform == 'mac': |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
223 return os.sep + path |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
224 else: |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
225 if path[0:1] == os.sep: |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
226 path = drive + path[1:] |
|
3d2869de2336
Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents:
26285
diff
changeset
|
227 return path |
