[Python-checkins] bpo-34738: Add directory entries in ZIP files created by distutils. (GH-9419)

Miss Islington (bot) webhook-mailer at python.org
Wed Dec 5 15:29:34 EST 2018


https://github.com/python/cpython/commit/53bed18d93233b030bb5b2637daf1b5e87548ef1
commit: 53bed18d93233b030bb5b2637daf1b5e87548ef1
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-12-05T12:29:31-08:00
summary:

bpo-34738: Add directory entries in ZIP files created by distutils. (GH-9419)

(cherry picked from commit 67a93b3a0b3814e97ef9d077b21325fc8ce351b2)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
A Misc/NEWS.d/next/Library/2018-09-19-16-51-04.bpo-34738.Pr3-iG.rst
M Lib/distutils/archive_util.py
M Lib/distutils/tests/test_archive_util.py
M Lib/distutils/tests/test_bdist_dumb.py
M Lib/distutils/tests/test_sdist.py

diff --git a/Lib/distutils/archive_util.py b/Lib/distutils/archive_util.py
index 78ae5757c36d..b002dc3b8456 100644
--- a/Lib/distutils/archive_util.py
+++ b/Lib/distutils/archive_util.py
@@ -166,7 +166,15 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
                 zip = zipfile.ZipFile(zip_filename, "w",
                                       compression=zipfile.ZIP_STORED)
 
+            if base_dir != os.curdir:
+                path = os.path.normpath(os.path.join(base_dir, ''))
+                zip.write(path, path)
+                log.info("adding '%s'", path)
             for dirpath, dirnames, filenames in os.walk(base_dir):
+                for name in dirnames:
+                    path = os.path.normpath(os.path.join(dirpath, name, ''))
+                    zip.write(path, path)
+                    log.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/distutils/tests/test_archive_util.py b/Lib/distutils/tests/test_archive_util.py
index 14ba4ca34b4a..e9aad0e40fd1 100644
--- a/Lib/distutils/tests/test_archive_util.py
+++ b/Lib/distutils/tests/test_archive_util.py
@@ -122,12 +122,13 @@ def _tarinfo(self, path):
         try:
             names = tar.getnames()
             names.sort()
-            return tuple(names)
+            return names
         finally:
             tar.close()
 
-    _created_files = ('dist', 'dist/file1', 'dist/file2',
-                      'dist/sub', 'dist/sub/file3', 'dist/sub2')
+    _zip_created_files = ['dist/', 'dist/file1', 'dist/file2',
+                          'dist/sub/', 'dist/sub/file3', 'dist/sub2/']
+    _created_files = [p.rstrip('/') for p in _zip_created_files]
 
     def _create_files(self):
         # creating something to tar
@@ -244,8 +245,7 @@ def test_make_zipfile(self):
         tarball = base_name + '.zip'
         self.assertTrue(os.path.exists(tarball))
         with zipfile.ZipFile(tarball) as zf:
-            self.assertEqual(sorted(zf.namelist()),
-                             ['dist/file1', 'dist/file2', 'dist/sub/file3'])
+            self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
 
     @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
     def test_make_zipfile_no_zlib(self):
@@ -271,8 +271,7 @@ def fake_zipfile(*a, **kw):
                          [((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
         self.assertTrue(os.path.exists(tarball))
         with zipfile.ZipFile(tarball) as zf:
-            self.assertEqual(sorted(zf.namelist()),
-                             ['dist/file1', 'dist/file2', 'dist/sub/file3'])
+            self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
 
     def test_check_archive_formats(self):
         self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
diff --git a/Lib/distutils/tests/test_bdist_dumb.py b/Lib/distutils/tests/test_bdist_dumb.py
index c8ccdc2383de..01a233bce37f 100644
--- a/Lib/distutils/tests/test_bdist_dumb.py
+++ b/Lib/distutils/tests/test_bdist_dumb.py
@@ -84,7 +84,7 @@ def test_simple_built(self):
         finally:
             fp.close()
 
-        contents = sorted(os.path.basename(fn) for fn in contents)
+        contents = sorted(filter(None, map(os.path.basename, contents)))
         wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
         if not sys.dont_write_bytecode:
             wanted.append('foo.%s.pyc' % sys.implementation.cache_tag)
diff --git a/Lib/distutils/tests/test_sdist.py b/Lib/distutils/tests/test_sdist.py
index 5444b815a8b2..23db1269591d 100644
--- a/Lib/distutils/tests/test_sdist.py
+++ b/Lib/distutils/tests/test_sdist.py
@@ -128,7 +128,9 @@ def test_prune_file_list(self):
             zip_file.close()
 
         # making sure everything has been pruned correctly
-        self.assertEqual(len(content), 4)
+        expected = ['', 'PKG-INFO', 'README', 'setup.py',
+                    'somecode/', 'somecode/__init__.py']
+        self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected])
 
     @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
     @unittest.skipIf(find_executable('tar') is None,
@@ -226,7 +228,13 @@ def test_add_defaults(self):
             zip_file.close()
 
         # making sure everything was added
-        self.assertEqual(len(content), 12)
+        expected = ['', 'PKG-INFO', 'README', 'buildout.cfg',
+                    'data/', 'data/data.dt', 'inroot.txt',
+                    'scripts/', 'scripts/script.py', 'setup.py',
+                    'some/', 'some/file.txt', 'some/other_file.txt',
+                    'somecode/', 'somecode/__init__.py', 'somecode/doc.dat',
+                    'somecode/doc.txt']
+        self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected])
 
         # checking the MANIFEST
         f = open(join(self.tmp_dir, 'MANIFEST'))
diff --git a/Misc/NEWS.d/next/Library/2018-09-19-16-51-04.bpo-34738.Pr3-iG.rst b/Misc/NEWS.d/next/Library/2018-09-19-16-51-04.bpo-34738.Pr3-iG.rst
new file mode 100644
index 000000000000..c3f402d39ae0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-09-19-16-51-04.bpo-34738.Pr3-iG.rst
@@ -0,0 +1,2 @@
+ZIP files created by :mod:`distutils` will now include entries for
+directories.



More information about the Python-checkins mailing list