-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
add filter to zipapp #75255
Comments
As briefly discussed on comp.lang.python, I propose to add an optional filter callback function to zipapp.create_archive. The function could perhaps work like the os.walk generator or maybe just lets you to return a simple boolean for every folder/file that it wants to include in the zip. My use case is that I sometimes don't want to include every file in the root folder into the zip file (I want to be able to skip temporary or irrelevant folders such as .git/.svn, .tox, .tmp and sometimes want to avoid including *.pyc/*.pyo files). Right now, I first have to manually clean up the folder before I can use zipapp.create_archive. (Instead of providing a filter callback fuction, another approach may be to provide your own dir/file generator instead, that fully replaces the internal file listing logic of zipapp.create_archive?) |
I'd propose an extra argument to zipapp.create_archive, include_file=None (feel free to bikeshed on the name). If the argument is not None, then it should be a callable which will be called with a pathlib.Path object for each file that's selected for inclusion in the archive. The function should return a boolean - False means don't include this file. Because the create_archive function only gets a list of files internally (it uses Path.rglob()), the callable won't get passed directories, only the actual files (but it can of course check the full path to see what directory the file is in). The include_file argument is ignored when copying anything other than a filesystem directory (i.e., when the source argument is a filename or an open file object). |
That sounds fine to me. I guess the paths passed to the function should be relative to the root folder being zipped? |
Yes, they can be. |
Thanks to Jeffrey Rackauckas for the implementation of this feature. |
Wouldn't be better to name the parameter filterfunc for conformity with PyZipFile? I think the new feature needs at least the versionadded directive in the module documentation. And may be an entry in the What's New document. |
Good point - I wasn't even aware of the filterfunc argument in PyZipFile. I'll rename the argument. I wasn't initially sure about a what's new entry. I'll add one - and thanks for the reminder about versionadded. |
I've created a new PR 3049 adding the fixes you suggested (and tightening up the tests, as I noticed an untested aspect of the change while editing). |
There are two differences between filterfunc arguments in zipapp and PyZipFile:
I afraid that these differences can cause confusions. |
Zipapp uses path objects throughout, so making the filter function take a path object is consistent with that. I guess modifying PyZipFile to take either a string or a path object would be possible. As for the relative path, that's deliberate as I expect that a common use case will be to exclude a directory, and doing that via
is a simple possibility. Having to do this with an absolute path would just require the user to make the path relative, and we've already done that in zipapp so why duplicate the work? So I guess I'm saying I want to keep both those choices. Do you think this is a sufficient problem that we should not use the same name? Any suggestions on a better name (or should we stick with the original |
What about simply 'filter' as a name? or 'path_filter'? |
Sounds reasonable :-) I'm not going to be checking mails for a week or so, so I'll revisit this once I get back. |
OK. There's been no further comments, and I think the differences with PyZipFile's filterfunc are sufficient to warrant using a different name. I'm going to go with "filter". It's short, and says what it means. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: