Skip to content

Commit

Permalink
chore: Add maturin setup and release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg committed Apr 22, 2023
1 parent be11c85 commit dab7f5d
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/set_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
import os
import re
import sys
from pathlib import Path


def main() -> int:
if not (cargo_path := Path(os.getenv('CARGO_PATH', 'Cargo.toml'))).is_file():
print(f'✖ path "{cargo_path}" does not exist')
return 1

if len(sys.argv) == 2:
version = sys.argv[1]
print(f'Found input version {version}')
elif version := os.getenv('VERSION'):
print(f'Found $VERSION {version}')
elif version := os.getenv('GITHUB_REF'):
print(f'Found $GITHUB_REF {version}')
else:
print(f'✖ "Version env variables not found')
return 1

version = version.lower().replace('refs/tags/v', '').replace('a', '-alpha').replace('b', '-beta')
print(f'writing version "{version}", to {cargo_path}')

version_regex = re.compile('^version ?= ?".*"', re.M)
cargo_content = cargo_path.read_text()
if not version_regex.search(cargo_content):
print(f'✖ {version_regex!r} not found in {cargo_path}')
return 1

new_content = version_regex.sub(f'version = "{version}"', cargo_content)
cargo_path.write_text(new_content)
return 0


if __name__ == '__main__':
sys.exit(main())
145 changes: 145 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ on:
types:
- created

workflow_dispatch:
inputs:
version:
description: 'Package version'
required: true
default: refs/tags/v0.1.0

jobs:

# This builds Rust binaries which we upload to our GitHub release

release:
if: github.event_name == 'release'
name: release - ${{ matrix.platform.release_for }}
runs-on: ${{ matrix.platform.os }}
permissions:
Expand Down Expand Up @@ -75,3 +86,137 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: startsWith( github.ref, 'refs/tags/v' )

# Steps below this line builds wheels for publishing to PyPI

build-wheel-linux:
name: build linux wheel for ${{ matrix.target }}
runs-on: ubuntu-latest
strategy:
matrix:
target: [x86_64, x86, aarch64, armv7, s390x, ppc64le]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'

- if: github.event_name == 'workflow_dispatch'
run: python .github/set_version.py ${{ inputs.version }}
name: set package version

- if: github.event_name != 'workflow_dispatch'
run: python .github/set_version.py
name: set package version

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --zig
sccache: 'true'
manylinux: auto
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

build-wheel-windows:
name: build wheel for ${{ matrix.target }}
runs-on: windows-latest
strategy:
matrix:
target: [x64, x86]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'

- if: github.event_name == 'workflow_dispatch'
run: python .github/set_version.py ${{ inputs.version }}
name: set package version

- if: github.event_name != 'workflow_dispatch'
run: python .github/set_version.py
name: set package version

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist
sccache: 'true'
manylinux: auto
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

build-wheel-macos:
name: build wheel for ${{ matrix.target }}
runs-on: macos-latest
strategy:
matrix:
target: [x86_64, aarch64]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'

- if: github.event_name == 'workflow_dispatch'
run: python .github/set_version.py ${{ inputs.version }}
name: set package version

- if: github.event_name != 'workflow_dispatch'
run: python .github/set_version.py
name: set package version

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist
sccache: 'true'
manylinux: auto
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

sdist:
needs: [build-wheel-linux, build-wheel-macos, build-wheel-windows]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
- name: Upload sdist
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

release-to-pypi:
if: github.event_name == 'release'
name: release to PyPI
runs-on: ubuntu-latest
needs: [ build-wheel-linux, build-wheel-windows, build-wheel-macos, sdist ]
steps:
- uses: actions/download-artifact@v3
with:
name: wheels
- name: Publish to PyPI
uses: PyO3/maturin-action@v1
env:
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
with:
command: upload
args: --skip-existing *
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.idea/
.venv
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ rustpython-parser = { features = [
"lalrpop",
], git = "https://github.com/RustPython/RustPython.git", rev = "c15f670f2c30cfae6b41a1874893590148c74bc4" }


[dev-dependencies]
assert-panic = "*"
tokio-test = "*"
33 changes: 33 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[build-system]
requires = ["maturin>=0.14,<0.15"]
build-backend = "maturin"

[project]
name = "printf-log-formatter"
description = "Convert logger f-strings and str.format syntax to printf-style strings"
readme = "README.md"
authors = [{ name = "Sondre Lillebø Gundersen", email = "[email protected]" }]
license = { file = "LICENSE" }
requires-python = ">=3.7"
dependencies = []
keywords = ["linter", "formatter", "logging", "sentry"]
classifiers = [
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Rust",
]

[project.urls]
Repository = "https://github.com/sondrelg/printf-log-formatter"
Changelog = "https://github.com/sondrelg/printf-log-formatter/releases"

0 comments on commit dab7f5d

Please sign in to comment.