Skip to content

Commit b24255e

Browse files
committed
Merge branch 'mr/add_hook' into 'master'
Add ability to run a hook during Anod DAG creation See merge request it/e3-core!110
2 parents 4eeace4 + 65a742a commit b24255e

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/e3/anod/context.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
str, Platform, Platform, Platform, Optional[str], Optional[str], Optional[str]
5050
]
5151
ResolverType = Callable[[Action, Decision], bool]
52-
52+
QualifierType = str | dict[str, str | bool | Iterable[str]] | None
53+
SpecLoadHook = Callable[
54+
[str, QualifierType, BaseEnv, PRIMITIVE], tuple[str, QualifierType]
55+
]
5356

5457
logger = e3.log.getLogger("anod.context")
5558

@@ -102,6 +105,7 @@ def __init__(
102105
spec_repository: AnodSpecRepository,
103106
default_env: BaseEnv | None = None,
104107
reject_duplicates: bool = False,
108+
spec_load_hook: SpecLoadHook | None = None,
105109
):
106110
"""Initialize a new context.
107111
@@ -112,6 +116,16 @@ def __init__(
112116
context if the local server
113117
:param reject_duplicates: if True, raise SchedulingError when two
114118
duplicated action are generated
119+
:param spec_load_hook: a function that allows tweaking the anod DAG
120+
creation by doing substitution of an anod instance by another
121+
at load time. The hook function has the following signature:
122+
123+
hook(name="SPEC_NAME",
124+
qualifier=SPEC_QUALIFIER,
125+
env=SPEC_ENV,
126+
kind=ANOD_PRIMITIVE) -> (NEW_SPEC_NAME, NEW_QUALIFIER)
127+
128+
kind can be "install", "build", "test" or "source".
115129
"""
116130
self.repo = spec_repository
117131
if default_env is None:
@@ -126,6 +140,7 @@ def __init__(
126140
self.add(self.root)
127141
self.cache: dict[CacheKeyType, Anod] = {}
128142
self.sources: dict[str, tuple[str, SourceBuilder]] = {}
143+
self.spec_load_hook = spec_load_hook
129144

130145
def load(
131146
self,
@@ -150,6 +165,12 @@ def load(
150165
if env is None:
151166
env = self.default_env
152167

168+
# A hook that allows redirection to another spec or automatic adjustment of
169+
# of qualifiers. This feature is only needed in user mode for development
170+
# purposes.
171+
if self.spec_load_hook is not None:
172+
name, qualifier = self.spec_load_hook(name, qualifier, env, kind)
173+
153174
# Key used for the spec instance cache
154175
if isinstance(qualifier, str) or qualifier is None:
155176
qualifier_key = qualifier

0 commit comments

Comments
 (0)