blob: d1dc90952038a362e82b9383a3b313037071827e [file] [log] [blame]
Greg Wardaebf7062000-04-04 02:05:59 +00001"""distutils.archive_util
2
3Utility functions for creating archive files (tarballs, zip files,
4that sort of thing)."""
5
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00006# This module should be kept compatible with Python 1.5.2.
7
Greg Wardaebf7062000-04-04 02:05:59 +00008__revision__ = "$Id$"
9
10import os
11from distutils.errors import DistutilsExecError
12from distutils.spawn import spawn
Greg Ward04e25a12000-08-22 01:48:54 +000013from distutils.dir_util import mkpath
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000014from distutils import log
Greg Wardaebf7062000-04-04 02:05:59 +000015
16def make_tarball (base_name, base_dir, compress="gzip",
17 verbose=0, dry_run=0):
18 """Create a (possibly compressed) tar file from all the files under
Greg Wardca4289f2000-09-26 02:13:49 +000019 'base_dir'. 'compress' must be "gzip" (the default), "compress",
20 "bzip2", or None. Both "tar" and the compression utility named by
21 'compress' must be on the default program search path, so this is
22 probably Unix-specific. The output tar file will be named 'base_dir' +
23 ".tar", possibly plus the appropriate compression extension (".gz",
24 ".bz2" or ".Z"). Return the output filename.
25 """
Greg Wardaebf7062000-04-04 02:05:59 +000026 # XXX GNU tar 1.13 has a nifty option to add a prefix directory.
27 # It's pretty new, though, so we certainly can't require it --
28 # but it would be nice to take advantage of it to skip the
29 # "create a tree of hardlinks" step! (Would also be nice to
30 # detect GNU tar to use its 'z' option and save a step.)
31
32 compress_ext = { 'gzip': ".gz",
Greg Wardf1948782000-04-25 01:38:20 +000033 'bzip2': '.bz2',
Greg Wardaebf7062000-04-04 02:05:59 +000034 'compress': ".Z" }
Fred Drakeb94b8492001-12-06 20:51:35 +000035
Greg Wardf1948782000-04-25 01:38:20 +000036 # flags for compression program, each element of list will be an argument
37 compress_flags = {'gzip': ["-f9"],
38 'compress': ["-f"],
39 'bzip2': ['-f9']}
Greg Wardaebf7062000-04-04 02:05:59 +000040
Greg Wardf1948782000-04-25 01:38:20 +000041 if compress is not None and compress not in compress_ext.keys():
Greg Wardaebf7062000-04-04 02:05:59 +000042 raise ValueError, \
43 "bad value for 'compress': must be None, 'gzip', or 'compress'"
44
45 archive_name = base_name + ".tar"
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000046 mkpath(os.path.dirname(archive_name), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000047 cmd = ["tar", "-cf", archive_name, base_dir]
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000048 spawn(cmd, dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000049
50 if compress:
Greg Wardca4289f2000-09-26 02:13:49 +000051 spawn([compress] + compress_flags[compress] + [archive_name],
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000052 dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000053 return archive_name + compress_ext[compress]
54 else:
55 return archive_name
56
57# make_tarball ()
58
59
60def make_zipfile (base_name, base_dir, verbose=0, dry_run=0):
Greg Wardca4289f2000-09-26 02:13:49 +000061 """Create a zip file from all the files under 'base_dir'. The output
62 zip file will be named 'base_dir' + ".zip". Uses either the InfoZIP
63 "zip" utility (if installed and found on the default search path) or
64 the "zipfile" Python module (if available). If neither tool is
65 available, raises DistutilsExecError. Returns the name of the output
66 zip file.
67 """
Greg Wardaebf7062000-04-04 02:05:59 +000068 # This initially assumed the Unix 'zip' utility -- but
69 # apparently InfoZIP's zip.exe works the same under Windows, so
70 # no changes needed!
71
72 zip_filename = base_name + ".zip"
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000073 mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000074 try:
Greg Wardca4289f2000-09-26 02:13:49 +000075 spawn(["zip", "-rq", zip_filename, base_dir],
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000076 dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000077 except DistutilsExecError:
78
79 # XXX really should distinguish between "couldn't find
80 # external 'zip' command" and "zip failed" -- shouldn't try
81 # again in the latter case. (I think fixing this will
82 # require some cooperation from the spawn module -- perhaps
83 # a utility function to search the path, so we can fallback
84 # on zipfile.py without the failed spawn.)
85 try:
86 import zipfile
87 except ImportError:
88 raise DistutilsExecError, \
Fred Drakeb94b8492001-12-06 20:51:35 +000089 ("unable to create zip file '%s': " +
Greg Wardaebf7062000-04-04 02:05:59 +000090 "could neither find a standalone zip utility nor " +
91 "import the 'zipfile' module") % zip_filename
92
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000093
94 log.info("creating '%s' and adding '%s' to it",
95 zip_filename, base_dir)
96
Greg Wardaebf7062000-04-04 02:05:59 +000097 def visit (z, dirname, names):
98 for name in names:
Greg Ward65bc20c2000-05-31 02:17:19 +000099 path = os.path.normpath(os.path.join(dirname, name))
Greg Wardca4289f2000-09-26 02:13:49 +0000100 if os.path.isfile(path):
101 z.write(path, path)
Greg Wardaebf7062000-04-04 02:05:59 +0000102
103 if not dry_run:
Guido van Rossumb61914d2001-04-14 16:17:00 +0000104 z = zipfile.ZipFile(zip_filename, "w",
Greg Wardca4289f2000-09-26 02:13:49 +0000105 compression=zipfile.ZIP_DEFLATED)
Greg Wardaebf7062000-04-04 02:05:59 +0000106
Greg Wardca4289f2000-09-26 02:13:49 +0000107 os.path.walk(base_dir, visit, z)
Greg Wardaebf7062000-04-04 02:05:59 +0000108 z.close()
109
110 return zip_filename
111
112# make_zipfile ()
113
114
Greg Warddb807542000-04-22 03:09:56 +0000115ARCHIVE_FORMATS = {
Greg Ward2ff78872000-06-24 00:23:20 +0000116 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
117 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
118 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
119 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
Greg Ward04e25a12000-08-22 01:48:54 +0000120 'zip': (make_zipfile, [],"ZIP file")
Greg Warddb807542000-04-22 03:09:56 +0000121 }
122
123def check_archive_formats (formats):
124 for format in formats:
125 if not ARCHIVE_FORMATS.has_key(format):
126 return format
127 else:
128 return None
129
Greg Wardaebf7062000-04-04 02:05:59 +0000130def make_archive (base_name, format,
131 root_dir=None, base_dir=None,
132 verbose=0, dry_run=0):
Greg Wardaebf7062000-04-04 02:05:59 +0000133 """Create an archive file (eg. zip or tar). 'base_name' is the name
134 of the file to create, minus any format-specific extension; 'format'
135 is the archive format: one of "zip", "tar", "ztar", or "gztar".
136 'root_dir' is a directory that will be the root directory of the
137 archive; ie. we typically chdir into 'root_dir' before creating the
138 archive. 'base_dir' is the directory where we start archiving from;
139 ie. 'base_dir' will be the common prefix of all files and
140 directories in the archive. 'root_dir' and 'base_dir' both default
Greg Ward87909612000-06-01 01:07:55 +0000141 to the current directory. Returns the name of the archive file.
142 """
Greg Wardaebf7062000-04-04 02:05:59 +0000143 save_cwd = os.getcwd()
144 if root_dir is not None:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000145 log.debug("changing into '%s'", root_dir)
Greg Wardca4289f2000-09-26 02:13:49 +0000146 base_name = os.path.abspath(base_name)
Greg Wardaebf7062000-04-04 02:05:59 +0000147 if not dry_run:
Greg Wardca4289f2000-09-26 02:13:49 +0000148 os.chdir(root_dir)
Greg Wardaebf7062000-04-04 02:05:59 +0000149
150 if base_dir is None:
151 base_dir = os.curdir
152
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000153 kwargs = { 'dry_run': dry_run }
Fred Drakeb94b8492001-12-06 20:51:35 +0000154
Greg Warddb807542000-04-22 03:09:56 +0000155 try:
156 format_info = ARCHIVE_FORMATS[format]
157 except KeyError:
158 raise ValueError, "unknown archive format '%s'" % format
Greg Wardaebf7062000-04-04 02:05:59 +0000159
Greg Warddb807542000-04-22 03:09:56 +0000160 func = format_info[0]
161 for (arg,val) in format_info[1]:
162 kwargs[arg] = val
Greg Wardca4289f2000-09-26 02:13:49 +0000163 filename = apply(func, (base_name, base_dir), kwargs)
Greg Wardaebf7062000-04-04 02:05:59 +0000164
165 if root_dir is not None:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000166 log.debug("changing back to '%s'", save_cwd)
Greg Wardca4289f2000-09-26 02:13:49 +0000167 os.chdir(save_cwd)
Greg Wardaebf7062000-04-04 02:05:59 +0000168
Greg Ward87909612000-06-01 01:07:55 +0000169 return filename
170
Greg Wardaebf7062000-04-04 02:05:59 +0000171# make_archive ()