Skip to content

Commit afb5e89

Browse files
committed
Add load_plugins to pulp-glue
This allows to auto-detect and load all installed pulp-glue plugins. It is primarily useful for workflows where knowing all sub-types of an Entity is only important at runtime. [noissue]
1 parent 1cce481 commit afb5e89

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `load_plugins` to `pulp_glue.common` so plugins providing a "pulp_glue.plugins" entrypoint can be enumerated and loaded.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1+
import sys
2+
import typing as t
3+
4+
if sys.version_info >= (3, 10):
5+
from importlib.metadata import entry_points
6+
else:
7+
from importlib_metadata import entry_points
8+
19
__version__ = "0.28.0.dev"
10+
11+
# Keep track to prevent loading plugins twice
12+
loaded_plugins: t.Set[str] = set()
13+
14+
15+
def load_plugins(enabled_plugins: t.Optional[t.List[str]] = None) -> None:
16+
"""
17+
Load glue plugins that provide a `pulp_glue.plugins` entrypoint.
18+
This may be needed when you rely on the `TYPE_REGISTRY` attributes but cannot load the modules
19+
explicitely.
20+
21+
Parameters:
22+
enabled_plugins: Optional list of plugins to consider for loading.
23+
"""
24+
for entry_point in entry_points(group="pulp_glue.plugins"):
25+
name = entry_point.name
26+
if (
27+
enabled_plugins is None or entry_point.name in enabled_plugins
28+
) and entry_point.name not in loaded_plugins:
29+
plugin = entry_point.load()
30+
if hasattr(plugin, "mount"):
31+
plugin.mount()
32+
loaded_plugins.add(name)
33+
>>>>>>> c548775 (Add load_plugins to pulp-glue)

pulp-glue/pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ documentation = "https://docs.pulpproject.org/pulp_cli/"
3333
repository = "https://github.com/pulp/pulp-cli"
3434
changelog = "https://docs.pulpproject.org/pulp_cli/CHANGES/"
3535

36+
[project.entry-points."pulp_glue.plugins"]
37+
ansible = "pulp_glue.ansible"
38+
certguard = "pulp_glue.certguard"
39+
container = "pulp_glue.container"
40+
core = "pulp_glue.core"
41+
file = "pulp_glue.file"
42+
python = "pulp_glue.python"
43+
rpm = "pulp_glue.rpm"
44+
3645
[tool.setuptools.packages.find]
3746
where = ["."]
3847
include = ["pulp_glue.*"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# pulp_glue.common
2+
3+
::: pulp_glue.common

pulp-glue/tests/test_entity_context.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from pulp_glue.common import load_plugins, loaded_plugins
78
from pulp_glue.common.context import PulpContext, PulpRepositoryContext
89
from pulp_glue.file.context import PulpFileRepositoryContext
910

@@ -18,6 +19,15 @@ def file_repository(pulp_ctx: PulpContext) -> t.Dict[str, t.Any]:
1819
file_repository_ctx.delete()
1920

2021

22+
def test_plugin_loading() -> None:
23+
load_plugins()
24+
assert "core" in loaded_plugins
25+
26+
27+
def test_type_registry() -> None:
28+
assert "file:file" in PulpRepositoryContext.TYPE_REGISTRY
29+
30+
2131
def test_detail_context(pulp_ctx: PulpContext, file_repository: t.Dict[str, t.Any]) -> None:
2232
master_ctx = PulpRepositoryContext(pulp_ctx)
2333
detail_ctx = master_ctx.detail_context(pulp_href=file_repository["pulp_href"])

0 commit comments

Comments
 (0)