""" The far end of a remote session. pyrepld sends this over ssh stdin, after the sh script in pyrepld.REMOTE_SCRIPT has found an interpreter, with `FILES = {...}` put in front: worker.py, tk.py and vendor/, as source. Nothing is written on that host. vendor/ is served by an import hook from memory, placed after the normal path so a session's own packages still win, and the sources go into linecache so tracebacks show their lines. Then worker.py runs as the remote form (--stdio), exactly as it would from disk. Must stay runnable on python3.6. """ import importlib.abc import importlib.util import linecache import sys ROOT = "" def _cache(name, src): linecache.cache[name] = (len(src), None, src.splitlines(True), name) class _Vendor(importlib.abc.MetaPathFinder, importlib.abc.Loader): def find_spec(self, name, path=None, target=None): rel = "vendor/" + name.replace(".", "/") for key, pkg in ((rel + "/__init__.py", True), (rel + ".py", False)): if key in FILES: spec = importlib.util.spec_from_loader(name, self, origin=ROOT + "/" + key, is_package=pkg) spec.has_location = True return spec return None def create_module(self, spec): return None def exec_module(self, module): name = module.__spec__.origin src = FILES[name[len(ROOT) + 1:]] _cache(name, src) exec(compile(src, name, "exec"), module.__dict__) sys.meta_path.append(_Vendor()) _name = ROOT + "/worker.py" _cache(_name, FILES["worker.py"]) sys.argv = [_name, "--stdio"] exec(compile(FILES["worker.py"], _name, "exec"), {"__name__": "__main__", "__file__": _name, "__builtins__": __builtins__, "PYREPL_FILES": FILES})