import importlib
import pathlib
import subprocess
import sys
import tempfile
_SRC = pathlib.Path(__file__).parent.parent.parent
_PROTO_DIR = _SRC / 'build/bench/protos'
sys.path.append(str(_SRC / 'third_party/protobuf/python'))
def error(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
exit(1)
def import_protobufs(fname: str):
assert fname.endswith('.proto')
path = _PROTO_DIR / fname
with tempfile.TemporaryDirectory() as d:
subprocess.run(
[
'protoc',
f'--python_out={d}',
f'--proto_path={_PROTO_DIR}',
str(path),
],
check=True,
)
sys.path.append(d)
mod = importlib.import_module(f'{path.stem}_pb2')
sys.path.pop()
return mod
class CapacitorFile:
def __init__(self, out: pathlib.Path):
if out.suffix != '.capacitor':
error(f'Output file must be a capacitor file. Got {out}')
if not out.parent.resolve().is_dir():
error(f'{out.parent} is not a directory')
self.out = out
def write(self, obj):
cmd = [
'gqui',
'from',
'rawproto:-',
'proto',
f'{obj.DESCRIPTOR.file.package}.{obj.__class__.__name__}',
f'--protofiles={_PROTO_DIR / obj.DESCRIPTOR.file.name}',
f'--outfile=capacitor:{self.out}',
]
subprocess.run(
cmd,
check=True,
input=obj.SerializeToString(),
)
def __str__(self):
return str(self.out)
def __repr__(self):
return repr(self.out)