my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

replaced check-exercises.pu with check-exercises.zig

Chris Boesch (Apr 25, 2023, 12:13 PM +0200) cffbbff8 752efd89

+108 -97
-97
tools/check-exercises.py
··· 1 - #!/usr/bin/env python 2 - 3 - import difflib 4 - import io 5 - import os 6 - import os.path 7 - import subprocess 8 - import sys 9 - 10 - 11 - IGNORE = subprocess.DEVNULL 12 - PIPE = subprocess.PIPE 13 - 14 - EXERCISES_PATH = "exercises" 15 - HEALED_PATH = "patches/healed" 16 - PATCHES_PATH = "patches/patches" 17 - 18 - 19 - # Heals all the exercises. 20 - def heal(): 21 - maketree(HEALED_PATH) 22 - 23 - with os.scandir(EXERCISES_PATH) as it: 24 - for entry in it: 25 - name = entry.name 26 - 27 - original_path = entry.path 28 - patch_path = os.path.join(PATCHES_PATH, patch_name(name)) 29 - output_path = os.path.join(HEALED_PATH, name) 30 - 31 - patch(original_path, patch_path, output_path) 32 - 33 - 34 - # Yields all the healed exercises that are not correctly formatted. 35 - def check_healed(): 36 - term = subprocess.run( 37 - ["zig", "fmt", "--check", HEALED_PATH], stdout=PIPE, text=True 38 - ) 39 - if term.stdout == "" and term.returncode != 0: 40 - term.check_returncode() 41 - 42 - stream = io.StringIO(term.stdout) 43 - for line in stream: 44 - yield line.strip() 45 - 46 - 47 - def main(): 48 - heal() 49 - 50 - # Show the unified diff between the original example and the correctly 51 - # formatted one. 52 - for i, original in enumerate(check_healed()): 53 - if i > 0: 54 - print() 55 - 56 - name = os.path.basename(original) 57 - print(f"checking exercise {name}...\n") 58 - 59 - from_file = open(original) 60 - to_file = zig_fmt_file(original) 61 - 62 - diff = difflib.unified_diff( 63 - from_file.readlines(), to_file.readlines(), name, name + "-fmt" 64 - ) 65 - sys.stderr.writelines(diff) 66 - 67 - 68 - def maketree(path): 69 - return os.makedirs(path, exist_ok=True) 70 - 71 - 72 - # Returns path with the patch extension. 73 - def patch_name(path): 74 - name, _ = os.path.splitext(path) 75 - 76 - return name + ".patch" 77 - 78 - 79 - # Applies patch to original, and write the file to output. 80 - def patch(original, patch, output): 81 - subprocess.run( 82 - ["patch", "-i", patch, "-o", output, original], stdout=IGNORE, check=True 83 - ) 84 - 85 - 86 - # Formats the Zig file at path, and returns the possibly reformatted file as a 87 - # file object. 88 - def zig_fmt_file(path): 89 - with open(path) as stdin: 90 - term = subprocess.run( 91 - ["zig", "fmt", "--stdin"], stdin=stdin, stdout=PIPE, check=True, text=True 92 - ) 93 - 94 - return io.StringIO(term.stdout) 95 - 96 - 97 - main()
+108
tools/check-exercises.zig
··· 1 + const std = @import("std"); 2 + const print = std.debug.print; 3 + const string = []const u8; 4 + 5 + const cwd = std.fs.cwd(); 6 + const Dir = std.fs.Dir; 7 + const Allocator = std.mem.Allocator; 8 + 9 + const EXERCISES_PATH = "exercises"; 10 + const HEALED_PATH = "patches/healed"; 11 + const TEMP_PATH = "patches/healed/tmp"; 12 + const PATCHES_PATH = "patches/patches"; 13 + 14 + // Heals all the exercises. 15 + fn heal(alloc: Allocator) !void { 16 + try cwd.makePath(HEALED_PATH); 17 + 18 + const org_path = try cwd.realpathAlloc(alloc, EXERCISES_PATH); 19 + const patch_path = try cwd.realpathAlloc(alloc, PATCHES_PATH); 20 + const healed_path = try cwd.realpathAlloc(alloc, HEALED_PATH); 21 + 22 + var idir = try cwd.openIterableDir(EXERCISES_PATH, Dir.OpenDirOptions{}); 23 + defer idir.close(); 24 + 25 + var it = idir.iterate(); 26 + while (try it.next()) |entry| { 27 + 28 + // create filenames 29 + const healed_file = try concat(alloc, &.{ healed_path, "/", entry.name }); 30 + const patch_file = try concat(alloc, &.{ patch_path, "/", try patch_name(alloc, entry.name) }); 31 + 32 + // patch file 33 + const result = try std.ChildProcess.exec(.{ 34 + .allocator = alloc, 35 + .argv = &.{ "patch", "-i", patch_file, "-o", healed_file, entry.name }, 36 + .cwd = org_path, 37 + }); 38 + 39 + print("{s}", .{result.stderr}); 40 + } 41 + } 42 + 43 + // Yields all the healed exercises that are not correctly formatted. 44 + fn check_healed(alloc: Allocator) !void { 45 + try cwd.makePath(TEMP_PATH); 46 + 47 + const temp_path = try cwd.realpathAlloc(alloc, TEMP_PATH); 48 + const healed_path = try cwd.realpathAlloc(alloc, HEALED_PATH); 49 + 50 + var idir = try cwd.openIterableDir(HEALED_PATH, Dir.OpenDirOptions{}); 51 + defer idir.close(); 52 + 53 + var it = idir.iterate(); 54 + while (try it.next()) |entry| { 55 + 56 + // Check the healed file 57 + const result = try std.ChildProcess.exec(.{ 58 + .allocator = alloc, 59 + .argv = &.{ "zig", "fmt", "--check", entry.name }, 60 + .cwd = healed_path, 61 + }); 62 + 63 + // Is there something to fix? 64 + if (result.stdout.len > 0) { 65 + const temp_file = try concat(alloc, &.{ temp_path, "/", entry.name }); 66 + const healed_file = try concat(alloc, &.{ healed_path, "/", entry.name }); 67 + try std.fs.copyFileAbsolute(healed_file, temp_file, std.fs.CopyFileOptions{}); 68 + 69 + // Formats the temp file 70 + _ = try std.ChildProcess.exec(.{ 71 + .allocator = alloc, 72 + .argv = &.{ "zig", "fmt", entry.name }, 73 + .cwd = temp_path, 74 + }); 75 + 76 + // Show the differences 77 + const diff = try std.ChildProcess.exec(.{ 78 + .allocator = alloc, 79 + .argv = &.{ "diff", "-c", healed_file, entry.name }, 80 + .cwd = temp_path, 81 + }); 82 + 83 + print("{s}", .{diff.stdout}); 84 + try std.fs.deleteFileAbsolute(temp_file); 85 + } 86 + } 87 + } 88 + 89 + fn concat(alloc: Allocator, slices: []const string) !string { 90 + const buf = try std.mem.concat(alloc, u8, slices); 91 + return buf; 92 + } 93 + 94 + fn patch_name(alloc: Allocator, path: string) !string { 95 + var filename = path; 96 + const index = std.mem.lastIndexOfScalar(u8, path, '.') orelse return path; 97 + if (index > 0) filename = path[0..index]; 98 + return try concat(alloc, &.{ filename, ".patch" }); 99 + } 100 + 101 + pub fn main() !void { 102 + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); 103 + defer arena.deinit(); 104 + const alloc = arena.allocator(); 105 + 106 + try heal(alloc); 107 + try check_healed(alloc); 108 + }