generator: Autoformat and normalize quotation with black

This commit is contained in:
Marijn Suijten 2020-11-24 20:20:18 +01:00
parent 5b7c882497
commit 9a96dd05b8

View file

@ -10,9 +10,9 @@ NOTHING_TO_BE_DONE = 0
NEED_UPDATE = 1 NEED_UPDATE = 1
FAILURE = 2 FAILURE = 2
DEFAULT_GIR_FILES_DIRECTORY = Path('./gir-files') DEFAULT_GIR_FILES_DIRECTORY = Path("./gir-files")
DEFAULT_GIR_DIRECTORY = Path('./gir/') DEFAULT_GIR_DIRECTORY = Path("./gir/")
DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / 'target/release/gir' DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / "target/release/gir"
def run_command(command, folder=None): def run_command(command, folder=None):
@ -26,33 +26,35 @@ def run_command(command, folder=None):
def update_workspace(): def update_workspace():
return run_command(['cargo', 'build', '--release'], 'gir') return run_command(["cargo", "build", "--release"], "gir")
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: if conf.yes:
print(question + 'y') print(question + "y")
return True return True
line = input(question) line = input(question)
return line.strip().lower() == 'y' return line.strip().lower() == "y"
def def_check_submodule(submodule_path, conf): def def_check_submodule(submodule_path, conf):
if any(submodule_path.iterdir()): if any(submodule_path.iterdir()):
return NOTHING_TO_BE_DONE return NOTHING_TO_BE_DONE
print('=> Initializing {} submodule...'.format(submodule_path)) print("=> Initializing {} submodule...".format(submodule_path))
if not run_command(['git', 'submodule', 'update', '--init', submodule_path]): 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 {} submodule?'.format(submodule_path), conf): if ask_yes_no_question(
print('=> Updating submodule...') "Do you want to update {} submodule?".format(submodule_path), conf
if not run_command(['git', 'reset', '--hard', 'HEAD'], submodule_path): ):
print("=> Updating submodule...")
if not run_command(["git", "reset", "--hard", "HEAD"], submodule_path):
return FAILURE return FAILURE
if not run_command(['git', 'pull', '-f', 'origin', 'master'], submodule_path): 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
return NOTHING_TO_BE_DONE return NOTHING_TO_BE_DONE
@ -60,11 +62,11 @@ def def_check_submodule(submodule_path, conf):
def build_gir_if_needed(updated_submodule): 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(): if update_workspace():
print('<= Done!') print("<= Done!")
else: else:
print('<= Failed...') print("<= Failed...")
return False return False
return True return True
@ -77,21 +79,21 @@ def regen_crates(path, conf):
elif path.match("Gir*.toml"): elif path.match("Gir*.toml"):
print('==> Regenerating "{}"...'.format(path)) print('==> Regenerating "{}"...'.format(path))
args = [conf.gir_path, '-c', path, '-o', path.parent, '-d', conf.gir_files_path] args = [conf.gir_path, "-c", path, "-o", path.parent, "-d", conf.gir_files_path]
if path.parent.name.endswith("sys"): if path.parent.name.endswith("sys"):
args.extend(['-m', 'sys']) args.extend(["-m", "sys"])
error = False error = False
try: try:
error = not run_command(args) error = not run_command(args)
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:
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: else:
print('==> {} is not a valid Gir*.toml file'.format(path)) print("==> {} is not a valid Gir*.toml file".format(path))
return False return False
return True return True
@ -118,22 +120,41 @@ def file_path(path):
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description='Helper to regenerate gtk-rs crates using gir.', parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter) description="Helper to regenerate gtk-rs crates using gir.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument('path', nargs="*", default=[Path('.')], parser.add_argument(
type=valid_path, "path",
help='Paths in which to look for Gir.toml files') nargs="*",
parser.add_argument('--gir-files-directory', dest="gir_files_path", default=DEFAULT_GIR_FILES_DIRECTORY, default=[Path(".")],
type=directory_path, type=valid_path,
help='Path of the gir-files folder') help="Paths in which to look for Gir.toml files",
parser.add_argument('--gir-path', default=DEFAULT_GIR_PATH, )
type=file_path, parser.add_argument(
help='Path of the gir executable to run') "--gir-files-directory",
parser.add_argument('--yes', action='store_true', dest="gir_files_path",
help=' Always answer `yes` to any question asked by the script') default=DEFAULT_GIR_FILES_DIRECTORY,
parser.add_argument('--no-fmt', action='store_true', type=directory_path,
help='If set, this script will not run `cargo fmt`') 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`",
)
return parser.parse_args() return parser.parse_args()
@ -145,14 +166,14 @@ def main():
if not build_gir_if_needed(def_check_submodule(DEFAULT_GIR_DIRECTORY, conf)): if not build_gir_if_needed(def_check_submodule(DEFAULT_GIR_DIRECTORY, conf)):
return 1 return 1
print('=> Regenerating crates...') print("=> Regenerating crates...")
for path in conf.path: for path in conf.path:
print('=> Looking in path `{}`'.format(path)) print("=> Looking in path `{}`".format(path))
if not regen_crates(path, conf): if not regen_crates(path, conf):
return 1 return 1
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
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!")
return 0 return 0