Skip to content

Commit

Permalink
Merge pull request #158 from jedie/make-uv-python
Browse files Browse the repository at this point in the history
Make uv python
  • Loading branch information
jedie authored Nov 14, 2024
2 parents b14d71f + b5f6e8a commit e9e2dae
Show file tree
Hide file tree
Showing 37 changed files with 4,002 additions and 6 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ All Templates are tested via unittests!
Patches welcome!

[comment]: <> (✂✂✂ auto generated start ✂✂✂)
## uv based python package with a Makefile

* Requirement management with [uv](https://github.com/astral-sh/uv)
* Makefile with a simple "help" menu
* used [pyproject.toml](https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/) for everything
* Base requirements are only: `python3-venv` and `python3-pip` (uv will ne installed via pip in `.venv`)
* Has basic unittest


Cookiecutter template tests are here: [make-uv-python/tests.py](https://github.com/jedie/cookiecutter_templates/blob/main/make-uv-python/tests.py)


Use with vanilla [cookiecutter](https://github.com/cookiecutter/cookiecutter), e.g.:
```shell
cookiecutter https://github.com/jedie/cookiecutter_templates/ --directory make-uv-python
```

Use with [manageprojects](https://github.com/jedie/manageprojects), e.g.:
```shell
./cli.py start-project https://github.com/jedie/cookiecutter_templates/ --directory make-uv-python ~/foobar/
```

## Django Project

* Requirement management with [pip-tools](https://github.com/jazzband/pip-tools):
Expand Down Expand Up @@ -83,6 +105,7 @@ Use with [manageprojects](https://github.com/jedie/manageprojects), e.g.:
* [click](https://click.palletsprojects.com) based CLI for app and dev mode
* Auto virtualenv bootstrap, just by calling the `cli.py` / `dev-cli.py`
* used [pyproject.toml](https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/) for everything
* Base requirements are only: `python3-venv` and `python3-pip` (uv will ne installed via pip in `.venv`)
* Has basic unittest


Expand Down Expand Up @@ -207,6 +230,7 @@ Usage: ./dev-cli.py [OPTIONS] COMMAND [ARGS]...
[comment]: <> (✂✂✂ auto generated history start ✂✂✂)

* [v0.5.0](https://github.com/jedie/cookiecutter_templates/compare/v0.3.0...v0.5.0)
* 2024-11-14 - New template: "make-uv-python"
* 2024-11-13 - Migrate to uv
* 2024-11-13 - Bugfix uv-python
* 2024-10-29 - Add "uv-python" template
Expand Down
23 changes: 23 additions & 0 deletions generated_templates/make-uv-python/your_cool_package/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# see https://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{html,css,js}]
insert_final_newline = false

[*.py]
max_line_length = 119

[{Makefile,**.mk}]
indent_style = tab
insert_final_newline = false

[{*.yaml,*.yml}]
indent_size = 2
7 changes: 7 additions & 0 deletions generated_templates/make-uv-python/your_cool_package/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Move to pyproject.toml after: https://github.com/PyCQA/flake8/issues/234
#
[flake8]
exclude = .*, dist, htmlcov
#ignore = E402
max-line-length = 119
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@


name: tests

on:
push:
branches:
- master
pull_request:
schedule:
- cron: '0 8 * * *'

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.12", "3.11"]
steps:
- name: Checkout
run: |
echo $GITHUB_REF $GITHUB_SHA
git clone https://github.com/$GITHUB_REPOSITORY.git .
git fetch origin $GITHUB_SHA:temporary-ci-branch
git checkout $GITHUB_SHA || (git fetch && git checkout $GITHUB_SHA)
- name: 'Set up Python ${{ matrix.python-version }}'
uses: actions/setup-python@v5
# https://github.com/marketplace/actions/setup-python
with:
python-version: '${{ matrix.python-version }}'
cache: 'pip' # caching pip dependencies

- name: 'Install'
run: |
make install
- name: 'Run tests with Python v${{ matrix.python-version }}'
env:
PYTHONUNBUFFERED: 1
PYTHONWARNINGS: always
run: |
make coverage
- name: 'Upload coverage report'
uses: codecov/codecov-action@v4
# https://github.com/marketplace/actions/codecov
with:
fail_ci_if_error: false
verbose: true

13 changes: 13 additions & 0 deletions generated_templates/make-uv-python/your_cool_package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.*
*.egg-info
__pycache__
/dist/
/build/
/coverage.*
*.orig

!.github
!.editorconfig
!.flake8
!.gitignore
!.gitkeep
67 changes: 67 additions & 0 deletions generated_templates/make-uv-python/your_cool_package/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
SHELL := /bin/bash
export PATH := .venv/bin:$(PATH)

all: help

help: ## List all commands
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9 -_]+:.*?## / {printf "\033[36m%-26s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

install-base-req: ## Install needed base packages via apt
sudo apt install python3-pip python3-venv

install: ## Install the project in a Python virtualenv
python3 -m venv .venv
.venv/bin/pip install -U pip
.venv/bin/pip install -U uv
.venv/bin/uv sync

update-requirements: ## Update requirements
python3 -m venv .venv
.venv/bin/pip install -U pip
.venv/bin/pip install -U uv
.venv/bin/uv lock --upgrade
.venv/bin/uv sync

lint: ## Run code formatters and linter
.venv/bin/darker --diff --check
.venv/bin/flake8 .

fix-code-style: ## Fix code formatting
.venv/bin/darker
.venv/bin/flake8 .

tox-listenvs: ## List all tox test environments
.venv/bin/tox --listenvs

tox: ## Run tests via tox with all environments
.venv/bin/tox

test: ## Run tests
.venv/bin/python3 -m unittest --verbose --locals

coverage: ## Run tests with coverage
.venv/bin/coverage run
.venv/bin/coverage combine --append
.venv/bin/coverage report
.venv/bin/coverage xml
.venv/bin/coverage json

update-test-snapshot-files: ## Update all snapshot files (by remove and recreate all snapshot files)
find . -type f -name '*.snapshot.*' -delete
RAISE_SNAPSHOT_ERRORS=0 .venv/bin/python3 -m unittest

mypy: ## Run mypy
.venv/bin/mypy .

pip-audit: ## Run https://github.com/pypa/pip-audit
.venv/bin/uv export --no-header --frozen --no-editable --no-emit-project -o /tmp/temp_requirements.txt
.venv/bin/pip-audit --strict --require-hashes -r /tmp/temp_requirements.txt

publish: ## Release new version to PyPi
.venv/bin/pip install -e .
.venv/bin/python3 your_cool_package_tests/publish.py

clean: ## Remove created files from the test project
git clean -dfX your_cool_package_tests/

.PHONY: help install lint fix publish test clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# your_cool_package

[![tests](https://github.com/john-doh/your_cool_package/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/john-doh/your_cool_package/actions/workflows/tests.yml)
[![codecov](https://codecov.io/github/john-doh/your_cool_package/branch/main/graph/badge.svg)](https://app.codecov.io/github/john-doh/your_cool_package)
[![your_cool_package @ PyPi](https://img.shields.io/pypi/v/your_cool_package?label=your_cool_package%20%40%20PyPi)](https://pypi.org/project/your_cool_package/)
[![Python Versions](https://img.shields.io/pypi/pyversions/your_cool_package)](https://github.com/john-doh/your_cool_package/blob/main/pyproject.toml)
[![License GPL-3.0-or-later](https://img.shields.io/pypi/l/your_cool_package)](https://github.com/john-doh/your_cool_package/blob/main/LICENSE)

A minimal Python package
124 changes: 124 additions & 0 deletions generated_templates/make-uv-python/your_cool_package/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
[project]
name = "your_cool_package"
dynamic = ["version"]
description = "A minimal Python package"
license = {text = "GPL-3.0-or-later"}
readme = "README.md"
authors = [
{name = 'John Doh', email = '[email protected]'}
]
requires-python = ">=3.11"
dependencies = []
[dependency-groups]
dev = [
"manageprojects", # https://github.com/jedie/manageprojects
"urllib3", # for bx_py_utils.test_utils.deny_requests.deny_any_real_request() in tests
"uv", # https://github.com/astral-sh/uv
"tox", # https://github.com/tox-dev/tox
"tox-uv", # https://github.com/tox-dev/tox-uv
"coverage", # https://github.com/nedbat/coveragepy
"autopep8", # https://github.com/hhatto/autopep8
"pyupgrade", # https://github.com/asottile/pyupgrade
"flake8", # https://github.com/pycqa/flake8
"flake8-bugbear", # https://github.com/PyCQA/flake8-bugbear
"pyflakes", # https://github.com/PyCQA/pyflakes
"codespell", # https://github.com/codespell-project/codespell
"EditorConfig", # https://github.com/editorconfig/editorconfig-core-py
"pip-audit", # https://github.com/pypa/pip-audit
"mypy", # https://github.com/python/mypy
"twine", # https://github.com/pypa/twine
"typeguard", # https://github.com/agronholm/typeguard/

# https://github.com/akaihola/darker
# https://github.com/ikamensh/flynt
# https://github.com/pycqa/isort
# https://github.com/pygments/pygments
"darker[flynt, isort, color]",

# Work-a-round for: https://github.com/jazzband/pip-tools/issues/1866
# see also: https://github.com/jazzband/pip-tools/issues/994#issuecomment-1321226661
# backports.tarfile is needed for python <3.12
'backports.tarfile', # via jaraco-context -> keyring -> twine
]

[project.urls]
Documentation = "https://github.com/john-doh/your_cool_package/"
Source = "https://github.com/john-doh/your_cool_package/"

[project.scripts]
your_cool_package = "your_cool_package.__main__:main"

[build-system]
requires = ["setuptools>=61.0", "setuptools_scm>=7.1"]
build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
where = ["."]
include = ["your_cool_package*"]

[tool.setuptools.dynamic]
version = {attr = "your_cool_package.__version__"}


[tool.darker]
src = ['.']
revision = "origin/main..."
line_length = 119
color = true
skip_string_normalization = true
diff = false
check = false
stdout = false
isort = true
lint = []
log_level = "INFO"


[tool.isort]
profile = "black"


[tool.pytest.ini_options]
filterwarnings = []


[tool.coverage.run]
branch = true
parallel = true
source = ['.']
concurrency = ["multiprocessing"]
command_line = "-m unittest --locals --verbose"

[tool.coverage.report]
omit = ['.*', '*/tests/*']
skip_empty = true
fail_under = 30
show_missing = true
exclude_lines = [
'if self.debug:',
'pragma: no cover',
'raise NotImplementedError',
'if __name__ == .__main__.:',
]


[tool.tox] # https://tox.wiki/en/latest/config.html#pyproject-toml
isolated_build = true
env_list = ["3.13", "3.12", "3.11"]
skip_missing_interpreters = true

[tool.tox.env_run_base]
runner = "uv-venv-lock-runner"
with_dev = true
commands = [
["python3", "-m", "coverage", "run", "--context", "'{envname}'"]
]


[tool.mypy]
warn_unused_configs = true
ignore_missing_imports = true
allow_redefinition = true # https://github.com/python/mypy/issues/7165
show_error_codes = true
plugins = []
exclude = ['.venv', 'tests']
Loading

0 comments on commit e9e2dae

Please sign in to comment.