"""Execute hash-checked pinned methods without installing upstream ML dependencies.""" import ast import hashlib import json from pathlib import Path ROOT = Path(__file__).resolve().parents[1] / 'fixtures/local_s2s_upstream' def tree(name): manifest = json.loads((ROOT / 'manifest.json').read_text()) data = (ROOT / name).read_bytes() assert hashlib.sha256(data).hexdigest() == manifest['files'][name]['sha256'] return ast.parse(data.decode()) def functions(name, selection, namespace): definitions = [] for cls, method in selection: nodes = tree(name).body if cls: nodes = next(n for n in nodes if isinstance(n, ast.ClassDef) and n.name == cls).body node = next(n for n in nodes if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) and n.name == method) node.decorator_list = [] definitions.append(node) module = ast.Module(body=[ast.ImportFrom(module='__future__', names=[ast.alias(name='annotations')], level=0)] + definitions, type_ignores=[]) exec(compile(ast.fix_missing_locations(module), str(ROOT / name), 'exec'), namespace) return namespace def constant(name, identifier): """Read a literal from the hash-checked source, never a fixture override.""" node = next(n for n in tree(name).body if isinstance(n, ast.Assign) and any(isinstance(t, ast.Name) and t.id == identifier for t in n.targets)) return ast.literal_eval(node.value)