Skip to content
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

Convert template to use PEP-621/metadata in pyproject.toml #5

Merged
merged 1 commit into from
Sep 28, 2023
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
53 changes: 30 additions & 23 deletions .github/workflows/test-cookiecutters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ on:
jobs:
build-cli:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.9', '3.10']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: |
3.8
3.9
3.10
3.11

- name: Install dependencies
run: |
pip install cookiecutter tox tox-gh-actions
pip install cookiecutter tox

- run: mkdir -p .tmp/

Expand All @@ -39,20 +40,21 @@ jobs:

build-lib:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: |
3.8
3.9
3.10
3.11

- name: Install dependencies
run: |
pip install cookiecutter tox tox-gh-actions build twine
pip install cookiecutter tox build twine

- run: mkdir -p .tmp/

Expand All @@ -72,27 +74,32 @@ jobs:

build-service:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version-loop }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: |
3.8
3.9
3.10
3.11

- name: Install dependencies
run: |
pip install cookiecutter tox tox-gh-actions
pip install cookiecutter tox

- run: mkdir -p .tmp/

- name: Build cli
- name: Build service
working-directory: .tmp
run: cookiecutter --config-file ../tests/service.yaml --no-input ../

- name: Generate requirements
working-directory: .tmp/python_service
run: tox run -m requirements

- name: Run tox
working-directory: .tmp/python_service
run: tox
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ A generated example can be found at https://github.com/vikahl/python-example
- Always package the Python code as a library, even if is a service that will
not be distributed through PyPI. Doing this makes it easy to write tests or
extend functionality in various directions.
- Use [Setuptools] as the build system. It is a proven build system that has
gotten much better with the static configuration files (`setup.cfg`) and PEP
517 compliant builds. I do not see a reason to use another system.
- Use [Setuptools] as the build system. It is a proven build system that
supports the latest standards. I do not see a reason to use another system.
- Use Github Actions to run tests configured in tox and build and upload to
PyPI for libraries.
- Selection for library, cli and/or service support.
Expand All @@ -28,12 +27,13 @@ A generated example can be found at https://github.com/vikahl/python-example
package.
- __module_name__: Project name, following specification in [PEP 508]. Will be
suggested based on the project name.
- __author__: Name of the project author. Added in package metadata (setup.cfg)
- __author__: Name of the project author. Added in package metadata
(pyproject.toml)
and in some cases the license.
- __email__: Email for the project author. Added in package metadata (setup.cfg).
- __homepage__: Homepage for the project. Added in package metadata (setup.cfg).
- __email__: Email for the project author. Added in package metadata (pyproject.toml).
- __homepage__: Homepage for the project. Added in package metadata (pyproject.toml).
- __license__: Select a license for the project. Will add a trove classifier
(in setup.cfg) and a license file. If another license is needed they can be
(in pyproject.toml) and a license file. If another license is needed they can be
replaced later.
- __version__: Initial version.
- __min_python__: Minimum Python version that is supported. Tox will generate
Expand All @@ -42,20 +42,20 @@ A generated example can be found at https://github.com/vikahl/python-example
- __library__: Toggle library functions, adds a Github workflow for uploading
to PyPi.
- __cli__: Toggle cli functions, adds a basic cli (based on Typer) with
entrypoint defined in setup.cfg.
entrypoint defined in pyproject.toml.
- __service__: Toggle service functions, adds a dockerfile and instructions on
compiled requirements.

## Linters and tools

_A note about configs_: This template aims to collect all configuration in
`pyproject.toml`, except for Setuptools which uses `setup.cfg` and tox which
uses `tox.ini` (until tox better supports pyproject.toml).
`pyproject.toml`, except for tox which uses `tox.ini` until tox better supports
pyproject.toml.

The linters and formatters are configured in tox and can be invoked by running
`tox`. Github Actions ensures the same linters and formatters are run in CI.

- [Setuptools]: Packaging tool. Uses `setup.cfg` for package configuration.
- [Setuptools]: Packaging tool. Uses `pyproject.toml` for package configuration.
- [tox](https://tox.wiki/en/latest/): Test automation framework. Run in Github
actions and can be run locally.
- [Black](https://github.com/psf/black): Code formatter, formats the code in a
Expand Down
4 changes: 2 additions & 2 deletions cookiecutter.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"h": "]\nA reasonable modern Python project template.\n\n- __project_name__: Human friendly name for the project.\n- __description__: Short description of the project. Added to the README and\n package.\n- __module_name__: Project name, following specification in [PEP 508]. Will be\n suggested based on the project name.\n- __author__: Name of the project author. Added in package metadata (setup.cfg)\n and in some cases the license.\n- __email__: Email for the project author. Added in package metadata (setup.cfg).\n- __homepage__: Homepage for the project. Added in package metadata (setup.cfg).\n- __license__: Select a license for the project. Will add a trove classifier\n (in setup.cfg) and a license file. If another license is needed they can be\n replaced later.\n- __version__: Initial version.\n- __min_python__: Minimum Python version that is supported. Tox will generate\n test environments for all newer versions. Backported modules might be used\n for older Python versions.\n- __library__: Toggle library functions, adds a Github workflow for uploading\n to PyPi.\n- __cli__: Toggle cli functions, adds a basic cli (based on Typer) with\n entrypoint defined in setup.cfg.\n- __service__: Toggle service functions, adds a dockerfile and instructions on\n compiled requirements.\n\n [Press enter to continue",
"h": "]\nA reasonable modern Python project template.\n\n- __project_name__: Human friendly name for the project.\n- __description__: Short description of the project. Added to the README and\n package.\n- __module_name__: Project name, following specification in [PEP 508]. Will be\n suggested based on the project name.\n- __author__: Name of the project author. Added in package metadata (pyproject.toml)\n and in some cases the license.\n- __email__: Email for the project author. Added in package metadata (pyproject.toml).\n- __homepage__: Homepage for the project. Added in package metadata (pyproject.toml).\n- __license__: Select a license for the project. Will add a trove classifier\n (in pyproject.toml) and a license file. If another license is needed they can be\n replaced later.\n- __version__: Initial version.\n- __min_python__: Minimum Python version that is supported. Tox will generate\n test environments for all newer versions. Backported modules might be used\n for older Python versions.\n- __library__: Toggle library functions, adds a Github workflow for uploading\n to PyPi.\n- __cli__: Toggle cli functions, adds a basic cli (based on Typer) with\n entrypoint defined in pyproject.toml.\n- __service__: Toggle service functions, adds a dockerfile and instructions on\n compiled requirements.\n\n [Press enter to continue",

"project_name": "Python project template",
"description": "",
Expand All @@ -11,7 +11,7 @@
"Apache - explicitly grants patent rights",
"GPL - ensures that code based on this is shared with the same terms"],
"version": "0.0.1",
"min_python": "3.7",
"min_python": "3.8",

"library": ["y", "n"],
"cli": ["n", "y"],
Expand Down
2 changes: 1 addition & 1 deletion tests/lib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ default_context:
homepage: "example.com"
license: "MIT - simple and permissive"
version: "1.0.0"
min_python: "3.7"
min_python: "3.8"
library: "y"
cli: "n"
service: "n"
2 changes: 1 addition & 1 deletion tests/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ default_context:
homepage: ""
license: "GPL - ensures that code based on this is shared with the same terms"
version: "0.0.1"
min_python: "3.7"
min_python: "3.8"
library: "n"
cli: "n"
service: "y"
25 changes: 9 additions & 16 deletions {{cookiecutter.module_name}}/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# This workflow runs the test suite defined in tox.
#
# Note the python-version matrix, if new Python versions are wanted they need
# to be added in this matrix AS WELL as in the tox.ini file for
# "tox-gh-actions". Read more about tox-gh-actions on
# https://github.com/ymyzk/tox-gh-actions
name: Test package

on:
Expand All @@ -15,26 +11,23 @@ on:
jobs:
run-tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [{%- for version in (range(cookiecutter.min_python | replace('3.', '') | int, 11)) -%}'3.{{ version }}'{{ ", " if not loop.last else ""}}{%- endfor %}]

{% raw -%}
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: |
{% for version in (range(cookiecutter.min_python | replace('3.', '') | int, 11)) -%}
{{10 * " "}}3.{{ version }}{{ "\n" if not loop.last else ""}}
{%- endfor %}

# To get completely reproducible builds these dependencies should be
# pinned. For most smaller projects it should be ok to leave them
# unpinend as they are stable projects.
# unpinned as they are stable projects.
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
pip install tox

- name: Run tox
run: tox
{%- endraw %}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: '3.x'

Expand All @@ -27,14 +27,13 @@ jobs:
# unpinend as they are stable projects.
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build

- name: Build package
run: python -m build

- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: '3.x'

Expand All @@ -23,7 +23,6 @@ jobs:
# unpinend as they are stable projects.
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine

- name: Build package
Expand Down
91 changes: 87 additions & 4 deletions {{cookiecutter.module_name}}/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,92 @@
[build-system]
# >= 51 is required since "entry_points" in setup.cfg was added in this
# version.
requires = ["setuptools>=51.0", "wheel"]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "{{ cookiecutter.module_name }}"
description = "{{ cookiecutter.description }}"
{% if cookiecutter.author or cookiecutter.email -%}
authors = [
{ {% if cookiecutter.author -%} name = "{{ cookiecutter.author }}",{% endif -%}{% if cookiecutter.email -%} email = "{{ cookiecutter.email }}" {% endif -%} }
]
{% endif -%}
{%- if cookiecutter.homepage %}
urls = { homepage = "{{ cookiecutter.homepage }}" }
{% endif -%}

classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
{%- for version in (range(cookiecutter.min_python | replace('3.', '') | int, 11) | list) %}
"Programming Language :: Python :: 3.{{ version }}",
{%- endfor %}
{%- if cookiecutter.license == "MIT - simple and permissive" %}
"License :: OSI Approved :: MIT License"
{%- elif cookiecutter.license == "Apache - explicitly grants patent rights" %}
"License :: OSI Approved :: Apache Software License"
{%- elif cookiecutter.license == "GPL - ensures that code based on this is shared with the same terms" %}
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)"
{%- endif %}
]
dynamic = ["version", "readme"]

requires-python = ">={{ cookiecutter.min_python }}"

# Specify the packages required by the project. For libraries, the constraints
# should be loosely defined as too narrow constraints will make it hard to
# install the package together with other libraries and will prevent users from
# getting security updates.
{% if cookiecutter.service == "y" %}
# For services, the dependencies (including transitive) should be compiled with
# pip-tools. See requirements.txt for details.
{% elif cookiecutter.cli == "y" -%}
dependencies = [
"typer"
]
{% else %}
# dependencies = [
# "some_package"
# ]
{%- endif %}

[project.optional-dependencies]
# pip-compile doesn't work well with extras referencing the package itself when
# hashes are required, as the package itself will be added to the list.
# Therefore the dependency groups "dev" is just for developer convenience and
# should not list dependencies itself. The compilation in tox lists all extras
# explicitly (i.e., --extra format,lint,types,test
dev = [ # All dev dependencies
"{{ cookiecutter.module_name }}[format]",
"{{ cookiecutter.module_name }}[lint]",
"{{ cookiecutter.module_name }}[test]",
]
format = [
"black",
"isort"
]
lint = [
"black",
"isort",
"pylint"
]
types = [
"mypy"
]
test = [
"pytest",
"pytest-cov"
]

{%- if cookiecutter.cli == "y" %}
[project.scripts]
{{ cookiecutter.module_name }} = "{{ cookiecutter.module_name }}:app"
{%- endif %}

[tool.setuptools.dynamic]
version = {attr = "{{ cookiecutter.module_name }}.__version__"}
readme = {file = "README.md", content-type = "text/markdown"}

[tool.isort]
# For compatibility with Black
profile = "black"
Expand Down Expand Up @@ -36,7 +119,7 @@ source = ["src", ".tox/*/site-packages/"]
show_missing = true

[tool.pylint.'FORMAT']
# Black defaults to 88 character line length,
# Black defaults to 88 character line length,
max-line-length = 88

[tool.pylint.'MESSAGES_CONTROL']
Expand Down
Loading