| 29 | |
| 30 | |
| 31 | def guarded_import(path, expected, name): |
| 32 | path = Path(path).resolve(strict=True) |
| 33 | with path.open("rb") as stream: |
| 34 | snapshot = stream.read(256 * 1024 + 1) |
| 35 | require(len(snapshot) <= 256 * 1024 and hashlib.sha256(snapshot).hexdigest() == expected, |
| 36 | "reviewed source snapshot differs: " + str(path)) |
| 37 | spec = importlib.util.spec_from_file_location(name, str(path)) |
| 38 | require(spec is not None, "cannot construct guarded import") |
| 39 | module = importlib.util.module_from_spec(spec) |
| 40 | exec(compile(snapshot, str(path), "exec"), module.__dict__) |
| 41 | return module |
| 42 | |
| 43 | |
| 44 | def reducer_worker(): |