Skip to content

Commit

Permalink
ci: added action to publish wheels to pypi
Browse files Browse the repository at this point in the history
This PR does not sign or publish to TestPyPI, which we may want
to consider.

Fixes: #127
  • Loading branch information
retr0h committed Jan 30, 2024
1 parent 90d4c78 commit 09a4b3a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: goreleaser
name: release packages
on:
push:
tags:
Expand All @@ -9,9 +9,11 @@ permissions:
contents: write
# packages: write
# issues: write
# permission is mandatory for trusted publishing
id-token: write

jobs:
goreleaser:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -28,3 +30,14 @@ jobs:
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
- name: Install Task
uses: arduino/setup-task@v1
with:
version: 3.x
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Build Wheels
run: task build:wheel
- uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
packages-dir: dist/whl/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
cobertura.xml
cover.out
dist/
Expand Down
64 changes: 43 additions & 21 deletions python/dist2wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WHEEL_TEMPLATE = """Wheel-Version: 1.0
Generator: dist2wheel.py
Root-Is-Purelib: false
Tag: {py_tag}-{abi_tag}-{platform_tag}
Tag: {py_tag}-{abi_tag}-{platform_tag}
"""
METADATA_TEMPLATE = """Metadata-Version: 2.1
Name: {distribution}
Expand All @@ -43,18 +43,20 @@

class Wheels:
def __init__(self):
self.dist_dir = "dist"
self.wheel_dir = os.path.join(self.dist_dir, "whl")
# Pull in all the project metadata from known locations.
# No, this isn't customizable. Cope.
with open("dist/metadata.json") as f:
with open(os.path.join(self.dist_dir, "metadata.json")) as f:
self.metadata = json.load(f)

with open("dist/artifacts.json") as f:
with open(os.path.join(self.dist_dir, "artifacts.json")) as f:
self.artifacts = json.load(f)

with open("README.md") as f:
self.readme = f.read()

self.distribution = self.metadata["project_name"]
self.distribution = f"python-{self.metadata['project_name']}"
self.version = self.metadata["version"]
self.py_tag = "py3"
self.abi_tag = "none"
Expand All @@ -73,33 +75,51 @@ def create_all(self):
for artifact in self.artifacts:
try:
self.path = artifact["path"]
self.platform_tag = self._fixup_platform_tag(artifact)
self._set_platform_props(artifact)
self.checksum = self._fix_checksum(artifact["extra"]["Checksum"])
self.size = os.path.getsize(self.path)
except KeyError:
continue

self.wheel_file = WHEEL_TEMPLATE.format(**self.__dict__).encode()
self.metadata_file = METADATA_TEMPLATE.format(**self.__dict__).encode()
os.makedirs(self.wheel_dir, exist_ok=True)
self._emit()

@staticmethod
def _fixup_platform_tag(artifact):
def _matrix(self):
return {
"darwin": {
"amd64": {
"arch": "x86_64",
"platform": "macosx_10_7_x86_64",
"tags": [
f"{self.py_tag}-{self.abi_tag}-macosx_10_7_x86_64",
],
},
},
"linux": {
"amd64": {
"arch": "x86_64",
"platform": "manylinux2014_x86_64.manylinux_2_17_x86_64.musllinux_1_1_x86_64",
"tags": [
f"{self.py_tag}-{self.abi_tag}-manylinux2014_x86_64",
f"{self.py_tag}-{self.abi_tag}-manylinux_2_17_x86_64",
f"{self.py_tag}-{self.abi_tag}-musllinux_1_1_x86_64",
],
},
},
}

def _set_platform_props(self, artifact):
"""Convert Go binary nomenclature to Python wheel nomenclature."""

# Go 1.21 will require macOS 10.15 or later
_map = dict(darwin="macosx_10_15", linux="linux")
platform = _map[artifact["goos"]]

arch = artifact["goarch"]
if arch == "arm64" and platform == "linux":
arch = "aarch64"
elif arch == "amd64":
arch = "x86_64"
elif arch == "386":
arch = "i686"
_map = self._matrix()
_platform = artifact["goos"]
_goarch = artifact["goarch"]

return f"{platform}_{arch}"
platform = _map[_platform][_goarch]["platform"]
self.platform = f"{platform}"
self.platform_tag = "\n".join(_map[_platform][_goarch]["tags"])

@staticmethod
def _fix_checksum(checksum):
Expand All @@ -110,8 +130,10 @@ def _fix_checksum(checksum):
return base64.urlsafe_b64encode(bytes.fromhex(checksum)).decode().rstrip("=")

def _emit(self):
name_ver = f"{self.distribution}-{self.version}"
filename = f"dist/{name_ver}-{self.py_tag}-{self.abi_tag}-{self.platform_tag}.whl"
pypi_name = self.distribution.replace("-", "_")
name_ver = f"{pypi_name}-{self.version}"
filename = os.path.join( self.wheel_dir, f"{name_ver}-{self.py_tag}-{self.abi_tag}-{self.platform}.whl")

with zipfile.ZipFile(filename, "w", compression=zipfile.ZIP_DEFLATED) as zf:
record = []
print(f"writing {zf.filename}")
Expand Down

0 comments on commit 09a4b3a

Please sign in to comment.