bpo-31072: Add filter to zipapp (#3021)

bpo-31072: Add a filter argument to zipapp.create_archive (GH-3021)

* Add an include_file argument to allow callers to decide which files to include
* Document the new argument
This commit is contained in:
Jeffrey Rackauckas 2017-08-09 06:37:17 -07:00 committed by Paul Moore
parent 9b0d1d647e
commit b811d664de
5 changed files with 29 additions and 3 deletions

View file

@ -73,7 +73,8 @@ def _copy_archive(archive, new_archive, interpreter=None):
os.chmod(new_archive, os.stat(new_archive).st_mode | stat.S_IEXEC)
def create_archive(source, target=None, interpreter=None, main=None):
def create_archive(source, target=None, interpreter=None, main=None,
include_file=None):
"""Create an application archive from SOURCE.
The SOURCE can be the name of a directory, or a filename or a file-like
@ -135,7 +136,8 @@ def create_archive(source, target=None, interpreter=None, main=None):
with zipfile.ZipFile(fd, 'w') as z:
for child in source.rglob('*'):
arcname = child.relative_to(source).as_posix()
z.write(child, arcname)
if include_file is None or include_file(pathlib.Path(arcname)):
z.write(child, arcname)
if main_py:
z.writestr('__main__.py', main_py.encode('utf-8'))