Skip to content

Commit 5b7d322

Browse files
committed
More fixes for python versions
1 parent 35e1919 commit 5b7d322

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

easybuild/tools/entrypoints.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from easybuild.tools.build_log import EasyBuildError
1414

1515
try:
16-
from importlib.metadata import entry_points
16+
from importlib.metadata import entry_points, EntryPoints
1717
except ModuleNotFoundError:
1818
HAVE_ENTRY_POINTS = False
1919
else:
@@ -25,15 +25,31 @@
2525

2626
def get_group_entrypoints(group: str):
2727
"""Get all entrypoints for a group"""
28-
# Default True needed to work with commands like --list-toolchains that do not initialize the BuildOptions
29-
if not build_option('use_entrypoints', default=True):
30-
return set()
31-
if not HAVE_ENTRY_POINTS:
32-
msg = "Python importlib.metadata requires Python >= 3.8"
33-
_log.warning(msg)
34-
raise EasyBuildError(msg)
35-
# Can't use the group keyword argument in entry_points() for Python < 3.10
36-
return set(ep for ep in entry_points() if ep.group == group)
28+
strict_python = True
29+
use_eps = build_option('use_entrypoints', default=None)
30+
if use_eps is None:
31+
# Default True needed to work with commands like --list-toolchains that do not initialize the BuildOptions
32+
use_eps = True
33+
# Needed to work with older Python versions: do not raise errors when entry points are default enabled
34+
strict_python = False
35+
res = set()
36+
if use_eps:
37+
if not HAVE_ENTRY_POINTS and strict_python:
38+
msg = "`--use-entrypoints` requires importlib.metadata (Python >= 3.8)"
39+
_log.warning(msg)
40+
raise EasyBuildError(msg)
41+
# Can't use the group keyword argument in entry_points() for Python < 3.10
42+
try:
43+
eps = entry_points()
44+
if isinstance(eps, EntryPoints):
45+
# Python >= 3.10
46+
res = set(ep for ep in eps if ep.group == group)
47+
elif isinstance(eps, dict):
48+
# Python < 3.10
49+
res = set(eps.get(group, []))
50+
except NameError:
51+
_log.debug("`get_group_entrypoints` called before BuildOptions initialized, with python < 3.8")
52+
return res
3753

3854

3955
# EASYCONFIG_ENTRYPOINT = "easybuild.easyconfig"

0 commit comments

Comments
 (0)