Skip to content

Commit a0934b2

Browse files
committed
#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
1 parent 8efe3df commit a0934b2

File tree

1 file changed

+15
-40
lines changed

1 file changed

+15
-40
lines changed

‎Lib/shutil.py‎

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -630,23 +630,6 @@ def _set_uid_gid(tarinfo):
630630

631631
return archive_name
632632

633-
def _call_external_zip(base_dir, zip_filename, verbose=False, dry_run=False):
634-
# XXX see if we want to keep an external call here
635-
if verbose:
636-
zipoptions = "-r"
637-
else:
638-
zipoptions = "-rq"
639-
from distutils.errors import DistutilsExecError
640-
from distutils.spawn import spawn
641-
try:
642-
spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
643-
except DistutilsExecError:
644-
# XXX really should distinguish between "couldn't find
645-
# external 'zip' command" and "zip failed".
646-
raise ExecError("unable to create zip file '%s': "
647-
"could neither import the 'zipfile' module nor "
648-
"find a standalone zip utility") % zip_filename
649-
650633
def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
651634
"""Create a zip file from all the files under 'base_dir'.
652635
@@ -656,6 +639,8 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
656639
available, raises ExecError. Returns the name of the output zip
657640
file.
658641
"""
642+
import zipfile
643+
659644
zip_filename = base_name + ".zip"
660645
archive_dir = os.path.dirname(base_name)
661646

@@ -665,30 +650,20 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
665650
if not dry_run:
666651
os.makedirs(archive_dir)
667652

668-
# If zipfile module is not available, try spawning an external 'zip'
669-
# command.
670-
try:
671-
import zipfile
672-
except ImportError:
673-
zipfile = None
674-
675-
if zipfile is None:
676-
_call_external_zip(base_dir, zip_filename, verbose, dry_run)
677-
else:
678-
if logger is not None:
679-
logger.info("creating '%s' and adding '%s' to it",
680-
zip_filename, base_dir)
653+
if logger is not None:
654+
logger.info("creating '%s' and adding '%s' to it",
655+
zip_filename, base_dir)
681656

682-
if not dry_run:
683-
with zipfile.ZipFile(zip_filename, "w",
684-
compression=zipfile.ZIP_DEFLATED) as zf:
685-
for dirpath, dirnames, filenames in os.walk(base_dir):
686-
for name in filenames:
687-
path = os.path.normpath(os.path.join(dirpath, name))
688-
if os.path.isfile(path):
689-
zf.write(path, path)
690-
if logger is not None:
691-
logger.info("adding '%s'", path)
657+
if not dry_run:
658+
with zipfile.ZipFile(zip_filename, "w",
659+
compression=zipfile.ZIP_DEFLATED) as zf:
660+
for dirpath, dirnames, filenames in os.walk(base_dir):
661+
for name in filenames:
662+
path = os.path.normpath(os.path.join(dirpath, name))
663+
if os.path.isfile(path):
664+
zf.write(path, path)
665+
if logger is not None:
666+
logger.info("adding '%s'", path)
692667

693668
return zip_filename
694669

0 commit comments

Comments
 (0)