|
4 | 4 | # out. |
5 | 5 |
|
6 | 6 | import configparser |
7 | | -import os |
8 | 7 | import re |
9 | 8 | from pathlib import Path |
10 | 9 | from typing import Match |
11 | 10 |
|
12 | | -THIS_DIR = Path(os.path.abspath(__file__)).parent |
| 11 | +THIS_DIR = Path(__file__).absolute().parent |
13 | 12 | TEMPLATE_DIR = THIS_DIR / "templates" |
14 | 13 |
|
15 | 14 | # This is very simplistic... |
@@ -41,33 +40,34 @@ def main() -> None: |
41 | 40 | if "vars" not in parser: |
42 | 41 | parser.add_section("vars") |
43 | 42 |
|
44 | | - for dirpath, dirnames, filenames in os.walk(TEMPLATE_DIR): |
45 | | - for fn in filenames: |
46 | | - if fn.endswith(".in"): |
47 | | - template_path = Path(dirpath) / fn |
48 | | - local_path = (Path(dirpath) / fn[:-3]).relative_to(TEMPLATE_DIR) |
49 | | - with template_path.open("r") as f: |
50 | | - data = f.read() |
51 | | - variables = VARIABLE_RE.findall(data) |
52 | | - for v in variables: |
53 | | - if v not in parser["vars"]: |
54 | | - parser["vars"][v] = input(f"Value for {v}? ").strip() |
55 | | - with open(VARS_FILENAME, "w") as f: |
56 | | - parser.write(f) |
57 | | - |
58 | | - interpolated_data = variable_format(data, **parser["vars"]) |
59 | | - |
60 | | - if local_path.exists(): |
61 | | - with local_path.open("r") as f: |
62 | | - existing_data = f.read() |
63 | | - if existing_data == interpolated_data: |
64 | | - print(f"Unchanged {local_path}") |
65 | | - continue |
66 | | - |
67 | | - print(f"Writing {local_path}") |
68 | | - local_path.parent.mkdir(parents=True, exist_ok=True) |
69 | | - with local_path.open("w") as f: |
70 | | - f.write(interpolated_data) |
| 43 | + for template_path in TEMPLATE_DIR.glob('**/*'): |
| 44 | + if template_path.suffix == '.in': |
| 45 | + data = template_path.read_text() |
| 46 | + |
| 47 | + variables = [] |
| 48 | + variables.extend(VARIABLE_RE.findall(data)) |
| 49 | + variables.extend(VARIABLE_RE.findall(str(template_path))) |
| 50 | + |
| 51 | + for v in variables: |
| 52 | + if v not in parser["vars"]: |
| 53 | + parser["vars"][v] = input(f"Value for {v}? ").strip() |
| 54 | + with open(VARS_FILENAME, "w") as f: |
| 55 | + parser.write(f) |
| 56 | + |
| 57 | + interpolated_data = variable_format(data, **parser["vars"]) |
| 58 | + |
| 59 | + local_path = template_path.with_suffix('').relative_to(TEMPLATE_DIR) |
| 60 | + local_path = Path(variable_format(str(local_path), **parser["vars"])) |
| 61 | + |
| 62 | + if local_path.exists(): |
| 63 | + existing_data = local_path.read_text() |
| 64 | + if existing_data == interpolated_data: |
| 65 | + print(f"Unchanged {local_path}") |
| 66 | + continue |
| 67 | + |
| 68 | + print(f"Writing {local_path}") |
| 69 | + local_path.parent.mkdir(parents=True, exist_ok=True) |
| 70 | + local_path.write_text(interpolated_data) |
71 | 71 |
|
72 | 72 |
|
73 | 73 | if __name__ == "__main__": |
|
0 commit comments