22 lines
763 B
Python
22 lines
763 B
Python
import ast
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
MAIN = ROOT / "ansible/roles/cezen-backend/files/main.py"
|
|
|
|
|
|
def load_symbols(*names, globals_dict=None):
|
|
tree = ast.parse(MAIN.read_text())
|
|
wanted = []
|
|
for node in tree.body:
|
|
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names:
|
|
wanted.append(node)
|
|
elif isinstance(node, ast.Assign):
|
|
assigned = {target.id for target in node.targets if isinstance(target, ast.Name)}
|
|
if assigned.intersection(names):
|
|
wanted.append(node)
|
|
namespace = dict(globals_dict or {})
|
|
exec(compile(ast.Module(body=wanted, type_ignores=[]), str(MAIN), "exec"), namespace)
|
|
return namespace
|