blob: d051f917bb4f37afa414ebb7d62d5cc8d3494e1b [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
Greg Wardaebf7062000-04-04 02:05:59 +00006__revision__ = "$Id$"
7
8import os
Tarek Ziadé77c8b372009-05-28 13:01:13 +00009from warnings import warn
10import sys
11
Greg Wardaebf7062000-04-04 02:05:59 +000012from distutils.errors import DistutilsExecError
13from distutils.spawn import spawn
Greg Ward04e25a12000-08-22 01:48:54 +000014from distutils.dir_util import mkpath
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000015from distutils import log
Greg Wardaebf7062000-04-04 02:05:59 +000016
Tarek Ziadé05b30342009-10-02 23:56:02 +000017try:
18 from pwd import getpwnam
Tarek Ziadé81c9a952009-10-03 14:54:15 +000019except ImportError:
Tarek Ziadé05b30342009-10-02 23:56:02 +000020 getpwnam = None
21
22try:
23 from grp import getgrnam
Tarek Ziadé81c9a952009-10-03 14:54:15 +000024except ImportError:
Tarek Ziadé05b30342009-10-02 23:56:02 +000025 getgrnam = None
26
27def _get_gid(name):
28 """Returns a gid, given a group name."""
29 if getgrnam is None or name is None:
30 return None
31 try:
32 result = getgrnam(name)
33 except KeyError:
34 result = None
35 if result is not None:
36 return result[2]
37 return None
38
39def _get_uid(name):
40 """Returns an uid, given a user name."""
41 if getpwnam is None or name is None:
42 return None
43 try:
44 result = getpwnam(name)
45 except KeyError:
46 result = None
47 if result is not None:
48 return result[2]
49 return None
50
51def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
52 owner=None, group=None):
Greg Wardaebf7062000-04-04 02:05:59 +000053 """Create a (possibly compressed) tar file from all the files under
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +000054 'base_dir'.
55
56 'compress' must be "gzip" (the default), "compress", "bzip2", or None.
Tarek Ziadé05b30342009-10-02 23:56:02 +000057 (compress will be deprecated in Python 3.2)
58
59 'owner' and 'group' can be used to define an owner and a group for the
60 archive that is being built. If not provided, the current owner and group
61 will be used.
62
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +000063 The output tar file will be named 'base_dir' + ".tar", possibly plus
64 the appropriate compression extension (".gz", ".bz2" or ".Z").
Tarek Ziadé05b30342009-10-02 23:56:02 +000065
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +000066 Returns the output filename.
Greg Wardca4289f2000-09-26 02:13:49 +000067 """
Tarek Ziadé77c8b372009-05-28 13:01:13 +000068 tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: '', 'compress': ''}
69 compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'}
Fred Drakeb94b8492001-12-06 20:51:35 +000070
Greg Wardf1948782000-04-25 01:38:20 +000071 # flags for compression program, each element of list will be an argument
Greg Wardf1948782000-04-25 01:38:20 +000072 if compress is not None and compress not in compress_ext.keys():
Collin Winter5b7e9d72007-08-30 03:52:21 +000073 raise ValueError(
Tarek Ziadé77c8b372009-05-28 13:01:13 +000074 "bad value for 'compress': must be None, 'gzip', 'bzip2' "
75 "or 'compress'")
Greg Wardaebf7062000-04-04 02:05:59 +000076
Tarek Ziadé77c8b372009-05-28 13:01:13 +000077 archive_name = base_name + '.tar'
78 if compress != 'compress':
79 archive_name += compress_ext.get(compress, '')
80
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000081 mkpath(os.path.dirname(archive_name), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000082
Tarek Ziadé77c8b372009-05-28 13:01:13 +000083 # creating the tarball
84 import tarfile # late import so Python build itself doesn't break
85
86 log.info('Creating tar archive')
Tarek Ziadé05b30342009-10-02 23:56:02 +000087
88 uid = _get_uid(owner)
89 gid = _get_gid(group)
90
91 def _set_uid_gid(tarinfo):
92 if gid is not None:
93 tarinfo.gid = gid
94 tarinfo.gname = group
95 if uid is not None:
96 tarinfo.uid = uid
97 tarinfo.uname = owner
98 return tarinfo
99
Tarek Ziadé77c8b372009-05-28 13:01:13 +0000100 if not dry_run:
101 tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
102 try:
Tarek Ziadé05b30342009-10-02 23:56:02 +0000103 tar.add(base_dir, filter=_set_uid_gid)
Tarek Ziadé77c8b372009-05-28 13:01:13 +0000104 finally:
105 tar.close()
106
107 # compression using `compress`
108 if compress == 'compress':
109 warn("'compress' will be deprecated.", PendingDeprecationWarning)
110 # the option varies depending on the platform
111 compressed_name = archive_name + compress_ext[compress]
112 if sys.platform == 'win32':
113 cmd = [compress, archive_name, compressed_name]
114 else:
115 cmd = [compress, '-f', archive_name]
116 spawn(cmd, dry_run=dry_run)
117 return compressed_name
118
119 return archive_name
Greg Wardaebf7062000-04-04 02:05:59 +0000120
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000121def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
122 """Create a zip file from all the files under 'base_dir'.
Greg Wardaebf7062000-04-04 02:05:59 +0000123
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000124 The output zip file will be named 'base_dir' + ".zip". Uses either the
125 "zipfile" Python module (if available) or the InfoZIP "zip" utility
126 (if installed and found on the default search path). If neither tool is
127 available, raises DistutilsExecError. Returns the name of the output zip
128 file.
Greg Wardca4289f2000-09-26 02:13:49 +0000129 """
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000130 try:
131 import zipfile
132 except ImportError:
133 zipfile = None
Tim Peters182b5ac2004-07-18 06:16:08 +0000134
Greg Wardaebf7062000-04-04 02:05:59 +0000135 zip_filename = base_name + ".zip"
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000136 mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +0000137
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000138 # If zipfile module is not available, try spawning an external
139 # 'zip' command.
140 if zipfile is None:
141 if verbose:
142 zipoptions = "-r"
143 else:
144 zipoptions = "-rq"
Tim Peters182b5ac2004-07-18 06:16:08 +0000145
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000146 try:
147 spawn(["zip", zipoptions, zip_filename, base_dir],
148 dry_run=dry_run)
149 except DistutilsExecError:
150 # XXX really should distinguish between "couldn't find
151 # external 'zip' command" and "zip failed".
Collin Winter5b7e9d72007-08-30 03:52:21 +0000152 raise DistutilsExecError(("unable to create zip file '%s': "
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000153 "could neither import the 'zipfile' module nor "
Collin Winter5b7e9d72007-08-30 03:52:21 +0000154 "find a standalone zip utility") % zip_filename)
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000155
156 else:
157 log.info("creating '%s' and adding '%s' to it",
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000158 zip_filename, base_dir)
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000159
Greg Wardaebf7062000-04-04 02:05:59 +0000160 if not dry_run:
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000161 zip = zipfile.ZipFile(zip_filename, "w",
162 compression=zipfile.ZIP_DEFLATED)
Greg Wardaebf7062000-04-04 02:05:59 +0000163
Benjamin Peterson699adb92008-05-08 22:27:58 +0000164 for dirpath, dirnames, filenames in os.walk(base_dir):
165 for name in filenames:
166 path = os.path.normpath(os.path.join(dirpath, name))
167 if os.path.isfile(path):
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000168 zip.write(path, path)
Benjamin Peterson699adb92008-05-08 22:27:58 +0000169 log.info("adding '%s'" % path)
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000170 zip.close()
Greg Wardaebf7062000-04-04 02:05:59 +0000171
172 return zip_filename
173
Greg Warddb807542000-04-22 03:09:56 +0000174ARCHIVE_FORMATS = {
Greg Ward2ff78872000-06-24 00:23:20 +0000175 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
176 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
177 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
178 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
Greg Ward04e25a12000-08-22 01:48:54 +0000179 'zip': (make_zipfile, [],"ZIP file")
Greg Warddb807542000-04-22 03:09:56 +0000180 }
181
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000182def check_archive_formats(formats):
183 """Returns the first format from the 'format' list that is unknown.
184
185 If all formats are known, returns None
186 """
Greg Warddb807542000-04-22 03:09:56 +0000187 for format in formats:
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000188 if format not in ARCHIVE_FORMATS:
Greg Warddb807542000-04-22 03:09:56 +0000189 return format
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000190 return None
Greg Warddb807542000-04-22 03:09:56 +0000191
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000192def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
Tarek Ziadé05b30342009-10-02 23:56:02 +0000193 dry_run=0, owner=None, group=None):
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000194 """Create an archive file (eg. zip or tar).
195
196 'base_name' is the name of the file to create, minus any format-specific
197 extension; 'format' is the archive format: one of "zip", "tar", "ztar",
198 or "gztar".
199
Greg Wardaebf7062000-04-04 02:05:59 +0000200 'root_dir' is a directory that will be the root directory of the
201 archive; ie. we typically chdir into 'root_dir' before creating the
202 archive. 'base_dir' is the directory where we start archiving from;
203 ie. 'base_dir' will be the common prefix of all files and
204 directories in the archive. 'root_dir' and 'base_dir' both default
Greg Ward87909612000-06-01 01:07:55 +0000205 to the current directory. Returns the name of the archive file.
Tarek Ziadé05b30342009-10-02 23:56:02 +0000206
207 'owner' and 'group' are used when creating a tar archive. By default,
208 uses the current owner and group.
Greg Ward87909612000-06-01 01:07:55 +0000209 """
Greg Wardaebf7062000-04-04 02:05:59 +0000210 save_cwd = os.getcwd()
211 if root_dir is not None:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000212 log.debug("changing into '%s'", root_dir)
Greg Wardca4289f2000-09-26 02:13:49 +0000213 base_name = os.path.abspath(base_name)
Greg Wardaebf7062000-04-04 02:05:59 +0000214 if not dry_run:
Greg Wardca4289f2000-09-26 02:13:49 +0000215 os.chdir(root_dir)
Greg Wardaebf7062000-04-04 02:05:59 +0000216
217 if base_dir is None:
218 base_dir = os.curdir
219
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000220 kwargs = {'dry_run': dry_run}
Fred Drakeb94b8492001-12-06 20:51:35 +0000221
Greg Warddb807542000-04-22 03:09:56 +0000222 try:
223 format_info = ARCHIVE_FORMATS[format]
224 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000225 raise ValueError("unknown archive format '%s'" % format)
Greg Wardaebf7062000-04-04 02:05:59 +0000226
Greg Warddb807542000-04-22 03:09:56 +0000227 func = format_info[0]
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000228 for arg, val in format_info[1]:
Greg Warddb807542000-04-22 03:09:56 +0000229 kwargs[arg] = val
Tarek Ziadé05b30342009-10-02 23:56:02 +0000230
231 if format != 'zip':
232 kwargs['owner'] = owner
233 kwargs['group'] = group
234
Neal Norwitzd9108552006-03-17 08:00:19 +0000235 filename = func(base_name, base_dir, **kwargs)
Greg Wardaebf7062000-04-04 02:05:59 +0000236
237 if root_dir is not None:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000238 log.debug("changing back to '%s'", save_cwd)
Greg Wardca4289f2000-09-26 02:13:49 +0000239 os.chdir(save_cwd)
Greg Wardaebf7062000-04-04 02:05:59 +0000240
Greg Ward87909612000-06-01 01:07:55 +0000241 return filename