Skip to content

Commit 0edd625

Browse files
committed
Improved version check logic
1 parent 165ef96 commit 0edd625

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

easybuild/tools/entrypoints.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
55
* Davide Grassano (CECAM)
66
"""
7-
7+
import sys
88
import importlib
99
from easybuild.tools.config import build_option
1010
from typing import Callable, List
1111

1212
from easybuild.base import fancylogger
1313
from easybuild.tools.build_log import EasyBuildError
1414

15-
try:
16-
from importlib.metadata import entry_points, EntryPoints
17-
except ModuleNotFoundError:
18-
HAVE_ENTRY_POINTS = False
19-
else:
15+
16+
HAVE_ENTRY_POINTS = False
17+
HAVE_ENTRY_POINTS_CLS = False
18+
if sys.version_info >= (3, 8):
2019
HAVE_ENTRY_POINTS = True
20+
from importlib.metadata import entry_points
21+
22+
if sys.version_info >= (3, 10):
23+
# Python >= 3.10 uses importlib.metadata.EntryPoints as a type for entry_points()
24+
HAVE_ENTRY_POINTS_CLS = True
2125

2226

2327
_log = fancylogger.getLogger('entrypoints', fname=False)
@@ -34,21 +38,21 @@ def get_group_entrypoints(group: str):
3438
strict_python = False
3539
res = set()
3640
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
41+
if not HAVE_ENTRY_POINTS:
42+
if strict_python:
43+
msg = "`--use-entrypoints` requires importlib.metadata (Python >= 3.8)"
44+
_log.warning(msg)
45+
raise EasyBuildError(msg)
46+
else:
47+
_log.debug("`get_group_entrypoints` called before BuildOptions initialized, with python < 3.8")
48+
else:
49+
if HAVE_ENTRY_POINTS_CLS:
50+
eps = entry_points(group=group)
51+
res = set(eps)
52+
else:
53+
eps = entry_points()
4954
res = set(eps.get(group, []))
50-
except NameError:
51-
_log.debug("`get_group_entrypoints` called before BuildOptions initialized, with python < 3.8")
55+
5256
return res
5357

5458

0 commit comments

Comments
 (0)