-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarter_checker.py
More file actions
48 lines (42 loc) · 1.73 KB
/
Copy pathstarter_checker.py
File metadata and controls
48 lines (42 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import difflib
from pathlib import Path
def submission_uses_starter(output_lines, submission_path, template_path):
"""
Helper for _precheck_submission. Checks that submission_file follows the
template in template_file as follows;
1. Extract the template lines from both files and check that the line contents
are identical.
2. Report the any extra/missing lines.
"""
MESSAGE = "Does the submission follow the starter file?"
template_lines = _extract_template_lines(Path(template_path))
submission_lines = _extract_template_lines(Path(submission_path))
if template_lines == submission_lines:
output_lines.append(f"✅ {MESSAGE}")
return True
differ = difflib.Differ()
diff = differ.compare(template_lines, submission_lines)
output_lines.append("FeedBot was not run because the submission does not follow the starter code.")
output_lines.append("")
output_lines.append("Lines starting with '-' are missing from your submission.")
output_lines.append("Lines starting with '+' are extra lines in your submission.")
output_lines.append("")
output_lines.append("```")
output_lines.extend(diff)
output_lines.append("```")
return False
def _extract_template_lines(file: Path):
"""
Returns a list of the lines that begin with ";;!".
"""
template_lines = []
with file.open("rt") as f:
for line in f:
# Just a hack to ensure we can test with the homework master.
if line.startswith(";;!hide"):
continue
if line.startswith(";;!show"):
continue
if line.startswith(";;!"):
template_lines.append(line.rstrip())
return template_lines