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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#!/usr/bin/env python
import os
import os.path
import subprocess
IGNORE = subprocess.DEVNULL
EXERCISES_PATH = "exercises"
ANSWERS_PATH = "answers"
PATCHES_PATH = "patches/patches"
# Heals all the exercises.
def heal():
maketree(ANSWERS_PATH)
with os.scandir(EXERCISES_PATH) as it:
for entry in it:
name = entry.name
original_path = entry.path
patch_path = os.path.join(PATCHES_PATH, patch_name(name))
output_path = os.path.join(ANSWERS_PATH, name)
patch(original_path, patch_path, output_path)
def main():
heal()
with os.scandir(EXERCISES_PATH) as it:
for entry in it:
name = entry.name
broken_path = entry.path
healed_path = os.path.join(ANSWERS_PATH, name)
patch_path = os.path.join(PATCHES_PATH, patch_name(name))
with open(patch_path, "w") as file:
term = subprocess.run(
["diff", broken_path, healed_path],
stdout=file,
text=True,
)
assert term.returncode == 1
def maketree(path):
return os.makedirs(path, exist_ok=True)
# Returns path with the patch extension.
def patch_name(path):
name, _ = os.path.splitext(path)
return name + ".patch"
# Applies patch to original, and write the file to output.
def patch(original, patch, output):
subprocess.run(
["patch", "-i", patch, "-o", output, original], stdout=IGNORE, check=True
)
main()
|