Commit 66ffcc64 authored by Andrew M. Kuchling's avatar Andrew M. Kuchling

Bug #639118 from Ollie Oldham: archiver should use zipfile before zip

        Previously archive_util.py attempted to spawn an
        external 'zip' program for the zip action, if this fails, an
        attempt to import zipfile.py is made...

        This bites folks who have 'old' or non-conforming zip
        programs on windows platforms.  This change tries the 'zipfile'
        module first, falling back to spawning a zip process if
        the module isn't available.
parent 6ff384d0
...@@ -59,38 +59,39 @@ def make_tarball (base_name, base_dir, compress="gzip", ...@@ -59,38 +59,39 @@ def make_tarball (base_name, base_dir, compress="gzip",
def make_zipfile (base_name, base_dir, verbose=0, dry_run=0): def make_zipfile (base_name, base_dir, verbose=0, dry_run=0):
"""Create a zip file from all the files under 'base_dir'. The output """Create a zip file from all the files under 'base_dir'. The output
zip file will be named 'base_dir' + ".zip". Uses either the InfoZIP zip file will be named 'base_dir' + ".zip". Uses either the "zipfile"
"zip" utility (if installed and found on the default search path) or Python module (if available) or the InfoZIP "zip" utility (if installed
the "zipfile" Python module (if available). If neither tool is and found on the default search path). If neither tool is available,
available, raises DistutilsExecError. Returns the name of the output raises DistutilsExecError. Returns the name of the output zip file.
zip file.
""" """
# This initially assumed the Unix 'zip' utility -- but try:
# apparently InfoZIP's zip.exe works the same under Windows, so import zipfile
# no changes needed! except ImportError:
zipfile = None
zip_filename = base_name + ".zip" zip_filename = base_name + ".zip"
mkpath(os.path.dirname(zip_filename), dry_run=dry_run) mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
# If zipfile module is not available, try spawning an external
# 'zip' command.
if zipfile is None:
if verbose:
zipoptions = "-r"
else:
zipoptions = "-rq"
try: try:
spawn(["zip", "-rq", zip_filename, base_dir], spawn(["zip", zipoptions, zip_filename, base_dir],
dry_run=dry_run) dry_run=dry_run)
except DistutilsExecError: except DistutilsExecError:
# XXX really should distinguish between "couldn't find # XXX really should distinguish between "couldn't find
# external 'zip' command" and "zip failed" -- shouldn't try # external 'zip' command" and "zip failed".
# again in the latter case. (I think fixing this will
# require some cooperation from the spawn module -- perhaps
# a utility function to search the path, so we can fallback
# on zipfile.py without the failed spawn.)
try:
import zipfile
except ImportError:
raise DistutilsExecError, \ raise DistutilsExecError, \
("unable to create zip file '%s': " + ("unable to create zip file '%s': "
"could neither find a standalone zip utility nor " + "could neither import the 'zipfile' module nor "
"import the 'zipfile' module") % zip_filename "find a standalone zip utility") % zip_filename
else:
log.info("creating '%s' and adding '%s' to it", log.info("creating '%s' and adding '%s' to it",
zip_filename, base_dir) zip_filename, base_dir)
...@@ -99,6 +100,7 @@ def make_zipfile (base_name, base_dir, verbose=0, dry_run=0): ...@@ -99,6 +100,7 @@ def make_zipfile (base_name, base_dir, verbose=0, dry_run=0):
path = os.path.normpath(os.path.join(dirname, name)) path = os.path.normpath(os.path.join(dirname, name))
if os.path.isfile(path): if os.path.isfile(path):
z.write(path, path) z.write(path, path)
log.info("adding '%s'" % path)
if not dry_run: if not dry_run:
z = zipfile.ZipFile(zip_filename, "w", z = zipfile.ZipFile(zip_filename, "w",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment