]> granicus.if.org Git - python/commitdiff
Issue #24982: shutil.make_archive() with the "zip" format now adds entries
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 8 Sep 2015 02:51:00 +0000 (05:51 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 8 Sep 2015 02:51:00 +0000 (05:51 +0300)
for directories (including empty directories) in ZIP file.
Added test for comparing shutil.make_archive() with the "zip" command.

1  2 
Lib/shutil.py
Lib/test/test_shutil.py
Misc/NEWS

diff --cc Lib/shutil.py
index a5da58790ecf8ec7d18d00c455145fec6b2d888f,d767a0c94542a165dcae7a7a1198d80a03b8022e..3f4b6bf663ffbbde199aa5377637ba7ceb0066a7
@@@ -672,17 -670,30 +672,26 @@@ def _make_zipfile(base_name, base_dir, 
          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 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 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:
 -                path = os.path.normpath(base_dir)
 -                zf.write(path, path)
 -                if logger is not None:
 -                    logger.info("adding '%s'", path)
 -                for dirpath, dirnames, filenames in os.walk(base_dir):
 -                    for name in sorted(dirnames):
 -                        path = os.path.normpath(os.path.join(dirpath, name))
 +    if not dry_run:
 +        with zipfile.ZipFile(zip_filename, "w",
 +                             compression=zipfile.ZIP_DEFLATED) as zf:
++            path = os.path.normpath(base_dir)
++            zf.write(path, path)
++            if logger is not None:
++                logger.info("adding '%s'", path)
 +            for dirpath, dirnames, filenames in os.walk(base_dir):
++                for name in sorted(dirnames):
++                    path = os.path.normpath(os.path.join(dirpath, name))
++                    zf.write(path, path)
++                    if logger is not None:
++                        logger.info("adding '%s'", path)
 +                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)
index 869aa2cf9387f4c52b577f0c3252cdd5f51a08a7,04738d365b06913b5b40544eb38711c5949d62a3..bb073c909874bb8b6cd2e2f05a58ed49469e8a44
@@@ -1187,15 -1208,11 +1214,13 @@@ class TestShutil(unittest.TestCase)
          formats = ['tar', 'gztar', 'zip']
          if BZ2_SUPPORTED:
              formats.append('bztar')
 +        if LZMA_SUPPORTED:
 +            formats.append('xztar')
  
          root_dir, base_dir = self._create_files()
+         expected = rlistdir(root_dir)
+         expected.remove('outer')
          for format in formats:
-             expected = rlistdir(root_dir)
-             expected.remove('outer')
-             if format == 'zip':
-                 expected.remove('dist/sub2/')
              base_name = os.path.join(self.mkdtemp(), 'archive')
              filename = make_archive(base_name, format, root_dir, base_dir)
  
diff --cc Misc/NEWS
index 8b4c8594d7bdf74a65586fdeb3c81464ca0638e2,79faea9710a076d76cb8c2301beb2256884e3bd4..31acc9fc7aa7e3fb8be12b8e1d8451c862aaf4fd
+++ b/Misc/NEWS
@@@ -11,15 -10,6 +11,18 @@@ Release date: TB
  Core and Builtins
  -----------------
  
 +Library
 +-------
 +
++- Issue #24982: shutil.make_archive() with the "zip" format now adds entries
++  for directories (including empty directories) in ZIP file.
++
 +- Issue #25019: Fixed a crash caused by setting non-string key of expat parser.
 +  Based on patch by John Leitch.
 +
 +- Issue #16180: Exit pdb if file has syntax error, instead of trapping user
 +  in an infinite loop.  Patch by Xavier de Gaye.
 +
  - Issue #24891: Fix a race condition at Python startup if the file descriptor
    of stdin (0), stdout (1) or stderr (2) is closed while Python is creating
    sys.stdin, sys.stdout and sys.stderr objects. These attributes are now set