blob: 4470bb02225537bbfc21376464b571e0c6635b59 [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 +00006import os
Tarek Ziadé77c8b372009-05-28 13:01:13 +00007from warnings import warn
8import sys
9
Antoine Pitrou2c50a092011-03-15 21:02:59 +010010try:
11 import zipfile
Brett Cannoncd171c82013-07-04 17:43:24 -040012except ImportError:
Antoine Pitrou2c50a092011-03-15 21:02:59 +010013 zipfile = None
14
15
Greg Wardaebf7062000-04-04 02:05:59 +000016from distutils.errors import DistutilsExecError
17from distutils.spawn import spawn
Greg Ward04e25a12000-08-22 01:48:54 +000018from distutils.dir_util import mkpath
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000019from distutils import log
Greg Wardaebf7062000-04-04 02:05:59 +000020
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050021try:
22 from pwd import getpwnam
Victor Stinneraa327792013-11-15 23:13:17 +010023except ImportError:
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050024 getpwnam = None
25
26try:
27 from grp import getgrnam
Victor Stinneraa327792013-11-15 23:13:17 +010028except ImportError:
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050029 getgrnam = None
30
31def _get_gid(name):
32 """Returns a gid, given a group name."""
33 if getgrnam is None or name is None:
34 return None
35 try:
36 result = getgrnam(name)
37 except KeyError:
38 result = None
39 if result is not None:
40 return result[2]
41 return None
42
43def _get_uid(name):
44 """Returns an uid, given a user name."""
45 if getpwnam is None or name is None:
46 return None
47 try:
48 result = getpwnam(name)
49 except KeyError:
50 result = None
51 if result is not None:
52 return result[2]
53 return None
54
55def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
56 owner=None, group=None):
Greg Wardaebf7062000-04-04 02:05:59 +000057 """Create a (possibly compressed) tar file from all the files under
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +000058 'base_dir'.
59
60 'compress' must be "gzip" (the default), "compress", "bzip2", or None.
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050061 (compress will be deprecated in Python 3.2)
62
63 'owner' and 'group' can be used to define an owner and a group for the
64 archive that is being built. If not provided, the current owner and group
65 will be used.
66
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +000067 The output tar file will be named 'base_dir' + ".tar", possibly plus
68 the appropriate compression extension (".gz", ".bz2" or ".Z").
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050069
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +000070 Returns the output filename.
Greg Wardca4289f2000-09-26 02:13:49 +000071 """
Tarek Ziadé77c8b372009-05-28 13:01:13 +000072 tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: '', 'compress': ''}
73 compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'}
Fred Drakeb94b8492001-12-06 20:51:35 +000074
Greg Wardf1948782000-04-25 01:38:20 +000075 # flags for compression program, each element of list will be an argument
Greg Wardf1948782000-04-25 01:38:20 +000076 if compress is not None and compress not in compress_ext.keys():
Collin Winter5b7e9d72007-08-30 03:52:21 +000077 raise ValueError(
Tarek Ziadé77c8b372009-05-28 13:01:13 +000078 "bad value for 'compress': must be None, 'gzip', 'bzip2' "
79 "or 'compress'")
Greg Wardaebf7062000-04-04 02:05:59 +000080
Tarek Ziadé77c8b372009-05-28 13:01:13 +000081 archive_name = base_name + '.tar'
82 if compress != 'compress':
83 archive_name += compress_ext.get(compress, '')
84
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000085 mkpath(os.path.dirname(archive_name), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000086
Tarek Ziadé77c8b372009-05-28 13:01:13 +000087 # creating the tarball
88 import tarfile # late import so Python build itself doesn't break
89
90 log.info('Creating tar archive')
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050091
92 uid = _get_uid(owner)
93 gid = _get_gid(group)
94
95 def _set_uid_gid(tarinfo):
96 if gid is not None:
97 tarinfo.gid = gid
98 tarinfo.gname = group
99 if uid is not None:
100 tarinfo.uid = uid
101 tarinfo.uname = owner
102 return tarinfo
103
Tarek Ziadé77c8b372009-05-28 13:01:13 +0000104 if not dry_run:
105 tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
106 try:
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500107 tar.add(base_dir, filter=_set_uid_gid)
Tarek Ziadé77c8b372009-05-28 13:01:13 +0000108 finally:
109 tar.close()
110
111 # compression using `compress`
112 if compress == 'compress':
113 warn("'compress' will be deprecated.", PendingDeprecationWarning)
114 # the option varies depending on the platform
115 compressed_name = archive_name + compress_ext[compress]
116 if sys.platform == 'win32':
117 cmd = [compress, archive_name, compressed_name]
118 else:
119 cmd = [compress, '-f', archive_name]
120 spawn(cmd, dry_run=dry_run)
121 return compressed_name
122
123 return archive_name
Greg Wardaebf7062000-04-04 02:05:59 +0000124
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000125def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
126 """Create a zip file from all the files under 'base_dir'.
Greg Wardaebf7062000-04-04 02:05:59 +0000127
Éric Araujo7e2e3212010-12-15 20:30:51 +0000128 The output zip file will be named 'base_name' + ".zip". Uses either the
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000129 "zipfile" Python module (if available) or the InfoZIP "zip" utility
130 (if installed and found on the default search path). If neither tool is
131 available, raises DistutilsExecError. Returns the name of the output zip
132 file.
Greg Wardca4289f2000-09-26 02:13:49 +0000133 """
Greg Wardaebf7062000-04-04 02:05:59 +0000134 zip_filename = base_name + ".zip"
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000135 mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +0000136
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000137 # If zipfile module is not available, try spawning an external
138 # 'zip' command.
139 if zipfile is None:
140 if verbose:
141 zipoptions = "-r"
142 else:
143 zipoptions = "-rq"
Tim Peters182b5ac2004-07-18 06:16:08 +0000144
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000145 try:
146 spawn(["zip", zipoptions, zip_filename, base_dir],
147 dry_run=dry_run)
148 except DistutilsExecError:
149 # XXX really should distinguish between "couldn't find
150 # external 'zip' command" and "zip failed".
Collin Winter5b7e9d72007-08-30 03:52:21 +0000151 raise DistutilsExecError(("unable to create zip file '%s': "
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000152 "could neither import the 'zipfile' module nor "
Collin Winter5b7e9d72007-08-30 03:52:21 +0000153 "find a standalone zip utility") % zip_filename)
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000154
155 else:
156 log.info("creating '%s' and adding '%s' to it",
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000157 zip_filename, base_dir)
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000158
Greg Wardaebf7062000-04-04 02:05:59 +0000159 if not dry_run:
Antoine Pitrou2c50a092011-03-15 21:02:59 +0100160 try:
161 zip = zipfile.ZipFile(zip_filename, "w",
162 compression=zipfile.ZIP_DEFLATED)
163 except RuntimeError:
164 zip = zipfile.ZipFile(zip_filename, "w",
165 compression=zipfile.ZIP_STORED)
Greg Wardaebf7062000-04-04 02:05:59 +0000166
Benjamin Peterson699adb92008-05-08 22:27:58 +0000167 for dirpath, dirnames, filenames in os.walk(base_dir):
168 for name in filenames:
169 path = os.path.normpath(os.path.join(dirpath, name))
170 if os.path.isfile(path):
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000171 zip.write(path, path)
Benjamin Peterson699adb92008-05-08 22:27:58 +0000172 log.info("adding '%s'" % path)
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000173 zip.close()
Greg Wardaebf7062000-04-04 02:05:59 +0000174
175 return zip_filename
176
Greg Warddb807542000-04-22 03:09:56 +0000177ARCHIVE_FORMATS = {
Greg Ward2ff78872000-06-24 00:23:20 +0000178 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
179 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
180 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
181 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
Greg Ward04e25a12000-08-22 01:48:54 +0000182 'zip': (make_zipfile, [],"ZIP file")
Greg Warddb807542000-04-22 03:09:56 +0000183 }
184
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000185def check_archive_formats(formats):
186 """Returns the first format from the 'format' list that is unknown.
187
188 If all formats are known, returns None
189 """
Greg Warddb807542000-04-22 03:09:56 +0000190 for format in formats:
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000191 if format not in ARCHIVE_FORMATS:
Greg Warddb807542000-04-22 03:09:56 +0000192 return format
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000193 return None
Greg Warddb807542000-04-22 03:09:56 +0000194
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000195def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500196 dry_run=0, owner=None, group=None):
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000197 """Create an archive file (eg. zip or tar).
198
199 'base_name' is the name of the file to create, minus any format-specific
200 extension; 'format' is the archive format: one of "zip", "tar", "ztar",
201 or "gztar".
202
Greg Wardaebf7062000-04-04 02:05:59 +0000203 'root_dir' is a directory that will be the root directory of the
204 archive; ie. we typically chdir into 'root_dir' before creating the
205 archive. 'base_dir' is the directory where we start archiving from;
206 ie. 'base_dir' will be the common prefix of all files and
207 directories in the archive. 'root_dir' and 'base_dir' both default
Greg Ward87909612000-06-01 01:07:55 +0000208 to the current directory. Returns the name of the archive file.
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500209
210 'owner' and 'group' are used when creating a tar archive. By default,
211 uses the current owner and group.
Greg Ward87909612000-06-01 01:07:55 +0000212 """
Greg Wardaebf7062000-04-04 02:05:59 +0000213 save_cwd = os.getcwd()
214 if root_dir is not None:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000215 log.debug("changing into '%s'", root_dir)
Greg Wardca4289f2000-09-26 02:13:49 +0000216 base_name = os.path.abspath(base_name)
Greg Wardaebf7062000-04-04 02:05:59 +0000217 if not dry_run:
Greg Wardca4289f2000-09-26 02:13:49 +0000218 os.chdir(root_dir)
Greg Wardaebf7062000-04-04 02:05:59 +0000219
220 if base_dir is None:
221 base_dir = os.curdir
222
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000223 kwargs = {'dry_run': dry_run}
Fred Drakeb94b8492001-12-06 20:51:35 +0000224
Greg Warddb807542000-04-22 03:09:56 +0000225 try:
226 format_info = ARCHIVE_FORMATS[format]
227 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000228 raise ValueError("unknown archive format '%s'" % format)
Greg Wardaebf7062000-04-04 02:05:59 +0000229
Greg Warddb807542000-04-22 03:09:56 +0000230 func = format_info[0]
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000231 for arg, val in format_info[1]:
Greg Warddb807542000-04-22 03:09:56 +0000232 kwargs[arg] = val
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500233
234 if format != 'zip':
235 kwargs['owner'] = owner
236 kwargs['group'] = group
237
Tarek Ziadé53fdb182009-10-24 13:42:10 +0000238 try:
239 filename = func(base_name, base_dir, **kwargs)
240 finally:
241 if root_dir is not None:
242 log.debug("changing back to '%s'", save_cwd)
243 os.chdir(save_cwd)
Greg Wardaebf7062000-04-04 02:05:59 +0000244
Greg Ward87909612000-06-01 01:07:55 +0000245 return filename