Skip to content

Commit d1c73ac

Browse files
CPython Developersyouknowone
authored andcommitted
Update zipapp from v3.14.3
1 parent 5ab631d commit d1c73ac

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

Lib/test/test_zipapp.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ def test_target_overwrites_source_file(self):
113113
with self.assertRaises(zipapp.ZipAppError):
114114
zipapp.create_archive(source, target)
115115

116+
def test_target_overwrites_filtered_source_file(self):
117+
# If there's a filter that excludes the target,
118+
# the overwrite check shouldn't trigger.
119+
source = self.tmpdir
120+
(source / '__main__.py').touch()
121+
target = source / 'target.pyz'
122+
target.touch()
123+
pyz_filter = lambda p: not p.match('*.pyz')
124+
zipapp.create_archive(source, target, filter=pyz_filter)
125+
with zipfile.ZipFile(target, 'r') as z:
126+
self.assertEqual(len(z.namelist()), 1)
127+
self.assertIn('__main__.py', z.namelist())
128+
116129
def test_create_archive_filter_exclude_dir(self):
117130
# Test packing a directory and using a filter to exclude a
118131
# subdirectory (ensures that the path supplied to include
@@ -246,7 +259,7 @@ def test_pack_to_fileobj(self):
246259
(source / '__main__.py').touch()
247260
target = io.BytesIO()
248261
zipapp.create_archive(str(source), target, interpreter='python')
249-
self.assertTrue(target.getvalue().startswith(b'#!python\n'))
262+
self.assertStartsWith(target.getvalue(), b'#!python\n')
250263

251264
def test_read_shebang(self):
252265
# Test that we can read the shebang line correctly.
@@ -287,7 +300,7 @@ def test_write_shebang_to_fileobj(self):
287300
zipapp.create_archive(str(source), str(target), interpreter='python')
288301
new_target = io.BytesIO()
289302
zipapp.create_archive(str(target), new_target, interpreter='python2.7')
290-
self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n'))
303+
self.assertStartsWith(new_target.getvalue(), b'#!python2.7\n')
291304

292305
def test_read_from_pathlike_obj(self):
293306
# Test that we can copy an archive using a path-like object
@@ -313,7 +326,7 @@ def test_read_from_fileobj(self):
313326
new_target = io.BytesIO()
314327
temp_archive.seek(0)
315328
zipapp.create_archive(temp_archive, new_target, interpreter='python2.7')
316-
self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n'))
329+
self.assertStartsWith(new_target.getvalue(), b'#!python2.7\n')
317330

318331
def test_remove_shebang(self):
319332
# Test that we can remove the shebang from a file.

Lib/zipapp.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ def create_archive(source, target=None, interpreter=None, main=None,
134134
# Create the list of files to add to the archive now, in case
135135
# the target is being created in the source directory - we
136136
# don't want the target being added to itself
137-
files_to_add = sorted(source.rglob('*'))
137+
files_to_add = {}
138+
for path in sorted(source.rglob('*')):
139+
relative_path = path.relative_to(source)
140+
if filter is None or filter(relative_path):
141+
files_to_add[path] = relative_path
138142

139143
# The target cannot be in the list of files to add. If it were, we'd
140144
# end up overwriting the source file and writing the archive into
@@ -159,10 +163,8 @@ def create_archive(source, target=None, interpreter=None, main=None,
159163
compression = (zipfile.ZIP_DEFLATED if compressed else
160164
zipfile.ZIP_STORED)
161165
with zipfile.ZipFile(fd, 'w', compression=compression) as z:
162-
for child in files_to_add:
163-
arcname = child.relative_to(source)
164-
if filter is None or filter(arcname):
165-
z.write(child, arcname.as_posix())
166+
for path, relative_path in files_to_add.items():
167+
z.write(path, relative_path.as_posix())
166168
if main_py:
167169
z.writestr('__main__.py', main_py.encode('utf-8'))
168170

@@ -185,7 +187,7 @@ def main(args=None):
185187
"""
186188
import argparse
187189

188-
parser = argparse.ArgumentParser()
190+
parser = argparse.ArgumentParser(color=True)
189191
parser.add_argument('--output', '-o', default=None,
190192
help="The name of the output archive. "
191193
"Required if SOURCE is an archive.")

0 commit comments

Comments
 (0)