-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-132933: Allow zipapp target to overwrite filtered source file #132934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
fddf365
f1bf9e2
ff363e8
bc371c7
1a76a0d
0f8979f
3b20cf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,6 +135,11 @@ def create_archive(source, target=None, interpreter=None, main=None, | |
| # the target is being created in the source directory - we | ||
| # don't want the target being added to itself | ||
| files_to_add = sorted(source.rglob('*')) | ||
| if filter: | ||
| files_to_add = {f: f2 for f in files_to_add | ||
| if filter(f2 := f.relative_to(source))} | ||
| else: | ||
| files_to_add = {f: f.relative_to(source) for f in files_to_add} | ||
|
||
|
|
||
| # The target cannot be in the list of files to add. If it were, we'd | ||
| # end up overwriting the source file and writing the archive into | ||
|
|
@@ -154,15 +159,14 @@ def create_archive(source, target=None, interpreter=None, main=None, | |
| raise ZipAppError( | ||
| f"The target archive {target} overwrites one of the source files.") | ||
|
|
||
|
|
||
|
||
| with _maybe_open(target, 'wb') as fd: | ||
| _write_file_prefix(fd, interpreter) | ||
| compression = (zipfile.ZIP_DEFLATED if compressed else | ||
| zipfile.ZIP_STORED) | ||
| with zipfile.ZipFile(fd, 'w', compression=compression) as z: | ||
| for child in files_to_add: | ||
| arcname = child.relative_to(source) | ||
| if filter is None or filter(arcname): | ||
| z.write(child, arcname.as_posix()) | ||
| for child, arcname in files_to_add.items(): | ||
| z.write(child, arcname.as_posix()) | ||
| if main_py: | ||
| z.writestr('__main__.py', main_py.encode('utf-8')) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The zipapp module now applies the provided filter to the list of files to be added before determining whether the target file overwrites a source file. This allows the target file to be written inside the source directory provided it is excluded by the filter. | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, copy/paste error!