2020-08-11 06:10:15 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-06-18 10:10:24 +00:00
|
|
|
|
|
|
|
from os import listdir
|
2020-10-29 09:26:05 +00:00
|
|
|
from os.path import isfile, isdir, join
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(command, folder=None):
|
|
|
|
if folder is None:
|
|
|
|
folder = "."
|
|
|
|
child = subprocess.Popen(command, cwd=folder)
|
|
|
|
child.communicate()
|
|
|
|
if child.returncode != 0:
|
|
|
|
print("Command `{}` failed with return code `{}`...".format(command, child.returncode))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-06-18 10:10:24 +00:00
|
|
|
|
|
|
|
def update_workspace():
|
|
|
|
try:
|
2020-10-29 09:26:05 +00:00
|
|
|
return run_command(['cargo', 'build', '--release'], 'gir')
|
2019-06-18 10:10:24 +00:00
|
|
|
except:
|
2019-10-30 09:22:11 +00:00
|
|
|
return False
|
2019-06-18 10:10:24 +00:00
|
|
|
|
|
|
|
|
2020-10-29 09:26:05 +00:00
|
|
|
def def_check_submodule(submodule_path):
|
|
|
|
if len(listdir(submodule_path)) != 0:
|
|
|
|
return NOTHING_TO_BE_DONE
|
2019-06-18 10:10:24 +00:00
|
|
|
print('=> Initializing gir submodule...')
|
2020-10-29 09:26:05 +00:00
|
|
|
if not run_command(['git', 'submodule', 'update', '--init']):
|
|
|
|
return FAILURE
|
2019-06-18 10:10:24 +00:00
|
|
|
print('<= Done!')
|
|
|
|
|
2020-10-29 09:26:05 +00:00
|
|
|
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':
|
|
|
|
print('=> Updating gir submodule...')
|
|
|
|
if not run_command(['git', 'reset', '--hard', 'HEAD'], 'gir'):
|
|
|
|
return FAILURE
|
|
|
|
if not run_command(['git', 'pull', '-f', 'origin', 'master'], 'gir'):
|
|
|
|
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
|
|
|
|
elif updated_submodule == NEED_UPDATE or not isfile('./gir/target/release/gir'):
|
|
|
|
print('=> Building gir...')
|
|
|
|
if update_workspace() is True:
|
|
|
|
print('<= Done!')
|
|
|
|
else:
|
|
|
|
print('<= Failed...')
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-10-30 15:52:12 +00:00
|
|
|
def regen_crates(path, 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):
|
|
|
|
return False
|
|
|
|
elif entry == 'Gir.toml':
|
|
|
|
print('==> Regenerating "{}"...'.format(entry_file))
|
|
|
|
|
|
|
|
args = ['./gir/target/release/gir', '-c', entry_file, '-o', path, '-d', 'gir-files']
|
|
|
|
if level > 1:
|
|
|
|
args.append('-m')
|
|
|
|
args.append('sys')
|
|
|
|
error = False
|
2020-10-29 09:26:05 +00:00
|
|
|
try:
|
2020-10-30 15:52:12 +00:00
|
|
|
error = run_command(args) is False
|
2020-10-29 09:26:05 +00:00
|
|
|
except Exception as err:
|
|
|
|
print('The following error occurred: {}'.format(err))
|
2020-10-30 15:52:12 +00:00
|
|
|
error = True
|
|
|
|
if error is True:
|
2020-10-29 09:26:05 +00:00
|
|
|
line = input('Do you want to continue? [y/N] ').strip().lower()
|
|
|
|
if line != 'y':
|
2020-10-30 15:52:12 +00:00
|
|
|
return False
|
2020-10-29 09:26:05 +00:00
|
|
|
print('<== Done!')
|
2020-10-30 15:52:12 +00:00
|
|
|
return True
|
2020-10-29 09:26:05 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if not build_gir_if_needed(def_check_submodule("gir")):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
print('=> Regenerating crates...')
|
2020-10-30 15:52:12 +00:00
|
|
|
if not regen_crates("."):
|
2020-10-29 09:26:05 +00:00
|
|
|
return 1
|
|
|
|
if not run_command(['cargo', 'fmt']):
|
|
|
|
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())
|