From 7910199023e77b002ae6594440634f52855fff93 Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Wed, 12 Feb 2025 14:55:24 -0500 Subject: [PATCH 1/8] Change option name and add command-line help --- contrib/ci/fix_prelude.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index 4dd181bfe..d3f87231f 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -74,14 +74,14 @@ def compare_content(title, actual_str): return retval -def process_one_file(filename, interactive): +def process_one_file(filename, explain): global VALIDATORS failures = None sections_needed = ["BEGIN", "ROLES", "SYMBOLS", "REQUIRES", "PROVIDES", "END"] - if interactive: + if explain: failures = [] else: failures = "" @@ -99,12 +99,12 @@ def process_one_file(filename, interactive): if name in VALIDATORS.keys(): validator = VALIDATORS[name] if not globals()[validator](name, content): - if interactive: + if explain: failures.extend(compare_content(name, content)) else: failures = failures + " " + name if len(sections_needed) > 0: - if interactive: + if explain: failures.append("Missing Section(s)") for section in sections_needed: failures.append(" " + section) @@ -118,9 +118,12 @@ def process_one_file(filename, interactive): if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument( - "--files-to-check", type=Path, default=CONTRIB / "rst_files_with_prelude.txt" + "--files-to-check", type=Path, default=CONTRIB / "rst_files_with_prelude.txt", + help="Contains list of files (wildcards allowed) relative to " + str(PROJECT) ) - ap.add_argument("--interactive", action="store_true") + ap.add_argument("--explain", + help="Give details as to why a file failed the check", + action="store_true") args = ap.parse_args() total_failures = 0 @@ -135,10 +138,10 @@ def process_one_file(filename, interactive): continue for one in f_prel: - failures = process_one_file(one, args.interactive) + failures = process_one_file(one, args.explain) if len(failures) > 0: total_failures = total_failures + 1 - if args.interactive: + if args.explain: print("FAIL: " + str(one)) for line in failures: print(" " + line) From fc6b14e510861b3f659980b4954ee944f8670171 Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Wed, 12 Feb 2025 15:00:44 -0500 Subject: [PATCH 2/8] Reformatting per 'black' --- contrib/ci/fix_prelude.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index d3f87231f..bb0ae1cc5 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -118,12 +118,16 @@ def process_one_file(filename, explain): if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument( - "--files-to-check", type=Path, default=CONTRIB / "rst_files_with_prelude.txt", - help="Contains list of files (wildcards allowed) relative to " + str(PROJECT) + "--files-to-check", + type=Path, + default=CONTRIB / "rst_files_with_prelude.txt", + help="Contains list of files (wildcards allowed) relative to " + str(PROJECT), + ) + ap.add_argument( + "--explain", + help="Give details as to why a file failed the check", + action="store_true", ) - ap.add_argument("--explain", - help="Give details as to why a file failed the check", - action="store_true") args = ap.parse_args() total_failures = 0 From bb2bea35550cd407b3216ae83105bf120a2472db Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Wed, 12 Feb 2025 15:48:00 -0500 Subject: [PATCH 3/8] Add new argument to fix prelude on failure --- contrib/ci/fix_prelude.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index bb0ae1cc5..8a61b2b7a 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -128,6 +128,11 @@ def process_one_file(filename, explain): help="Give details as to why a file failed the check", action="store_true", ) + ap.add_argument( + "--update", + help="Run 'rst_update_prelude' script to fix errors", + action="store_true", + ) args = ap.parse_args() total_failures = 0 @@ -145,7 +150,16 @@ def process_one_file(filename, explain): failures = process_one_file(one, args.explain) if len(failures) > 0: total_failures = total_failures + 1 - if args.explain: + if args.update: + subprocess.check_call( + str(sys.executable) + + " " + + str(os.path.join(CONTRIB, "rst_update_prelude.py")) + + " " + + "-i " + + str(one) + ) + elif args.explain: print("FAIL: " + str(one)) for line in failures: print(" " + line) From ad710f9eaadc57489b9ffdc37455115c710bddcf Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:49:22 -0500 Subject: [PATCH 4/8] Update help message to be more explicit about 'files-to-check' --- contrib/ci/fix_prelude.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index 8a61b2b7a..3aa292bdc 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -121,7 +121,9 @@ def process_one_file(filename, explain): "--files-to-check", type=Path, default=CONTRIB / "rst_files_with_prelude.txt", - help="Contains list of files (wildcards allowed) relative to " + str(PROJECT), + help=("File containing list of files (wildcards allowed). " + + "Can be relative to the repository (" + str(PROJECT) + ") " + "or current directory (if no matches in the repository)") ) ap.add_argument( "--explain", From e5da98fa56da759e68f5bf6a6a8b1dc1b56c0b76 Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:56:00 -0500 Subject: [PATCH 5/8] Forgot to run 'black' to clean up formatting --- contrib/ci/fix_prelude.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index 3aa292bdc..850916d99 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -121,9 +121,13 @@ def process_one_file(filename, explain): "--files-to-check", type=Path, default=CONTRIB / "rst_files_with_prelude.txt", - help=("File containing list of files (wildcards allowed). " + - "Can be relative to the repository (" + str(PROJECT) + ") " - "or current directory (if no matches in the repository)") + help=( + "File containing list of files (wildcards allowed). " + + "Can be relative to the repository (" + + str(PROJECT) + + ") " + "or current directory (if no matches in the repository)" + ), ) ap.add_argument( "--explain", From 8ba56a0005a61f84ca0aa6dfbedde0e0613312c5 Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:56:44 -0500 Subject: [PATCH 6/8] Print warning message if an empty line is found in 'files-to-check' Rather than crashing --- contrib/ci/fix_prelude.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index 850916d99..0f8a53bb1 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -148,6 +148,10 @@ def process_one_file(filename, explain): files_with_prelude_glob = f.read().splitlines() for glob in files_with_prelude_glob: + if len(glob) == 0: + print("WARN: empty line found in " + str(args.files_to_check)) + continue + f_prel = list(PROJECT.glob(glob)) if not f_prel: continue From 495b468aacf0b1b98c0926d1c3b1114072e68b2d Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Thu, 13 Feb 2025 10:19:28 -0500 Subject: [PATCH 7/8] Better handling of non-files and bad files --- contrib/ci/fix_prelude.py | 59 ++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index 0f8a53bb1..6266c52fe 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -85,32 +85,39 @@ def process_one_file(filename, explain): failures = [] else: failures = "" - with open(filename, "r") as file: - content = file.read() - pieces = content.split(PRELUDE_FLAG) - for section in pieces: - stripped = section.strip() - name, content = section.split("\n", 1) - try: - sections_needed.remove(name) - except: - pass - content = content.strip() - if name in VALIDATORS.keys(): - validator = VALIDATORS[name] - if not globals()[validator](name, content): - if explain: - failures.extend(compare_content(name, content)) - else: - failures = failures + " " + name - if len(sections_needed) > 0: - if explain: - failures.append("Missing Section(s)") - for section in sections_needed: - failures.append(" " + section) - else: - for section in sections_needed: - failures = failures + " " + section + + if not os.path.isfile(filename): + return failures + + try: + with open(filename, "r") as file: + content = file.read() + pieces = content.split(PRELUDE_FLAG) + for section in pieces: + stripped = section.strip() + name, content = section.split("\n", 1) + try: + sections_needed.remove(name) + except: + pass + content = content.strip() + if name in VALIDATORS.keys(): + validator = VALIDATORS[name] + if not globals()[validator](name, content): + if explain: + failures.extend(compare_content(name, content)) + else: + failures = failures + " " + name + if len(sections_needed) > 0: + if explain: + failures.append("Missing Section(s)") + for section in sections_needed: + failures.append(" " + section) + else: + for section in sections_needed: + failures = failures + " " + section + except: + print("WARN: unable to process " + str(filename)) return failures From 0bf3685fa728d0680153788c829099eb9de213c6 Mon Sep 17 00:00:00 2001 From: Michael Frank <55284511+frank-at-adacore@users.noreply.github.com> Date: Thu, 13 Feb 2025 11:26:11 -0500 Subject: [PATCH 8/8] Fix 'check_call' to take a list for arguments --- contrib/ci/fix_prelude.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/contrib/ci/fix_prelude.py b/contrib/ci/fix_prelude.py index 6266c52fe..db2cd07d7 100644 --- a/contrib/ci/fix_prelude.py +++ b/contrib/ci/fix_prelude.py @@ -168,14 +168,13 @@ def process_one_file(filename, explain): if len(failures) > 0: total_failures = total_failures + 1 if args.update: - subprocess.check_call( - str(sys.executable) - + " " - + str(os.path.join(CONTRIB, "rst_update_prelude.py")) - + " " - + "-i " - + str(one) - ) + command = [ + str(sys.executable), + os.path.join(CONTRIB, "rst_update_prelude.py"), + "--in-place", + one, + ] + subprocess.check_call(command) elif args.explain: print("FAIL: " + str(one)) for line in failures: