generator: Simplify error handling

Similar to Rusts return and try, just use exceptions and pretty-print
them at the highest level (instead of presenting the user with a
stacktrace that's harder to read than the error alone).
This commit is contained in:
Marijn Suijten 2021-04-11 20:46:42 +02:00 committed by Sebastian Dröge
parent 1c18a54177
commit 46080de113

View file

@ -5,11 +5,6 @@ import argparse
import subprocess import subprocess
import sys import sys
NOTHING_TO_BE_DONE = 0
NEED_UPDATE = 1
FAILURE = 2
DEFAULT_GIR_FILES_DIRECTORY = Path("./gir-files") DEFAULT_GIR_FILES_DIRECTORY = Path("./gir-files")
DEFAULT_GST_GIR_FILES_DIRECTORY = Path("./gst-gir-files") DEFAULT_GST_GIR_FILES_DIRECTORY = Path("./gst-gir-files")
DEFAULT_GIR_DIRECTORY = Path("./gir/") DEFAULT_GIR_DIRECTORY = Path("./gir/")
@ -19,11 +14,7 @@ DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / "target/release/gir"
def run_command(command, folder=None): def run_command(command, folder=None):
if folder is None: if folder is None:
folder = "." folder = "."
ret = subprocess.run(command, cwd=folder) return subprocess.run(command, cwd=folder, check=True)
if ret.returncode != 0:
print("Command `{}` failed with `{}`...".format(command, ret))
return False
return True
def spawn_process(command): def spawn_process(command):
@ -43,37 +34,28 @@ def ask_yes_no_question(question, conf):
return line.strip().lower() == "y" return line.strip().lower() == "y"
def def_check_submodule(submodule_path, conf): def update_submodule(submodule_path, conf):
if any(submodule_path.iterdir()): if any(submodule_path.iterdir()):
return NOTHING_TO_BE_DONE return False
print("=> Initializing {} submodule...".format(submodule_path)) print("=> Initializing {} submodule...".format(submodule_path))
if not run_command(["git", "submodule", "update", "--init", submodule_path]): run_command(["git", "submodule", "update", "--init", submodule_path])
return FAILURE
print("<= Done!") print("<= Done!")
if ask_yes_no_question( if ask_yes_no_question(
"Do you want to update {} submodule?".format(submodule_path), conf "Do you want to update {} submodule?".format(submodule_path), conf
): ):
print("=> Updating submodule...") print("=> Updating submodule...")
if not run_command(["git", "reset", "--hard", "HEAD"], submodule_path): run_command(["git", "reset", "--hard", "HEAD"], submodule_path)
return FAILURE run_command(["git", "pull", "-f", "origin", "master"], submodule_path)
if not run_command(["git", "pull", "-f", "origin", "master"], submodule_path):
return FAILURE
print("<= Done!") print("<= Done!")
return NEED_UPDATE return True
return NOTHING_TO_BE_DONE return False
def build_gir_if_needed(updated_submodule): def build_gir():
if updated_submodule == FAILURE:
return False
print("=> Building gir...") print("=> Building gir...")
if update_workspace(): update_workspace()
print("<= Done!") print("<= Done!")
else:
print("<= Failed...")
return False
return True
def regen_crates(path, conf): def regen_crates(path, conf):
@ -97,11 +79,7 @@ def regen_crates(path, conf):
# doc-target-path is relative to `-c` # doc-target-path is relative to `-c`
path_depth = len(path.parent.parts) path_depth = len(path.parent.parts)
doc_path = ( doc_path = Path(*[".."] * path_depth, "docs", path.parent, "docs.md")
Path(*[".."] * path_depth, "docs")
.joinpath(path.parent)
.joinpath("docs.md")
)
doc_args = args + [ doc_args = args + [
"-m", "-m",
"doc", "doc",
@ -191,8 +169,8 @@ def main():
conf = parse_args() conf = parse_args()
if conf.gir_path == DEFAULT_GIR_PATH: if conf.gir_path == DEFAULT_GIR_PATH:
if not build_gir_if_needed(def_check_submodule(DEFAULT_GIR_DIRECTORY, conf)): update_submodule(DEFAULT_GIR_DIRECTORY, conf)
return 1 build_gir()
print("=> Regenerating crates...") print("=> Regenerating crates...")
for path in conf.path: for path in conf.path:
@ -201,10 +179,13 @@ def main():
for log, p in processes: for log, p in processes:
print("==> {}".format(log)) print("==> {}".format(log))
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
stdout = stdout.decode("utf-8")
stderr = stderr.decode("utf-8")
assert p.returncode == 0, stderr.strip()
# Gir doesn't print anything to stdout. If it does, this is likely out of # Gir doesn't print anything to stdout. If it does, this is likely out of
# order with stderr, unless the printer/logging flushes in between. # order with stderr, unless the printer/logging flushes in between.
assert stdout == b"" assert not stdout, "`gir` printed unexpected stdout: {}".format(stdout)
print(stderr.decode("utf-8"), end="") print(stderr, end="")
if not conf.no_fmt and not run_command(["cargo", "fmt"]): if not conf.no_fmt and not run_command(["cargo", "fmt"]):
return 1 return 1
@ -214,4 +195,8 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(main()) try:
main()
except Exception as e:
print("Error: {}".format(e), file=sys.stderr)
sys.exit(1)