blob: b40373c4866344d3ffeb54a699ee7849d7c0183e [file] [log] [blame]
Greg Ward2689e3d1999-03-22 14:52:19 +00001"""distutils.util
2
3General-purpose utility functions used throughout the Distutils
4(especially in command classes). Mostly filesystem manipulation, but
5not limited to that. The functions in this module generally raise
6DistutilsFileError when they have problems with the filesystem, because
7os.error in pre-1.5.2 Python only gives the error message and not the
8file causing it."""
9
10# created 1999/03/08, Greg Ward
11
Greg Ward3ce77fd2000-03-02 01:49:45 +000012__revision__ = "$Id$"
Greg Ward2689e3d1999-03-22 14:52:19 +000013
Greg Warda7540bd2000-03-23 04:39:16 +000014import sys, os, string, re, shutil
Greg Ward2689e3d1999-03-22 14:52:19 +000015from distutils.errors import *
Greg Ward7c1a6d42000-03-29 02:48:40 +000016from distutils.spawn import spawn
Greg Ward2689e3d1999-03-22 14:52:19 +000017
Greg Wardac1424a1999-09-21 18:37:51 +000018# cache for by mkpath() -- in addition to cheapening redundant calls,
19# eliminates redundant "creating /foo/bar/baz" messages in dry-run mode
20PATH_CREATED = {}
21
Greg Ward2689e3d1999-03-22 14:52:19 +000022# I don't use os.makedirs because a) it's new to Python 1.5.2, and
23# b) it blows up if the directory already exists (I want to silently
24# succeed in that case).
Greg Warde765a3b1999-04-04 02:54:20 +000025def mkpath (name, mode=0777, verbose=0, dry_run=0):
Greg Ward2689e3d1999-03-22 14:52:19 +000026 """Create a directory and any missing ancestor directories. If the
27 directory already exists, return silently. Raise
28 DistutilsFileError if unable to create some directory along the
29 way (eg. some sub-path exists, but is a file rather than a
30 directory). If 'verbose' is true, print a one-line summary of
31 each mkdir to stdout."""
32
Greg Wardac1424a1999-09-21 18:37:51 +000033 global PATH_CREATED
34
Greg Ward2689e3d1999-03-22 14:52:19 +000035 # XXX what's the better way to handle verbosity? print as we create
36 # each directory in the path (the current behaviour), or only announce
Greg Wardac1424a1999-09-21 18:37:51 +000037 # the creation of the whole path? (quite easy to do the latter since
38 # we're not using a recursive algorithm)
Greg Ward2689e3d1999-03-22 14:52:19 +000039
Greg Wardf3b997a1999-10-03 20:50:41 +000040 name = os.path.normpath (name)
41
Greg Ward96182d72000-03-03 03:00:02 +000042 if os.path.isdir (name) or name == '':
Greg Ward2689e3d1999-03-22 14:52:19 +000043 return
Greg Wardac1424a1999-09-21 18:37:51 +000044 if PATH_CREATED.get (name):
45 return
Greg Ward2689e3d1999-03-22 14:52:19 +000046
47 (head, tail) = os.path.split (name)
48 tails = [tail] # stack of lone dirs to create
49
50 while head and tail and not os.path.isdir (head):
51 #print "splitting '%s': " % head,
52 (head, tail) = os.path.split (head)
53 #print "to ('%s','%s')" % (head, tail)
54 tails.insert (0, tail) # push next higher dir onto stack
55
56 #print "stack of tails:", tails
57
Greg Warde765a3b1999-04-04 02:54:20 +000058 # now 'head' contains the deepest directory that already exists
59 # (that is, the child of 'head' in 'name' is the highest directory
60 # that does *not* exist)
Greg Ward2689e3d1999-03-22 14:52:19 +000061 for d in tails:
62 #print "head = %s, d = %s: " % (head, d),
63 head = os.path.join (head, d)
Greg Wardcd1486f1999-09-29 12:14:16 +000064 if PATH_CREATED.get (head):
65 continue
66
Greg Ward2689e3d1999-03-22 14:52:19 +000067 if verbose:
68 print "creating", head
Greg Warde765a3b1999-04-04 02:54:20 +000069
70 if not dry_run:
71 try:
72 os.mkdir (head)
73 except os.error, (errno, errstr):
Greg Ward1b4ede52000-03-22 00:22:44 +000074 raise DistutilsFileError, \
75 "could not create '%s': %s" % (head, errstr)
Greg Ward2689e3d1999-03-22 14:52:19 +000076
Greg Wardac1424a1999-09-21 18:37:51 +000077 PATH_CREATED[head] = 1
78
Greg Ward2689e3d1999-03-22 14:52:19 +000079# mkpath ()
80
81
Greg Ward138ce651999-09-13 03:09:38 +000082def newer (source, target):
83 """Return true if 'source' exists and is more recently modified than
84 'target', or if 'source' exists and 'target' doesn't. Return
85 false if both exist and 'target' is the same age or younger than
86 'source'. Raise DistutilsFileError if 'source' does not
87 exist."""
Greg Ward2689e3d1999-03-22 14:52:19 +000088
Greg Ward138ce651999-09-13 03:09:38 +000089 if not os.path.exists (source):
90 raise DistutilsFileError, "file '%s' does not exist" % source
91 if not os.path.exists (target):
Greg Ward2689e3d1999-03-22 14:52:19 +000092 return 1
93
Greg Ward138ce651999-09-13 03:09:38 +000094 from stat import ST_MTIME
95 mtime1 = os.stat(source)[ST_MTIME]
96 mtime2 = os.stat(target)[ST_MTIME]
Greg Ward2689e3d1999-03-22 14:52:19 +000097
98 return mtime1 > mtime2
99
100# newer ()
101
102
Greg Ward138ce651999-09-13 03:09:38 +0000103def newer_pairwise (sources, targets):
Greg Ward95526652000-03-06 03:44:32 +0000104 """Walk two filename lists in parallel, testing if each source is newer
105 than its corresponding target. Return a pair of lists (sources,
106 targets) where source is newer than target, according to the
107 semantics of 'newer()'."""
Greg Ward138ce651999-09-13 03:09:38 +0000108
109 if len (sources) != len (targets):
110 raise ValueError, "'sources' and 'targets' must be same length"
111
Greg Ward95526652000-03-06 03:44:32 +0000112 # build a pair of lists (sources, targets) where source is newer
113 n_sources = []
114 n_targets = []
115 for i in range (len (sources)):
116 if newer (sources[i], targets[i]):
117 n_sources.append (sources[i])
118 n_targets.append (targets[i])
119
120 return (n_sources, n_targets)
Greg Ward138ce651999-09-13 03:09:38 +0000121
122# newer_pairwise ()
123
124
Greg Ward7b7679e2000-01-09 22:48:59 +0000125def newer_group (sources, target, missing='error'):
Greg Ward138ce651999-09-13 03:09:38 +0000126 """Return true if 'target' is out-of-date with respect to any
127 file listed in 'sources'. In other words, if 'target' exists and
128 is newer than every file in 'sources', return false; otherwise
Greg Ward7b7679e2000-01-09 22:48:59 +0000129 return true. 'missing' controls what we do when a source file is
130 missing; the default ("error") is to blow up with an OSError from
131 inside 'stat()'; if it is "ignore", we silently drop any missing
132 source files; if it is "newer", any missing source files make us
133 assume that 'target' is out-of-date (this is handy in "dry-run"
134 mode: it'll make you pretend to carry out commands that wouldn't
135 work because inputs are missing, but that doesn't matter because
136 you're not actually going to run the commands)."""
Greg Ward138ce651999-09-13 03:09:38 +0000137
138 # If the target doesn't even exist, then it's definitely out-of-date.
139 if not os.path.exists (target):
140 return 1
141
142 # Otherwise we have to find out the hard way: if *any* source file
143 # is more recent than 'target', then 'target' is out-of-date and
144 # we can immediately return true. If we fall through to the end
145 # of the loop, then 'target' is up-to-date and we return false.
146 from stat import ST_MTIME
147 target_mtime = os.stat (target)[ST_MTIME]
148 for source in sources:
Greg Ward7b7679e2000-01-09 22:48:59 +0000149 if not os.path.exists (source):
150 if missing == 'error': # blow up when we stat() the file
151 pass
152 elif missing == 'ignore': # missing source dropped from
153 continue # target's dependency list
154 elif missing == 'newer': # missing source means target is
155 return 1 # out-of-date
156
Greg Ward138ce651999-09-13 03:09:38 +0000157 source_mtime = os.stat(source)[ST_MTIME]
158 if source_mtime > target_mtime:
159 return 1
160 else:
161 return 0
162
163# newer_group ()
164
165
Greg Wardf3b997a1999-10-03 20:50:41 +0000166# XXX this isn't used anywhere, and worse, it has the same name as a method
167# in Command with subtly different semantics. (This one just has one
168# source -> one dest; that one has many sources -> one dest.) Nuke it?
Greg Ward2689e3d1999-03-22 14:52:19 +0000169def make_file (src, dst, func, args,
170 verbose=0, update_message=None, noupdate_message=None):
171 """Makes 'dst' from 'src' (both filenames) by calling 'func' with
172 'args', but only if it needs to: i.e. if 'dst' does not exist or
173 'src' is newer than 'dst'."""
174
175 if newer (src, dst):
176 if verbose and update_message:
177 print update_message
178 apply (func, args)
179 else:
180 if verbose and noupdate_message:
181 print noupdate_message
182
183# make_file ()
184
185
186def _copy_file_contents (src, dst, buffer_size=16*1024):
187 """Copy the file 'src' to 'dst'; both must be filenames. Any error
188 opening either file, reading from 'src', or writing to 'dst',
189 raises DistutilsFileError. Data is read/written in chunks of
190 'buffer_size' bytes (default 16k). No attempt is made to handle
191 anything apart from regular files."""
192
193 # Stolen from shutil module in the standard library, but with
194 # custom error-handling added.
195
196 fsrc = None
197 fdst = None
198 try:
199 try:
200 fsrc = open(src, 'rb')
201 except os.error, (errno, errstr):
Greg Ward96182d72000-03-03 03:00:02 +0000202 raise DistutilsFileError, \
203 "could not open '%s': %s" % (src, errstr)
Greg Ward2689e3d1999-03-22 14:52:19 +0000204
205 try:
206 fdst = open(dst, 'wb')
207 except os.error, (errno, errstr):
Greg Ward96182d72000-03-03 03:00:02 +0000208 raise DistutilsFileError, \
209 "could not create '%s': %s" % (dst, errstr)
Greg Ward2689e3d1999-03-22 14:52:19 +0000210
211 while 1:
212 try:
213 buf = fsrc.read (buffer_size)
214 except os.error, (errno, errstr):
215 raise DistutilsFileError, \
Greg Ward96182d72000-03-03 03:00:02 +0000216 "could not read from '%s': %s" % (src, errstr)
Greg Ward2689e3d1999-03-22 14:52:19 +0000217
218 if not buf:
219 break
220
221 try:
222 fdst.write(buf)
223 except os.error, (errno, errstr):
224 raise DistutilsFileError, \
Greg Ward96182d72000-03-03 03:00:02 +0000225 "could not write to '%s': %s" % (dst, errstr)
Greg Ward2689e3d1999-03-22 14:52:19 +0000226
227 finally:
228 if fdst:
229 fdst.close()
230 if fsrc:
231 fsrc.close()
232
233# _copy_file_contents()
234
235
236def copy_file (src, dst,
237 preserve_mode=1,
238 preserve_times=1,
239 update=0,
Greg Warde765a3b1999-04-04 02:54:20 +0000240 verbose=0,
241 dry_run=0):
Greg Ward2689e3d1999-03-22 14:52:19 +0000242
243 """Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src'
244 is copied there with the same name; otherwise, it must be a
245 filename. (If the file exists, it will be ruthlessly clobbered.)
246 If 'preserve_mode' is true (the default), the file's mode (type
247 and permission bits, or whatever is analogous on the current
248 platform) is copied. If 'preserve_times' is true (the default),
249 the last-modified and last-access times are copied as well. If
250 'update' is true, 'src' will only be copied if 'dst' does not
251 exist, or if 'dst' does exist but is older than 'src'. If
252 'verbose' is true, then a one-line summary of the copy will be
Greg Ward884df451999-05-02 21:42:05 +0000253 printed to stdout.
254
255 Return true if the file was copied (or would have been copied),
256 false otherwise (ie. 'update' was true and the destination is
257 up-to-date)."""
Greg Ward2689e3d1999-03-22 14:52:19 +0000258
259 # XXX doesn't copy Mac-specific metadata
260
Greg Ward2689e3d1999-03-22 14:52:19 +0000261 from stat import *
262
263 if not os.path.isfile (src):
264 raise DistutilsFileError, \
Greg Ward96182d72000-03-03 03:00:02 +0000265 "can't copy '%s': not a regular file" % src
Greg Ward2689e3d1999-03-22 14:52:19 +0000266
267 if os.path.isdir (dst):
268 dir = dst
269 dst = os.path.join (dst, os.path.basename (src))
270 else:
271 dir = os.path.dirname (dst)
272
273 if update and not newer (src, dst):
Greg Ward884df451999-05-02 21:42:05 +0000274 if verbose:
275 print "not copying %s (output up-to-date)" % src
276 return 0
Greg Ward2689e3d1999-03-22 14:52:19 +0000277
278 if verbose:
279 print "copying %s -> %s" % (src, dir)
280
Greg Warde765a3b1999-04-04 02:54:20 +0000281 if dry_run:
Greg Ward884df451999-05-02 21:42:05 +0000282 return 1
Greg Warde765a3b1999-04-04 02:54:20 +0000283
Greg Ward911d8662000-03-07 03:34:16 +0000284 # On a Mac, use the native file copy routine
285 if os.name == 'mac':
286 import macostools
287 try:
288 macostools.copy (src, dst, 0, preserve_times)
289 except OSError, exc:
290 raise DistutilsFileError, \
291 "could not copy '%s' to '%s': %s" % (src, dst, exc[-1])
292 return 1
293
294 # Otherwise use custom routine
Greg Warde765a3b1999-04-04 02:54:20 +0000295 _copy_file_contents (src, dst)
Greg Ward2689e3d1999-03-22 14:52:19 +0000296 if preserve_mode or preserve_times:
297 st = os.stat (src)
Greg Ward5116f901999-06-08 17:05:21 +0000298
299 # According to David Ascher <da@ski.org>, utime() should be done
300 # before chmod() (at least under NT).
Greg Ward2689e3d1999-03-22 14:52:19 +0000301 if preserve_times:
302 os.utime (dst, (st[ST_ATIME], st[ST_MTIME]))
Greg Ward5116f901999-06-08 17:05:21 +0000303 if preserve_mode:
304 os.chmod (dst, S_IMODE (st[ST_MODE]))
Greg Ward2689e3d1999-03-22 14:52:19 +0000305
Greg Ward884df451999-05-02 21:42:05 +0000306 return 1
307
Greg Ward2689e3d1999-03-22 14:52:19 +0000308# copy_file ()
309
310
311def copy_tree (src, dst,
312 preserve_mode=1,
313 preserve_times=1,
314 preserve_symlinks=0,
315 update=0,
Greg Warde765a3b1999-04-04 02:54:20 +0000316 verbose=0,
317 dry_run=0):
318
Greg Ward2689e3d1999-03-22 14:52:19 +0000319 """Copy an entire directory tree 'src' to a new location 'dst'. Both
320 'src' and 'dst' must be directory names. If 'src' is not a
321 directory, raise DistutilsFileError. If 'dst' does not exist, it
Greg Wardf3b997a1999-10-03 20:50:41 +0000322 is created with 'mkpath()'. The end result of the copy is that
Greg Ward2689e3d1999-03-22 14:52:19 +0000323 every file in 'src' is copied to 'dst', and directories under
Greg Ward884df451999-05-02 21:42:05 +0000324 'src' are recursively copied to 'dst'. Return the list of files
325 copied (under their output names) -- note that if 'update' is true,
326 this might be less than the list of files considered. Return
327 value is not affected by 'dry_run'.
Greg Ward2689e3d1999-03-22 14:52:19 +0000328
329 'preserve_mode' and 'preserve_times' are the same as for
330 'copy_file'; note that they only apply to regular files, not to
331 directories. If 'preserve_symlinks' is true, symlinks will be
332 copied as symlinks (on platforms that support them!); otherwise
333 (the default), the destination of the symlink will be copied.
334 'update' and 'verbose' are the same as for 'copy_file'."""
335
Greg Ward138ce651999-09-13 03:09:38 +0000336 if not dry_run and not os.path.isdir (src):
Greg Ward2689e3d1999-03-22 14:52:19 +0000337 raise DistutilsFileError, \
Greg Ward96182d72000-03-03 03:00:02 +0000338 "cannot copy tree '%s': not a directory" % src
Greg Ward2689e3d1999-03-22 14:52:19 +0000339 try:
340 names = os.listdir (src)
341 except os.error, (errno, errstr):
Greg Ward138ce651999-09-13 03:09:38 +0000342 if dry_run:
343 names = []
344 else:
345 raise DistutilsFileError, \
Greg Ward96182d72000-03-03 03:00:02 +0000346 "error listing files in '%s': %s" % (src, errstr)
Greg Ward2689e3d1999-03-22 14:52:19 +0000347
Greg Warde765a3b1999-04-04 02:54:20 +0000348 if not dry_run:
349 mkpath (dst, verbose=verbose)
Greg Ward2689e3d1999-03-22 14:52:19 +0000350
Greg Ward884df451999-05-02 21:42:05 +0000351 outputs = []
352
Greg Ward2689e3d1999-03-22 14:52:19 +0000353 for n in names:
354 src_name = os.path.join (src, n)
355 dst_name = os.path.join (dst, n)
356
357 if preserve_symlinks and os.path.islink (src_name):
358 link_dest = os.readlink (src_name)
Greg Warde765a3b1999-04-04 02:54:20 +0000359 if verbose:
360 print "linking %s -> %s" % (dst_name, link_dest)
361 if not dry_run:
362 os.symlink (link_dest, dst_name)
Greg Ward884df451999-05-02 21:42:05 +0000363 outputs.append (dst_name)
364
Greg Ward2689e3d1999-03-22 14:52:19 +0000365 elif os.path.isdir (src_name):
Greg Warda002edc2000-01-30 19:57:48 +0000366 outputs.extend (
Greg Ward884df451999-05-02 21:42:05 +0000367 copy_tree (src_name, dst_name,
368 preserve_mode, preserve_times, preserve_symlinks,
Greg Warda002edc2000-01-30 19:57:48 +0000369 update, verbose, dry_run))
Greg Ward2689e3d1999-03-22 14:52:19 +0000370 else:
Greg Ward884df451999-05-02 21:42:05 +0000371 if (copy_file (src_name, dst_name,
372 preserve_mode, preserve_times,
373 update, verbose, dry_run)):
374 outputs.append (dst_name)
375
376 return outputs
Greg Ward2689e3d1999-03-22 14:52:19 +0000377
378# copy_tree ()
Greg Ward138ce651999-09-13 03:09:38 +0000379
380
Greg Wardb98fe362000-03-18 15:42:22 +0000381def remove_tree (directory, verbose=0, dry_run=0):
382 """Recursively remove an entire directory tree. Any errors are ignored
383 (apart from being reported to stdout if 'verbose' is true)."""
384
385 if verbose:
386 print "removing '%s' (and everything under it)" % directory
387 if dry_run:
388 return
389 try:
390 shutil.rmtree(directory,1)
391 except (IOError, OSError), exc:
392 if verbose:
393 if exc.filename:
394 print "error removing %s: %s (%s)" % \
395 (directory, exc.strerror, exc.filename)
396 else:
397 print "error removing %s: %s" % (directory, exc.strerror)
398
399
Greg Ward138ce651999-09-13 03:09:38 +0000400# XXX I suspect this is Unix-specific -- need porting help!
401def move_file (src, dst,
402 verbose=0,
403 dry_run=0):
404
405 """Move a file 'src' to 'dst'. If 'dst' is a directory, the file
406 will be moved into it with the same name; otherwise, 'src' is
407 just renamed to 'dst'. Return the new full name of the file.
408
409 Handles cross-device moves on Unix using
410 'copy_file()'. What about other systems???"""
411
412 from os.path import exists, isfile, isdir, basename, dirname
413
414 if verbose:
415 print "moving %s -> %s" % (src, dst)
416
417 if dry_run:
418 return dst
419
420 if not isfile (src):
421 raise DistutilsFileError, \
422 "can't move '%s': not a regular file" % src
423
424 if isdir (dst):
425 dst = os.path.join (dst, basename (src))
426 elif exists (dst):
427 raise DistutilsFileError, \
428 "can't move '%s': destination '%s' already exists" % \
429 (src, dst)
430
431 if not isdir (dirname (dst)):
432 raise DistutilsFileError, \
433 "can't move '%s': destination '%s' not a valid path" % \
434 (src, dst)
435
436 copy_it = 0
437 try:
438 os.rename (src, dst)
439 except os.error, (num, msg):
440 if num == errno.EXDEV:
441 copy_it = 1
442 else:
443 raise DistutilsFileError, \
444 "couldn't move '%s' to '%s': %s" % (src, dst, msg)
445
446 if copy_it:
447 copy_file (src, dst)
448 try:
449 os.unlink (src)
450 except os.error, (num, msg):
451 try:
452 os.unlink (dst)
453 except os.error:
454 pass
455 raise DistutilsFileError, \
456 ("couldn't move '%s' to '%s' by copy/delete: " +
457 "delete '%s' failed: %s") % \
458 (src, dst, src, msg)
459
460 return dst
461
462# move_file ()
Greg Wardac1424a1999-09-21 18:37:51 +0000463
464
465def write_file (filename, contents):
Greg Wardf3b997a1999-10-03 20:50:41 +0000466 """Create a file with the specified name and write 'contents' (a
Greg Wardac1424a1999-09-21 18:37:51 +0000467 sequence of strings without line terminators) to it."""
468
469 f = open (filename, "w")
470 for line in contents:
471 f.write (line + "\n")
472 f.close ()
Greg Ward585df892000-03-01 14:40:15 +0000473
474
475def get_platform ():
476 """Return a string (suitable for tacking onto directory names) that
477 identifies the current platform. Under Unix, identifies both the OS
478 and hardware architecture, e.g. "linux-i586", "solaris-sparc",
479 "irix-mips". For Windows and Mac OS, just returns 'sys.platform' --
480 i.e. "???" or "???"."""
481
482 if os.name == 'posix':
483 uname = os.uname()
484 OS = uname[0]
485 arch = uname[4]
486 return "%s-%s" % (string.lower (OS), string.lower (arch))
487 else:
488 return sys.platform
489
490# get_platform()
Greg Ward50919292000-03-07 03:27:08 +0000491
492
493def native_path (pathname):
494 """Return 'pathname' as a name that will work on the native
495 filesystem, i.e. split it on '/' and put it back together again
496 using the current directory separator. Needed because filenames in
497 the setup script are always supplied in Unix style, and have to be
498 converted to the local convention before we can actually use them in
499 the filesystem. Raises DistutilsValueError if 'pathname' is
500 absolute (starts with '/') or contains local directory separators
501 (unless the local separator is '/', of course)."""
502
503 if pathname[0] == '/':
504 raise DistutilsValueError, "path '%s' cannot be absolute" % pathname
505 if pathname[-1] == '/':
506 raise DistutilsValueError, "path '%s' cannot end with '/'" % pathname
Greg Ward1b4ede52000-03-22 00:22:44 +0000507 if os.sep != '/' and os.sep in pathname:
508 raise DistutilsValueError, \
509 "path '%s' cannot contain '%c' character" % \
510 (pathname, os.sep)
Greg Ward50919292000-03-07 03:27:08 +0000511
512 paths = string.split (pathname, '/')
513 return apply (os.path.join, paths)
514 else:
515 return pathname
516
517# native_path ()
Greg Ward1b4ede52000-03-22 00:22:44 +0000518
519
520def _check_environ ():
521 """Ensure that 'os.environ' has all the environment variables we
522 guarantee that users can use in config files, command-line
523 options, etc. Currently this includes:
524 HOME - user's home directory (Unix only)
525 PLAT - desription of the current platform, including hardware
526 and OS (see 'get_platform()')
527 """
528
529 if os.name == 'posix' and not os.environ.has_key('HOME'):
530 import pwd
531 os.environ['HOME'] = pwd.getpwuid (os.getuid())[5]
532
533 if not os.environ.has_key('PLAT'):
534 os.environ['PLAT'] = get_platform ()
535
536
537def subst_vars (str, local_vars):
538 """Perform shell/Perl-style variable substitution on 'string'.
539 Every occurence of '$' followed by a name, or a name enclosed in
540 braces, is considered a variable. Every variable is substituted by
541 the value found in the 'local_vars' dictionary, or in 'os.environ'
542 if it's not in 'local_vars'. 'os.environ' is first checked/
543 augmented to guarantee that it contains certain values: see
544 '_check_environ()'. Raise ValueError for any variables not found in
545 either 'local_vars' or 'os.environ'."""
546
547 _check_environ ()
548 def _subst (match, local_vars=local_vars):
549 var_name = match.group(1)
550 if local_vars.has_key (var_name):
551 return str (local_vars[var_name])
552 else:
553 return os.environ[var_name]
554
555 return re.sub (r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, str)
556
557# subst_vars ()
Greg Ward7c1a6d42000-03-29 02:48:40 +0000558
559
560def make_tarball (base_dir, compress="gzip", verbose=0, dry_run=0):
561 """Create a (possibly compressed) tar file from all the files under
562 'base_dir'. 'compress' must be "gzip" (the default), "compress", or
563 None. Both "tar" and the compression utility named by 'compress'
564 must be on the default program search path, so this is probably
565 Unix-specific. The output tar file will be named 'base_dir' +
566 ".tar", possibly plus the appropriate compression extension
567 (".gz" or ".Z"). Return the output filename."""
568
569 # XXX GNU tar 1.13 has a nifty option to add a prefix directory.
570 # It's pretty new, though, so we certainly can't require it --
571 # but it would be nice to take advantage of it to skip the
572 # "create a tree of hardlinks" step! (Would also be nice to
573 # detect GNU tar to use its 'z' option and save a step.)
574
575 compress_ext = { 'gzip': ".gz",
576 'compress': ".Z" }
577
578 if compress is not None and compress not in ('gzip', 'compress'):
579 raise ValueError, \
580 "bad value for 'compress': must be None, 'gzip', or 'compress'"
581
582 archive_name = base_dir + ".tar"
583 cmd = ["tar", "-cf", archive_name, base_dir]
584 spawn (cmd, verbose=verbose, dry_run=dry_run)
585
586 if compress:
587 spawn ([compress, archive_name], verbose=verbose, dry_run=dry_run)
588 return archive_name + compress_ext[compress]
589 else:
590 return archive_name
591
592# make_tarball ()
593
594
595def make_zipfile (base_dir, verbose=0, dry_run=0):
596 """Create a ZIP file from all the files under 'base_dir'. The
597 output ZIP file will be named 'base_dir' + ".zip". Uses either the
598 InfoZIP "zip" utility (if installed and found on the default search
599 path) or the "zipfile" Python module (if available). If neither
600 tool is available, raises DistutilsExecError. Returns the name
601 of the output ZIP file."""
602
603 # This initially assumed the Unix 'zip' utility -- but
604 # apparently InfoZIP's zip.exe works the same under Windows, so
605 # no changes needed!
606
607 zip_filename = base_dir + ".zip"
608 try:
609 spawn (["zip", "-r", zip_filename, base_dir],
610 verbose=verbose, dry_run=dry_run)
611 except DistutilsExecError:
612
613 # XXX really should distinguish between "couldn't find
614 # external 'zip' command" and "zip failed" -- shouldn't try
615 # again in the latter case. (I think fixing this will
616 # require some cooperation from the spawn module -- perhaps
617 # a utility function to search the path, so we can fallback
618 # on zipfile.py without the failed spawn.)
619 try:
620 import zipfile
621 except ImportError:
622 raise DistutilsExecError, \
623 ("unable to create zip file '%s': " +
624 "could neither find a standalone zip utility nor " +
625 "import the 'zipfile' module") % zip_filename
626
627 if verbose:
628 print "creating '%s' and adding '%s' to it" % \
629 (zip_filename, base_dir)
630
631 def visit (z, dirname, names):
632 for name in names:
633 path = os.path.join (dirname, name)
634 if os.path.isfile (path):
635 z.write (path, path)
636
637 if not dry_run:
638 z = zipfile.ZipFile (zip_filename, "wb",
639 compression=zipfile.ZIP_DEFLATED)
640
641 os.path.walk (base_dir, visit, z)
642 z.close()
643
644 return zip_filename
645
646# make_zipfile ()