4
4
5
5
* Davide Grassano (CECAM)
6
6
"""
7
-
7
+ import sys
8
8
import importlib
9
9
from easybuild .tools .config import build_option
10
10
from typing import Callable , List
11
11
12
12
from easybuild .base import fancylogger
13
13
from easybuild .tools .build_log import EasyBuildError
14
14
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 ):
20
19
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
21
26
22
27
23
28
_log = fancylogger .getLogger ('entrypoints' , fname = False )
@@ -34,21 +39,21 @@ def get_group_entrypoints(group: str):
34
39
strict_python = False
35
40
res = set ()
36
41
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 ()
49
55
res = set (eps .get (group , []))
50
- except NameError :
51
- _log .debug ("`get_group_entrypoints` called before BuildOptions initialized, with python < 3.8" )
56
+
52
57
return res
53
58
54
59
0 commit comments