Skip to content

Commit 71c6490

Browse files
Merge branch 'slides/214-make-fix_prelude-actually-fix-the-prelude-when-run-from-the-command-line' into 'master'
Resolve "Make "fix_prelude" actually fix the prelude when run from the command line" Closes #214 See merge request feng/training/material!288
2 parents b150b54 + 57b1b92 commit 71c6490

File tree

1 file changed

+69
-32
lines changed

1 file changed

+69
-32
lines changed

contrib/ci/fix_prelude.py

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,53 +74,78 @@ def compare_content(title, actual_str):
7474
return retval
7575

7676

77-
def process_one_file(filename, interactive):
77+
def process_one_file(filename, explain):
7878
global VALIDATORS
7979

8080
failures = None
8181

8282
sections_needed = ["BEGIN", "ROLES", "SYMBOLS", "REQUIRES", "PROVIDES", "END"]
8383

84-
if interactive:
84+
if explain:
8585
failures = []
8686
else:
8787
failures = ""
88-
with open(filename, "r") as file:
89-
content = file.read()
90-
pieces = content.split(PRELUDE_FLAG)
91-
for section in pieces:
92-
stripped = section.strip()
93-
name, content = section.split("\n", 1)
94-
try:
95-
sections_needed.remove(name)
96-
except:
97-
pass
98-
content = content.strip()
99-
if name in VALIDATORS.keys():
100-
validator = VALIDATORS[name]
101-
if not globals()[validator](name, content):
102-
if interactive:
103-
failures.extend(compare_content(name, content))
104-
else:
105-
failures = failures + " " + name
106-
if len(sections_needed) > 0:
107-
if interactive:
108-
failures.append("Missing Section(s)")
109-
for section in sections_needed:
110-
failures.append(" " + section)
111-
else:
112-
for section in sections_needed:
113-
failures = failures + " " + section
88+
89+
if not os.path.isfile(filename):
90+
return failures
91+
92+
try:
93+
with open(filename, "r") as file:
94+
content = file.read()
95+
pieces = content.split(PRELUDE_FLAG)
96+
for section in pieces:
97+
stripped = section.strip()
98+
name, content = section.split("\n", 1)
99+
try:
100+
sections_needed.remove(name)
101+
except:
102+
pass
103+
content = content.strip()
104+
if name in VALIDATORS.keys():
105+
validator = VALIDATORS[name]
106+
if not globals()[validator](name, content):
107+
if explain:
108+
failures.extend(compare_content(name, content))
109+
else:
110+
failures = failures + " " + name
111+
if len(sections_needed) > 0:
112+
if explain:
113+
failures.append("Missing Section(s)")
114+
for section in sections_needed:
115+
failures.append(" " + section)
116+
else:
117+
for section in sections_needed:
118+
failures = failures + " " + section
119+
except:
120+
print("WARN: unable to process " + str(filename))
114121

115122
return failures
116123

117124

118125
if __name__ == "__main__":
119126
ap = argparse.ArgumentParser()
120127
ap.add_argument(
121-
"--files-to-check", type=Path, default=CONTRIB / "rst_files_with_prelude.txt"
128+
"--files-to-check",
129+
type=Path,
130+
default=CONTRIB / "rst_files_with_prelude.txt",
131+
help=(
132+
"File containing list of files (wildcards allowed). "
133+
+ "Can be relative to the repository ("
134+
+ str(PROJECT)
135+
+ ") "
136+
"or current directory (if no matches in the repository)"
137+
),
138+
)
139+
ap.add_argument(
140+
"--explain",
141+
help="Give details as to why a file failed the check",
142+
action="store_true",
143+
)
144+
ap.add_argument(
145+
"--update",
146+
help="Run 'rst_update_prelude' script to fix errors",
147+
action="store_true",
122148
)
123-
ap.add_argument("--interactive", action="store_true")
124149
args = ap.parse_args()
125150

126151
total_failures = 0
@@ -130,15 +155,27 @@ def process_one_file(filename, interactive):
130155
files_with_prelude_glob = f.read().splitlines()
131156

132157
for glob in files_with_prelude_glob:
158+
if len(glob) == 0:
159+
print("WARN: empty line found in " + str(args.files_to_check))
160+
continue
161+
133162
f_prel = list(PROJECT.glob(glob))
134163
if not f_prel:
135164
continue
136165

137166
for one in f_prel:
138-
failures = process_one_file(one, args.interactive)
167+
failures = process_one_file(one, args.explain)
139168
if len(failures) > 0:
140169
total_failures = total_failures + 1
141-
if args.interactive:
170+
if args.update:
171+
command = [
172+
str(sys.executable),
173+
os.path.join(CONTRIB, "rst_update_prelude.py"),
174+
"--in-place",
175+
one,
176+
]
177+
subprocess.check_call(command)
178+
elif args.explain:
142179
print("FAIL: " + str(one))
143180
for line in failures:
144181
print(" " + line)

0 commit comments

Comments
 (0)