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