Skip to content

Commit 1c914d4

Browse files
committed
Prepare for initial release on PyPI
1 parent 77ba590 commit 1c914d4

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

Diff for: .github/workflows/pypi.yml

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Publish Python distribution to PyPI and TestPyPI
2+
# Source:
3+
# https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
4+
on:
5+
push:
6+
branches:
7+
- main
8+
tags:
9+
- 'v*'
10+
jobs:
11+
build:
12+
name: Build distribution package
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Set up Python
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: "3.x"
20+
- name: Install pypa/build
21+
run: python3 -m pip install build --user
22+
- name: Add untagged version suffix
23+
if: ${{ ! startsWith(github.ref, 'refs/tags/v') }}
24+
run: python3 version.py update ${{ github.sha }}
25+
- name: Build a binary wheel and a source tarball
26+
run: python3 -m build
27+
- name: Store the distribution packages
28+
uses: actions/upload-artifact@v4
29+
with:
30+
name: python-package-distributions
31+
path: dist/
32+
version-check:
33+
name: Check for version match in git tag and unmagic.__version__
34+
runs-on: ubuntu-latest
35+
if: startsWith(github.ref, 'refs/tags/v')
36+
steps:
37+
- uses: actions/checkout@v4
38+
- name: Set up Python
39+
uses: actions/setup-python@v5
40+
with:
41+
python-version: "3.x"
42+
- name: Install pytest-unmagic
43+
run: pip install -e .
44+
- name: Check version
45+
run: python version.py check "${{ github.ref }}"
46+
pypi-publish:
47+
name: Upload release to PyPI
48+
needs: [build, version-check]
49+
runs-on: ubuntu-latest
50+
if: startsWith(github.ref, 'refs/tags/v')
51+
environment:
52+
name: pypi
53+
url: https://pypi.org/p/pytest-unmagic
54+
permissions:
55+
id-token: write
56+
steps:
57+
- name: Download all the dists
58+
uses: actions/download-artifact@v4
59+
with:
60+
name: python-package-distributions
61+
path: dist/
62+
- name: Publish package distributions to PyPI
63+
uses: pypa/gh-action-pypi-publish@release/v1
64+
pypi-test-publish:
65+
name: Upload release to test PyPI
66+
needs: [build]
67+
runs-on: ubuntu-latest
68+
environment:
69+
name: testpypi
70+
url: https://test.pypi.org/p/pytest-unmagic
71+
permissions:
72+
id-token: write
73+
steps:
74+
- name: Download all the dists
75+
uses: actions/download-artifact@v4
76+
with:
77+
name: python-package-distributions
78+
path: dist/
79+
- name: Publish package distributions to PyPI
80+
uses: pypa/gh-action-pypi-publish@release/v1
81+
with:
82+
repository-url: https://test.pypi.org/legacy/

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
__pycache__
2+
/dist

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,14 @@ cd path/to/pytest-unmagic
193193
pip install -e .
194194
pytest
195195
```
196+
197+
198+
## Publishing a new verison to PyPI
199+
200+
Push a new tag to Github using the format vX.Y.Z where X.Y.Z matches the version
201+
in [`__init__.py`](src/unmagic/__init__.py).
202+
203+
A new version is published to https://test.pypi.org/p/pytest-unmagic on every
204+
push to the *main* branch.
205+
206+
Publishing is automated with [Github Actions](.github/workflows/pypi.yml).

Diff for: pyproject.toml

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "pytest-unmagic"
33
authors = [{name = "Daniel Miller", email = "[email protected]"}]
44
license = {file = "LICENSE"}
5+
readme = {file = "README.md", content-type = "text/markdown"}
56
dynamic = ["version", "description"]
67
requires-python = ">= 3.9"
78
classifiers = [
@@ -10,6 +11,10 @@ classifiers = [
1011
"License :: OSI Approved :: BSD License",
1112
"Programming Language :: Python :: 3",
1213
"Programming Language :: Python :: 3.9",
14+
"Programming Language :: Python :: 3.10",
15+
"Programming Language :: Python :: 3.11",
16+
"Programming Language :: Python :: 3.12",
17+
"Programming Language :: Python :: 3.13",
1318
"Topic :: Software Development :: Libraries",
1419
"Topic :: Software Development :: Testing",
1520
]

Diff for: version.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Check for version match between github tag and __init__.py"""
2+
import re
3+
import sys
4+
import unmagic
5+
6+
7+
def main(argv=sys.argv):
8+
if len(argv) < 3:
9+
sys.exit(f"usage: {argv[0]} (check|set-dev) ARG")
10+
cmd, arg, *ignore = sys.argv[1:]
11+
if cmd == "check":
12+
check(arg)
13+
elif cmd == "update":
14+
update(arg)
15+
else:
16+
sys.exit(f"unknown arguments: {argv[1:]}")
17+
18+
19+
def check(ref):
20+
if not ref.startswith("refs/tags/v"):
21+
sys.exit(f"unexpected ref: {ref}")
22+
version = ref.removeprefix("refs/tags/v")
23+
if version != unmagic.__version__:
24+
sys.exit(f"version mismatch: {version} != {unmagic.__version__}")
25+
26+
27+
def update(commit_hash):
28+
version = f"{unmagic.__version__}+{commit_hash}"
29+
print("new version:", version)
30+
vexpr = re.compile(r"(?<=^__version__ = ).+$", flags=re.MULTILINE)
31+
with open(unmagic.__file__, "r+") as file:
32+
text = file.read()
33+
file.seek(0)
34+
file.write(vexpr.sub(repr(version), text))
35+
file.truncate()
36+
37+
38+
if __name__ == "__main__":
39+
main()

0 commit comments

Comments
 (0)