[Python-checkins] cpython: #20744: don't try running an external 'zip' in shutil.make_archive()

andrew.kuchling python-checkins at python.org
Thu Mar 20 21:11:33 CET 2014


http://hg.python.org/cpython/rev/681e20f8b717
changeset:   89889:681e20f8b717
user:        Andrew Kuchling <amk at amk.ca>
date:        Thu Mar 20 16:11:16 2014 -0400
summary:
  #20744: don't try running an external 'zip' in shutil.make_archive()

Instead we'll just use the stdlib zipfile module.  Patch by Derek Chiang

files:
  Lib/shutil.py |  55 ++++++++++----------------------------
  1 files changed, 15 insertions(+), 40 deletions(-)


diff --git a/Lib/shutil.py b/Lib/shutil.py
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -630,23 +630,6 @@
 
     return archive_name
 
-def _call_external_zip(base_dir, zip_filename, verbose=False, dry_run=False):
-    # XXX see if we want to keep an external call here
-    if verbose:
-        zipoptions = "-r"
-    else:
-        zipoptions = "-rq"
-    from distutils.errors import DistutilsExecError
-    from distutils.spawn import spawn
-    try:
-        spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
-    except DistutilsExecError:
-        # XXX really should distinguish between "couldn't find
-        # external 'zip' command" and "zip failed".
-        raise ExecError("unable to create zip file '%s': "
-            "could neither import the 'zipfile' module nor "
-            "find a standalone zip utility") % zip_filename
-
 def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
     """Create a zip file from all the files under 'base_dir'.
 
@@ -656,6 +639,8 @@
     available, raises ExecError.  Returns the name of the output zip
     file.
     """
+    import zipfile
+
     zip_filename = base_name + ".zip"
     archive_dir = os.path.dirname(base_name)
 
@@ -665,30 +650,20 @@
         if not dry_run:
             os.makedirs(archive_dir)
 
-    # If zipfile module is not available, try spawning an external 'zip'
-    # command.
-    try:
-        import zipfile
-    except ImportError:
-        zipfile = None
+    if logger is not None:
+        logger.info("creating '%s' and adding '%s' to it",
+                    zip_filename, base_dir)
 
-    if zipfile is None:
-        _call_external_zip(base_dir, zip_filename, verbose, dry_run)
-    else:
-        if logger is not None:
-            logger.info("creating '%s' and adding '%s' to it",
-                        zip_filename, base_dir)
-
-        if not dry_run:
-            with zipfile.ZipFile(zip_filename, "w",
-                                 compression=zipfile.ZIP_DEFLATED) as zf:
-                for dirpath, dirnames, filenames in os.walk(base_dir):
-                    for name in filenames:
-                        path = os.path.normpath(os.path.join(dirpath, name))
-                        if os.path.isfile(path):
-                            zf.write(path, path)
-                            if logger is not None:
-                                logger.info("adding '%s'", path)
+    if not dry_run:
+        with zipfile.ZipFile(zip_filename, "w",
+                             compression=zipfile.ZIP_DEFLATED) as zf:
+            for dirpath, dirnames, filenames in os.walk(base_dir):
+                for name in filenames:
+                    path = os.path.normpath(os.path.join(dirpath, name))
+                    if os.path.isfile(path):
+                        zf.write(path, path)
+                        if logger is not None:
+                            logger.info("adding '%s'", path)
 
     return zip_filename
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list