|
| 1 | +# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 2 | +""" |
| 3 | +Bridge plugin that enables hydra-core to discover plugins registered via lerna. |
| 4 | +
|
| 5 | +When using hydra-core (not lerna directly), this plugin discovers plugins |
| 6 | +registered under the `hydra.lernaplugins` entry point group and makes them |
| 7 | +available to hydra's plugin system. |
| 8 | +
|
| 9 | +This enables gradual migration: plugins can be written for lerna and still |
| 10 | +work with hydra-core installations. |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | +from importlib import import_module |
| 15 | +from logging import getLogger |
| 16 | + |
| 17 | +from hydra.core.config_search_path import ConfigSearchPath |
| 18 | +from hydra.core.config_store import ConfigStore |
| 19 | +from hydra.plugins.search_path_plugin import SearchPathPlugin |
| 20 | + |
| 21 | +if sys.version_info < (3, 10): |
| 22 | + from importlib_metadata import entry_points |
| 23 | +else: |
| 24 | + from importlib.metadata import entry_points |
| 25 | + |
| 26 | +_log = getLogger("lerna") |
| 27 | + |
| 28 | +# NOTE: use `lernaplugins` instead of `plugins` |
| 29 | +# for https://github.com/facebookresearch/hydra/pull/3052 |
| 30 | +_discovered_plugins = entry_points(group="hydra.lernaplugins") |
| 31 | +_searchpaths_pkg = {} |
| 32 | +for entry_point in _discovered_plugins: |
| 33 | + if entry_point.value.startswith(("pkg:", "file:")): |
| 34 | + # This is a package style entry point |
| 35 | + kind, pkg_name = entry_point.value.split(":", 1) |
| 36 | + _searchpaths_pkg[entry_point.name] = f"{kind}://{pkg_name}" |
| 37 | + continue |
| 38 | + # Otherwise, it's a module style entry point |
| 39 | + try: |
| 40 | + mod = import_module(entry_point.value) |
| 41 | + except ImportError as e: |
| 42 | + _log.warning(f"Failed to import entry point {entry_point.name} from {entry_point.value}: {e}") |
| 43 | + continue |
| 44 | + for attr in dir(mod): |
| 45 | + thing = getattr(mod, attr) |
| 46 | + if isinstance(thing, type) and issubclass(thing, SearchPathPlugin): |
| 47 | + _log.info(f"Discovered search path plugin: {thing.__name__}") |
| 48 | + globals()[thing.__name__] = thing |
| 49 | + # search_path.append(provider=entry_point.name, path=entry_point.value) |
| 50 | + |
| 51 | + |
| 52 | +class LernaGenericSearchPathPlugin(SearchPathPlugin): |
| 53 | + """ |
| 54 | + A SearchPathPlugin that bridges lerna plugins to hydra-core. |
| 55 | +
|
| 56 | + This plugin is automatically discovered by hydra-core due to being in |
| 57 | + the hydra_plugins namespace. It then discovers any plugins registered |
| 58 | + under the `hydra.lernaplugins` entry point group. |
| 59 | + """ |
| 60 | + |
| 61 | + def manipulate_search_path(self, search_path: ConfigSearchPath) -> None: |
| 62 | + if _searchpaths_pkg: |
| 63 | + for provider, path in _searchpaths_pkg.items(): |
| 64 | + inst = ConfigStore.instance() |
| 65 | + inst.store(name=provider, node=None, group=provider, provider=provider) |
| 66 | + search_path.append(provider=provider, path=path) |
0 commit comments