diff --git a/pyproject.toml b/pyproject.toml index 5e60005..b691f80 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,6 @@ # yaml-language-server: $schema=https://json.schemastore.org/pyproject.json - [build-system] -requires = ["uv_build>=0.8"] +requires = ["uv_build"] build-backend = "uv_build" [project] @@ -16,8 +15,8 @@ dependencies = [] [dependency-groups] dev = [ - "poethepoet>=0.42.1", - "pre-commit>=4.5.1", + "poethepoet>=0.46.0", + "pre-commit>=4.6.0", ] test = [ "covdefaults>=2.3.0", @@ -27,14 +26,7 @@ test = [ [tool.uv] -required-version = ">=0.8" default-groups = "all" -#package = false - -#[tool.uv.build-backend] -#module-name = "src" -#module-root = "" -#namespace = true [tool.poe] diff --git a/tools/apply.py b/tools/apply.py index f4c25d4..620d87c 100755 --- a/tools/apply.py +++ b/tools/apply.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import argparse +import dataclasses import logging import pathlib import shutil @@ -9,25 +10,50 @@ SUCCESS = 0 FAILURE = 1 ROOT = pathlib.Path(__file__).resolve().parent.parent +THIS_REPO_NAME = "python-template" FILES_TO_COPY = [ ".github/workflows/tests.yaml", ".gitignore", ".pre-commit-config.yaml", "LICENSE", "pyproject.toml", + "README.md", ] +@dataclasses.dataclass +class Context: + dry_run: bool | None + target_repo: pathlib.Path | None + + +def _apply_template(target: pathlib.Path, context: Context) -> None: + # Right now, this just replaces `python-template` with the name of the + # target repo, but this could be extended to, say, apply Jinja templates + assert context.target_repo is not None # noqa: S101 + target.write_text( + ( + target.read_text(encoding="utf-8").replace( + THIS_REPO_NAME, context.target_repo.name + ) + ), + encoding="utf-8", + ) + + def _copy_file_to_target( source: pathlib.Path, destination: pathlib.Path, - dry_run: bool, + context: Context, ) -> None: - if dry_run: + if context.dry_run: print(f"Copying '{source}' to '{destination}") else: destination.parent.mkdir(parents=True, exist_ok=True) + # This is not atomic: it copies, then modifies. For atomicity, we should + # modify in transit or modify a temporary intermediate file shutil.copy(source, destination) + _apply_template(destination, context) def copy_files_to_target(target: pathlib.Path, dry_run: bool) -> int: @@ -44,7 +70,10 @@ def copy_files_to_target(target: pathlib.Path, dry_run: bool) -> int: _copy_file_to_target( source=ROOT / file, destination=target / file, - dry_run=dry_run, + context=Context( + dry_run=dry_run, + target_repo=target, + ), ) except Exception as e: logging.error(str(e))