Skip to content

Commit b811d66

Browse files
jeffreyrackpfmoore
authored andcommitted
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
1 parent 9b0d1d6 commit b811d66

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

Doc/library/zipapp.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ Python API
9898
The module defines two convenience functions:
9999

100100

101-
.. function:: create_archive(source, target=None, interpreter=None, main=None)
101+
.. function:: create_archive(source, target=None, interpreter=None, main=None,
102+
include_file=None)
102103

103104
Create an application archive from *source*. The source can be any
104105
of the following:
@@ -143,6 +144,10 @@ The module defines two convenience functions:
143144
contain a ``__main__.py`` file, as otherwise the resulting archive
144145
would not be executable.
145146

147+
The *include_file* argument specifies a callback function that is passed the
148+
relative path to the file in order to determine which files to store when
149+
being called against a directory.
150+
146151
If a file object is specified for *source* or *target*, it is the
147152
caller's responsibility to close it after calling create_archive.
148153

Lib/test/test_zipapp.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ def test_create_archive_with_subdirs(self):
5353
self.assertIn('foo/', z.namelist())
5454
self.assertIn('bar/', z.namelist())
5555

56+
def test_create_archive_with_include_file(self):
57+
# Test packing a directory and using include_file to specify which files to include.
58+
def skip_pyc_files(file):
59+
return '.pyc' not in str(file)
60+
source = self.tmpdir / 'source'
61+
source.mkdir()
62+
(source / '__main__.py').touch()
63+
(source / 'test.py').touch()
64+
(source / 'test.pyc').touch()
65+
target = self.tmpdir / 'source.pyz'
66+
67+
zipapp.create_archive(source, target, include_file=skip_pyc_files)
68+
with zipfile.ZipFile(target, 'r') as z:
69+
self.assertIn('__main__.py', z.namelist())
70+
self.assertIn('test.py', z.namelist())
71+
self.assertNotIn('test.pyc', z.namelist())
72+
5673
def test_create_archive_default_target(self):
5774
# Test packing a directory to the default name.
5875
source = self.tmpdir / 'source'

Lib/zipapp.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def _copy_archive(archive, new_archive, interpreter=None):
7373
os.chmod(new_archive, os.stat(new_archive).st_mode | stat.S_IEXEC)
7474

7575

76-
def create_archive(source, target=None, interpreter=None, main=None):
76+
def create_archive(source, target=None, interpreter=None, main=None,
77+
include_file=None):
7778
"""Create an application archive from SOURCE.
7879
7980
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):
135136
with zipfile.ZipFile(fd, 'w') as z:
136137
for child in source.rglob('*'):
137138
arcname = child.relative_to(source).as_posix()
138-
z.write(child, arcname)
139+
if include_file is None or include_file(pathlib.Path(arcname)):
140+
z.write(child, arcname)
139141
if main_py:
140142
z.writestr('__main__.py', main_py.encode('utf-8'))
141143

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,7 @@ Brian Quinlan
12531253
Anders Qvist
12541254
Thomas Rachel
12551255
Ram Rachum
1256+
Jeffrey Rackauckas
12561257
Jérôme Radix
12571258
Burton Radons
12581259
Abhilash Raj
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add an ``include_file`` parameter to ``zipapp.create_archive()``

0 commit comments

Comments
 (0)