annotate Lib/distutils/dir_util.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 8ffeaf649970
children 7d7f9783c86d
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
26423
3d2869de2336 Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
7 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
8 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
9 from distutils import log
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
10
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
11 # 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
12 # 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
13 _path_created = {}
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
14
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
15 # 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
16 # 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
17 # succeed in that case).
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
18 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
19 """Create a directory and any missing ancestor directories.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
20
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
21 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
22 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
23 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
24 (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
25 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
26 Return the list of directories actually created.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
27 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
28
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
29 global _path_created
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
30
12779
0570de9c8202 'mkpath()' now detects non-string 'name' arguments -- this is a fairly common
Greg Ward <gward@python.net>
parents: 12226
diff changeset
31 # Detect a common bug -- name is None
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
32 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
33 raise DistutilsInternalError, \
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 26423
diff changeset
34 "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
35
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
36 # 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
37 # 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
38 # 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
39 # 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
40
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
41 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
42 created_dirs = []
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
43 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
44 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
45 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
46 return created_dirs
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
47
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
48 (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
49 tails = [tail] # stack of lone dirs to create
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14947
diff changeset
50
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
51 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
52 (head, tail) = os.path.split(head)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
53 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
54
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
55 # 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
56 # (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
57 # that does *not* exist)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
58 for d in tails:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
59 #print "head = %s, d = %s: " % (head, d),
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
60 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
61 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
62
73cea59657ba Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents: 14863
diff changeset
63 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
64 continue
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
65
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
66 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
67 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
68
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
69 if not dry_run:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
70 try:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
71 os.mkdir(head)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
72 created_dirs.append(head)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
73 except OSError, exc:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
74 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
75 "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
76
14947
73cea59657ba Andrew Kuchling: changed so the '_path_created' dictionary is keyed on
Greg Ward <gward@python.net>
parents: 14863
diff changeset
77 _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
78 return created_dirs
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
79
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
80 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
81 """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
82 there.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
83
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
84 '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
85 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
86 '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
87 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
88 'dry_run' flags are as for 'mkpath()'.
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
89 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
90 # 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
91 need_dir = {}
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
92 for file in files:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
93 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
94 need_dirs = need_dir.keys()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
95 need_dirs.sort()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
96
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
97 # Now create them
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
98 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
99 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
100
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
101 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
102 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
103 """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
104
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 under 'dst'.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
114
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
115 '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
116 '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
117 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
118 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
119 (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
120 '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
121 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
122 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
123
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
124 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
125 raise DistutilsFileError, \
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14947
diff changeset
126 "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
127 try:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
128 names = os.listdir(src)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
129 except os.error, (errno, errstr):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
130 if dry_run:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
131 names = []
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
132 else:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
133 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
134 "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
135
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
136 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
137 mkpath(dst, verbose=verbose)
12226
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 outputs = []
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 for n in names:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
142 src_name = os.path.join(src, n)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
143 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
144
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
145 if preserve_symlinks and os.path.islink(src_name):
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
146 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
147 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
148 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
149 if not dry_run:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
150 os.symlink(link_dest, dst_name)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
151 outputs.append(dst_name)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14947
diff changeset
152
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
153 elif os.path.isdir(src_name):
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
154 outputs.extend(
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
155 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
156 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
157 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
158 else:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
159 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
160 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
161 dry_run=dry_run)
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 12938
diff changeset
162 outputs.append(dst_name)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
163
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
164 return outputs
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
165
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
166 def _build_cmdtuple(path, cmdtuples):
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
167 """Helper for remove_tree()."""
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
168 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
169 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
170 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
171 _build_cmdtuple(real_f, cmdtuples)
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
172 else:
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
173 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
174 cmdtuples.append((os.rmdir, path))
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
175
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
176 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
177 """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
178
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
179 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
180 is true).
12937
abbceb6a2532 Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents: 12934
diff changeset
181 """
abbceb6a2532 Changed 'remove_tree()' to use the new 'grok_environment_error()' function
Greg Ward <gward@python.net>
parents: 12934
diff changeset
182 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
183 global _path_created
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
184
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
185 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
186 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
187 if dry_run:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
188 return
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
189 cmdtuples = []
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
190 _build_cmdtuple(directory, cmdtuples)
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
191 for cmd in cmdtuples:
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
192 try:
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
193 apply(cmd[0], (cmd[1],))
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
194 # 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
195 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
196 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
197 del _path_created[abspath]
12934
753f9163df0b Bastian Kleineidam: added 'remove_tree()' function. Needed so that
Greg Ward <gward@python.net>
parents: 12779
diff changeset
198 except (IOError, OSError), exc:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
199 log.warn(grok_environment_error(
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
200 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
201
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
202 def ensure_relative(path):
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
203 """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
204
54158
8ffeaf649970 pep8-fied distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54155
diff changeset
205 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
206 """
3d2869de2336 Part of the fix for bug #410541: add ensure_relative() function
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
207 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
208 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
209 path = drive + path[1:]
df3826472e3b removed sys.platform == 'mac' usage in distutils.dir_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51366
diff changeset
210 return path