blob: bed1384900b1435c54c9f19d764113b666acd9d5 [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
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030060 'compress' must be "gzip" (the default), "bzip2", "xz", "compress", or
61 None. ("compress" will be deprecated in Python 3.2)
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050062
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
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030068 the appropriate compression extension (".gz", ".bz2", ".xz" 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 """
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030072 tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', 'xz': 'xz', None: '',
73 'compress': ''}
74 compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz',
75 'compress': '.Z'}
Fred Drakeb94b8492001-12-06 20:51:35 +000076
Greg Wardf1948782000-04-25 01:38:20 +000077 # flags for compression program, each element of list will be an argument
Greg Wardf1948782000-04-25 01:38:20 +000078 if compress is not None and compress not in compress_ext.keys():
Collin Winter5b7e9d72007-08-30 03:52:21 +000079 raise ValueError(
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030080 "bad value for 'compress': must be None, 'gzip', 'bzip2', "
81 "'xz' or 'compress'")
Greg Wardaebf7062000-04-04 02:05:59 +000082
Tarek Ziadé77c8b372009-05-28 13:01:13 +000083 archive_name = base_name + '.tar'
84 if compress != 'compress':
85 archive_name += compress_ext.get(compress, '')
86
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000087 mkpath(os.path.dirname(archive_name), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +000088
Tarek Ziadé77c8b372009-05-28 13:01:13 +000089 # creating the tarball
90 import tarfile # late import so Python build itself doesn't break
91
92 log.info('Creating tar archive')
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050093
94 uid = _get_uid(owner)
95 gid = _get_gid(group)
96
97 def _set_uid_gid(tarinfo):
98 if gid is not None:
99 tarinfo.gid = gid
100 tarinfo.gname = group
101 if uid is not None:
102 tarinfo.uid = uid
103 tarinfo.uname = owner
104 return tarinfo
105
Tarek Ziadé77c8b372009-05-28 13:01:13 +0000106 if not dry_run:
107 tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
108 try:
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500109 tar.add(base_dir, filter=_set_uid_gid)
Tarek Ziadé77c8b372009-05-28 13:01:13 +0000110 finally:
111 tar.close()
112
113 # compression using `compress`
114 if compress == 'compress':
115 warn("'compress' will be deprecated.", PendingDeprecationWarning)
116 # the option varies depending on the platform
117 compressed_name = archive_name + compress_ext[compress]
118 if sys.platform == 'win32':
119 cmd = [compress, archive_name, compressed_name]
120 else:
121 cmd = [compress, '-f', archive_name]
122 spawn(cmd, dry_run=dry_run)
123 return compressed_name
124
125 return archive_name
Greg Wardaebf7062000-04-04 02:05:59 +0000126
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000127def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
128 """Create a zip file from all the files under 'base_dir'.
Greg Wardaebf7062000-04-04 02:05:59 +0000129
Éric Araujo7e2e3212010-12-15 20:30:51 +0000130 The output zip file will be named 'base_name' + ".zip". Uses either the
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000131 "zipfile" Python module (if available) or the InfoZIP "zip" utility
132 (if installed and found on the default search path). If neither tool is
133 available, raises DistutilsExecError. Returns the name of the output zip
134 file.
Greg Wardca4289f2000-09-26 02:13:49 +0000135 """
Greg Wardaebf7062000-04-04 02:05:59 +0000136 zip_filename = base_name + ".zip"
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000137 mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
Greg Wardaebf7062000-04-04 02:05:59 +0000138
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000139 # If zipfile module is not available, try spawning an external
140 # 'zip' command.
141 if zipfile is None:
142 if verbose:
143 zipoptions = "-r"
144 else:
145 zipoptions = "-rq"
Tim Peters182b5ac2004-07-18 06:16:08 +0000146
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000147 try:
148 spawn(["zip", zipoptions, zip_filename, base_dir],
149 dry_run=dry_run)
150 except DistutilsExecError:
151 # XXX really should distinguish between "couldn't find
152 # external 'zip' command" and "zip failed".
Collin Winter5b7e9d72007-08-30 03:52:21 +0000153 raise DistutilsExecError(("unable to create zip file '%s': "
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000154 "could neither import the 'zipfile' module nor "
Collin Winter5b7e9d72007-08-30 03:52:21 +0000155 "find a standalone zip utility") % zip_filename)
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000156
157 else:
158 log.info("creating '%s' and adding '%s' to it",
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000159 zip_filename, base_dir)
Andrew M. Kuchlingcdd21572002-11-21 18:33:28 +0000160
Greg Wardaebf7062000-04-04 02:05:59 +0000161 if not dry_run:
Antoine Pitrou2c50a092011-03-15 21:02:59 +0100162 try:
163 zip = zipfile.ZipFile(zip_filename, "w",
164 compression=zipfile.ZIP_DEFLATED)
165 except RuntimeError:
166 zip = zipfile.ZipFile(zip_filename, "w",
167 compression=zipfile.ZIP_STORED)
Greg Wardaebf7062000-04-04 02:05:59 +0000168
Benjamin Peterson699adb92008-05-08 22:27:58 +0000169 for dirpath, dirnames, filenames in os.walk(base_dir):
170 for name in filenames:
171 path = os.path.normpath(os.path.join(dirpath, name))
172 if os.path.isfile(path):
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000173 zip.write(path, path)
Benjamin Peterson699adb92008-05-08 22:27:58 +0000174 log.info("adding '%s'" % path)
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000175 zip.close()
Greg Wardaebf7062000-04-04 02:05:59 +0000176
177 return zip_filename
178
Greg Warddb807542000-04-22 03:09:56 +0000179ARCHIVE_FORMATS = {
Greg Ward2ff78872000-06-24 00:23:20 +0000180 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
181 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +0300182 'xztar': (make_tarball, [('compress', 'xz')], "xz'ed tar-file"),
Greg Ward2ff78872000-06-24 00:23:20 +0000183 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
184 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
Greg Ward04e25a12000-08-22 01:48:54 +0000185 'zip': (make_zipfile, [],"ZIP file")
Greg Warddb807542000-04-22 03:09:56 +0000186 }
187
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000188def check_archive_formats(formats):
189 """Returns the first format from the 'format' list that is unknown.
190
191 If all formats are known, returns None
192 """
Greg Warddb807542000-04-22 03:09:56 +0000193 for format in formats:
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000194 if format not in ARCHIVE_FORMATS:
Greg Warddb807542000-04-22 03:09:56 +0000195 return format
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000196 return None
Greg Warddb807542000-04-22 03:09:56 +0000197
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000198def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500199 dry_run=0, owner=None, group=None):
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000200 """Create an archive file (eg. zip or tar).
201
202 'base_name' is the name of the file to create, minus any format-specific
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +0300203 extension; 'format' is the archive format: one of "zip", "tar", "gztar",
204 "bztar", "xztar", or "ztar".
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000205
Greg Wardaebf7062000-04-04 02:05:59 +0000206 'root_dir' is a directory that will be the root directory of the
207 archive; ie. we typically chdir into 'root_dir' before creating the
208 archive. 'base_dir' is the directory where we start archiving from;
209 ie. 'base_dir' will be the common prefix of all files and
210 directories in the archive. 'root_dir' and 'base_dir' both default
Greg Ward87909612000-06-01 01:07:55 +0000211 to the current directory. Returns the name of the archive file.
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500212
213 'owner' and 'group' are used when creating a tar archive. By default,
214 uses the current owner and group.
Greg Ward87909612000-06-01 01:07:55 +0000215 """
Greg Wardaebf7062000-04-04 02:05:59 +0000216 save_cwd = os.getcwd()
217 if root_dir is not None:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000218 log.debug("changing into '%s'", root_dir)
Greg Wardca4289f2000-09-26 02:13:49 +0000219 base_name = os.path.abspath(base_name)
Greg Wardaebf7062000-04-04 02:05:59 +0000220 if not dry_run:
Greg Wardca4289f2000-09-26 02:13:49 +0000221 os.chdir(root_dir)
Greg Wardaebf7062000-04-04 02:05:59 +0000222
223 if base_dir is None:
224 base_dir = os.curdir
225
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000226 kwargs = {'dry_run': dry_run}
Fred Drakeb94b8492001-12-06 20:51:35 +0000227
Greg Warddb807542000-04-22 03:09:56 +0000228 try:
229 format_info = ARCHIVE_FORMATS[format]
230 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000231 raise ValueError("unknown archive format '%s'" % format)
Greg Wardaebf7062000-04-04 02:05:59 +0000232
Greg Warddb807542000-04-22 03:09:56 +0000233 func = format_info[0]
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000234 for arg, val in format_info[1]:
Greg Warddb807542000-04-22 03:09:56 +0000235 kwargs[arg] = val
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500236
237 if format != 'zip':
238 kwargs['owner'] = owner
239 kwargs['group'] = group
240
Tarek Ziadé53fdb182009-10-24 13:42:10 +0000241 try:
242 filename = func(base_name, base_dir, **kwargs)
243 finally:
244 if root_dir is not None:
245 log.debug("changing back to '%s'", save_cwd)
246 os.chdir(save_cwd)
Greg Wardaebf7062000-04-04 02:05:59 +0000247
Greg Ward87909612000-06-01 01:07:55 +0000248 return filename