Skip to content
Open
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
67 changes: 0 additions & 67 deletions .github/workflows/ci-pip-tools.yml

This file was deleted.

64 changes: 0 additions & 64 deletions .github/workflows/ci-poetry.yml

This file was deleted.

13 changes: 4 additions & 9 deletions .github/workflows/ci-uv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ jobs:
prune-cache: false
python-version: ${{ matrix.python-version }}

- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
cache: pip

- name: Install dependencies
run: pip install -r dev-requirements.txt
run: uv sync

- name: Run unit tests
run: pytest
run: uv run -m pytest
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need -m for pytest but not for cookicutter?


- name: Create project from template
# Choose default options
run: cookiecutter . --no-input packaging=uv rse_team_as_coauthor=true use_bsd3_license=true
run: uv run cookiecutter . --no-input packaging=uv rse_team_as_coauthor=true use_bsd3_license=true

- name: Install project dependencies
working-directory: my_project
Expand All @@ -52,7 +47,7 @@ jobs:

- name: Run tests
working-directory: my_project
run: uv run pytest
run: uv run -m pytest

- name: Run pre-commit hooks for project
working-directory: my_project
Expand Down
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!-- markdownlint-disable MD041 -->
[![poetry](https://github.com/ImperialCollegeLondon/python-template/actions/workflows/ci-poetry.yml/badge.svg)](https://github.com/ImperialCollegeLondon/python-template/actions/workflows/ci-poetry.yml)
[![pip-tools](https://github.com/ImperialCollegeLondon/python-template/actions/workflows/ci-pip-tools.yml/badge.svg)](https://github.com/ImperialCollegeLondon/python-template/actions/workflows/ci-pip-tools.yml)

[![uv](https://github.com/ImperialCollegeLondon/python-template/actions/workflows/ci-uv.yml/badge.svg)](https://github.com/ImperialCollegeLondon/python-template/actions/workflows/ci-uv.yml)

# Python project template
Expand All @@ -9,8 +8,7 @@ This repo contains a [`cookiecutter`] template for a Python project, including
[`pre-commit`] hooks for linting and GitHub Actions for automatically running tests
using [`pytest`].

The template supports three Python packaging tools: [`poetry`], [`pip-tools`]
and [`uv`].
The template supports the Python packaging tool [`uv`].

It is particularly geared towards the needs of Imperial College's [Research Software
Engineering team], but hopefully it should be generically useful. If you find any
Expand All @@ -19,8 +17,6 @@ problems or have any questions, please [raise an issue].
[`cookiecutter`]: https://cookiecutter.readthedocs.io/en/stable/
[`pre-commit`]: https://pre-commit.com/
[`pytest`]: https://pytest.org/
[`poetry`]: https://python-poetry.org/
[`pip-tools`]: https://github.com/jazzband/pip-tools
[`uv`]: https://docs.astral.sh/uv/
[Research Software Engineering team]: https://www.imperial.ac.uk/admin-services/ict/self-service/research-support/rcs/service-offering/research-software-engineering/
[raise an issue]: https://github.com/ImperialCollegeLondon/python-template/issues/new
Expand All @@ -30,13 +26,13 @@ problems or have any questions, please [raise an issue].
To use this template for your own application:

1. [Install `cookiecutter`] following the instructions for your OS.
2. Create your own project using this template: `cookiecutter
gh:ImperialCollegeLondon/python-template`
3. Choose the options you want for your project
4. To get started, follow the instructions in the readme of the newly created project
2. [Install `uv`] followiung the instructions for your OS.
3. Create your own project using this template: `cookiecutter
gh:ImperialCollegeLondon/python-template`
Comment thread
Sahil590 marked this conversation as resolved.
4. Choose the options you want for your project
5. To get started, follow the instructions in the readme of the newly created project

[Install `cookiecutter`]:
https://cookiecutter.readthedocs.io/en/stable/README.html#installation
[Install `cookiecutter`]: https://cookiecutter.readthedocs.io/en/stable/README.html#installation

# Extra setup

Expand Down
6 changes: 1 addition & 5 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
"author_email": "jane_doe@imperial.ac.uk",
"rse_team_as_coauthor": false,
"use_bsd3_license": false,
"packaging": [
"poetry",
"pip-tools",
"uv"
],
"packaging": "uv",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As now there's only one option, we do not need to ask about it.

"mkdocs": true,
"add_precommit_workflows": true,
Comment thread
Sahil590 marked this conversation as resolved.
"automerge_bot_prs": false,
Comment on lines 7 to 12
Expand Down
3 changes: 0 additions & 3 deletions dev-requirements.txt

This file was deleted.

99 changes: 38 additions & 61 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,62 @@
import subprocess
import os
import re
from glob import glob
from shutil import rmtree

REMOVE_PATHS = (
"{% if not cookiecutter.use_bsd3_license %}LICENSE{% endif %}",
"{% if not cookiecutter.add_precommit_workflows %}.github/workflows/pre-commit*.yml{% endif %}",
"{% if not cookiecutter.automerge_bot_prs %}.github/workflows/auto-merge.yml{% endif %}",
"{% if cookiecutter.packaging != 'pip-tools' %}requirements.txt{% endif %}",
"{% if cookiecutter.packaging != 'pip-tools' %}dev-requirements.txt{% endif %}",
"{% if cookiecutter.packaging != 'pip-tools' or not cookiecutter.mkdocs %}doc-requirements.txt{% endif %}",
"{% if not cookiecutter.mkdocs %}docs{% endif %}",
"{% if not cookiecutter.mkdocs %}.github/workflows/docs.yml{% endif %}",
"README.*.jinja",
)

DEV_DEPS = (
"ruff>=0.15.10",
"mypy>=1.20.1",
"pre-commit>=4.5.1",
"pytest>=9.0.3",
"pytest-cov>=7.1.0",
"pytest-mock>=3.15.1",
)

def main():
# {% if cookiecutter.packaging == 'poetry' %}
update_poetry_dependencies()
# {% endif %}
# {% if cookiecutter.packaging == 'uv' %}
update_uv_dependencies()
# {% endif %}
remove_unneeded_files()

# mkdocs v2 will include breaking changes.
# See: https://github.com/ImperialCollegeLondon/python-template/discussions/530
DOC_DEPS = (
"mkdocs>=1.6.1,<2.0.0",
"mkdocstrings>=1.0.3",
"mkdocstrings-python>=2.0.3",
"mkdocs-material>=9.7.6",
"mkdocs-gen-files>=0.6.1",
"mkdocs-literate-nav>=0.6.3",
"mkdocs-section-index>=0.3.11",
)

def read_package_versions(*filenames: str) -> dict[str, str]:
"""Read package versions from *requirements.txt."""
regex = re.compile(r"^([^# ]+)==(.+)$")
packages: dict[str, str] = {}
for filename in filenames:
with open(filename) as f:
for line in f.readlines():
if match := regex.match(line):
name = match.group(1).lower()
version = match.group(2)
packages[name] = version
return packages
MKDOCS_ENABLED = "{{ cookiecutter.mkdocs }}" == "True"


def update_poetry_dependencies(pyproject_path: str = "pyproject.toml"):
"""Patch the versions of packages in pyproject.toml."""
packages = read_package_versions(
"doc-requirements.txt", "dev-requirements.txt", "requirements.txt"
)
def main():
check_uv_installed()
add_uv_dependencies()
remove_unneeded_files()

output = ""
regex = re.compile(r'^([^ ]+) = "VERSION"$')
with open(pyproject_path) as f:
for line in f.readlines():
if match := regex.match(line):
name = match.group(1)
output += f'{name} = "^{packages[name.lower()]}"\n'
else:
output += line
with open(pyproject_path, "w") as f:
f.write(output)

def check_uv_installed():
try:
subprocess.run(["uv", "--version"], check=True, stdout=subprocess.DEVNULL)
except FileNotFoundError:
print(
"Error: 'uv' command not found. Please install 'uv' to use this template."
)
exit(1)

def update_uv_dependencies(pyproject_path: str = "pyproject.toml"):
"""Patch the versions of packages in pyproject.toml."""
packages = read_package_versions(
"doc-requirements.txt", "dev-requirements.txt", "requirements.txt"
)

output = ""
regex = re.compile(r'"([A-Za-z0-9_\-]+)\s*([=<>!~]+)\s*VERSION\s*(.*)"')
with open(pyproject_path) as f:
for line in f.readlines():
if match := regex.match(line.strip()):
name = match.group(1)
operator = match.group(2)
version = packages[name.lower()]
trailing = match.group(3)
comma = "," if line.strip().endswith(",") else ""
output += f' "{name}{operator}{version}{trailing}"{comma}\n'
else:
output += line
with open(pyproject_path, "w") as f:
f.write(output)
def add_uv_dependencies():
subprocess.run(["git", "init"], check=True)
subprocess.run(["uv", "add", "--dev", *DEV_DEPS], check=True)
if MKDOCS_ENABLED:
subprocess.run(["uv", "add", "--group", "doc", *DOC_DEPS], check=True)
Comment thread
Sahil590 marked this conversation as resolved.
Comment thread
Sahil590 marked this conversation as resolved.
Comment on lines +55 to +59


def remove_unneeded_files():
Expand Down
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "python-template"
version = "0.1.0"
requires-python = ">=3.14"

[dependency-groups]
dev = [
"cookiecutter>=2.7.1",
"pytest>=9.0.3",
]

[tool.uv]
package = false
Loading
Loading