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

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

index e78a5758ca0a58077f56e3bc3dc2d0a399ebfa42..26dcfde7bcbea703cbd6b32b2162aad8cada94a1 100644 (file)
@@ -449,7 +449,16 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
         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):
index 3cc6f2b9ef916e1624acb020705f6e5955ccdca7..5b82a2c05fae9b1580f83995e475fdaf3b6796f8 100644 (file)
@@ -476,15 +476,42 @@ class TestShutil(unittest.TestCase):
         base_name = os.path.join(work_dir, rel_base_name)
 
         with support.change_cwd(work_dir):
-            res = make_archive(rel_base_name, 'zip', root_dir, 'dist')
+            res = make_archive(rel_base_name, 'zip', root_dir, base_dir)
 
         self.assertEqual(res, base_name + '.zip')
         self.assertTrue(os.path.isfile(res))
         self.assertTrue(zipfile.is_zipfile(res))
         with zipfile.ZipFile(res) as zf:
             self.assertEqual(sorted(zf.namelist()),
-                             ['dist/file1', 'dist/file2', 'dist/sub/file3'])
+                    ['dist/', 'dist/file1', 'dist/file2',
+                     'dist/sub/', 'dist/sub/file3', 'dist/sub2/'])
 
+    @unittest.skipUnless(zlib, "Requires zlib")
+    @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
+    @unittest.skipUnless(find_executable('zip'),
+                         'Need the zip command to run')
+    def test_zipfile_vs_zip(self):
+        root_dir, base_dir = self._create_files()
+        base_name = os.path.join(self.mkdtemp(), 'archive')
+        archive = make_archive(base_name, 'zip', root_dir, base_dir)
+
+        # check if ZIP file  was created
+        self.assertEqual(archive, base_name + '.zip')
+        self.assertTrue(os.path.isfile(archive))
+
+        # now create another ZIP file using `zip`
+        archive2 = os.path.join(root_dir, 'archive2.zip')
+        zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir]
+        with support.change_cwd(root_dir):
+            spawn(zip_cmd)
+
+        self.assertTrue(os.path.isfile(archive2))
+        # let's compare both ZIP files
+        with zipfile.ZipFile(archive) as zf:
+            names = zf.namelist()
+        with zipfile.ZipFile(archive2) as zf:
+            names2 = zf.namelist()
+        self.assertEqual(sorted(names), sorted(names2))
 
     def test_make_archive(self):
         tmpdir = self.mkdtemp()
index 26e41c334c9d37094d42fa0c0c45b5b95770ccbd..b983f041a70b54ee17045c385845b10d42442c95 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@ 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 #17849: Raise a sensible exception if an invalid response is
   received for a HTTP tunnel request, as seen with some servers that
   do not support tunnelling.  Initial patch from Cory Benfield.