Skip to content

Commit 19d6d06

Browse files
committed
Robustify version parsing, restore pypi version guard
1 parent 75f3757 commit 19d6d06

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

.github/workflows/publish.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,30 @@ jobs:
4848
exit 1
4949
fi
5050
51+
- name: Check if version already published
52+
id: pypi
53+
env:
54+
PACKAGE_NAME: ${{ steps.package.outputs.name }}
55+
run: |
56+
VERSION=$(python scripts/get-version.py "$PACKAGE_NAME")
57+
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://pypi.org/pypi/${PACKAGE_NAME}/${VERSION}/json")
58+
if [ "$HTTP_STATUS" = "200" ]; then
59+
echo "$PACKAGE_NAME $VERSION already exists on PyPI, skipping publish"
60+
echo "should-publish=false" >> "$GITHUB_OUTPUT"
61+
elif [ "$HTTP_STATUS" = "404" ]; then
62+
echo "$PACKAGE_NAME $VERSION not found on PyPI, proceeding with publish"
63+
echo "should-publish=true" >> "$GITHUB_OUTPUT"
64+
else
65+
echo "Unexpected PyPI response for $PACKAGE_NAME $VERSION: $HTTP_STATUS"
66+
exit 1
67+
fi
68+
5169
- name: Build package
70+
if: steps.pypi.outputs.should-publish == 'true'
5271
env:
5372
PACKAGE_NAME: ${{ steps.package.outputs.name }}
5473
run: uv build --package "$PACKAGE_NAME" --no-sources
5574

5675
- name: Publish package
76+
if: steps.pypi.outputs.should-publish == 'true'
5777
run: uv publish

scripts/get-version.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@
1111
def read_version(path: Path) -> str:
1212
module = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
1313
for node in module.body:
14-
if not isinstance(node, ast.Assign):
14+
value: ast.expr | None
15+
if isinstance(node, ast.Assign) and any(
16+
isinstance(target, ast.Name) and target.id == "__version__" for target in node.targets
17+
):
18+
value = node.value
19+
elif (
20+
isinstance(node, ast.AnnAssign)
21+
and isinstance(node.target, ast.Name)
22+
and node.target.id == "__version__"
23+
):
24+
value = node.value
25+
else:
1526
continue
16-
if not any(isinstance(target, ast.Name) and target.id == "__version__" for target in node.targets):
17-
continue
18-
if isinstance(node.value, ast.Constant) and isinstance(node.value.value, str):
19-
return node.value.value
27+
if isinstance(value, ast.Constant) and isinstance(value.value, str):
28+
return value.value
2029
raise RuntimeError(f"could not find __version__ in {path}")
2130

2231

0 commit comments

Comments
 (0)