From b84cead5ec57083ba6b2fbc7f495db1d41e5b773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 19 Nov 2020 19:51:59 +0200 Subject: [PATCH] Update generator.py from gtk-rs --- generator.py | 107 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 90 insertions(+), 17 deletions(-) diff --git a/generator.py b/generator.py index 11d550f49..b4c224ce5 100755 --- a/generator.py +++ b/generator.py @@ -29,7 +29,19 @@ def update_workspace(): return False -def def_check_submodule(submodule_path): +def ask_yes_no_question(question, conf): + question = '{} [y/N] '.format(question) + if conf["yes"] is True: + print(question) + return True + if sys.version_info[0] < 3: + line = raw_input(question) + else: + line = input(question) + return line.strip().lower() == 'y' + + +def def_check_submodule(submodule_path, conf): if len(listdir(submodule_path)) != 0: return NOTHING_TO_BE_DONE print('=> Initializing gir submodule...') @@ -37,13 +49,7 @@ def def_check_submodule(submodule_path): return FAILURE print('<= Done!') - question = 'Do you want to update gir submodule? [y/N] ' - if sys.version_info[0] < 3: - line = raw_input(question) - else: - line = input(question) - line = line.strip() - if line.lower() == 'y': + if ask_yes_no_question('Do you want to update gir submodule?', conf): print('=> Updating gir submodule...') if not run_command(['git', 'reset', '--hard', 'HEAD'], 'gir'): return FAILURE @@ -65,16 +71,17 @@ def build_gir_if_needed(updated_submodule): return False return True -def regen_crates(path, level=0): + +def regen_crates(path, conf, level=0): for entry in listdir(path): entry_file = join(path, entry) if isdir(entry_file): - if level < 2 and not regen_crates(entry_file, level + 1): + if level < 2 and not regen_crates(entry_file, conf, level + 1): return False - elif entry == 'Gir.toml': + elif entry.startswith("Gir") and entry.endswith(".toml"): print('==> Regenerating "{}"...'.format(entry_file)) - args = ['./gir/target/release/gir', '-c', entry_file, '-o', path, '-d', 'gir-files'] + args = [conf["gir_path"], '-c', entry_file, '-o', path, '-d', conf["gir_files"]] if level > 1: args.append('-m') args.append('sys') @@ -85,20 +92,86 @@ def regen_crates(path, level=0): print('The following error occurred: {}'.format(err)) error = True if error is True: - line = input('Do you want to continue? [y/N] ').strip().lower() - if line != 'y': + if not ask_yes_no_question('Do you want to continue?', conf): return False print('<== Done!') return True + +def print_help(): + print("generator.py Helper to regenerate gtk-rs crates using gir.") + print("") + print("[OPTIONS]") + print(" -h | --help Display this message") + print(" --gir-path [PATH] Sets the path of the gir executable to run") + print(" (`./gir/target/release/gir` by default)") + print(" --gir-files [PATH] Sets the path of the gir-files folder") + print(" (`gir-files` by default)") + print(" --yes Always answer `yes` to any question asked by the script") + print(" --no-fmt If set, this script won't run `cargo fmt`") + + +def parse_args(args): + conf = { + "gir_path": None, + "gir_files": None, + "yes": False, + "run_fmt": True, + } + i = 0 + + while i < len(args): + arg = args[i] + if arg == "-h" or arg == "--help": + print_help() + return None + elif arg == "--gir-path": + i += 1 + if i >= len(args): + print("Expected argument after `--gir-path` option...") + return None + if not isfile(args[i]): + print("`{}` file doesn't exist. Aborting...".format(args[i])) + return None + conf["gir_path"] = args[i] + elif arg == "--gir-files": + i += 1 + if i >= len(args): + print("Expected argument after `--gir-files` option...") + return None + if not isdir(args[i]): + print("`{}` folder doesn't exist. Aborting...".format(args[i])) + return None + conf["gir_files"] = args[i] + elif arg == "--yes": + conf["yes"] = True + elif arg == "--no-fmt": + conf["run_fmt"] = False + else: + print("Unknown argument `{}`.".format(arg)) + return None + i += 1 + return conf + + def main(): - if not build_gir_if_needed(def_check_submodule("gir")): + gir_path = None + + conf = parse_args(sys.argv[1:]) + if conf is None: return 1 + if conf["gir_files"] is None: + conf["gir_files"] = "gir-files" + if conf["gir_path"] is None: + if not build_gir_if_needed(def_check_submodule("gir", conf)): + return 1 + conf["gir_path"] = "./gir/target/release/gir" + print('=> Regenerating crates...') - if not regen_crates("."): + if not regen_crates(".", conf): return 1 - if not run_command(['cargo', 'fmt']): + if conf["run_fmt"] is True and not run_command(['cargo', 'fmt']): return 1 print('<= Done!') print("Don't forget to check if everything has been correctly generated!")