diff --git a/tools/apply.py b/tools/apply.py old mode 100644 new mode 100755 index dce5895..f4c25d4 --- a/tools/apply.py +++ b/tools/apply.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + import argparse import logging import pathlib @@ -6,7 +8,7 @@ SUCCESS = 0 FAILURE = 1 -ROOT = pathlib.Path(__file__).parent.parent +ROOT = pathlib.Path(__file__).resolve().parent.parent FILES_TO_COPY = [ ".github/workflows/tests.yaml", ".gitignore", @@ -19,12 +21,16 @@ def _copy_file_to_target( source: pathlib.Path, destination: pathlib.Path, + dry_run: bool, ) -> None: - destination.parent.mkdir(parents=True, exist_ok=True) - shutil.copy(source, destination) + if dry_run: + print(f"Copying '{source}' to '{destination}") + else: + destination.parent.mkdir(parents=True, exist_ok=True) + shutil.copy(source, destination) -def copy_files_to_target(target: pathlib.Path) -> int: +def copy_files_to_target(target: pathlib.Path, dry_run: bool) -> int: if not target.exists(): logging.error(f"target {target} does not exist") return FAILURE @@ -35,7 +41,11 @@ def copy_files_to_target(target: pathlib.Path) -> int: ret = SUCCESS for file in FILES_TO_COPY: try: - _copy_file_to_target(source=ROOT / file, destination=target / file) + _copy_file_to_target( + source=ROOT / file, + destination=target / file, + dry_run=dry_run, + ) except Exception as e: logging.error(str(e)) ret = FAILURE @@ -50,13 +60,21 @@ def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser() parser.add_argument("targets", nargs="*") + parser.add_argument( + "--dry-run", + action=argparse.BooleanOptionalAction, + default=False, + ) args = parser.parse_args(argv) ret = SUCCESS if not args.targets: parser.print_help() for target in args.targets: - ret |= copy_files_to_target(target=pathlib.Path(target).resolve()) + ret |= copy_files_to_target( + target=pathlib.Path(target).resolve(), + dry_run=args.dry_run, + ) return ret