Merge pull request #9300 from a3d4/introduce-errorcodes-next

Introduce error_codes.py --next flag
This commit is contained in:
chriseth 2020-07-06 15:17:30 +02:00 committed by GitHub
commit f11b0336ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,14 +56,11 @@ def get_used_ids(file_names):
return used_ids return used_ids
def get_id(available_ids, used_ids): def get_next_id(available_ids):
while len(available_ids) > 0: assert len(available_ids) > 0, "Out of IDs"
k = random.randrange(len(available_ids)) next_id = random.choice(list(available_ids))
id = list(available_ids.keys())[k] available_ids.remove(next_id)
del available_ids[id] return next_id
if id not in used_ids:
return id
assert False, "Out of IDs"
def fix_ids_in_file(file_name, available_ids, used_ids): def fix_ids_in_file(file_name, available_ids, used_ids):
@ -80,7 +77,8 @@ def fix_ids_in_file(file_name, available_ids, used_ids):
# incorrect id or id has a duplicate somewhere # incorrect id or id has a duplicate somewhere
if not in_comment(source, m.start()) and (len(id) != 4 or id[0] == "0" or used_ids[id] > 1): if not in_comment(source, m.start()) and (len(id) != 4 or id[0] == "0" or used_ids[id] > 1):
assert id in used_ids assert id in used_ids
new_id = get_id(available_ids, used_ids) new_id = get_next_id(available_ids)
assert new_id not in used_ids
used_ids[id] -= 1 used_ids[id] -= 1
else: else:
new_id = id new_id = id
@ -97,7 +95,7 @@ def fix_ids_in_file(file_name, available_ids, used_ids):
def fix_ids(used_ids, file_names): def fix_ids(used_ids, file_names):
available_ids = {str(id): None for id in range(1000, 10000)} available_ids = {str(id) for id in range(1000, 10000)} - used_ids.keys()
for file_name in file_names: for file_name in file_names:
fix_ids_in_file(file_name, available_ids, used_ids) fix_ids_in_file(file_name, available_ids, used_ids)
@ -172,26 +170,29 @@ def examine_id_coverage(top_dir, used_ids):
def main(argv): def main(argv):
# pylint: disable=too-many-branches, too-many-locals # pylint: disable=too-many-branches, too-many-locals, too-many-statements
check = False check = False
fix = False fix = False
noconfirm = False no_confirm = False
examine_coverage = False examine_coverage = False
opts, args = getopt.getopt(argv, "", ["check", "fix", "noconfirm", "examine-coverage"]) next = False
opts, args = getopt.getopt(argv, "", ["check", "fix", "no-confirm", "examine-coverage", "next"])
for opt, arg in opts: for opt, arg in opts:
if opt == '--check': if opt == '--check':
check = True check = True
elif opt == "--fix": elif opt == "--fix":
fix = True fix = True
elif opt == '--noconfirm': elif opt == '--no-confirm':
noconfirm = True no_confirm = True
elif opt == '--examine-coverage': elif opt == '--examine-coverage':
examine_coverage = True examine_coverage = True
elif opt == '--next':
next = True
if not check and not fix and not examine_coverage: if [check, fix, examine_coverage, next].count(True) != 1:
print("usage: python error_codes.py --check | --fix [--noconfirm] | --examine-coverage") print("usage: python error_codes.py --check | --fix [--no-confirm] | --examine-coverage | --next")
exit(1) exit(1)
cwd = os.getcwd() cwd = os.getcwd()
@ -217,10 +218,20 @@ def main(argv):
if examine_coverage: if examine_coverage:
if not ok: if not ok:
print("Incorrect IDs has to be fixed before applying --examine-coverage") print("Incorrect IDs have to be fixed before applying --examine-coverage")
res = examine_id_coverage(cwd, used_ids.keys()) res = examine_id_coverage(cwd, used_ids.keys())
exit(res) exit(res)
random.seed()
if next:
if not ok:
print("Incorrect IDs have to be fixed before applying --next")
available_ids = {str(id) for id in range(1000, 10000)} - used_ids.keys()
next_id = get_next_id(available_ids)
print(f"Next ID: {next_id}")
exit(0)
if ok: if ok:
print("No incorrect IDs found") print("No incorrect IDs found")
exit(0) exit(0)
@ -230,7 +241,7 @@ def main(argv):
assert fix, "Unexpected state, should not come here without --fix" assert fix, "Unexpected state, should not come here without --fix"
if not noconfirm: if not no_confirm:
answer = input( answer = input(
"\nDo you want to fix incorrect IDs?\n" "\nDo you want to fix incorrect IDs?\n"
"Please commit current changes first, and review the results when the script finishes.\n" "Please commit current changes first, and review the results when the script finishes.\n"
@ -241,7 +252,6 @@ def main(argv):
if answer not in "yY": if answer not in "yY":
exit(1) exit(1)
random.seed()
fix_ids(used_ids, source_file_names) fix_ids(used_ids, source_file_names)
print("Fixing completed") print("Fixing completed")
exit(2) exit(2)