aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorManlio Perillo <[email protected]>2023-04-17 09:19:49 +0200
committerManlio Perillo <[email protected]>2023-04-18 18:16:19 +0200
commitc7697a428206a46326a61b2c851aab2a7dadb8b8 (patch)
treed5be07fcca217587eb188d8e7b1ca1a6cdb463f6 /tools
parent7d7be0482c3a2cafc91c743c6d139b5c140457f3 (diff)
downloadziglings-c7697a428206a46326a61b2c851aab2a7dadb8b8.tar.xz
ziglings-c7697a428206a46326a61b2c851aab2a7dadb8b8.zip
Ensure the patches are up-to-date and consistent
Add the update-patches.py tool. Update all the patches, so that the files are up-to-date and use the same patch file format.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/update-patches.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/tools/update-patches.py b/tools/update-patches.py
new file mode 100755
index 0000000..76a1c46
--- /dev/null
+++ b/tools/update-patches.py
@@ -0,0 +1,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()