Skip to content

Commit

Permalink
Update cookiecutter-snekpack to publish using uv (#35)
Browse files Browse the repository at this point in the history
Closes #29 thanks to the help of documentation in
astral-sh/uv#7963 and
astral-sh/uv#8806
  • Loading branch information
cthoyt authored Nov 5, 2024
1 parent 6c5dac2 commit 12edfcf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 88 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Your new python package will have the following:
- Testing of MANIFEST correctness with `check-manifest`
- Testing of optional static typing with `mypy`
- Version management with [`bump-my-version`](https://github.com/callowayproject/bump-my-version)
- Building with `uv build`
- Releasing to PyPI with `twine`
- Building with [`uv build`](https://docs.astral.sh/uv/guides/publish/#building-your-package)
- Releasing to PyPI with [`uv publish`](https://docs.astral.sh/uv/guides/publish/#publishing-your-package)
- A command line interface with `click`
- A vanity CLI via python entrypoints
- A `py.typed` file so other packages can use your type hints
Expand Down
32 changes: 8 additions & 24 deletions {{cookiecutter.package_name}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,30 +221,15 @@ You only have to do the following steps once.

#### Configuring your machine's connection to PyPI

You have to do the following steps once per machine. Create a file in your home directory called
`.pypirc` and include the following:

```ini
[distutils]
index-servers =
pypi
testpypi

[pypi]
username = __token__
password = <the API token you just got>

# This block is optional in case you want to be able to make test releases to the Test PyPI server
[testpypi]
repository = https://test.pypi.org/legacy/
username = __token__
password = <an API token from test PyPI>
You have to do the following steps once per machine.

```console
$ uv tool install keyring
$ keyring set https://upload.pypi.org/legacy/ __token__
$ keyring set https://test.pypi.org/legacy/ __token__
```

Note that since PyPI is requiring token-based authentication, we use `__token__` as the user, verbatim.
If you already have a `.pypirc` file with a `[distutils]` section, just make sure that there is an `index-servers`
key and that `pypi` is in its associated list. More information on configuring the `.pypirc` file can
be found [here](https://packaging.python.org/en/latest/specifications/pypirc).
Note that this deprecates previous workflows using `.pypirc`.

#### Uploading to PyPI

Expand All @@ -263,8 +248,7 @@ This script does the following:
and [`docs/source/conf.py`](docs/source/conf.py) to not have the `-dev` suffix
2. Packages the code in both a tar archive and a wheel using
[`uv build`](https://docs.astral.sh/uv/guides/publish/#building-your-package)
3. Uploads to PyPI using [`twine upload`](https://github.com/pypa/twine).
This will be replaced soon with `uv publish` (see https://github.com/cthoyt/cookiecutter-snekpack/issues/29)
3. Uploads to PyPI using [`uv publish`](https://docs.astral.sh/uv/guides/publish/#publishing-your-package).
4. Push to GitHub. You'll need to make a release going with the commit where the version was bumped.
5. Bump the version to the next patch. If you made big changes and want to bump the version by minor, you can
use `tox -e bumpversion -- minor` after.
Expand Down
4 changes: 2 additions & 2 deletions {{cookiecutter.package_name}}/docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ It comes with the following:
- Testing of MANIFEST correctness with ``check-manifest``
- Testing of optional static typing with ``mypy``
- Version management with `bump-my-version <https://github.com/callowayproject/bump-my-version>`_
- Building with ``uv build``
- Releasing to PyPI with ``twine``
- Building with `uv build <https://docs.astral.sh/uv/guides/publish/#building-your-package>`_
- Releasing to PyPI with `uv publish <https://docs.astral.sh/uv/guides/publish/#publishing-your-package>`_
- A command line interface with ``click``
- A vanity CLI via python entrypoints
- A `py.typed` file so other packages can use your type hints
Expand Down
34 changes: 19 additions & 15 deletions {{cookiecutter.package_name}}/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
as a guide for preparing this.
"""

import nox

from pathlib import Path

import nox

# see https://nox.thea.codes/en/stable/usage.html#changing-the-sessions-default-backend
nox.options.default_venv_backend = "uv|virtualenv"

Expand Down Expand Up @@ -147,16 +147,16 @@ def pyroma(session: nox.Session) -> None:
@nox.session(tags=["dev"], default=False)
def build(session: nox.Session) -> None:
"""Build the package."""
session.install("wheel", "build[uv]", "setuptools")
session.run("python", "-m", "build", "--sdist", "--wheel", "--no-isolation")
session.install("uv", "setuptools")
session.run("uv", "build", "--sdist", "--wheel", "--no-build-isolation")


@nox.session(tags=["dev"], default=False)
def release(session: nox.Session) -> None:
"""Build a pip package."""
build(session)
session.install("twine>=1.5.0")
session.run("python", "-m", "twine", "upload", "--skip-existing", "dist/*")
session.install("keyring")
_uv_publish(session, "https://upload.pypi.org/legacy/")


@nox.session(tags=["dev"], default=False)
Expand All @@ -174,16 +174,20 @@ def finish(session: nox.Session) -> None:
def test_release(session: nox.Session) -> None:
"""Build a pip package."""
build(session)
session.install("twine>=1.5.0")
session.install("keyring")
_uv_publish(session, "https://test.pypi.org/legacy/")


def _uv_publish(session: nox.Session, publish_url: str) -> None:
session.run(
"python",
"-m",
"twine",
"upload",
"--skip-existing",
"--repository",
"testpypi",
"dist/*",
"uv",
"publish",
"--username",
"__token__",
"--keyring-provider",
"subprocess",
"--publish-url",
publish_url,
)


Expand Down
55 changes: 10 additions & 45 deletions {{cookiecutter.package_name}}/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -209,34 +209,19 @@ commands =
# 3. Get account recovery codes
# 4. Set up 2-Factor Authentication
# 5. Get an API token from https://pypi.org/manage/account/token/
# 6. Create a file called .pypirc in the home directory if it does not already exist.
# 7. Add the following content to the .pypirc file
#
# [distutils]
# index-servers=
# pypi
# testpypi
#
# [pypi]
# username = __token__
# password = <the API token you just got>
#
# If there's already an `index-servers =` list, just make sure you add `pypi` to it.
# More information about .pypirc can be found at https://packaging.python.org/en/latest/specifications/pypirc/
# 6. Install keyring with `uv tool install keyring`
# 7. Add your token to keyring with `keyring set https://upload.pypi.org/legacy/ __token__`

[testenv:release]
description = Release the code to PyPI so users can pip install it
skip_install = true
passenv =
TWINE_USERNAME
TWINE_PASSWORD
deps =
{[testenv:build]deps}
twine >= 1.5.0
uv
keyring
commands =
{[testenv:build]commands}
twine check dist/*
twine upload --skip-existing dist/*
uv publish --username __token__ --keyring-provider subprocess --publish-url https://upload.pypi.org/legacy/

[testenv:finish]
description =
Expand All @@ -245,8 +230,6 @@ description =
skip_install = true
passenv =
HOME
TWINE_USERNAME
TWINE_PASSWORD
deps =
{[testenv:release]deps}
bump-my-version
Expand All @@ -271,35 +254,19 @@ allowlist_externals =
# 3. Get account recovery codes
# 4. Set up 2-Factor Authentication
# 5. Get an API token from https://test.pypi.org/manage/account/token/
# 6. Create a file called .pypirc in the home directory if it does not already exist.
# 7. Add the following content to the .pypirc file
#
# [distutils]
# index-servers=
# pypi
# testpypi
#
# [testpypi]
# repository = https://test.pypi.org/legacy/
# username = __token__
# password = <the API token you just got>
#
# If there's already an `index-servers =` list, just make sure you add `testpypi` to it.
# More information about .pypirc can be found at https://packaging.python.org/en/latest/specifications/pypirc/
# 6. Install keyring with `uv tool install keyring`
# 7. Add your token to keyring with `keyring set https://test.pypi.org/legacy/ __token__`

[testenv:testrelease]
description = Release the code to the test PyPI site
skip_install = true
passenv =
TWINE_USERNAME
TWINE_PASSWORD
deps =
{[testenv:build]deps}
twine >= 1.5.0
uv
keyring
commands =
{[testenv:build]commands}
twine check dist/*
twine upload --skip-existing --repository testpypi dist/*
uv publish --username __token__ --keyring-provider subprocess --publish-url https://test.pypi.org/legacy/

[testenv:testfinish]
description =
Expand All @@ -308,8 +275,6 @@ description =
skip_install = true
passenv =
HOME
TWINE_USERNAME
TWINE_PASSWORD
deps =
{[testenv:testrelease]deps}
bump-my-version
Expand Down

0 comments on commit 12edfcf

Please sign in to comment.