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 |
| 13 | |
| 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 | f194878 | 2000-04-25 01:38:20 +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.""" |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 24 | |
| 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" |
| 45 | cmd = ["tar", "-cf", archive_name, base_dir] |
| 46 | spawn (cmd, verbose=verbose, dry_run=dry_run) |
| 47 | |
| 48 | if compress: |
Greg Ward | f194878 | 2000-04-25 01:38:20 +0000 | [diff] [blame] | 49 | spawn ([compress] + compress_flags[compress] + [archive_name], |
| 50 | verbose=verbose, 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): |
| 59 | """Create a zip file from all the files under 'base_dir'. The |
| 60 | output zip file will be named 'base_dir' + ".zip". Uses either the |
| 61 | InfoZIP "zip" utility (if installed and found on the default search |
| 62 | path) or the "zipfile" Python module (if available). If neither |
| 63 | tool is available, raises DistutilsExecError. Returns the name |
| 64 | of the output zip file.""" |
| 65 | |
| 66 | # This initially assumed the Unix 'zip' utility -- but |
| 67 | # apparently InfoZIP's zip.exe works the same under Windows, so |
| 68 | # no changes needed! |
| 69 | |
| 70 | zip_filename = base_name + ".zip" |
| 71 | try: |
| 72 | spawn (["zip", "-rq", zip_filename, base_dir], |
| 73 | verbose=verbose, dry_run=dry_run) |
| 74 | except DistutilsExecError: |
| 75 | |
| 76 | # XXX really should distinguish between "couldn't find |
| 77 | # external 'zip' command" and "zip failed" -- shouldn't try |
| 78 | # again in the latter case. (I think fixing this will |
| 79 | # require some cooperation from the spawn module -- perhaps |
| 80 | # a utility function to search the path, so we can fallback |
| 81 | # on zipfile.py without the failed spawn.) |
| 82 | try: |
| 83 | import zipfile |
| 84 | except ImportError: |
| 85 | raise DistutilsExecError, \ |
| 86 | ("unable to create zip file '%s': " + |
| 87 | "could neither find a standalone zip utility nor " + |
| 88 | "import the 'zipfile' module") % zip_filename |
| 89 | |
| 90 | if verbose: |
| 91 | print "creating '%s' and adding '%s' to it" % \ |
| 92 | (zip_filename, base_dir) |
| 93 | |
| 94 | def visit (z, dirname, names): |
| 95 | for name in names: |
Greg Ward | 65bc20c | 2000-05-31 02:17:19 +0000 | [diff] [blame] | 96 | path = os.path.normpath(os.path.join(dirname, name)) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 97 | if os.path.isfile (path): |
| 98 | z.write (path, path) |
| 99 | |
| 100 | if not dry_run: |
| 101 | z = zipfile.ZipFile (zip_filename, "wb", |
| 102 | compression=zipfile.ZIP_DEFLATED) |
| 103 | |
| 104 | os.path.walk (base_dir, visit, z) |
| 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 | 9d17a7a | 2000-06-07 03:00:06 +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: |
| 122 | if not ARCHIVE_FORMATS.has_key(format): |
| 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: |
| 142 | if verbose: |
| 143 | print "changing into '%s'" % root_dir |
| 144 | base_name = os.path.abspath (base_name) |
| 145 | if not dry_run: |
| 146 | os.chdir (root_dir) |
| 147 | |
| 148 | if base_dir is None: |
| 149 | base_dir = os.curdir |
| 150 | |
| 151 | kwargs = { 'verbose': verbose, |
| 152 | 'dry_run': dry_run } |
| 153 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 154 | try: |
| 155 | format_info = ARCHIVE_FORMATS[format] |
| 156 | except KeyError: |
| 157 | raise ValueError, "unknown archive format '%s'" % format |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 158 | |
Greg Ward | db80754 | 2000-04-22 03:09:56 +0000 | [diff] [blame] | 159 | func = format_info[0] |
| 160 | for (arg,val) in format_info[1]: |
| 161 | kwargs[arg] = val |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 162 | filename = apply (func, (base_name, base_dir), kwargs) |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 163 | |
| 164 | if root_dir is not None: |
| 165 | if verbose: |
| 166 | print "changing back to '%s'" % save_cwd |
| 167 | os.chdir (save_cwd) |
| 168 | |
Greg Ward | 8790961 | 2000-06-01 01:07:55 +0000 | [diff] [blame] | 169 | return filename |
| 170 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 171 | # make_archive () |