Skip to content

Feature/uv #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 26, 2025
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
11 changes: 10 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"3.12",
"3.13"
],
"package_manager": [
"uv",
"pip"
],
"use_requests": [
"n",
"y"
Expand All @@ -43,7 +47,7 @@
"https"
],
"__template_repo": "https://github.com/btr1975/cookiecutter-python-fastapi-openapi",
"__template_version": "1.0.13",
"__template_version": "2.0.0",
"_new_lines": "\n",
"_copy_without_render": [
"{{cookiecutter.__app_name}}/templates",
Expand Down Expand Up @@ -75,6 +79,11 @@
"3.12": "3.12",
"3.13": "3.13"
},
"package_manager": {
"__prompt__": "Which pacakge manager for Python will be supported",
"uv": "UV By Astral",
"pip": "PIP (The built in Python Package Installer)"
},
"use_requests": {
"__prompt__": "Will you use the requests library",
"n": "No",
Expand Down
6 changes: 6 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
'{% if cookiecutter.container_runtime == "docker" %}containers/Containerfile{% endif %}',
]

REMOVE_PATHS_UV = [
'{% if cookiecutter.package_manager == "uv" %}requirements.txt{% endif %}',
'{% if cookiecutter.package_manager == "uv" %}requirements-dev.txt{% endif %}',
]


def remove_paths(paths_to_remove: List[str]) -> None:
"""Remove files and directories
Expand All @@ -41,3 +46,4 @@ def remove_paths(paths_to_remove: List[str]) -> None:
remove_paths(REMOVE_PATHS_NO_WEBPAGES)
remove_paths(REMOVE_PATHS_PODMAN)
remove_paths(REMOVE_PATHS_DOCKER)
remove_paths(REMOVE_PATHS_UV)
62 changes: 62 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ def bake_project_api_only_podman() -> dict:
"email": "name@example.com",
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"package_manager": "pip",
}

return options


@pytest.fixture
def bake_project_uv_api_only_podman() -> dict:
options = {
"git_repo_name": "api-only",
"include_webpages": "n",
"email": "name@example.com",
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"package_manager": "uv",
}

return options
Expand All @@ -28,6 +43,22 @@ def bake_project_api_only_docker() -> dict:
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"container_runtime": "docker",
"package_manager": "pip",
}

return options


@pytest.fixture
def bake_project_uv_api_only_docker() -> dict:
options = {
"git_repo_name": "api-only",
"include_webpages": "n",
"email": "name@example.com",
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"container_runtime": "docker",
"package_manager": "uv",
}

return options
Expand All @@ -41,6 +72,21 @@ def bake_project_api_with_webpages_podman() -> dict:
"email": "name@example.com",
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"package_manager": "pip",
}

return options


@pytest.fixture
def bake_project_uv_api_with_webpages_podman() -> dict:
options = {
"git_repo_name": "api-with-webpages",
"include_webpages": "y",
"email": "name@example.com",
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"package_manager": "uv",
}

return options
Expand All @@ -55,6 +101,22 @@ def bake_project_api_with_webpages_docker() -> dict:
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"container_runtime": "docker",
"package_manager": "pip",
}

return options


@pytest.fixture
def bake_project_uv_api_with_webpages_docker() -> dict:
options = {
"git_repo_name": "api-with-webpages",
"include_webpages": "y",
"email": "name@example.com",
"git_username": "some-username",
"git_url": "https://github.com/some-username/python-with-cli",
"container_runtime": "docker",
"package_manager": "uv",
}

return options
84 changes: 84 additions & 0 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ def test_bake_project_api_only_podman(cookies, bake_project_api_only_podman):
assert result.project_path.name == "api-only"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert result.project_path.joinpath("requirements.txt").is_file()
assert result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_only").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert result.project_path.joinpath("containers", "Containerfile").is_file()
assert not result.project_path.joinpath("containers", "Dockerfile").is_file()
assert not result.project_path.joinpath("api_only", "static").is_dir()
assert not result.project_path.joinpath("api_only", "templates").is_dir()
assert not result.project_path.joinpath("api_only", "routers", "hello_world.py").is_file()


def test_bake_project_uv_api_only_podman(cookies, bake_project_uv_api_only_podman):
result = cookies.bake(extra_context=bake_project_uv_api_only_podman)

assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == "api-only"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert not result.project_path.joinpath("requirements.txt").is_file()
assert not result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_only").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert result.project_path.joinpath("containers", "Containerfile").is_file()
Expand All @@ -23,6 +44,27 @@ def test_bake_project_api_only_docker(cookies, bake_project_api_only_docker):
assert result.project_path.name == "api-only"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert result.project_path.joinpath("requirements.txt").is_file()
assert result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_only").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert not result.project_path.joinpath("containers", "Containerfile").is_file()
assert result.project_path.joinpath("containers", "Dockerfile").is_file()
assert not result.project_path.joinpath("api_only", "static").is_dir()
assert not result.project_path.joinpath("api_only", "templates").is_dir()
assert not result.project_path.joinpath("api_only", "routers", "hello_world.py").is_file()


def test_bake_project_uv_api_only_docker(cookies, bake_project_uv_api_only_docker):
result = cookies.bake(extra_context=bake_project_uv_api_only_docker)

assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == "api-only"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert not result.project_path.joinpath("requirements.txt").is_file()
assert not result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_only").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert not result.project_path.joinpath("containers", "Containerfile").is_file()
Expand All @@ -40,6 +82,27 @@ def test_bake_project_api_with_webpages_podman(cookies, bake_project_api_with_we
assert result.project_path.name == "api-with-webpages"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert result.project_path.joinpath("requirements.txt").is_file()
assert result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_with_webpages").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert result.project_path.joinpath("containers", "Containerfile").is_file()
assert not result.project_path.joinpath("containers", "Dockerfile").is_file()
assert result.project_path.joinpath("api_with_webpages", "static").is_dir()
assert result.project_path.joinpath("api_with_webpages", "templates").is_dir()
assert result.project_path.joinpath("api_with_webpages", "routers", "hello_world.py").is_file()


def test_bake_project_uv_api_with_webpages_podman(cookies, bake_project_uv_api_with_webpages_podman):
result = cookies.bake(extra_context=bake_project_uv_api_with_webpages_podman)

assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == "api-with-webpages"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert not result.project_path.joinpath("requirements.txt").is_file()
assert not result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_with_webpages").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert result.project_path.joinpath("containers", "Containerfile").is_file()
Expand All @@ -57,6 +120,27 @@ def test_bake_project_api_with_webpages_docker(cookies, bake_project_api_with_we
assert result.project_path.name == "api-with-webpages"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert result.project_path.joinpath("requirements.txt").is_file()
assert result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_with_webpages").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert not result.project_path.joinpath("containers", "Containerfile").is_file()
assert result.project_path.joinpath("containers", "Dockerfile").is_file()
assert result.project_path.joinpath("api_with_webpages", "static").is_dir()
assert result.project_path.joinpath("api_with_webpages", "templates").is_dir()
assert result.project_path.joinpath("api_with_webpages", "routers", "hello_world.py").is_file()


def test_bake_project_uv_api_with_webpages_docker(cookies, bake_project_uv_api_with_webpages_docker):
result = cookies.bake(extra_context=bake_project_uv_api_with_webpages_docker)

assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == "api-with-webpages"
assert result.project_path.is_dir()
assert result.project_path.joinpath("README.md").is_file()
assert not result.project_path.joinpath("requirements.txt").is_file()
assert not result.project_path.joinpath("requirements-dev.txt").is_file()
assert result.project_path.joinpath("api_with_webpages").is_dir()
assert result.project_path.joinpath("containers").is_dir()
assert not result.project_path.joinpath("containers", "Containerfile").is_file()
Expand Down
74 changes: 56 additions & 18 deletions {{cookiecutter.git_repo_name}}/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Makefile for project needs
# Author: Ben Trachtenberg
# Version: 1.0.8
# Version: 2.0.0
#

.PHONY: all info build build-container coverage format pylint pytest gh-pages build dev-run start-container \
stop-container remove-container check-vuln check-security
stop-container remove-container check-vuln check-security pip-export

info:
@echo "make options"
Expand All @@ -21,23 +21,16 @@ info:
@echo " start-container To start the container"
@echo " stop-container To stop the container"
@echo " remove-container To remove the container"
{% if cookiecutter.package_manager == 'uv' %} @echo " pip-export To export the requirements to requirements.txt and requirements-dev.txt"{% endif %}
{% if cookiecutter.app_documents_location == 'github-pages' %} @echo " gh-pages To create the GitHub pages"{% endif %}

{% if cookiecutter.package_manager == 'pip' %}

all: format pylint coverage check-security check-vuln

build:
@python -m build

{% if cookiecutter.container_runtime == "podman" %}
build-container:
@cd containers && podman build --ssh=default --build-arg=build_branch=main -t {{ cookiecutter.git_repo_name }}:latest -f Containerfile
{% endif %}

{% if cookiecutter.container_runtime == "docker" %}
build-container:
@cd containers && docker build --ssh=default --build-arg=build_branch=main -t {{ cookiecutter.git_repo_name }}:latest -f Dockerfile
{% endif %}

coverage:
@pytest --cov --cov-report=html -vvv

Expand All @@ -54,14 +47,62 @@ pytest:
dev-run:
@python -c "from {{cookiecutter.__app_name}} import cli;cli()" start -p 8080 -r

check-vuln:
@pip-audit -r requirements.txt

check-security:
@bandit -c pyproject.toml -r .

{% if cookiecutter.app_documents_location == 'github-pages' %}
gh-pages:
@rm -rf ./docs/source/code
@sphinx-apidoc -o ./docs/source/code ./{{cookiecutter.__app_name}}
@sphinx-build ./docs ./docs/gh-pages
{% endif %}

{% elif cookiecutter.package_manager == 'uv' %}

all: format pylint coverage check-security pip-export

build:
@uv build --wheel --sdist

coverage:
@uv run pytest --cov --cov-report=html -vvv

format:
@uv run black {{cookiecutter.__app_name}}/
@uv run black tests/

pylint:
@uv run pylint {{cookiecutter.__app_name}}/

pytest:
@uv run pytest --cov -vvv

dev-run:
@uv run python -c "from {{cookiecutter.__app_name}} import cli;cli()" start -p 8080 -r

check-security:
@uv run bandit -c pyproject.toml -r .

pip-export:
@uv export --no-dev --no-emit-project --no-editable > requirements.txt
@uv export --no-emit-project --no-editable > requirements-dev.txt

{% if cookiecutter.app_documents_location == 'github-pages' %}
gh-pages:
@rm -rf ./docs/source/code
@uv run sphinx-apidoc -o ./docs/source/code ./{{cookiecutter.__app_name}}
@uv run sphinx-build ./docs ./docs/gh-pages
{% endif %}

{% endif %}

{% if cookiecutter.container_runtime == "podman" %}
build-container:
@cd containers && podman build --ssh=default --build-arg=build_branch=main -t {{ cookiecutter.git_repo_name }}:latest -f Containerfile

start-container:
@podman run -itd --name {{ cookiecutter.git_repo_name }} -p 8080:8080 localhost/{{ cookiecutter.git_repo_name }}:latest

Expand All @@ -73,6 +114,9 @@ remove-container:
{% endif %}

{% if cookiecutter.container_runtime == "docker" %}
build-container:
@cd containers && docker build --ssh=default --build-arg=build_branch=main -t {{ cookiecutter.git_repo_name }}:latest -f Dockerfile

start-container:
@docker run -itd --name {{ cookiecutter.git_repo_name }} -p 8080:8080 localhost/{{ cookiecutter.git_repo_name }}:latest

Expand All @@ -82,9 +126,3 @@ stop-container:
remove-container:
@docker rm {{ cookiecutter.git_repo_name }}
{% endif %}

check-vuln:
@pip-audit -r requirements.txt

check-security:
@bandit -c pyproject.toml -r .
Loading