Update generator.py from gtk-rs

This commit is contained in:
Sebastian Dröge 2020-11-24 13:16:54 +02:00
parent 7dc5a90b8a
commit de0ed0040a

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from os import listdir from pathlib import Path
from os.path import isfile, isdir, join import argparse
import subprocess import subprocess
import sys import sys
@ -10,29 +10,29 @@ NOTHING_TO_BE_DONE = 0
NEED_UPDATE = 1 NEED_UPDATE = 1
FAILURE = 2 FAILURE = 2
DEFAULT_GIR_FILES_DIRECTORY = Path('./gir-files')
DEFAULT_GIR_DIRECTORY = Path('./gir/')
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 = "."
child = subprocess.Popen(command, cwd=folder) ret = subprocess.run(command, cwd=folder)
child.communicate() if ret.returncode != 0:
if child.returncode != 0: print("Command `{}` failed with `{}`...".format(command, ret))
print("Command `{}` failed with return code `{}`...".format(command, child.returncode))
return False return False
return True return True
def update_workspace(): def update_workspace():
try: return run_command(['cargo', 'build', '--release'], 'gir')
return run_command(['cargo', 'build', '--release'], 'gir')
except:
return False
def ask_yes_no_question(question, conf): def ask_yes_no_question(question, conf):
question = '{} [y/N] '.format(question) question = '{} [y/N] '.format(question)
if conf["yes"] is True: if conf.yes:
print(question) print(question + 'y')
return True return True
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
line = raw_input(question) line = raw_input(question)
@ -42,18 +42,18 @@ def ask_yes_no_question(question, conf):
def def_check_submodule(submodule_path, conf): def def_check_submodule(submodule_path, conf):
if len(listdir(submodule_path)) != 0: if any(submodule_path.iterdir()):
return NOTHING_TO_BE_DONE return NOTHING_TO_BE_DONE
print('=> Initializing gir submodule...') print('=> Initializing {} submodule...'.format(submodule_path))
if not run_command(['git', 'submodule', 'update', '--init']): if not run_command(['git', 'submodule', 'update', '--init', submodule_path]):
return FAILURE return FAILURE
print('<= Done!') print('<= Done!')
if ask_yes_no_question('Do you want to update gir submodule?', conf): if ask_yes_no_question('Do you want to update {} submodule?'.format(submodule_path), conf):
print('=> Updating gir submodule...') print('=> Updating submodule...')
if not run_command(['git', 'reset', '--hard', 'HEAD'], 'gir'): if not run_command(['git', 'reset', '--hard', 'HEAD'], submodule_path):
return FAILURE return FAILURE
if not run_command(['git', 'pull', '-f', 'origin', 'master'], 'gir'): if not run_command(['git', 'pull', '-f', 'origin', 'master'], submodule_path):
return FAILURE return FAILURE
print('<= Done!') print('<= Done!')
return NEED_UPDATE return NEED_UPDATE
@ -64,7 +64,7 @@ def build_gir_if_needed(updated_submodule):
if updated_submodule == FAILURE: if updated_submodule == FAILURE:
return False return False
print('=> Building gir...') print('=> Building gir...')
if update_workspace() is True: if update_workspace():
print('<= Done!') print('<= Done!')
else: else:
print('<= Failed...') print('<= Failed...')
@ -72,106 +72,88 @@ def build_gir_if_needed(updated_submodule):
return True return True
def regen_crates(path, conf, level=0): def regen_crates(path, conf):
for entry in listdir(path): if path.is_dir():
entry_file = join(path, entry) for entry in path.rglob("Gir*.toml"):
if isdir(entry_file): if not regen_crates(entry, conf):
if level < 2 and not regen_crates(entry_file, conf, level + 1):
return False return False
elif entry.startswith("Gir") and entry.endswith(".toml"): elif path.match("Gir*.toml"):
print('==> Regenerating "{}"...'.format(entry_file)) print('==> Regenerating "{}"...'.format(path))
args = [conf["gir_path"], '-c', entry_file, '-o', path, '-d', conf["gir_files"]] args = [conf.gir_path, '-c', path, '-o', path.parent, '-d', conf.gir_files_path]
if level > 1: if path.parent.name.endswith("sys"):
args.append('-m') args.extend(['-m', 'sys'])
args.append('sys') error = False
error = False try:
try: error = not run_command(args)
error = run_command(args) is False except Exception as err:
except Exception as err: print('The following error occurred: {}'.format(err))
print('The following error occurred: {}'.format(err)) error = True
error = True if error:
if error is True: if not ask_yes_no_question('Do you want to continue?', conf):
if not ask_yes_no_question('Do you want to continue?', conf): return False
return False print('<== Done!')
print('<== Done!') else:
print('==> {} is not a valid Gir*.toml file'.format(path))
return False
return True return True
def print_help(): def valid_path(path):
print("generator.py Helper to regenerate gtk-rs crates using gir.") path = Path(path)
print("") if not path.exists():
print("[OPTIONS]") raise argparse.ArgumentTypeError("`{}` no such file or directory".format(path))
print(" -h | --help Display this message") return path
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): def directory_path(path):
conf = { path = Path(path)
"gir_path": None, if not path.is_dir():
"gir_files": None, raise argparse.ArgumentTypeError("`{}` directory not found".format(path))
"yes": False, return path
"run_fmt": True,
}
i = 0
while i < len(args):
arg = args[i] def file_path(path):
if arg == "-h" or arg == "--help": path = Path(path)
print_help() if not path.is_file():
return None raise argparse.ArgumentTypeError("`{}` file not found".format(path))
elif arg == "--gir-path": return path
i += 1
if i >= len(args):
print("Expected argument after `--gir-path` option...") def parse_args():
return None parser = argparse.ArgumentParser(description='Helper to regenerate gtk-rs crates using gir.',
if not isfile(args[i]): formatter_class=argparse.ArgumentDefaultsHelpFormatter)
print("`{}` file doesn't exist. Aborting...".format(args[i]))
return None parser.add_argument('path', nargs="*", default=[Path('.')],
conf["gir_path"] = args[i] type=valid_path,
elif arg == "--gir-files": help='Paths in which to look for Gir.toml files')
i += 1 parser.add_argument('--gir-files-directory', dest="gir_files_path", default=DEFAULT_GIR_FILES_DIRECTORY,
if i >= len(args): type=directory_path,
print("Expected argument after `--gir-files` option...") help='Path of the gir-files folder')
return None parser.add_argument('--gir-path', default=DEFAULT_GIR_PATH,
if not isdir(args[i]): type=file_path,
print("`{}` folder doesn't exist. Aborting...".format(args[i])) help='Path of the gir executable to run')
return None parser.add_argument('--yes', action='store_true',
conf["gir_files"] = args[i] help=' Always answer `yes` to any question asked by the script')
elif arg == "--yes": parser.add_argument('--no-fmt', action='store_true',
conf["yes"] = True help='If set, this script will not run `cargo fmt`')
elif arg == "--no-fmt":
conf["run_fmt"] = False return parser.parse_args()
else:
print("Unknown argument `{}`.".format(arg))
return None
i += 1
return conf
def main(): def main():
gir_path = None conf = parse_args()
conf = parse_args(sys.argv[1:]) if conf.gir_path == DEFAULT_GIR_PATH:
if conf is None: if not build_gir_if_needed(def_check_submodule(DEFAULT_GIR_DIRECTORY, conf)):
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 return 1
conf["gir_path"] = "./gir/target/release/gir"
print('=> Regenerating crates...') print('=> Regenerating crates...')
if not regen_crates(".", conf): for path in conf.path:
return 1 print('=> Looking in path `{}`'.format(path))
if conf["run_fmt"] is True and not run_command(['cargo', 'fmt']): if not regen_crates(path, conf):
return 1
if not conf.no_fmt and not run_command(['cargo', 'fmt']):
return 1 return 1
print('<= Done!') print('<= Done!')
print("Don't forget to check if everything has been correctly generated!") print("Don't forget to check if everything has been correctly generated!")