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-10-29 09:26:05 +00:00
|
|
|
|
|
|
|
NOTHING_TO_BE_DONE = 0
|
|
|
|
NEED_UPDATE = 1
|
|
|
|
FAILURE = 2
|
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
DEFAULT_GIR_FILES_DIRECTORY = Path('./gir-files')
|
|
|
|
DEFAULT_GIR_DIRECTORY = Path('./gir/')
|
|
|
|
DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / 'target/release/gir'
|
|
|
|
|
2020-10-29 09:26:05 +00:00
|
|
|
|
|
|
|
def run_command(command, folder=None):
|
|
|
|
if folder is None:
|
|
|
|
folder = "."
|
2020-11-24 11:16:54 +00:00
|
|
|
ret = subprocess.run(command, cwd=folder)
|
|
|
|
if ret.returncode != 0:
|
|
|
|
print("Command `{}` failed with `{}`...".format(command, ret))
|
2020-10-29 09:26:05 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-06-18 10:10:24 +00:00
|
|
|
|
|
|
|
def update_workspace():
|
2020-11-24 11:16:54 +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):
|
|
|
|
question = '{} [y/N] '.format(question)
|
2020-11-24 11:16:54 +00:00
|
|
|
if conf.yes:
|
|
|
|
print(question + 'y')
|
2020-11-19 17:51:59 +00:00
|
|
|
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):
|
2020-11-24 11:16:54 +00:00
|
|
|
if any(submodule_path.iterdir()):
|
2020-10-29 09:26:05 +00:00
|
|
|
return NOTHING_TO_BE_DONE
|
2020-11-24 11:16:54 +00:00
|
|
|
print('=> Initializing {} submodule...'.format(submodule_path))
|
|
|
|
if not run_command(['git', 'submodule', 'update', '--init', submodule_path]):
|
2020-10-29 09:26:05 +00:00
|
|
|
return FAILURE
|
2019-06-18 10:10:24 +00:00
|
|
|
print('<= Done!')
|
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
if ask_yes_no_question('Do you want to update {} submodule?'.format(submodule_path), conf):
|
|
|
|
print('=> Updating submodule...')
|
|
|
|
if not run_command(['git', 'reset', '--hard', 'HEAD'], submodule_path):
|
2020-10-29 09:26:05 +00:00
|
|
|
return FAILURE
|
2020-11-24 11:16:54 +00:00
|
|
|
if not run_command(['git', 'pull', '-f', 'origin', 'master'], submodule_path):
|
2020-10-29 09:26:05 +00:00
|
|
|
return FAILURE
|
|
|
|
print('<= Done!')
|
|
|
|
return NEED_UPDATE
|
|
|
|
return NOTHING_TO_BE_DONE
|
|
|
|
|
|
|
|
|
|
|
|
def build_gir_if_needed(updated_submodule):
|
|
|
|
if updated_submodule == FAILURE:
|
|
|
|
return False
|
2020-11-12 19:08:55 +00:00
|
|
|
print('=> Building gir...')
|
2020-11-24 11:16:54 +00:00
|
|
|
if update_workspace():
|
2020-11-12 19:08:55 +00:00
|
|
|
print('<= Done!')
|
|
|
|
else:
|
|
|
|
print('<= Failed...')
|
|
|
|
return False
|
2020-10-29 09:26:05 +00:00
|
|
|
return True
|
|
|
|
|
2020-11-19 17:51:59 +00:00
|
|
|
|
2020-11-24 11:16:54 +00:00
|
|
|
def regen_crates(path, conf):
|
|
|
|
if path.is_dir():
|
|
|
|
for entry in path.rglob("Gir*.toml"):
|
|
|
|
if not regen_crates(entry, conf):
|
|
|
|
return False
|
|
|
|
elif path.match("Gir*.toml"):
|
|
|
|
print('==> Regenerating "{}"...'.format(path))
|
|
|
|
|
|
|
|
args = [conf.gir_path, '-c', path, '-o', path.parent, '-d', conf.gir_files_path]
|
|
|
|
if path.parent.name.endswith("sys"):
|
|
|
|
args.extend(['-m', 'sys'])
|
|
|
|
error = False
|
|
|
|
try:
|
|
|
|
error = not run_command(args)
|
|
|
|
except Exception as err:
|
|
|
|
print('The following error occurred: {}'.format(err))
|
|
|
|
error = True
|
|
|
|
if error:
|
|
|
|
if not ask_yes_no_question('Do you want to continue?', conf):
|
2020-10-30 15:52:12 +00:00
|
|
|
return False
|
2020-11-24 11:16:54 +00:00
|
|
|
print('<== Done!')
|
|
|
|
else:
|
|
|
|
print('==> {} is not a valid Gir*.toml file'.format(path))
|
|
|
|
return False
|
2020-10-30 15:52:12 +00:00
|
|
|
return True
|
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():
|
|
|
|
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('--gir-files-directory', dest="gir_files_path", default=DEFAULT_GIR_FILES_DIRECTORY,
|
|
|
|
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`')
|
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
conf = parse_args()
|
|
|
|
|
|
|
|
if conf.gir_path == DEFAULT_GIR_PATH:
|
|
|
|
if not build_gir_if_needed(def_check_submodule(DEFAULT_GIR_DIRECTORY, conf)):
|
2020-11-19 17:51:59 +00:00
|
|
|
return 1
|
|
|
|
|
2020-10-29 09:26:05 +00:00
|
|
|
print('=> Regenerating crates...')
|
2020-11-24 11:16:54 +00:00
|
|
|
for path in conf.path:
|
|
|
|
print('=> Looking in path `{}`'.format(path))
|
|
|
|
if not regen_crates(path, conf):
|
|
|
|
return 1
|
|
|
|
if not conf.no_fmt and not run_command(['cargo', 'fmt']):
|
2020-10-29 09:26:05 +00:00
|
|
|
return 1
|
2019-06-18 10:10:24 +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__":
|
|
|
|
sys.exit(main())
|