anki/pylib/tools/protoc_wrapper.py
2021-10-02 23:52:02 +10:00

41 lines
1.0 KiB
Python

#!/usr/bin/env
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
#
# Wrapper for protoc that strips the dirname from the output files,
# and generates mypy typechecking info.
import os
import shutil
import subprocess
import sys
(protoc, mypy_protobuf, outdir, *protos) = sys.argv[1:]
if protos[0].startswith("external"):
prefix = "external/ankidesktop/proto/"
else:
prefix = "proto/"
# invoke protoc
subprocess.run(
[
protoc,
f"--plugin=protoc-gen-mypy={mypy_protobuf}",
"--python_out=.",
"--mypy_out=.",
f"-I{prefix}",
f"-Iexternal/ankidesktop/{prefix}",
*protos,
],
# mypy prints to stderr on success :-(
stderr=subprocess.DEVNULL,
check=True,
)
for proto in protos:
without_prefix_and_ext, _ = os.path.splitext(proto[len(prefix) :])
for ext in "_pb2.py", "_pb2.pyi":
path = without_prefix_and_ext + ext
shutil.move(path, os.path.join(outdir, os.path.basename(path)))