Skip to content

Commit c94312d

Browse files
committed
Fix release versioning and wheel build cache
1 parent 12dbc5c commit c94312d

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

.github/workflows/build.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,23 @@ jobs:
2323

2424
steps:
2525
- uses: actions/checkout@v4
26-
- uses: actions/setup-python@v3
26+
- uses: actions/setup-python@v4
27+
with:
28+
python-version: "3.12"
29+
30+
- name: Set release version
31+
run: echo "SETUPTOOLS_SCM_PRETEND_VERSION=${GITHUB_REF_NAME#v}" >> "$GITHUB_ENV"
32+
33+
- name: Cache cibuildwheel downloads
34+
uses: actions/cache@v4
35+
with:
36+
path: |
37+
~/.cache/cibuildwheel
38+
~/Library/Caches/cibuildwheel
39+
key: cibuildwheel-${{ runner.os }}-${{ matrix.python[0] }}-${{ matrix.buildplat[1] }}
40+
restore-keys: |
41+
cibuildwheel-${{ runner.os }}-${{ matrix.python[0] }}-
42+
cibuildwheel-${{ runner.os }}-
2743
2844
- name: Setup micromamba
2945
uses: mamba-org/setup-micromamba@v2.0.1
@@ -33,6 +49,17 @@ jobs:
3349
init-shell: bash
3450
generate-run-shell: true
3551

52+
- name: Prime cibuildwheel virtualenv cache
53+
if: runner.os == 'macOS'
54+
env:
55+
GITHUB_TOKEN: ${{ github.token }}
56+
run: |
57+
mkdir -p ~/Library/Caches/cibuildwheel
58+
curl -fL --retry 5 --retry-all-errors \
59+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
60+
-o ~/Library/Caches/cibuildwheel/virtualenv-20.26.2.pyz \
61+
https://github.com/pypa/get-virtualenv/raw/20.26.2/public/virtualenv.pyz
62+
3663
- name: Build wheels
3764
uses: pypa/cibuildwheel@v2.19.0
3865
env:
@@ -75,6 +102,9 @@ jobs:
75102
steps:
76103
- uses: actions/checkout@v4
77104

105+
- name: Set release version
106+
run: echo "SETUPTOOLS_SCM_PRETEND_VERSION=${GITHUB_REF_NAME#v}" >> "$GITHUB_ENV"
107+
78108
- name: Set up Python
79109
uses: actions/setup-python@v4
80110
with:
@@ -175,4 +205,4 @@ jobs:
175205
release.upload_asset(f"./wheelhouse/{asset}")
176206
177207
print("Release created and assets uploaded successfully")
178-
EOF
208+
EOF

meson.build

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
project('pyensmallen',
22
['cpp'],
3-
version: '0.3.2',
3+
version: run_command(
4+
'python3',
5+
files('tools/get_version.py'),
6+
check: true
7+
).stdout().strip(),
48
default_options: ['cpp_std=c++14'])
59

610
py = import('python').find_installation(pure: false)

tools/get_version.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
"""Resolve the package version for Meson builds.
3+
4+
This supports three build contexts:
5+
6+
1. Tagged GitHub Actions builds via ``SETUPTOOLS_SCM_PRETEND_VERSION``.
7+
2. Source distributions, which include ``PKG-INFO`` with the resolved version.
8+
3. Git checkouts, using ``setuptools_scm`` directly.
9+
"""
10+
11+
from __future__ import annotations
12+
13+
from pathlib import Path
14+
import os
15+
import re
16+
import sys
17+
18+
19+
ROOT = Path(__file__).resolve().parents[1]
20+
21+
22+
def _version_from_env() -> str | None:
23+
version = os.environ.get("SETUPTOOLS_SCM_PRETEND_VERSION")
24+
if version:
25+
return version
26+
return None
27+
28+
29+
def _version_from_pkg_info() -> str | None:
30+
pkg_info = ROOT / "PKG-INFO"
31+
if not pkg_info.exists():
32+
return None
33+
34+
for line in pkg_info.read_text(encoding="utf-8").splitlines():
35+
if line.startswith("Version: "):
36+
return line.split("Version: ", 1)[1].strip()
37+
return None
38+
39+
40+
def _version_from_generated_file() -> str | None:
41+
version_file = ROOT / "pyensmallen" / "_version.py"
42+
if not version_file.exists():
43+
return None
44+
45+
match = re.search(
46+
r'^__version__\s*=\s*"([^"]+)"\s*$',
47+
version_file.read_text(encoding="utf-8"),
48+
re.MULTILINE,
49+
)
50+
if match:
51+
return match.group(1)
52+
return None
53+
54+
55+
def _version_from_scm() -> str | None:
56+
try:
57+
from setuptools_scm import get_version
58+
except ImportError:
59+
return None
60+
61+
try:
62+
return get_version(root=str(ROOT), fallback_version="0.0.0")
63+
except Exception:
64+
return None
65+
66+
67+
def main() -> int:
68+
for getter in (
69+
_version_from_env,
70+
_version_from_pkg_info,
71+
_version_from_generated_file,
72+
_version_from_scm,
73+
):
74+
version = getter()
75+
if version:
76+
print(version)
77+
return 0
78+
79+
print("0.0.0")
80+
return 0
81+
82+
83+
if __name__ == "__main__":
84+
raise SystemExit(main())

0 commit comments

Comments
 (0)