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 | __revision__ = "$Id$" |
| 7 | |
| 8 | import os |
| 9 | from distutils.errors import DistutilsExecError |
| 10 | from distutils.spawn import spawn |
Greg Ward | 04e25a1 | 2000-08-22 01:48:54 +0000 | [diff] [blame] | 11 | from distutils.dir_util import mkpath |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 12 | from distutils import log |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 13 | |
| 14 | def make_tarball (base_name, base_dir, compress="gzip", |
| 15 | verbose=0, dry_run=0): |
| 16 | """Create a (possibly compressed) tar file from all the files under |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 17 | 'base_dir'. 'compress' must be "gzip" (the default), "compress", |
| 18 | "bzip2", or None. Both "tar" and the compression utility named by |
| 19 | 'compress' must be on the default program search path, so this is |
| 20 | probably Unix-specific. The output tar file will be named 'base_dir' + |
| 21 | ".tar", possibly plus the appropriate compression extension (".gz", |
| 22 | ".bz2" or ".Z"). Return the output filename. |
| 23 | """ |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 24 | # XXX GNU tar 1.13 has a nifty option to add a prefix directory. |
| 25 | # It's pretty new, though, so we certainly can't require it -- |
| 26 | # but it would be nice to take advantage of it to skip the |
| 27 | # "create a tree of hardlinks" step! (Would also be nice to |
| 28 | # detect GNU tar to use its 'z' option and save a step.) |
| 29 | |
| 30 | compress_ext = { 'gzip': ".gz", |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 31 | 'bzip2': '.bz2', |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 32 | 'compress': ".Z" } |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 33 | |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 34 | # flags for compression program, each element of list will be an argument |
| 35 | compress_flags = {'gzip': ["-f9"], |
| 36 | 'compress': ["-f"], |
| 37 | 'bzip2': ['-f9']} |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 38 | |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 39 | if compress is not None and compress not in compress_ext.keys(): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 40 | raise ValueError( |
| 41 | "bad value for 'compress': must be None, 'gzip', or 'compress'") |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 42 | |
| 43 | archive_name = base_name + ".tar" |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 44 | mkpath(os.path.dirname(archive_name), dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 45 | cmd = ["tar", "-cf", archive_name, base_dir] |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 46 | spawn(cmd, dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 47 | |
| 48 | if compress: |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 49 | spawn([compress] + compress_flags[compress] + [archive_name], |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 50 | dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 51 | return archive_name + compress_ext[compress] |
| 52 | else: |
| 53 | return archive_name |
| 54 | |
| 55 | # make_tarball () |
| 56 | |
| 57 | |
| 58 | def make_zipfile (base_name, base_dir, verbose=0, dry_run=0): |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 59 | """Create a zip file from all the files under 'base_dir'. The output |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 60 | zip file will be named 'base_dir' + ".zip". Uses either the "zipfile" |
| 61 | Python module (if available) or the InfoZIP "zip" utility (if installed |
| 62 | and found on the default search path). If neither tool is available, |
| 63 | raises DistutilsExecError. Returns the name of the output zip file. |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 64 | """ |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 65 | try: |
| 66 | import zipfile |
| 67 | except ImportError: |
| 68 | zipfile = None |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 69 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 70 | zip_filename = base_name + ".zip" |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 71 | mkpath(os.path.dirname(zip_filename), dry_run=dry_run) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 72 | |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 73 | # If zipfile module is not available, try spawning an external |
| 74 | # 'zip' command. |
| 75 | if zipfile is None: |
| 76 | if verbose: |
| 77 | zipoptions = "-r" |
| 78 | else: |
| 79 | zipoptions = "-rq" |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 80 | |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 81 | try: |
| 82 | spawn(["zip", zipoptions, zip_filename, base_dir], |
| 83 | dry_run=dry_run) |
| 84 | except DistutilsExecError: |
| 85 | # XXX really should distinguish between "couldn't find |
| 86 | # external 'zip' command" and "zip failed". |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 87 | raise DistutilsExecError(("unable to create zip file '%s': " |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 88 | "could neither import the 'zipfile' module nor " |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 89 | "find a standalone zip utility") % zip_filename) |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 90 | |
| 91 | else: |
| 92 | log.info("creating '%s' and adding '%s' to it", |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 93 | zip_filename, base_dir) |
Andrew M. Kuchling | cdd2157 | 2002-11-21 18:33:28 +0000 | [diff] [blame] | 94 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 95 | if not dry_run: |
Guido van Rossum | b61914d | 2001-04-14 16:17:00 +0000 | [diff] [blame] | 96 | z = zipfile.ZipFile(zip_filename, "w", |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 97 | compression=zipfile.ZIP_DEFLATED) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 98 | |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 99 | for dirpath, dirnames, filenames in os.walk(base_dir): |
| 100 | for name in filenames: |
| 101 | path = os.path.normpath(os.path.join(dirpath, name)) |
| 102 | if os.path.isfile(path): |
| 103 | z.write(path, path) |
| 104 | log.info("adding '%s'" % path) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 105 | z.close() |
| 106 | |
| 107 | return zip_filename |
| 108 | |
| 109 | # make_zipfile () |
| 110 | |
| 111 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 112 | ARCHIVE_FORMATS = { |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 113 | 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"), |
| 114 | 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"), |
| 115 | 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"), |
| 116 | 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"), |
Greg Ward | 04e25a1 | 2000-08-22 01:48:54 +0000 | [diff] [blame] | 117 | 'zip': (make_zipfile, [],"ZIP file") |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | def check_archive_formats (formats): |
| 121 | for format in formats: |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 122 | if format not in ARCHIVE_FORMATS: |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 123 | return format |
| 124 | else: |
| 125 | return None |
| 126 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 127 | def make_archive (base_name, format, |
| 128 | root_dir=None, base_dir=None, |
| 129 | verbose=0, dry_run=0): |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 130 | """Create an archive file (eg. zip or tar). 'base_name' is the name |
| 131 | of the file to create, minus any format-specific extension; 'format' |
| 132 | is the archive format: one of "zip", "tar", "ztar", or "gztar". |
| 133 | 'root_dir' is a directory that will be the root directory of the |
| 134 | archive; ie. we typically chdir into 'root_dir' before creating the |
| 135 | archive. 'base_dir' is the directory where we start archiving from; |
| 136 | ie. 'base_dir' will be the common prefix of all files and |
| 137 | directories in the archive. 'root_dir' and 'base_dir' both default |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 138 | to the current directory. Returns the name of the archive file. |
| 139 | """ |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 140 | save_cwd = os.getcwd() |
| 141 | if root_dir is not None: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 142 | log.debug("changing into '%s'", root_dir) |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 143 | base_name = os.path.abspath(base_name) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 144 | if not dry_run: |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 145 | os.chdir(root_dir) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 146 | |
| 147 | if base_dir is None: |
| 148 | base_dir = os.curdir |
| 149 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 150 | kwargs = { 'dry_run': dry_run } |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 151 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 152 | try: |
| 153 | format_info = ARCHIVE_FORMATS[format] |
| 154 | except KeyError: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 155 | raise ValueError("unknown archive format '%s'" % format) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 156 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 157 | func = format_info[0] |
| 158 | for (arg,val) in format_info[1]: |
| 159 | kwargs[arg] = val |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 160 | filename = func(base_name, base_dir, **kwargs) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 161 | |
| 162 | if root_dir is not None: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 163 | log.debug("changing back to '%s'", save_cwd) |
Greg Ward | ca4289f | 2000-09-26 02:13:49 +0000 | [diff] [blame] | 164 | os.chdir(save_cwd) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 165 | |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 166 | return filename |
| 167 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 168 | # make_archive () |