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 | |
| 60 | 'compress' must be "gzip" (the default), "compress", "bzip2", or None. |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 61 | (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é | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 67 | The output tar file will be named 'base_dir' + ".tar", possibly plus |
| 68 | the appropriate compression extension (".gz", ".bz2" 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 | """ |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 72 | tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: '', 'compress': ''} |
| 73 | compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'} |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 74 | |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 75 | # flags for compression program, each element of list will be an argument |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 76 | if compress is not None and compress not in compress_ext.keys(): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 77 | raise ValueError( |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 78 | "bad value for 'compress': must be None, 'gzip', 'bzip2' " |
| 79 | "or 'compress'") |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 80 | |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 81 | archive_name = base_name + '.tar' |
| 82 | if compress != 'compress': |
| 83 | archive_name += compress_ext.get(compress, '') |
| 84 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 85 | mkpath(os.path.dirname(archive_name), dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 86 | |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 87 | # creating the tarball |
| 88 | import tarfile # late import so Python build itself doesn't break |
| 89 | |
| 90 | log.info('Creating tar archive') |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 91 | |
| 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é | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 104 | if not dry_run: |
| 105 | tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress]) |
| 106 | try: |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 107 | tar.add(base_dir, filter=_set_uid_gid) |
Tarek Ziadé | 77c8b37 | 2009-05-28 13:01:13 +0000 | [diff] [blame] | 108 | 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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 124 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 125 | def make_zipfile(base_name, base_dir, verbose=0, dry_run=0): |
| 126 | """Create a zip file from all the files under 'base_dir'. |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 127 | |
Éric Araujo | 7e2e321 | 2010-12-15 20:30:51 +0000 | [diff] [blame] | 128 | 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] | 129 | "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 Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 133 | """ |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 134 | zip_filename = base_name + ".zip" |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 135 | mkpath(os.path.dirname(zip_filename), dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 136 | |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 137 | # 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 Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 144 | |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 145 | 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 Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 151 | raise DistutilsExecError(("unable to create zip file '%s': " |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 152 | "could neither import the 'zipfile' module nor " |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 153 | "find a standalone zip utility") % zip_filename) |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 154 | |
| 155 | else: |
| 156 | log.info("creating '%s' and adding '%s' to it", |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 157 | zip_filename, base_dir) |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 158 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 159 | if not dry_run: |
Antoine Pitrou | 2c50a09 | 2011-03-15 21:02:59 +0100 | [diff] [blame] | 160 | 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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 166 | |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 167 | 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é | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 171 | zip.write(path, path) |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 172 | log.info("adding '%s'" % path) |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 173 | zip.close() |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 174 | |
| 175 | return zip_filename |
| 176 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 177 | ARCHIVE_FORMATS = { |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 178 | '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 Ward | 04e25a1 | 2000-08-22 01:48:54 +0000 | [diff] [blame] | 182 | 'zip': (make_zipfile, [],"ZIP file") |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 185 | def 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 Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 190 | for format in formats: |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 191 | if format not in ARCHIVE_FORMATS: |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 192 | return format |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 193 | return None |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 194 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 195 | 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] | 196 | dry_run=0, owner=None, group=None): |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 197 | """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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 203 | '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 Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 208 | to the current directory. Returns the name of the archive file. |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 209 | |
| 210 | 'owner' and 'group' are used when creating a tar archive. By default, |
| 211 | uses the current owner and group. |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 212 | """ |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 213 | save_cwd = os.getcwd() |
| 214 | if root_dir is not None: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 215 | log.debug("changing into '%s'", root_dir) |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 216 | base_name = os.path.abspath(base_name) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 217 | if not dry_run: |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 218 | os.chdir(root_dir) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 219 | |
| 220 | if base_dir is None: |
| 221 | base_dir = os.curdir |
| 222 | |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 223 | kwargs = {'dry_run': dry_run} |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 224 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 225 | try: |
| 226 | format_info = ARCHIVE_FORMATS[format] |
| 227 | except KeyError: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 228 | raise ValueError("unknown archive format '%s'" % format) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 229 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 230 | func = format_info[0] |
Tarek Ziadé | eb5f27e | 2009-05-17 12:12:02 +0000 | [diff] [blame] | 231 | for arg, val in format_info[1]: |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 232 | kwargs[arg] = val |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 233 | |
| 234 | if format != 'zip': |
| 235 | kwargs['owner'] = owner |
| 236 | kwargs['group'] = group |
| 237 | |
Tarek Ziadé | 53fdb18 | 2009-10-24 13:42:10 +0000 | [diff] [blame] | 238 | 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 Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 244 | |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 245 | return filename |