blob: 565a3117b4b5e1a750bf2a4c9fdfa2d61381b0e2 [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
Serhiy Storchakac5d5dfd2018-12-20 19:00:14 +0200169 with zip:
170 if base_dir != os.curdir:
171 path = os.path.normpath(os.path.join(base_dir, ''))
Serhiy Storchaka67a93b32018-12-05 21:46:25 +0200172 zip.write(path, path)
173 log.info("adding '%s'", path)
Serhiy Storchakac5d5dfd2018-12-20 19:00:14 +0200174 for dirpath, dirnames, filenames in os.walk(base_dir):
175 for name in dirnames:
176 path = os.path.normpath(os.path.join(dirpath, name, ''))
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000177 zip.write(path, path)
Vinay Sajipdd917f82016-08-31 08:22:29 +0100178 log.info("adding '%s'", path)
Serhiy Storchakac5d5dfd2018-12-20 19:00:14 +0200179 for name in filenames:
180 path = os.path.normpath(os.path.join(dirpath, name))
181 if os.path.isfile(path):
182 zip.write(path, path)
183 log.info("adding '%s'", path)
Greg Wardaebf7062000-04-04 02:05:59 +0000184
185 return zip_filename
186
Greg Warddb807542000-04-22 03:09:56 +0000187ARCHIVE_FORMATS = {
Greg Ward2ff78872000-06-24 00:23:20 +0000188 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
189 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +0300190 'xztar': (make_tarball, [('compress', 'xz')], "xz'ed tar-file"),
Greg Ward2ff78872000-06-24 00:23:20 +0000191 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
192 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
Greg Ward04e25a12000-08-22 01:48:54 +0000193 'zip': (make_zipfile, [],"ZIP file")
Greg Warddb807542000-04-22 03:09:56 +0000194 }
195
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000196def check_archive_formats(formats):
197 """Returns the first format from the 'format' list that is unknown.
198
199 If all formats are known, returns None
200 """
Greg Warddb807542000-04-22 03:09:56 +0000201 for format in formats:
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000202 if format not in ARCHIVE_FORMATS:
Greg Warddb807542000-04-22 03:09:56 +0000203 return format
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000204 return None
Greg Warddb807542000-04-22 03:09:56 +0000205
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000206def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500207 dry_run=0, owner=None, group=None):
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000208 """Create an archive file (eg. zip or tar).
209
210 'base_name' is the name of the file to create, minus any format-specific
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +0300211 extension; 'format' is the archive format: one of "zip", "tar", "gztar",
212 "bztar", "xztar", or "ztar".
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000213
Greg Wardaebf7062000-04-04 02:05:59 +0000214 'root_dir' is a directory that will be the root directory of the
215 archive; ie. we typically chdir into 'root_dir' before creating the
216 archive. 'base_dir' is the directory where we start archiving from;
217 ie. 'base_dir' will be the common prefix of all files and
218 directories in the archive. 'root_dir' and 'base_dir' both default
Greg Ward87909612000-06-01 01:07:55 +0000219 to the current directory. Returns the name of the archive file.
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500220
221 'owner' and 'group' are used when creating a tar archive. By default,
222 uses the current owner and group.
Greg Ward87909612000-06-01 01:07:55 +0000223 """
Greg Wardaebf7062000-04-04 02:05:59 +0000224 save_cwd = os.getcwd()
225 if root_dir is not None:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000226 log.debug("changing into '%s'", root_dir)
Greg Wardca4289f2000-09-26 02:13:49 +0000227 base_name = os.path.abspath(base_name)
Greg Wardaebf7062000-04-04 02:05:59 +0000228 if not dry_run:
Greg Wardca4289f2000-09-26 02:13:49 +0000229 os.chdir(root_dir)
Greg Wardaebf7062000-04-04 02:05:59 +0000230
231 if base_dir is None:
232 base_dir = os.curdir
233
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000234 kwargs = {'dry_run': dry_run}
Fred Drakeb94b8492001-12-06 20:51:35 +0000235
Greg Warddb807542000-04-22 03:09:56 +0000236 try:
237 format_info = ARCHIVE_FORMATS[format]
238 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000239 raise ValueError("unknown archive format '%s'" % format)
Greg Wardaebf7062000-04-04 02:05:59 +0000240
Greg Warddb807542000-04-22 03:09:56 +0000241 func = format_info[0]
Tarek Ziadéeb5f27e2009-05-17 12:12:02 +0000242 for arg, val in format_info[1]:
Greg Warddb807542000-04-22 03:09:56 +0000243 kwargs[arg] = val
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500244
245 if format != 'zip':
246 kwargs['owner'] = owner
247 kwargs['group'] = group
248
Tarek Ziadé53fdb182009-10-24 13:42:10 +0000249 try:
250 filename = func(base_name, base_dir, **kwargs)
251 finally:
252 if root_dir is not None:
253 log.debug("changing back to '%s'", save_cwd)
254 os.chdir(save_cwd)
Greg Wardaebf7062000-04-04 02:05:59 +0000255
Greg Ward87909612000-06-01 01:07:55 +0000256 return filename