import base64 import functools import inspect import pickle import subprocess def indent(obj, lvl): return "\n".join([(lvl * " ") + line for line in str(obj).splitlines()]) def log_grpc_request(func): @functools.wraps(func) def wrap(self, request, context): try: res = func(self, request, context) print( f"""{func.__name__}({{ {indent(request, 2)} }}) = {{ {indent(res, 2)} }}""" ) return res except Exception as exc: ret = (str(context._state.code), context._state.details) print( f"""{func.__name__}({{ {indent(request, 2)} }}) = {ret} """ ) raise exc return wrap def run(cmd): return subprocess.run(cmd, shell=True, check=True) class remote_fn(object): def __init__(self, fn): self.fn = fn def as_cmd(self, *args, **kwargs): call_data = [inspect.getsource(self.fn).encode(), args, kwargs] call_data_serialized = base64.b64encode(pickle.dumps(call_data)) run_cmd = f""" python <