Skip to content
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

gh-117829: add --include and --exclude flag to zipapp #120537

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

VietThan
Copy link
Contributor

@VietThan VietThan commented Jun 15, 2024

Hi, I'm a first time contributor to Python. I saw issue gh-117829 is interesting and took a stab at it. Currently it's a Work in Progress PR for others to see if my direction is good. Will add tests soon.

@VietThan VietThan marked this pull request as ready for review August 17, 2024 03:39
(source / 'bin' / 'baz').touch()
(source / '__main__.py').touch()

args = [str(source), '--include-pattern', r'.*\.py', '--exclude-pattern', r'.*z.*']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can self.tmpdir contain a z? If so, I believe this exclude pattern would ignore all files (since AFAICS it's matched against absolute paths, not against paths relative to the source directory)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @godlygeek , note that this is fixed.

@@ -94,6 +94,17 @@ The following options are understood:
this case, any other options are ignored and SOURCE must be an archive, not a
directory.

.. option:: --include-pattern
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tian suggested --include and --exclude; what is the reason for the -pattern suffix?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, --include-pattern is more verbose without adding any significant benefit (IMO).

Comment on lines +78 to +79
filter=None, compressed=False, include_pattern=None,
exclude_pattern=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to directly translate the --include and --exclude CLI options into a filter function, thus leaving the create_archive API unchanged, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'm a strong -1 on changing the create_archive API for this. The functionality is already available there, this should be purely a CLI change.

@@ -316,6 +317,73 @@ def test_content_of_copied_archive(self):
with zipfile.ZipFile(new_target, 'r') as z:
self.assertEqual(set(z.namelist()), {'__main__.py'})

def test_create_archive_with_include_pattern(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't needed, as we shouldn't be changing create_archive.

self.assertEqual(zf.namelist(),
["__main__.py", "zed.py"])

def test_create_archive_with_exclude_pattern(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nor this one.

self.assertEqual(zf.namelist(),
[".DS_Store", "bin/", "bin/baz", "bin/qux"])

def test_create_archive_with_include_and_exclude_pattern(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nor this...

def test_cmdline_create_with_exclude_pattern(self):
source = self.tmpdir / 'source'
source.mkdir()
(source / '.DS_Store').touch()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use .DS_Store as a test file. The whole point of the issue that started this is that MacOS has a bad habit of randomly adding such files - let's not risk our tests interacting badly with that OS misfeature.

def test_cmdline_create_with_include_pattern(self):
source = self.tmpdir / 'source'
source.mkdir()
(source / '.DS_Store').touch()
Copy link
Member

@pfmoore pfmoore Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the comment below, let's not use .DS_Store as a test file.

def test_cmdline_create_with_include_and_exclude_pattern(self):
source = self.tmpdir / 'source'
source.mkdir()
(source / '.DS_Store').touch()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and here.

Can we not refactor these tests so they aren't so repetitive?

Copy link
Member

@pfmoore pfmoore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, are regex patterns the best UI here? Generally, people would (I imagine) expect glob patterns rather than regular expressions when filtering files.

@bedevere-app
Copy link

bedevere-app bot commented Mar 24, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants