-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow FlowDecorators, FlowMutators and StepMutators to add_to_package #3082
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
Open
romain-intel
wants to merge
5
commits into
master
Choose a base branch
from
romain/worktree/add_files
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f014a3
Allow FlowDecorators, FlowMutators and StepMutators to add_to_package
romain-intel 4dcc74f
Fix handling of user code (temporary)
romain-intel 5bb47c1
Set phase earlier; minor other fixes
romain-intel 5266cf2
Merge USER_CONTENT from add_to_package with user-code walker
romain-intel 2ef9783
Address comment
romain-intel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """ | ||
| Example FlowMutator that extends the set of file suffixes included in the code | ||
| package — mirrors the ``--package-suffixes`` CLI option as a decorator. | ||
|
|
||
| By default Metaflow's packaging walks the flow directory and includes files | ||
| whose suffixes are in ``DEFAULT_PACKAGE_SUFFIXES`` (``.py,.R,.RDS``) plus | ||
| whatever was passed via ``--package-suffixes``. This mutator lets a flow | ||
| author declare additional suffixes directly on the flow class:: | ||
|
|
||
| from metaflow import FlowSpec, step | ||
| from metaflow.plugins.package_suffixes_mutator import package_suffixes | ||
|
|
||
| @package_suffixes([".yaml", ".json"]) | ||
| class MyFlow(FlowSpec): | ||
| @step | ||
| def start(self): | ||
| ... | ||
|
|
||
| The mutator walks the flow directory itself and yields every file with a | ||
| matching suffix as ``USER_CONTENT``. The packaging layer deduplicates against | ||
| the files that the default walker already picked up, so files that are already | ||
| included (e.g. ``.py`` files) are not added twice. | ||
| """ | ||
|
|
||
| import inspect | ||
| import os | ||
| from typing import List, Union | ||
|
|
||
| from metaflow.packaging_sys import ContentType | ||
| from metaflow.packaging_sys.utils import walk | ||
| from metaflow.user_decorators.user_flow_decorator import FlowMutator | ||
|
|
||
|
|
||
| class package_suffixes(FlowMutator): | ||
| """Include additional file suffixes in the code package. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| suffixes : list of str or comma-separated str | ||
| Additional file suffixes to include (e.g. ``[".yaml", ".json"]`` or | ||
| ``".yaml,.json"``). Leading dots are optional; a suffix without a | ||
| leading dot is treated as an extension (``yaml`` → ``.yaml``). | ||
| """ | ||
|
|
||
| def init(self, suffixes: Union[List[str], str]): | ||
| if isinstance(suffixes, str): | ||
| suffixes = [s.strip() for s in suffixes.split(",") if s.strip()] | ||
| self._suffixes = tuple( | ||
| (s if s.startswith(".") else "." + s).lower() for s in suffixes | ||
| ) | ||
|
|
||
| def add_to_package(self): | ||
| if not self._suffixes: | ||
| return | ||
|
|
||
| try: | ||
| flow_file = inspect.getfile(self._flow_cls) | ||
| except (TypeError, OSError): | ||
| return | ||
| flow_dir = os.path.dirname(os.path.abspath(flow_file)) | ||
| if not flow_dir or not os.path.isdir(flow_dir): | ||
| return | ||
|
|
||
| def _filter(fname: str) -> bool: | ||
| lname = fname.lower() | ||
| return any(lname.endswith(sfx) for sfx in self._suffixes) | ||
|
|
||
| for file_path, arcname in walk(flow_dir + os.sep, file_filter=_filter): | ||
| yield (file_path, arcname, ContentType.USER_CONTENT) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what would this change when you moved it up?
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.
You can now use the phase in the external_init of mutators (for example)