Skip to content

Commit f8a8f49

Browse files
committed
Load REZ plugins from setuptools entry points
Signed-off-by: Nig3l <[email protected]>
1 parent 91b3a3c commit f8a8f49

File tree

1 file changed

+54
-35
lines changed

1 file changed

+54
-35
lines changed

src/rez/plugin_managers.py

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def register_plugin(self, plugin_name, plugin_class, plugin_module):
109109
self.plugin_modules[plugin_name] = plugin_module
110110

111111
def load_plugins(self):
112+
self.load_plugins_from_namespace()
113+
self.load_plugins_from_entry_points()
114+
115+
def load_plugins_from_namespace(self):
112116
import pkgutil
113117
from importlib import import_module
114118
type_module_name = 'rezplugins.' + self.type_name
@@ -153,44 +157,15 @@ def load_plugins(self):
153157
if config.debug("plugins"):
154158
print_debug("loading %s plugin at %s: %s..."
155159
% (self.type_name, path, modname))
160+
156161
try:
157-
# https://github.com/AcademySoftwareFoundation/rez/pull/218
158-
# load_module will force reload the module if it's
159-
# already loaded, so check for that
160162
plugin_module = sys.modules.get(modname)
161163
if plugin_module is None:
162164
loader = importer.find_module(modname)
163165
plugin_module = loader.load_module(modname)
164166

165-
elif os.path.dirname(plugin_module.__file__) != path:
166-
if config.debug("plugins"):
167-
# this should not happen but if it does, tell why.
168-
print_warning(
169-
"plugin module %s is not loaded from current "
170-
"load path but reused from previous imported "
171-
"path: %s" % (modname, plugin_module.__file__))
172-
173-
if (hasattr(plugin_module, "register_plugin")
174-
and callable(plugin_module.register_plugin)):
175-
176-
plugin_class = plugin_module.register_plugin()
177-
if plugin_class is not None:
178-
self.register_plugin(plugin_name,
179-
plugin_class,
180-
plugin_module)
181-
else:
182-
if config.debug("plugins"):
183-
print_warning(
184-
"'register_plugin' function at %s: %s did "
185-
"not return a class." % (path, modname))
186-
else:
187-
if config.debug("plugins"):
188-
print_warning(
189-
"no 'register_plugin' function at %s: %s"
190-
% (path, modname))
191-
192-
# delete from sys.modules?
193-
167+
self.register_plugin_module(plugin_name, plugin_module, path)
168+
self.load_config_from_plugin(plugin_module)
194169
except Exception as e:
195170
nameish = modname.split('.')[-1]
196171
self.failed_plugins[nameish] = str(e)
@@ -201,9 +176,53 @@ def load_plugins(self):
201176
traceback.print_exc(file=out)
202177
print_debug(out.getvalue())
203178

204-
# load config
205-
data, _ = _load_config_from_filepaths([os.path.join(path, "rezconfig")])
206-
deep_update(self.config_data, data)
179+
def load_plugins_from_entry_points(self):
180+
if sys.version_info < (3, 10):
181+
from importlib_metadata import entry_points
182+
else:
183+
from importlib.metadata import entry_points
184+
185+
discovered_plugins = entry_points(group='rez.plugins')
186+
for plugin in discovered_plugins:
187+
plugin = plugin.load()
188+
plugin_name = plugin.__name__.split('.')[-1]
189+
plugin_path = os.path.dirname(plugin.__file__)
190+
self.register_plugin_module(plugin_name, plugin, plugin_path)
191+
self.load_config_from_plugin(plugin)
192+
193+
def load_config_from_plugin(self, plugin):
194+
plugin_path = os.path.dirname(plugin.__file__)
195+
data, _ = _load_config_from_filepaths([os.path.join(plugin_path, "rezconfig")])
196+
deep_update(self.config_data, data)
197+
198+
def register_plugin_module(self, plugin_name, plugin_module, plugin_path):
199+
module_name = plugin_module.__name__
200+
if os.path.dirname(plugin_module.__file__) != plugin_path:
201+
if config.debug("plugins"):
202+
# this should not happen but if it does, tell why.
203+
print_warning(
204+
"plugin module %s is not loaded from current "
205+
"load path but reused from previous imported "
206+
"path: %s" % (module_name, plugin_module.__file__))
207+
208+
if (hasattr(plugin_module, "register_plugin")
209+
and callable(plugin_module.register_plugin)):
210+
211+
plugin_class = plugin_module.register_plugin()
212+
if plugin_class is not None:
213+
self.register_plugin(plugin_name,
214+
plugin_class,
215+
plugin_module)
216+
else:
217+
if config.debug("plugins"):
218+
print_warning(
219+
"'register_plugin' function at %s: %s did "
220+
"not return a class." % (plugin_path, module_name))
221+
else:
222+
if config.debug("plugins"):
223+
print_warning(
224+
"no 'register_plugin' function at %s: %s"
225+
% (plugin_path, module_name))
207226

208227
def get_plugin_class(self, plugin_name):
209228
"""Returns the class registered under the given plugin name."""

0 commit comments

Comments
 (0)