Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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",
Expand All @@ -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]
Expand Down
35 changes: 32 additions & 3 deletions tools/apply.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import argparse
import dataclasses
import logging
import pathlib
import shutil
Expand All @@ -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:
Expand All @@ -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))
Expand Down