|
| 1 | +#!/usr/bin/python3 -u |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | +import os.path |
| 7 | +import re |
| 8 | + |
| 9 | + |
| 10 | +def process_file(src, dst, pattern, replace, remove, replace_pairs, end_string=None): |
| 11 | + if not pattern or not replace or not remove: |
| 12 | + print( |
| 13 | + f"ERROR: The script behaviour is not properly specified for {src}", |
| 14 | + file=sys.stderr, |
| 15 | + ) |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | + fin = open(src, "r") |
| 19 | + os.makedirs(os.path.dirname(dst), exist_ok=True) |
| 20 | + fout = open(dst, "w") |
| 21 | + |
| 22 | + remove_lines = 0 |
| 23 | + skip_lines = 0 |
| 24 | + uncomment_lines = 0 |
| 25 | + end_found = True |
| 26 | + makefile_special_handling = "Makefile" in src |
| 27 | + |
| 28 | + for i, l in enumerate(fin.readlines()): |
| 29 | + # Skip generation of file. |
| 30 | + if "SKIP_GENERATE" in l: |
| 31 | + fout.close() |
| 32 | + os.remove(dst) |
| 33 | + return |
| 34 | + |
| 35 | + if end_string and not end_found: |
| 36 | + fout.write(l) |
| 37 | + if end_string in l: |
| 38 | + end_found = True |
| 39 | + continue |
| 40 | + |
| 41 | + if remove_lines > 0: |
| 42 | + remove_lines -= 1 |
| 43 | + continue |
| 44 | + |
| 45 | + if skip_lines > 0: |
| 46 | + skip_lines -= 1 |
| 47 | + m = re.search(pattern, l) |
| 48 | + if m: |
| 49 | + l = "%s%s\n" % (m.group(1), m.group(3)) |
| 50 | + fout.write(l) |
| 51 | + continue |
| 52 | + |
| 53 | + if uncomment_lines > 0: |
| 54 | + uncomment_lines -= 1 |
| 55 | + for fro, to in replace_pairs: |
| 56 | + l = re.sub(fro, to, l) |
| 57 | + fout.write(l) |
| 58 | + continue |
| 59 | + |
| 60 | + if makefile_special_handling and "TODO" in l and "Uncomment" in l: |
| 61 | + fout.write(l) |
| 62 | + next_line = fin.readline() |
| 63 | + fout.write("# " + next_line) |
| 64 | + continue |
| 65 | + |
| 66 | + m = re.search(pattern, l) |
| 67 | + if m: |
| 68 | + if m.group(2): |
| 69 | + skip_lines = int(m.group(2)) |
| 70 | + else: |
| 71 | + skip_lines = 1 |
| 72 | + |
| 73 | + if end_string and end_string not in l: |
| 74 | + end_found = False |
| 75 | + |
| 76 | + l = "%s%s\n" % (m.group(1), m.group(3)) |
| 77 | + |
| 78 | + m = re.search(replace, l) |
| 79 | + if m: |
| 80 | + if m.group(2): |
| 81 | + uncomment_lines = int(m.group(2)) |
| 82 | + else: |
| 83 | + uncomment_lines = 1 |
| 84 | + continue |
| 85 | + |
| 86 | + m = re.search(remove, l) |
| 87 | + if m: |
| 88 | + if m.group(2): |
| 89 | + remove_lines = int(m.group(2)) |
| 90 | + else: |
| 91 | + remove_lines = 1 |
| 92 | + continue |
| 93 | + |
| 94 | + fout.write(l) |
| 95 | + |
| 96 | + fout.close() |
| 97 | + |
| 98 | +def main(): |
| 99 | + parser = argparse.ArgumentParser( |
| 100 | + description="Generate skeletons sources from reference solution sources" |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "--input", help="input directory to process files", required=True |
| 104 | + ) |
| 105 | + parser.add_argument( |
| 106 | + "--output", help="output directory to copy processed files", required=True |
| 107 | + ) |
| 108 | + args = parser.parse_args() |
| 109 | + |
| 110 | + for root, dirs, files in os.walk(args.input): |
| 111 | + new_root = os.path.join(args.output, os.path.relpath(root, args.input)) |
| 112 | + for d in dirs: |
| 113 | + os.makedirs(os.path.join(new_root, d), exist_ok=True) |
| 114 | + |
| 115 | + for src in files: |
| 116 | + if ( |
| 117 | + re.match("Makefile.*$", src) |
| 118 | + or re.match(r".*\.sh$", src) |
| 119 | + or re.match(r".*\.[sS]$", src) |
| 120 | + or re.match(r".*\.py$", src) |
| 121 | + ): |
| 122 | + pattern = r"(^\s*#\s*TODO)( [0-9]*)(:.*)" |
| 123 | + replace = r"(^\s*#\s*REPLACE)( [0-9]*)" |
| 124 | + remove = r"(^\s*#\s*REMOVE)( [0-9]*)" |
| 125 | + replace_pairs = [("# ", "")] |
| 126 | + end_string = None |
| 127 | + elif re.match(r".*\.asm$", src): |
| 128 | + pattern = r"(^\s*;\s*TODO)( [0-9]*)(:.*)" |
| 129 | + replace = r"(^\s*;\s*REPLACE)( [0-9]*)" |
| 130 | + remove = r"(^\s*;\s*REMOVE)( [0-9]*)" |
| 131 | + replace_pairs = [("; ", "")] |
| 132 | + end_string = None |
| 133 | + elif ( |
| 134 | + re.match(r".*\.[ch]$", src) |
| 135 | + or re.match(r".*\.cpp$", src) |
| 136 | + or re.match(r".*\.hpp$", src) |
| 137 | + ): |
| 138 | + pattern = r"(.*/\*\s*TODO)([ 0-9]*)(:.*)" |
| 139 | + replace = r"(.*/\*\s*REPLACE)( [0-9]*)" |
| 140 | + remove = r"(.*/\*\s*REMOVE)( [0-9]*)" |
| 141 | + replace_pairs = [(r"/\* ", ""), (r" \*/", "")] |
| 142 | + end_string = "*/" |
| 143 | + elif re.match(r".*\.d$", src): |
| 144 | + pattern = r"(.*//\s*TODO)([ 0-9]*)(:.*)" |
| 145 | + replace = r"(.*//\s*REPLACE)( [0-9]*)" |
| 146 | + remove = r"(.*//\s*REMOVE)( [0-9]*)" |
| 147 | + replace_pairs = [(r"// ", "")] |
| 148 | + end_string = None |
| 149 | + else: |
| 150 | + continue |
| 151 | + |
| 152 | + dst = os.path.join(new_root, src) |
| 153 | + src = os.path.join(root, src) |
| 154 | + print(dst) |
| 155 | + process_file(src, dst, pattern, replace, remove, replace_pairs, end_string) |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + sys.exit(main()) |
0 commit comments