Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 1 | """distutils.archive_util |
| 2 | |
| 3 | Utility functions for creating archive files (tarballs, zip files, |
| 4 | that sort of thing).""" |
| 5 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 6 | import os |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 7 | from warnings import warn |
| 8 | import sys |
| 9 | |
Antoine Pitrou | 2c50a09 | 2011-03-15 21:02:59 +0100 | [diff] [blame] | 10 | try: |
| 11 | import zipfile |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 12 | except ImportError: |
Antoine Pitrou | 2c50a09 | 2011-03-15 21:02:59 +0100 | [diff] [blame] | 13 | zipfile = None |
| 14 | |
| 15 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 16 | from distutils.errors import DistutilsExecError |
| 17 | from distutils.spawn import spawn |
Greg Ward | 04e25a1 | 2000-08-22 01:48:54 +0000 | [diff] [blame] | 18 | from distutils.dir_util import mkpath |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 19 | from distutils import log |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 20 | |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 21 | try: |
| 22 | from pwd import getpwnam |
Victor Stinner | aa32779 | 2013-11-15 23:13:17 +0100 | [diff] [blame] | 23 | except ImportError: |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 24 | getpwnam = None |
| 25 | |
| 26 | try: |
| 27 | from grp import getgrnam |
Victor Stinner | aa32779 | 2013-11-15 23:13:17 +0100 | [diff] [blame] | 28 | except ImportError: |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 29 | getgrnam = None |
| 30 | |
| 31 | def _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 | |
| 43 | def _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 | |
| 55 | def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, |
| 56 | owner=None, group=None): |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 57 | """Create a (possibly compressed) tar file from all the files under |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 58 | 'base_dir'. |
| 59 | |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 60 | 'compress' must be "gzip" (the default), "bzip2", "xz", "compress", or |
| 61 | None. ("compress" will be deprecated in Python 3.2) |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 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é | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 67 | The output tar file will be named 'base_dir' + ".tar", possibly plus |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 68 | the appropriate compression extension (".gz", ".bz2", ".xz" or ".Z"). |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 69 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 70 | Returns the output filename. |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 71 | """ |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 72 | tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', 'xz': 'xz', None: '', |
| 73 | 'compress': ''} |
| 74 | compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz', |
| 75 | 'compress': '.Z'} |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 76 | |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 77 | # flags for compression program, each element of list will be an argument |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 78 | if compress is not None and compress not in compress_ext.keys(): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 79 | raise ValueError( |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 80 | "bad value for 'compress': must be None, 'gzip', 'bzip2', " |
| 81 | "'xz' or 'compress'") |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 82 | |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 83 | archive_name = base_name + '.tar' |
| 84 | if compress != 'compress': |
| 85 | archive_name += compress_ext.get(compress, '') |
| 86 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 87 | mkpath(os.path.dirname(archive_name), dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 88 | |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 89 | # creating the tarball |
| 90 | import tarfile # late import so Python build itself doesn't break |
| 91 | |
| 92 | log.info('Creating tar archive') |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 93 | |
| 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é | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 106 | if not dry_run: |
| 107 | tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress]) |
| 108 | try: |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 109 | tar.add(base_dir, filter=_set_uid_gid) |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 110 | 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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 126 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 127 | def make_zipfile(base_name, base_dir, verbose=0, dry_run=0): |
| 128 | """Create a zip file from all the files under 'base_dir'. |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 129 | |
Éric Araujo | 7e2e321 | 2010-12-15 20:30:51 +0000 | [diff] [blame] | 130 | The output zip file will be named 'base_name' + ".zip". Uses either the |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 131 | "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 Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 135 | """ |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 136 | zip_filename = base_name + ".zip" |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 137 | mkpath(os.path.dirname(zip_filename), dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 138 | |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 139 | # 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 Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 146 | |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 147 | 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 Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 153 | raise DistutilsExecError(("unable to create zip file '%s': " |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 154 | "could neither import the 'zipfile' module nor " |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 155 | "find a standalone zip utility") % zip_filename) |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 156 | |
| 157 | else: |
| 158 | log.info("creating '%s' and adding '%s' to it", |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 159 | zip_filename, base_dir) |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 160 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 161 | if not dry_run: |
Antoine Pitrou | 2c50a09 | 2011-03-15 21:02:59 +0100 | [diff] [blame] | 162 | 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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 168 | |
Serhiy Storchaka | c5d5dfd | 2018-12-20 19:00:14 +0200 | [diff] [blame] | 169 | with zip: |
| 170 | if base_dir != os.curdir: |
| 171 | path = os.path.normpath(os.path.join(base_dir, '')) |
Serhiy Storchaka | 67a93b3 | 2018-12-05 21:46:25 +0200 | [diff] [blame] | 172 | zip.write(path, path) |
| 173 | log.info("adding '%s'", path) |
Serhiy Storchaka | c5d5dfd | 2018-12-20 19:00:14 +0200 | [diff] [blame] | 174 | 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é | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 177 | zip.write(path, path) |
Vinay Sajip | dd917f8 | 2016-08-31 08:22:29 +0100 | [diff] [blame] | 178 | log.info("adding '%s'", path) |
Serhiy Storchaka | c5d5dfd | 2018-12-20 19:00:14 +0200 | [diff] [blame] | 179 | 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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 184 | |
| 185 | return zip_filename |
| 186 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 187 | ARCHIVE_FORMATS = { |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 188 | 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"), |
| 189 | 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"), |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 190 | 'xztar': (make_tarball, [('compress', 'xz')], "xz'ed tar-file"), |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 191 | 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"), |
| 192 | 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"), |
Greg Ward | 04e25a1 | 2000-08-22 01:48:54 +0000 | [diff] [blame] | 193 | 'zip': (make_zipfile, [],"ZIP file") |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 196 | def 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 Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 201 | for format in formats: |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 202 | if format not in ARCHIVE_FORMATS: |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 203 | return format |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 204 | return None |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 205 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 206 | def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 207 | dry_run=0, owner=None, group=None): |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 208 | """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 Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 211 | extension; 'format' is the archive format: one of "zip", "tar", "gztar", |
| 212 | "bztar", "xztar", or "ztar". |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 213 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 214 | '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 Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 219 | to the current directory. Returns the name of the archive file. |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 220 | |
| 221 | 'owner' and 'group' are used when creating a tar archive. By default, |
| 222 | uses the current owner and group. |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 223 | """ |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 224 | save_cwd = os.getcwd() |
| 225 | if root_dir is not None: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 226 | log.debug("changing into '%s'", root_dir) |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 227 | base_name = os.path.abspath(base_name) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 228 | if not dry_run: |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 229 | os.chdir(root_dir) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 230 | |
| 231 | if base_dir is None: |
| 232 | base_dir = os.curdir |
| 233 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 234 | kwargs = {'dry_run': dry_run} |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 235 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 236 | try: |
| 237 | format_info = ARCHIVE_FORMATS[format] |
| 238 | except KeyError: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 239 | raise ValueError("unknown archive format '%s'" % format) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 240 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 241 | func = format_info[0] |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 242 | for arg, val in format_info[1]: |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 243 | kwargs[arg] = val |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 244 | |
| 245 | if format != 'zip': |
| 246 | kwargs['owner'] = owner |
| 247 | kwargs['group'] = group |
| 248 | |
Tarek Ziadé | 53fdb18 | 2009-10-24 13:42:10 +0000 | [diff] [blame] | 249 | 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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 255 | |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 256 | return filename |