summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-09-08 02:51:00 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-09-08 02:51:00 (GMT)
commitd941d7a586c8ea2f8253d7b238b3a56522b77a83 (patch)
tree3298e5e5255c20805314bd0d8887d3f3f4cb712e
parent6c8b66cd261c6418566700527784b17bb459db1f (diff)
parent2504cecebdff16588d7b753843f4a856c510851e (diff)
downloadcpython-d941d7a586c8ea2f8253d7b238b3a56522b77a83.zip
cpython-d941d7a586c8ea2f8253d7b238b3a56522b77a83.tar.gz
cpython-d941d7a586c8ea2f8253d7b238b3a56522b77a83.tar.bz2
Issue #24982: shutil.make_archive() with the "zip" format now adds entries
for directories (including empty directories) in ZIP file. Added test for comparing shutil.make_archive() with the "zip" command.
-rw-r--r--Lib/shutil.py9
-rw-r--r--Lib/test/test_shutil.py37
-rw-r--r--Misc/NEWS3
3 files changed, 43 insertions, 6 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py
index a5da587..3f4b6bf 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -679,7 +679,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):
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 869aa2c..bb073c9 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1070,15 +1070,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.assertCountEqual(zf.namelist(),
- ['dist/file1', 'dist/file2', 'dist/sub/file3'])
+ ['dist/', 'dist/sub/', 'dist/sub2/',
+ 'dist/file1', 'dist/file2', 'dist/sub/file3'])
+ @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()
@@ -1191,11 +1218,9 @@ class TestShutil(unittest.TestCase):
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 --git a/Misc/NEWS b/Misc/NEWS
index 8b4c859..31acc9f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -14,6 +14,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 #25019: Fixed a crash caused by setting non-string key of expat parser.
Based on patch by John Leitch.