Skip to content

Commit f1bf9e2

Browse files
author
Johannes Holmberg
committed
fixup! pythongh-132933: Allow zipapp target to overwrite filtered source file
1 parent fddf365 commit f1bf9e2

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Lib/zipapp.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ def create_archive(source, target=None, interpreter=None, main=None,
135135
# the target is being created in the source directory - we
136136
# don't want the target being added to itself
137137
files_to_add = sorted(source.rglob('*'))
138+
if filter:
139+
files_to_add = {f: f2 for f in files_to_add
140+
if filter(f2 := f.relative_to(source))}
141+
else:
142+
files_to_add = {f: f.relative_to(source) for f in files_to_add}
138143

139144
# The target cannot be in the list of files to add. If it were, we'd
140145
# end up overwriting the source file and writing the archive into
@@ -151,22 +156,17 @@ def create_archive(source, target=None, interpreter=None, main=None,
151156
# equal to any of the entries in files_to_add, so there's no need
152157
# to add a special check for that.
153158
if target in files_to_add:
154-
if filter:
155-
arcname = target.relative_to(source)
156-
not_filtered = filter(arcname)
157-
if not filter or not_filtered:
158-
raise ZipAppError(f"The target archive {target} overwrites "
159-
"one of the source files.")
159+
raise ZipAppError(
160+
f"The target archive {target} overwrites one of the source files.")
161+
160162

161163
with _maybe_open(target, 'wb') as fd:
162164
_write_file_prefix(fd, interpreter)
163165
compression = (zipfile.ZIP_DEFLATED if compressed else
164166
zipfile.ZIP_STORED)
165167
with zipfile.ZipFile(fd, 'w', compression=compression) as z:
166-
for child in files_to_add:
167-
arcname = child.relative_to(source)
168-
if filter is None or filter(arcname):
169-
z.write(child, arcname.as_posix())
168+
for child, arcname in files_to_add.items():
169+
z.write(child, arcname.as_posix())
170170
if main_py:
171171
z.writestr('__main__.py', main_py.encode('utf-8'))
172172

0 commit comments

Comments
 (0)