diff options
author | Chris Boesch <chrboesch@noreply.codeberg.org> | 2023-04-19 15:25:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-19 15:25:12 +0200 |
commit | 2ed24ab3cfffdf6181462d118157094fdf2f5128 (patch) | |
tree | d5be07fcca217587eb188d8e7b1ca1a6cdb463f6 /tools | |
parent | b84217161cfb18aed2b42506c193abe778aa3b5f (diff) | |
parent | c7697a428206a46326a61b2c851aab2a7dadb8b8 (diff) |
Merge pull request #234 from perillo/improve-ci
Improve CI
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/check-exercises.py | 97 | ||||
-rwxr-xr-x | tools/update-patches.py | 68 |
2 files changed, 165 insertions, 0 deletions
diff --git a/tools/check-exercises.py b/tools/check-exercises.py new file mode 100755 index 0000000..fa0b5cb --- /dev/null +++ b/tools/check-exercises.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python + +import difflib +import io +import os +import os.path +import subprocess +import sys + + +IGNORE = subprocess.DEVNULL +PIPE = subprocess.PIPE + +EXERCISES_PATH = "exercises" +HEALED_PATH = "patches/healed" +PATCHES_PATH = "patches/patches" + + +# Heals all the exercises. +def heal(): + maketree(HEALED_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(HEALED_PATH, name) + + patch(original_path, patch_path, output_path) + + +# Yields all the healed exercises that are not correctly formatted. +def check_healed(): + term = subprocess.run( + ["zig", "fmt", "--check", HEALED_PATH], stdout=PIPE, text=True + ) + if term.stdout == "" and term.returncode != 0: + term.check_returncode() + + stream = io.StringIO(term.stdout) + for line in stream: + yield line.strip() + + +def main(): + heal() + + # Show the unified diff between the original example and the correctly + # formatted one. + for i, original in enumerate(check_healed()): + if i > 0: + print() + + name = os.path.basename(original) + print(f"checking exercise {name}...\n") + + from_file = open(original) + to_file = zig_fmt_file(original) + + diff = difflib.unified_diff( + from_file.readlines(), to_file.readlines(), name, name + "-fmt" + ) + sys.stderr.writelines(diff) + + +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 + ) + + +# Formats the Zig file at path, and returns the possibly reformatted file as a +# file object. +def zig_fmt_file(path): + with open(path) as stdin: + term = subprocess.run( + ["zig", "fmt", "--stdin"], stdin=stdin, stdout=PIPE, check=True, text=True + ) + + return io.StringIO(term.stdout) + + +main() 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() |