Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
run: cp CNAME public/CNAME

- name: Deploy
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
gh release view "nightly" && gh release delete "nightly" -y --cleanup-tag

- name: GH Release
uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2
uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
with:
prerelease: true
tag_name: nightly
Expand All @@ -64,7 +64,7 @@ jobs:
issues: write
steps:
- name: Create issue on failure
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
github.rest.issues.create({
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:

- name: Create Release
id: release
uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2
uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
with:
tag_name: v${{ steps.date.outputs.date }}
generate_release_notes: true
Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:

- name: Create issue on failure
if: steps.wait_for_wheels.outcome == 'failure'
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
github.rest.issues.create({
Expand Down
56 changes: 44 additions & 12 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,57 @@ on:
types: [labeled]

jobs:
setup-version:
name: Setup version and publication status
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get-version.outputs.version }}
should_publish: ${{ steps.get-version.outputs.should_publish }}
steps:
- name: Check out repo
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Install dependencies
run: pip install requests
- name: Get version
id: get-version
shell: bash
run: |
python scripts/get_version.py \
--event "${{ github.event_name }}" \
--ref "${{ github.ref }}" \
--tag "${{ github.event.release.tag_name }}" \
--package "heir_py" \
--gha

build_sdist:
name: Build source distribution
needs: [setup-version]
if: github.ref == 'refs/heads/main' || github.event.label.name == 'ci:wheels'
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v6
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
- name: Install Python 3.12
uses: actions/setup-python@v6
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
with:
python-version: "3.12"
- run: python -m pip install build
- name: Build sdist
run: python -m build --sdist
- uses: actions/upload-artifact@v6
run: |
if [[ -n "${{ needs.setup-version.outputs.version }}" ]]; then
export SETUPTOOLS_SCM_PRETEND_VERSION="${{ needs.setup-version.outputs.version }}"
fi
python -m build --sdist
- uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
with:
name: dist-sdist
path: dist/*.tar.gz

build_wheels:
name: Build HEIR wheels on ${{ matrix.name }}
needs: [setup-version]
# On PRs, only run when the ci:wheels label is added.
if: github.ref == 'refs/heads/main' || github.event.label.name == 'ci:wheels'
runs-on: ${{ matrix.runner }}
Expand All @@ -50,36 +78,40 @@ jobs:

steps:
- name: Check out HEIR
uses: actions/checkout@v6
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0

- uses: actions/setup-python@v6
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
name: Install Python 3.12
with:
python-version: "3.12"
- run: pip install --upgrade pip uv

- name: Build wheels on ${{ matrix.name }} using cibuildwheel
uses: pypa/cibuildwheel@v3.0.0
uses: pypa/cibuildwheel@5f22145df44122af0f5a201f93cf0207171beca7 # v3.0.0
env:
SETUPTOOLS_SCM_PRETEND_VERSION: ${{ needs.setup-version.outputs.version }}

- name: Upload HEIR ${{ matrix.name }} wheels
if: always()
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
with:
name: dist-${{ matrix.name }}
path: wheelhouse/*.whl

pypi_upload:
name: Publish heir wheels to PyPI
needs: [build_sdist, build_wheels]
# Only publish from main, not PRs
if: github.ref == 'refs/heads/main'
needs: [setup-version, build_sdist, build_wheels]
# Only publish from main or releases based on setup-version logic
if: needs.setup-version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
# Cf. trusted publishers on PyPI
environment: pypi
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v7
- uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
with:
path: dist
pattern: dist-*
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[build-system]
requires = ["setuptools"]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "heir_py"
version = "0.0.3"
dynamic = ["version"]
authors = [
{ name = "Jeremy Kun", email = "jkun@google.com" },
{ name = "Asra Ali", email = "asraa@google.com" },
Expand Down Expand Up @@ -126,3 +126,6 @@ ignore-words-list = "crate,fpt,LogArithmetic,olt,allOne"
write-changes = false
# files to exclude
skip = "*.lock"

[tool.setuptools_scm]
# Can be empty, setuptools-scm handles the rest by default.
92 changes: 92 additions & 0 deletions scripts/get_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Script to calculate the version for the HEIR Python package."""

import argparse
import datetime
import os
import re
import sys
import requests


def get_pypi_versions(package_name):
"""Fetch all published versions for a package from PyPI."""
url = f"https://pypi.org/pypi/{package_name}/json"
try:
response = requests.get(url, timeout=10)
if response.status_code == 200:
data = response.json()
return list(data.get("releases", {}).keys())
except Exception as e:
print(f"Error fetching from PyPI: {e}", file=sys.stderr)
return []


def get_next_dev_version(package_name):
"""Calculate the next .devN version for today's date."""
today = datetime.datetime.now(datetime.timezone.utc).strftime("%Y.%m.%d")
versions = get_pypi_versions(package_name)

# Find versions matching today's date and the .dev suffix
pattern = re.compile(rf"^{re.escape(today)}\.dev(\d+)$")
max_dev = -1

for v in versions:
match = pattern.match(v)
if match:
max_dev = max(max_dev, int(match.group(1)))

next_dev = max_dev + 1
return f"{today}.dev{next_dev}"


def main():
parser = argparse.ArgumentParser(
description="Calculate HEIR package version."
)
parser.add_argument(
"--event",
default="workflow_dispatch",
help="GitHub event name (e.g., release, workflow_dispatch)",
)
parser.add_argument(
"--ref",
default="refs/heads/main",
help="GitHub ref (e.g., refs/heads/main)",
)
parser.add_argument("--tag", help="Release tag name")
parser.add_argument("--package", default="heir_py", help="PyPI package name")
parser.add_argument(
"--gha", action="store_true", help="Output for GitHub Actions"
)

args = parser.parse_args()

version = ""
should_publish = "false"

if args.event == "release":
if args.tag:
version = args.tag.lstrip("v")
should_publish = "true"
elif args.event == "workflow_dispatch" and args.ref == "refs/heads/main":
version = get_next_dev_version(args.package)
should_publish = "true"
else:
# For PRs or other events, we let setuptools-scm handle it automatically
# or we don't publish.
should_publish = "false"

if args.gha:
# Writing to GITHUB_OUTPUT if available
output_file = os.environ.get("GITHUB_OUTPUT")
if output_file:
with open(output_file, "a") as f:
f.write(f"version={version}\n")
f.write(f"should_publish={should_publish}\n")

print(f"version={version}")
print(f"should_publish={should_publish}")


if __name__ == "__main__":
main()
Loading