1 | n/a | """distutils.dir_util |
---|
2 | n/a | |
---|
3 | n/a | Utility functions for manipulating directories and directory trees.""" |
---|
4 | n/a | |
---|
5 | n/a | import os |
---|
6 | n/a | import errno |
---|
7 | n/a | from distutils.errors import DistutilsFileError, DistutilsInternalError |
---|
8 | n/a | from distutils import log |
---|
9 | n/a | |
---|
10 | n/a | # cache for by mkpath() -- in addition to cheapening redundant calls, |
---|
11 | n/a | # eliminates redundant "creating /foo/bar/baz" messages in dry-run mode |
---|
12 | n/a | _path_created = {} |
---|
13 | n/a | |
---|
14 | n/a | # I don't use os.makedirs because a) it's new to Python 1.5.2, and |
---|
15 | n/a | # b) it blows up if the directory already exists (I want to silently |
---|
16 | n/a | # succeed in that case). |
---|
17 | n/a | def mkpath(name, mode=0o777, verbose=1, dry_run=0): |
---|
18 | n/a | """Create a directory and any missing ancestor directories. |
---|
19 | n/a | |
---|
20 | n/a | If the directory already exists (or if 'name' is the empty string, which |
---|
21 | n/a | means the current directory, which of course exists), then do nothing. |
---|
22 | n/a | Raise DistutilsFileError if unable to create some directory along the way |
---|
23 | n/a | (eg. some sub-path exists, but is a file rather than a directory). |
---|
24 | n/a | If 'verbose' is true, print a one-line summary of each mkdir to stdout. |
---|
25 | n/a | Return the list of directories actually created. |
---|
26 | n/a | """ |
---|
27 | n/a | |
---|
28 | n/a | global _path_created |
---|
29 | n/a | |
---|
30 | n/a | # Detect a common bug -- name is None |
---|
31 | n/a | if not isinstance(name, str): |
---|
32 | n/a | raise DistutilsInternalError( |
---|
33 | n/a | "mkpath: 'name' must be a string (got %r)" % (name,)) |
---|
34 | n/a | |
---|
35 | n/a | # XXX what's the better way to handle verbosity? print as we create |
---|
36 | n/a | # each directory in the path (the current behaviour), or only announce |
---|
37 | n/a | # the creation of the whole path? (quite easy to do the latter since |
---|
38 | n/a | # we're not using a recursive algorithm) |
---|
39 | n/a | |
---|
40 | n/a | name = os.path.normpath(name) |
---|
41 | n/a | created_dirs = [] |
---|
42 | n/a | if os.path.isdir(name) or name == '': |
---|
43 | n/a | return created_dirs |
---|
44 | n/a | if _path_created.get(os.path.abspath(name)): |
---|
45 | n/a | return created_dirs |
---|
46 | n/a | |
---|
47 | n/a | (head, tail) = os.path.split(name) |
---|
48 | n/a | tails = [tail] # stack of lone dirs to create |
---|
49 | n/a | |
---|
50 | n/a | while head and tail and not os.path.isdir(head): |
---|
51 | n/a | (head, tail) = os.path.split(head) |
---|
52 | n/a | tails.insert(0, tail) # push next higher dir onto stack |
---|
53 | n/a | |
---|
54 | n/a | # now 'head' contains the deepest directory that already exists |
---|
55 | n/a | # (that is, the child of 'head' in 'name' is the highest directory |
---|
56 | n/a | # that does *not* exist) |
---|
57 | n/a | for d in tails: |
---|
58 | n/a | #print "head = %s, d = %s: " % (head, d), |
---|
59 | n/a | head = os.path.join(head, d) |
---|
60 | n/a | abs_head = os.path.abspath(head) |
---|
61 | n/a | |
---|
62 | n/a | if _path_created.get(abs_head): |
---|
63 | n/a | continue |
---|
64 | n/a | |
---|
65 | n/a | if verbose >= 1: |
---|
66 | n/a | log.info("creating %s", head) |
---|
67 | n/a | |
---|
68 | n/a | if not dry_run: |
---|
69 | n/a | try: |
---|
70 | n/a | os.mkdir(head, mode) |
---|
71 | n/a | except OSError as exc: |
---|
72 | n/a | if not (exc.errno == errno.EEXIST and os.path.isdir(head)): |
---|
73 | n/a | raise DistutilsFileError( |
---|
74 | n/a | "could not create '%s': %s" % (head, exc.args[-1])) |
---|
75 | n/a | created_dirs.append(head) |
---|
76 | n/a | |
---|
77 | n/a | _path_created[abs_head] = 1 |
---|
78 | n/a | return created_dirs |
---|
79 | n/a | |
---|
80 | n/a | def create_tree(base_dir, files, mode=0o777, verbose=1, dry_run=0): |
---|
81 | n/a | """Create all the empty directories under 'base_dir' needed to put 'files' |
---|
82 | n/a | there. |
---|
83 | n/a | |
---|
84 | n/a | 'base_dir' is just the name of a directory which doesn't necessarily |
---|
85 | n/a | exist yet; 'files' is a list of filenames to be interpreted relative to |
---|
86 | n/a | 'base_dir'. 'base_dir' + the directory portion of every file in 'files' |
---|
87 | n/a | will be created if it doesn't already exist. 'mode', 'verbose' and |
---|
88 | n/a | 'dry_run' flags are as for 'mkpath()'. |
---|
89 | n/a | """ |
---|
90 | n/a | # First get the list of directories to create |
---|
91 | n/a | need_dir = set() |
---|
92 | n/a | for file in files: |
---|
93 | n/a | need_dir.add(os.path.join(base_dir, os.path.dirname(file))) |
---|
94 | n/a | |
---|
95 | n/a | # Now create them |
---|
96 | n/a | for dir in sorted(need_dir): |
---|
97 | n/a | mkpath(dir, mode, verbose=verbose, dry_run=dry_run) |
---|
98 | n/a | |
---|
99 | n/a | def copy_tree(src, dst, preserve_mode=1, preserve_times=1, |
---|
100 | n/a | preserve_symlinks=0, update=0, verbose=1, dry_run=0): |
---|
101 | n/a | """Copy an entire directory tree 'src' to a new location 'dst'. |
---|
102 | n/a | |
---|
103 | n/a | Both 'src' and 'dst' must be directory names. If 'src' is not a |
---|
104 | n/a | directory, raise DistutilsFileError. If 'dst' does not exist, it is |
---|
105 | n/a | created with 'mkpath()'. The end result of the copy is that every |
---|
106 | n/a | file in 'src' is copied to 'dst', and directories under 'src' are |
---|
107 | n/a | recursively copied to 'dst'. Return the list of files that were |
---|
108 | n/a | copied or might have been copied, using their output name. The |
---|
109 | n/a | return value is unaffected by 'update' or 'dry_run': it is simply |
---|
110 | n/a | the list of all files under 'src', with the names changed to be |
---|
111 | n/a | under 'dst'. |
---|
112 | n/a | |
---|
113 | n/a | 'preserve_mode' and 'preserve_times' are the same as for |
---|
114 | n/a | 'copy_file'; note that they only apply to regular files, not to |
---|
115 | n/a | directories. If 'preserve_symlinks' is true, symlinks will be |
---|
116 | n/a | copied as symlinks (on platforms that support them!); otherwise |
---|
117 | n/a | (the default), the destination of the symlink will be copied. |
---|
118 | n/a | 'update' and 'verbose' are the same as for 'copy_file'. |
---|
119 | n/a | """ |
---|
120 | n/a | from distutils.file_util import copy_file |
---|
121 | n/a | |
---|
122 | n/a | if not dry_run and not os.path.isdir(src): |
---|
123 | n/a | raise DistutilsFileError( |
---|
124 | n/a | "cannot copy tree '%s': not a directory" % src) |
---|
125 | n/a | try: |
---|
126 | n/a | names = os.listdir(src) |
---|
127 | n/a | except OSError as e: |
---|
128 | n/a | if dry_run: |
---|
129 | n/a | names = [] |
---|
130 | n/a | else: |
---|
131 | n/a | raise DistutilsFileError( |
---|
132 | n/a | "error listing files in '%s': %s" % (src, e.strerror)) |
---|
133 | n/a | |
---|
134 | n/a | if not dry_run: |
---|
135 | n/a | mkpath(dst, verbose=verbose) |
---|
136 | n/a | |
---|
137 | n/a | outputs = [] |
---|
138 | n/a | |
---|
139 | n/a | for n in names: |
---|
140 | n/a | src_name = os.path.join(src, n) |
---|
141 | n/a | dst_name = os.path.join(dst, n) |
---|
142 | n/a | |
---|
143 | n/a | if n.startswith('.nfs'): |
---|
144 | n/a | # skip NFS rename files |
---|
145 | n/a | continue |
---|
146 | n/a | |
---|
147 | n/a | if preserve_symlinks and os.path.islink(src_name): |
---|
148 | n/a | link_dest = os.readlink(src_name) |
---|
149 | n/a | if verbose >= 1: |
---|
150 | n/a | log.info("linking %s -> %s", dst_name, link_dest) |
---|
151 | n/a | if not dry_run: |
---|
152 | n/a | os.symlink(link_dest, dst_name) |
---|
153 | n/a | outputs.append(dst_name) |
---|
154 | n/a | |
---|
155 | n/a | elif os.path.isdir(src_name): |
---|
156 | n/a | outputs.extend( |
---|
157 | n/a | copy_tree(src_name, dst_name, preserve_mode, |
---|
158 | n/a | preserve_times, preserve_symlinks, update, |
---|
159 | n/a | verbose=verbose, dry_run=dry_run)) |
---|
160 | n/a | else: |
---|
161 | n/a | copy_file(src_name, dst_name, preserve_mode, |
---|
162 | n/a | preserve_times, update, verbose=verbose, |
---|
163 | n/a | dry_run=dry_run) |
---|
164 | n/a | outputs.append(dst_name) |
---|
165 | n/a | |
---|
166 | n/a | return outputs |
---|
167 | n/a | |
---|
168 | n/a | def _build_cmdtuple(path, cmdtuples): |
---|
169 | n/a | """Helper for remove_tree().""" |
---|
170 | n/a | for f in os.listdir(path): |
---|
171 | n/a | real_f = os.path.join(path,f) |
---|
172 | n/a | if os.path.isdir(real_f) and not os.path.islink(real_f): |
---|
173 | n/a | _build_cmdtuple(real_f, cmdtuples) |
---|
174 | n/a | else: |
---|
175 | n/a | cmdtuples.append((os.remove, real_f)) |
---|
176 | n/a | cmdtuples.append((os.rmdir, path)) |
---|
177 | n/a | |
---|
178 | n/a | def remove_tree(directory, verbose=1, dry_run=0): |
---|
179 | n/a | """Recursively remove an entire directory tree. |
---|
180 | n/a | |
---|
181 | n/a | Any errors are ignored (apart from being reported to stdout if 'verbose' |
---|
182 | n/a | is true). |
---|
183 | n/a | """ |
---|
184 | n/a | global _path_created |
---|
185 | n/a | |
---|
186 | n/a | if verbose >= 1: |
---|
187 | n/a | log.info("removing '%s' (and everything under it)", directory) |
---|
188 | n/a | if dry_run: |
---|
189 | n/a | return |
---|
190 | n/a | cmdtuples = [] |
---|
191 | n/a | _build_cmdtuple(directory, cmdtuples) |
---|
192 | n/a | for cmd in cmdtuples: |
---|
193 | n/a | try: |
---|
194 | n/a | cmd[0](cmd[1]) |
---|
195 | n/a | # remove dir from cache if it's already there |
---|
196 | n/a | abspath = os.path.abspath(cmd[1]) |
---|
197 | n/a | if abspath in _path_created: |
---|
198 | n/a | del _path_created[abspath] |
---|
199 | n/a | except OSError as exc: |
---|
200 | n/a | log.warn("error removing %s: %s", directory, exc) |
---|
201 | n/a | |
---|
202 | n/a | def ensure_relative(path): |
---|
203 | n/a | """Take the full path 'path', and make it a relative path. |
---|
204 | n/a | |
---|
205 | n/a | This is useful to make 'path' the second argument to os.path.join(). |
---|
206 | n/a | """ |
---|
207 | n/a | drive, path = os.path.splitdrive(path) |
---|
208 | n/a | if path[0:1] == os.sep: |
---|
209 | n/a | path = drive + path[1:] |
---|
210 | n/a | return path |
---|