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