Skip to content

Commit 67a93b3

Browse files
bpo-34738: Add directory entries in ZIP files created by distutils. (GH-9419)
1 parent 55f41e4 commit 67a93b3

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

‎Lib/distutils/archive_util.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,15 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
166166
zip = zipfile.ZipFile(zip_filename, "w",
167167
compression=zipfile.ZIP_STORED)
168168

169+
if base_dir != os.curdir:
170+
path = os.path.normpath(os.path.join(base_dir, ''))
171+
zip.write(path, path)
172+
log.info("adding '%s'", path)
169173
for dirpath, dirnames, filenames in os.walk(base_dir):
174+
for name in dirnames:
175+
path = os.path.normpath(os.path.join(dirpath, name, ''))
176+
zip.write(path, path)
177+
log.info("adding '%s'", path)
170178
for name in filenames:
171179
path = os.path.normpath(os.path.join(dirpath, name))
172180
if os.path.isfile(path):

‎Lib/distutils/tests/test_archive_util.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,13 @@ def _tarinfo(self, path):
122122
try:
123123
names = tar.getnames()
124124
names.sort()
125-
return tuple(names)
125+
return names
126126
finally:
127127
tar.close()
128128

129-
_created_files = ('dist', 'dist/file1', 'dist/file2',
130-
'dist/sub', 'dist/sub/file3', 'dist/sub2')
129+
_zip_created_files = ['dist/', 'dist/file1', 'dist/file2',
130+
'dist/sub/', 'dist/sub/file3', 'dist/sub2/']
131+
_created_files = [p.rstrip('/') for p in _zip_created_files]
131132

132133
def _create_files(self):
133134
# creating something to tar
@@ -244,8 +245,7 @@ def test_make_zipfile(self):
244245
tarball = base_name + '.zip'
245246
self.assertTrue(os.path.exists(tarball))
246247
with zipfile.ZipFile(tarball) as zf:
247-
self.assertEqual(sorted(zf.namelist()),
248-
['dist/file1', 'dist/file2', 'dist/sub/file3'])
248+
self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
249249

250250
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
251251
def test_make_zipfile_no_zlib(self):
@@ -271,8 +271,7 @@ def fake_zipfile(*a, **kw):
271271
[((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
272272
self.assertTrue(os.path.exists(tarball))
273273
with zipfile.ZipFile(tarball) as zf:
274-
self.assertEqual(sorted(zf.namelist()),
275-
['dist/file1', 'dist/file2', 'dist/sub/file3'])
274+
self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
276275

277276
def test_check_archive_formats(self):
278277
self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),

‎Lib/distutils/tests/test_bdist_dumb.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_simple_built(self):
8484
finally:
8585
fp.close()
8686

87-
contents = sorted(os.path.basename(fn) for fn in contents)
87+
contents = sorted(filter(None, map(os.path.basename, contents)))
8888
wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
8989
if not sys.dont_write_bytecode:
9090
wanted.append('foo.%s.pyc' % sys.implementation.cache_tag)

‎Lib/distutils/tests/test_sdist.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ def test_prune_file_list(self):
128128
zip_file.close()
129129

130130
# making sure everything has been pruned correctly
131-
self.assertEqual(len(content), 4)
131+
expected = ['', 'PKG-INFO', 'README', 'setup.py',
132+
'somecode/', 'somecode/__init__.py']
133+
self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected])
132134

133135
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
134136
@unittest.skipIf(find_executable('tar') is None,
@@ -226,7 +228,13 @@ def test_add_defaults(self):
226228
zip_file.close()
227229

228230
# making sure everything was added
229-
self.assertEqual(len(content), 12)
231+
expected = ['', 'PKG-INFO', 'README', 'buildout.cfg',
232+
'data/', 'data/data.dt', 'inroot.txt',
233+
'scripts/', 'scripts/script.py', 'setup.py',
234+
'some/', 'some/file.txt', 'some/other_file.txt',
235+
'somecode/', 'somecode/__init__.py', 'somecode/doc.dat',
236+
'somecode/doc.txt']
237+
self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected])
230238

231239
# checking the MANIFEST
232240
f = open(join(self.tmp_dir, 'MANIFEST'))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ZIP files created by :mod:`distutils` will now include entries for
2+
directories.

0 commit comments

Comments
 (0)