anki/pylib/tools/protoc_wrapper.py

41 lines
973 B
Python
Raw Normal View History

#!/usr/bin/env
2021-04-13 10:45:05 +02:00
# 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
2020-11-02 07:28:31 +01:00
import shutil
import subprocess
import sys
(protoc, mypy_protobuf, proto, outdir) = sys.argv[1:]
# copy to current dir
basename = os.path.basename(proto)
shutil.copyfile(proto, basename)
# output filenames
without_ext = os.path.splitext(basename)[0]
pb2_py = without_ext + "_pb2.py"
pb2_pyi = without_ext + "_pb2.pyi"
# invoke protoc
2020-11-02 07:28:31 +01:00
subprocess.run(
[
protoc,
"--plugin=protoc-gen-mypy=" + mypy_protobuf,
"--python_out=.",
"--mypy_out=.",
basename,
],
# mypy prints to stderr on success :-(
stderr=subprocess.DEVNULL,
2020-11-02 07:28:31 +01:00
check=True,
)
# move files into output
shutil.move(pb2_py, outdir + "/" + pb2_py)
shutil.move(pb2_pyi, outdir + "/" + pb2_pyi)