diff --git a/regen.py b/regen.py index 9b5331e..c64cdfd 100755 --- a/regen.py +++ b/regen.py @@ -4,12 +4,11 @@ # out. import configparser -import os import re from pathlib import Path from typing import Match -THIS_DIR = Path(os.path.abspath(__file__)).parent +THIS_DIR = Path(__file__).absolute().parent TEMPLATE_DIR = THIS_DIR / "templates" # This is very simplistic... @@ -41,33 +40,34 @@ def main() -> None: if "vars" not in parser: parser.add_section("vars") - for dirpath, dirnames, filenames in os.walk(TEMPLATE_DIR): - for fn in filenames: - if fn.endswith(".in"): - template_path = Path(dirpath) / fn - local_path = (Path(dirpath) / fn[:-3]).relative_to(TEMPLATE_DIR) - with template_path.open("r") as f: - data = f.read() - variables = VARIABLE_RE.findall(data) - for v in variables: - if v not in parser["vars"]: - parser["vars"][v] = input(f"Value for {v}? ").strip() - with open(VARS_FILENAME, "w") as f: - parser.write(f) - - interpolated_data = variable_format(data, **parser["vars"]) - - if local_path.exists(): - with local_path.open("r") as f: - existing_data = f.read() - if existing_data == interpolated_data: - print(f"Unchanged {local_path}") - continue - - print(f"Writing {local_path}") - local_path.parent.mkdir(parents=True, exist_ok=True) - with local_path.open("w") as f: - f.write(interpolated_data) + for template_path in TEMPLATE_DIR.glob('**/*'): + if template_path.suffix == '.in': + data = template_path.read_text() + + variables = [] + variables.extend(VARIABLE_RE.findall(data)) + variables.extend(VARIABLE_RE.findall(str(template_path))) + + for v in variables: + if v not in parser["vars"]: + parser["vars"][v] = input(f"Value for {v}? ").strip() + with open(VARS_FILENAME, "w") as f: + parser.write(f) + + interpolated_data = variable_format(data, **parser["vars"]) + + local_path = template_path.with_suffix('').relative_to(TEMPLATE_DIR) + local_path = Path(variable_format(str(local_path), **parser["vars"])) + + if local_path.exists(): + existing_data = local_path.read_text() + if existing_data == interpolated_data: + print(f"Unchanged {local_path}") + continue + + print(f"Writing {local_path}") + local_path.parent.mkdir(parents=True, exist_ok=True) + local_path.write_text(interpolated_data) if __name__ == "__main__":