annotate Lib/distutils/dir_util.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents a56cebff113a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 __revision__ = "$Id$"
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
6
57847
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56339
diff changeset
7 import os
66320
4ae2d011a80b Merged revisions 86601,86605 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 66016
diff changeset
8 import errno
12779
0570de9c8202 'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents: 12226
diff changeset
9 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
10 from distutils import log
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
11
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
12 # 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
13 # 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
14 _path_created = {}
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
15
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
16 # 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
17 # 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
18 # succeed in that case).
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
19 def mkpath(name, mode=0777, verbose=1, dry_run=0):
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
20 """Create a directory and any missing ancestor directories.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
21
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
22 If the directory already exists (or if 'name' is the empty string, which
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
23 means the current directory, which of course exists), then do nothing.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
24 Raise DistutilsFileError if unable to create some directory along the way
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
25 (eg. some sub-path exists, but is a file rather than a directory).
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
26 If 'verbose' is true, print a one-line summary of each mkdir to stdout.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
27 Return the list of directories actually created.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
28 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
29
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
30 global _path_created
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
31
12779
0570de9c8202 'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents: 12226
diff changeset
32 # Detect a common bug -- name is None
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
33 if not isinstance(name, basestring):
12779
0570de9c8202 'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents: 12226
diff changeset
34 raise DistutilsInternalError, \
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 26423
diff changeset
35 "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
36
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
37 # 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
38 # 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
39 # 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
40 # 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
41
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
42 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
43 created_dirs = []
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
44 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
45 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
46 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
47 return created_dirs
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
48
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
49 (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
50 tails = [tail] # stack of lone dirs to create
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14947
diff changeset
51
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
52 while head and tail and not os.path.isdir(head):
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
53 (head, tail) = os.path.split(head)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
54 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
55
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
56 # 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
57 # (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
58 # that does *not* exist)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
59 for d in tails:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
60 #print "head = %s, d = %s: " % (head, d),
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
61 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
62 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
63
73cea59657ba Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents: 14863
diff changeset
64 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
65 continue
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
66
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
67 if verbose >= 1:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
68 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
69
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
70 if not dry_run:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
71 try:
64792
e0641dbdc8f6 Merged revisions 84861 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57847
diff changeset
72 os.mkdir(head, mode)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
73 except OSError, exc:
66016
df34991c7d0e Merged revisions 86244 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 64792
diff changeset
74 if not (exc.errno == errno.EEXIST and os.path.isdir(head)):
df34991c7d0e Merged revisions 86244 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 64792
diff changeset
75 raise DistutilsFileError(
df34991c7d0e Merged revisions 86244 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 64792
diff changeset
76 "could not create '%s': %s" % (head, exc.args[-1]))
df34991c7d0e Merged revisions 86244 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 64792
diff changeset
77 created_dirs.append(head)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
78
14947
73cea59657ba Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents: 14863
diff changeset
79 _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
80 return created_dirs
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
81
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
82 def create_tree(base_dir, files, mode=0777, verbose=1, dry_run=0):
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
83 """Create all the empty directories under 'base_dir' needed to put 'files'
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
84 there.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
85
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
86 'base_dir' is just the a name of a directory which doesn't necessarily
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
87 exist yet; 'files' is a list of filenames to be interpreted relative to
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
88 'base_dir'. 'base_dir' + the directory portion of every file in 'files'
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
89 will be created if it doesn't already exist. 'mode', 'verbose' and
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
90 'dry_run' flags are as for 'mkpath()'.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
91 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
92 # 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
93 need_dir = {}
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
94 for file in files:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
95 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
96 need_dirs = need_dir.keys()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
97 need_dirs.sort()
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 # Now create them
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
100 for dir in need_dirs:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
101 mkpath(dir, mode, verbose=verbose, dry_run=dry_run)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
102
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
103 def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
104 preserve_symlinks=0, update=0, verbose=1, dry_run=0):
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
105 """Copy an entire directory tree 'src' to a new location 'dst'.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
106
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
107 Both 'src' and 'dst' must be directory names. If 'src' is not a
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
108 directory, raise DistutilsFileError. If 'dst' does not exist, it is
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
109 created with 'mkpath()'. The end result of the copy is that every
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
110 file in 'src' is copied to 'dst', and directories under 'src' are
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
111 recursively copied to 'dst'. Return the list of files that were
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
112 copied or might have been copied, using their output name. The
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
113 return value is unaffected by 'update' or 'dry_run': it is simply
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
114 the list of all files under 'src', with the names changed to be
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
115 under 'dst'.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
116
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
117 'preserve_mode' and 'preserve_times' are the same as for
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
118 'copy_file'; note that they only apply to regular files, not to
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
119 directories. If 'preserve_symlinks' is true, symlinks will be
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
120 copied as symlinks (on platforms that support them!); otherwise
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
121 (the default), the destination of the symlink will be copied.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
122 'update' and 'verbose' are the same as for 'copy_file'.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
123 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
124 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
125
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
126 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
127 raise DistutilsFileError, \
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14947
diff changeset
128 "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
129 try:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
130 names = os.listdir(src)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
131 except os.error, (errno, errstr):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
132 if dry_run:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
133 names = []
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
134 else:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
135 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
136 "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
137
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
138 if not dry_run:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
139 mkpath(dst, verbose=verbose)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
140
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
141 outputs = []
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
142
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
143 for n in names:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
144 src_name = os.path.join(src, n)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
145 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
146
77925
a56cebff113a Ignore .nfs* files in distutils (#7719).
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
147 if n.startswith('.nfs'):
a56cebff113a Ignore .nfs* files in distutils (#7719).
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
148 # skip NFS rename files
a56cebff113a Ignore .nfs* files in distutils (#7719).
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
149 continue
a56cebff113a Ignore .nfs* files in distutils (#7719).
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
150
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
151 if preserve_symlinks and os.path.islink(src_name):
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
152 link_dest = os.readlink(src_name)
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
153 if verbose >= 1:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
154 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
155 if not dry_run:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
156 os.symlink(link_dest, dst_name)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
157 outputs.append(dst_name)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14947
diff changeset
158
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
159 elif os.path.isdir(src_name):
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
160 outputs.extend(
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
161 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
162 preserve_times, preserve_symlinks, update,
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
163 verbose=verbose, dry_run=dry_run))
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
164 else:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
165 copy_file(src_name, dst_name, preserve_mode,
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
166 preserve_times, update, verbose=verbose,
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
167 dry_run=dry_run)
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
168 outputs.append(dst_name)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
169
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
170 return outputs
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
171
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
172 def _build_cmdtuple(path, cmdtuples):
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
173 """Helper for remove_tree()."""
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
174 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
175 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
176 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
177 _build_cmdtuple(real_f, cmdtuples)
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
178 else:
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
179 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
180 cmdtuples.append((os.rmdir, path))
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
181
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
182 def remove_tree(directory, verbose=1, dry_run=0):
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
183 """Recursively remove an entire directory tree.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
184
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
185 Any errors are ignored (apart from being reported to stdout if 'verbose'
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
186 is true).
12937
abbceb6a2532 Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents: 12934
diff changeset
187 """
abbceb6a2532 Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents: 12934
diff changeset
188 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
189 global _path_created
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
190
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
191 if verbose >= 1:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
192 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
193 if dry_run:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
194 return
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
195 cmdtuples = []
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
196 _build_cmdtuple(directory, cmdtuples)
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
197 for cmd in cmdtuples:
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
198 try:
63663
cdf6a3a50407 Merged revisions 79191 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 45413
diff changeset
199 cmd[0](cmd[1])
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
200 # 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
201 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
202 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
203 del _path_created[abspath]
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
204 except (IOError, OSError), exc:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
205 log.warn(grok_environment_error(
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
206 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
207
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
208 def ensure_relative(path):
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
209 """Take the full path 'path', and make it a relative path.
26423
3d2869de2336 Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
210
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
211 This is useful to make 'path' the second argument to os.path.join().
26423
3d2869de2336 Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
212 """
3d2869de2336 Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
213 drive, path = os.path.splitdrive(path)
54155
df3826472e3b removed sys.platform == 'mac' usage in distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51366
diff changeset
214 if path[0:1] == os.sep:
df3826472e3b removed sys.platform == 'mac' usage in distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51366
diff changeset
215 path = drive + path[1:]
df3826472e3b removed sys.platform == 'mac' usage in distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51366
diff changeset
216 return path