Repository navigation
Support loading entrypoints by passing a module instead of a function #58943
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc797a7
Add an utility module to load entry-points with whatever lib is avail…
s0undt3ch 108b479
Drop six usage from `salt/loader.py`
s0undt3ch f913dd2
Use `salt.utils.entrypoints.iter_entry_points()` instead
s0undt3ch 38c6356
Turn `VirtualEnv` into an attrs implementation. Add SaltVirtualEnv.
s0undt3ch 7a11d21
Include salt extensions in the versions report
s0undt3ch 4f2a9e8
Support loading entrypoints by passing a module instead of a function.
s0undt3ch d1dbadb
New style entry points can either return a package or a callable
s0undt3ch ab89cd5
Add tests for the loader entry points changes
s0undt3ch 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Salt's versions report `salt --versions-report` now includes all installed salt extensions into its versions report. |
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 @@ | ||
| Support loading entrypoints by passing a module instead of a function. |
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,81 @@ | ||
| import logging | ||
| import sys | ||
| import types | ||
|
|
||
| USE_IMPORTLIB_METADATA_STDLIB = USE_IMPORTLIB_METADATA = USE_PKG_RESOURCES = False | ||
|
|
||
| if sys.version_info >= (3, 10): | ||
| # Python 3.10 will include a fix in importlib.metadata which allows us to | ||
| # get the distribution of a loaded entry-point | ||
| import importlib.metadata # pylint: disable=no-member,no-name-in-module | ||
|
|
||
| USE_IMPORTLIB_METADATA_STDLIB = True | ||
| else: | ||
| if sys.version_info >= (3, 6): | ||
| # importlib_metadata available for python version lower than 3.6 do not | ||
| # include the functionality we need. | ||
| try: | ||
| import importlib_metadata | ||
|
|
||
| importlib_metadata_version = [ | ||
| int(part) | ||
| for part in importlib_metadata.version("importlib_metadata").split(".") | ||
| if part.isdigit() | ||
| ] | ||
| if tuple(importlib_metadata_version) >= (3, 3, 0): | ||
| # Version 3.3.0 of importlib_metadata includes a fix which allows us to | ||
| # get the distribution of a loaded entry-point | ||
| USE_IMPORTLIB_METADATA = True | ||
| except ImportError: | ||
| # We don't have importlib_metadata but USE_IMPORTLIB_METADATA is set to false by default | ||
| pass | ||
|
|
||
| if not USE_IMPORTLIB_METADATA_STDLIB and not USE_IMPORTLIB_METADATA: | ||
| # Try to use pkg_resources | ||
| try: | ||
| import pkg_resources | ||
|
|
||
| USE_PKG_RESOURCES = True | ||
| except ImportError: | ||
| # We don't have pkg_resources but USE_PKG_RESOURCES is set to false by default | ||
| pass | ||
|
|
||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def iter_entry_points(group, name=None): | ||
| entry_points_listing = [] | ||
| if USE_IMPORTLIB_METADATA_STDLIB: | ||
| log.debug("Using importlib.metadata to load entry points") | ||
| entry_points = importlib.metadata.entry_points() | ||
| elif USE_IMPORTLIB_METADATA: | ||
| log.debug("Using importlib_metadata to load entry points") | ||
| entry_points = importlib_metadata.entry_points() | ||
| elif USE_PKG_RESOURCES: | ||
| log.debug("Using pkg_resources to load entry points") | ||
| entry_points_listing = list(pkg_resources.iter_entry_points(group, name=name)) | ||
| else: | ||
| return entry_points_listing | ||
|
|
||
| if USE_IMPORTLIB_METADATA_STDLIB or USE_IMPORTLIB_METADATA: | ||
| for entry_point_group, entry_points_list in entry_points.items(): | ||
| if entry_point_group != group: | ||
| continue | ||
| for entry_point in entry_points_list: | ||
| if name is not None and entry_point.name != name: | ||
| continue | ||
| entry_points_listing.append(entry_point) | ||
|
|
||
| return entry_points_listing | ||
|
|
||
|
|
||
| def name_and_version_from_entry_point(entry_point): | ||
| if USE_IMPORTLIB_METADATA_STDLIB or USE_IMPORTLIB_METADATA: | ||
| return types.SimpleNamespace( | ||
| name=entry_point.dist.metadata["name"], version=entry_point.dist.version, | ||
| ) | ||
| elif USE_PKG_RESOURCES: | ||
| return types.SimpleNamespace( | ||
| name=entry_point.dist.key, version=entry_point.dist.version | ||
| ) |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.