2020-08-11 06:10:15 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-06-18 10:10:24 +00:00
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import argparse
|
2020-10-29 09:26:05 +00:00
|
|
|
import subprocess
|
2019-06-18 10:10:24 +00:00
|
|
|
import sys
|
|
|
|
|
2020-11-24 19:20:18 +00:00
|
|
|
DEFAULT_GIR_FILES_DIRECTORY = Path("./gir-files")
|
2021-03-10 19:14:09 +00:00
|
|
|
DEFAULT_GST_GIR_FILES_DIRECTORY = Path("./gst-gir-files")
|
2020-11-24 19:20:18 +00:00
|
|
|
DEFAULT_GIR_DIRECTORY = Path("./gir/")
|
|
|
|
DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / "target/release/gir"
|
2020-11-24 11:16:54 +00:00
|
|
|
|
2020-10-29 09:26:05 +00:00
|
|
|
|
|
|
|
def run_command(command, folder=None):
|
|
|
|
if folder is None:
|
|
|
|
folder = "."
|
2021-04-11 18:46:42 +00:00
|
|
|
return subprocess.run(command, cwd=folder, check=True)
|
2020-10-29 09:26:05 +00:00
|
|
|
|
2019-06-18 10:10:24 +00:00
|
|
|
|
2021-04-11 17:52:22 +00:00
|
|
|
def spawn_process(command):
|
|
|
|
return subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
2019-06-18 10:10:24 +00:00
|
|
|
def update_workspace():
|
2020-11-24 19:20:18 +00:00
|
|
|
return run_command(["cargo", "build", "--release"], "gir")
|
2019-06-18 10:10:24 +00:00
|
|
|
|
|
|
|
|
2020-11-19 17:51:59 +00:00
|
|
|
def ask_yes_no_question(question, conf):
|
2020-11-24 19:20:18 +00:00
|
|
|
question = "{} [y/N] ".format(question)
|
2020-11-24 11:16:54 +00:00
|
|
|
if conf.yes:
|
2020-11-24 19:20:18 +00:00
|
|
|
print(question + "y")
|
2020-11-19 17:51:59 +00:00
|
|
|
return True
|
2020-11-24 14:21:51 +00:00
|
|
|
line = input(question)
|
2020-11-24 19:20:18 +00:00
|
|
|
return line.strip().lower() == "y"
|
2020-11-19 17:51:59 +00:00
|
|
|
|
|
|
|
|
2021-04-11 18:46:42 +00:00
|
|
|
def update_submodule(submodule_path, conf):
|
2020-11-24 11:16:54 +00:00
|
|
|
if any(submodule_path.iterdir()):
|
2021-04-11 18:46:42 +00:00
|
|
|
return False
|
2020-11-24 19:20:18 +00:00
|
|
|
print("=> Initializing {} submodule...".format(submodule_path))
|
2021-04-11 18:46:42 +00:00
|
|
|
run_command(["git", "submodule", "update", "--init", submodule_path])
|
2020-11-24 19:20:18 +00:00
|
|
|
print("<= Done!")
|
2019-06-18 10:10:24 +00:00
|
|
|
|
2020-11-24 19:20:18 +00:00
|
|
|
if ask_yes_no_question(
|
|
|
|
"Do you want to update {} submodule?".format(submodule_path), conf
|
|
|
|
):
|
|
|
|
print("=> Updating submodule...")
|
2021-04-11 18:46:42 +00:00
|
|
|
run_command(["git", "reset", "--hard", "HEAD"], submodule_path)
|
|
|
|
run_command(["git", "pull", "-f", "origin", "master"], submodule_path)
|
2020-11-24 19:20:18 +00:00
|
|
|
print("<= Done!")
|
2021-04-11 18:46:42 +00:00
|
|
|
return True
|
|
|
|
return False
|
2020-10-29 09:26:05 +00:00
|
|
|
|
|
|
|
|
2021-04-11 18:46:42 +00:00
|
|
|
def build_gir():
|
2020-11-24 19:20:18 +00:00
|
|
|
print("=> Building gir...")
|
2021-04-11 18:46:42 +00:00
|
|
|
update_workspace()
|
|
|
|
print("<= Done!")
|
2020-10-29 09:26:05 +00:00
|
|
|
|
2020-11-19 17:51:59 +00:00
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
def regen_crates(path, conf):
|
2021-04-11 17:52:22 +00:00
|
|
|
processes = []
|
2020-11-24 11:16:54 +00:00
|
|
|
if path.is_dir():
|
|
|
|
for entry in path.rglob("Gir*.toml"):
|
2021-04-11 17:52:22 +00:00
|
|
|
processes += regen_crates(entry, conf)
|
2020-11-24 11:16:54 +00:00
|
|
|
elif path.match("Gir*.toml"):
|
2021-03-10 19:14:09 +00:00
|
|
|
args = [
|
|
|
|
conf.gir_path,
|
|
|
|
"-c",
|
|
|
|
path,
|
|
|
|
"-o",
|
|
|
|
path.parent,
|
|
|
|
] + [d for path in conf.gir_files_paths for d in ("-d", path)]
|
2021-04-11 17:52:22 +00:00
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
if path.parent.name.endswith("sys"):
|
2020-11-24 19:20:18 +00:00
|
|
|
args.extend(["-m", "sys"])
|
2021-04-11 13:44:50 +00:00
|
|
|
else:
|
|
|
|
# Update docs/**/docs.md for non-sys crates
|
|
|
|
|
|
|
|
# doc-target-path is relative to `-c`
|
|
|
|
path_depth = len(path.parent.parts)
|
2021-04-11 18:46:42 +00:00
|
|
|
doc_path = Path(*[".."] * path_depth, "docs", path.parent, "docs.md")
|
2021-04-11 13:44:50 +00:00
|
|
|
doc_args = args + [
|
|
|
|
"-m",
|
|
|
|
"doc",
|
|
|
|
"--doc-target-path",
|
|
|
|
doc_path,
|
|
|
|
]
|
2021-04-11 17:52:22 +00:00
|
|
|
processes.append(
|
|
|
|
(
|
|
|
|
"Regenerating documentation for `{}` into `{}`...".format(
|
|
|
|
path, doc_path
|
|
|
|
),
|
|
|
|
spawn_process(doc_args),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
processes.append(("Regenerating `{}`...".format(path), spawn_process(args)))
|
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
else:
|
2021-04-11 17:52:22 +00:00
|
|
|
raise Exception("`{}` is not a valid Gir*.toml file".format(path))
|
|
|
|
|
|
|
|
return processes
|
2020-10-29 09:26:05 +00:00
|
|
|
|
2020-11-19 17:51:59 +00:00
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
def valid_path(path):
|
|
|
|
path = Path(path)
|
|
|
|
if not path.exists():
|
|
|
|
raise argparse.ArgumentTypeError("`{}` no such file or directory".format(path))
|
|
|
|
return path
|
2020-11-19 17:51:59 +00:00
|
|
|
|
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
def directory_path(path):
|
|
|
|
path = Path(path)
|
|
|
|
if not path.is_dir():
|
|
|
|
raise argparse.ArgumentTypeError("`{}` directory not found".format(path))
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def file_path(path):
|
|
|
|
path = Path(path)
|
|
|
|
if not path.is_file():
|
|
|
|
raise argparse.ArgumentTypeError("`{}` file not found".format(path))
|
|
|
|
return path
|
2020-11-19 17:51:59 +00:00
|
|
|
|
2020-10-29 09:26:05 +00:00
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
def parse_args():
|
2020-11-24 19:20:18 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Helper to regenerate gtk-rs crates using gir.",
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"path",
|
|
|
|
nargs="*",
|
|
|
|
default=[Path(".")],
|
|
|
|
type=valid_path,
|
|
|
|
help="Paths in which to look for Gir.toml files",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-03-10 19:14:09 +00:00
|
|
|
"--gir-files-directories",
|
|
|
|
nargs="*",
|
|
|
|
dest="gir_files_paths",
|
|
|
|
default=[DEFAULT_GIR_FILES_DIRECTORY, DEFAULT_GST_GIR_FILES_DIRECTORY],
|
2020-11-24 19:20:18 +00:00
|
|
|
type=directory_path,
|
|
|
|
help="Path of the gir-files folder",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--gir-path",
|
|
|
|
default=DEFAULT_GIR_PATH,
|
|
|
|
type=file_path,
|
|
|
|
help="Path of the gir executable to run",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--yes",
|
|
|
|
action="store_true",
|
|
|
|
help=" Always answer `yes` to any question asked by the script",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-fmt",
|
|
|
|
action="store_true",
|
|
|
|
help="If set, this script will not run `cargo fmt`",
|
|
|
|
)
|
2020-11-24 11:16:54 +00:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
conf = parse_args()
|
|
|
|
|
|
|
|
if conf.gir_path == DEFAULT_GIR_PATH:
|
2021-04-11 18:46:42 +00:00
|
|
|
update_submodule(DEFAULT_GIR_DIRECTORY, conf)
|
|
|
|
build_gir()
|
2020-11-19 17:51:59 +00:00
|
|
|
|
2020-11-24 19:20:18 +00:00
|
|
|
print("=> Regenerating crates...")
|
2020-11-24 11:16:54 +00:00
|
|
|
for path in conf.path:
|
2020-11-24 19:20:18 +00:00
|
|
|
print("=> Looking in path `{}`".format(path))
|
2021-04-11 17:52:22 +00:00
|
|
|
processes = regen_crates(path, conf)
|
|
|
|
for log, p in processes:
|
|
|
|
print("==> {}".format(log))
|
|
|
|
stdout, stderr = p.communicate()
|
2021-04-11 18:46:42 +00:00
|
|
|
stdout = stdout.decode("utf-8")
|
|
|
|
stderr = stderr.decode("utf-8")
|
|
|
|
assert p.returncode == 0, stderr.strip()
|
2021-04-11 17:52:22 +00:00
|
|
|
# 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.
|
2021-04-11 18:46:42 +00:00
|
|
|
assert not stdout, "`gir` printed unexpected stdout: {}".format(stdout)
|
|
|
|
print(stderr, end="")
|
2021-04-11 17:52:22 +00:00
|
|
|
|
2020-11-24 19:20:18 +00:00
|
|
|
if not conf.no_fmt and not run_command(["cargo", "fmt"]):
|
2020-10-29 09:26:05 +00:00
|
|
|
return 1
|
2020-11-24 19:20:18 +00:00
|
|
|
print("<= Done!")
|
2020-10-29 09:26:05 +00:00
|
|
|
print("Don't forget to check if everything has been correctly generated!")
|
|
|
|
return 0
|
2019-06-18 10:10:24 +00:00
|
|
|
|
2020-10-29 09:26:05 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-04-11 18:46:42 +00:00
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except Exception as e:
|
|
|
|
print("Error: {}".format(e), file=sys.stderr)
|
|
|
|
sys.exit(1)
|