Skip to content

Commit 1fabe4c

Browse files
committed
Improved version check logic
1 parent 5b7d322 commit 1fabe4c

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

easybuild/tools/entrypoints.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
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
25+
from importlib.metadata import EntryPoints
2126

2227

2328
_log = fancylogger.getLogger('entrypoints', fname=False)
@@ -34,21 +39,21 @@ def get_group_entrypoints(group: str):
3439
strict_python = False
3540
res = set()
3641
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
42+
if not HAVE_ENTRY_POINTS:
43+
if strict_python:
44+
msg = "`--use-entrypoints` requires importlib.metadata (Python >= 3.8)"
45+
_log.warning(msg)
46+
raise EasyBuildError(msg)
47+
else:
48+
_log.debug("`get_group_entrypoints` called before BuildOptions initialized, with python < 3.8")
49+
else:
50+
if HAVE_ENTRY_POINTS_CLS:
51+
eps = entry_points(group=group)
52+
res = set(eps)
53+
else:
54+
eps = entry_points()
4955
res = set(eps.get(group, []))
50-
except NameError:
51-
_log.debug("`get_group_entrypoints` called before BuildOptions initialized, with python < 3.8")
56+
5257
return res
5358

5459

0 commit comments

Comments
 (0)