blob: 113ddf4fc035ca67cc4a2d15c1913c57405bed75 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
from __future__ import annotations
import importlib
import sys
from pathlib import Path
from types import ModuleType
def load_inline(path: Path) -> ModuleType | None:
# nox uses here the importlib.machinery.SourceFileLoader but I consider this similarly good, and we can keep any
# name for the tox file, its content will always be loaded in this module from a system point of view
for name in ("toxfile", "☣"):
candidate = path.parent / f"{name}.py"
if candidate.exists():
return _load_plugin(candidate)
return None
def _load_plugin(path: Path) -> ModuleType:
in_folder = path.parent
module_name = path.stem
sys.path.insert(0, str(in_folder))
try:
if module_name in sys.modules:
del sys.modules[module_name] # pragma: no cover
module = importlib.import_module(module_name)
return module
finally:
del sys.path[0]
|