Skip to content

cleanup codebase using pre-commit checks: ruff, codespell (#42) #262

cleanup codebase using pre-commit checks: ruff, codespell (#42)

cleanup codebase using pre-commit checks: ruff, codespell (#42) #262

name: Scribl Python package
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
on:
workflow_dispatch:
push:
paths-ignore:
- '**.md'
- '**.rst'
- '**.pdf'
- '**.png'
- 'CITATION.cff'
- '.zenodo.json'
- 'paper/**'
- '.github/dependabot.yml'
- '.github/release-drafter.yml'
- '.github/ISSUE_TEMPLATE/**'
- '.github/workflows/release-drafter.yml'
- '.gitattributes'
pull_request:
paths-ignore:
- '**.md'
- '**.rst'
- '**.pdf'
- '**.png'
- 'CITATION.cff'
- '.zenodo.json'
- 'paper/**'
- '.github/dependabot.yml'
- '.github/release-drafter.yml'
- '.github/ISSUE_TEMPLATE/**'
- '.github/workflows/release-drafter.yml'
- '.gitattributes'
release:
types:
- published
jobs:
skip_publish_zenodo:
name: Non publication, Zenodo skip
runs-on: ubuntu-latest
# FIXME: this is the default action, the logical inverse of the
# below "if" in publish_zenodo: a bit hacky, but it works
if: (github.event_name == 'release' && github.event.action == 'published' && github.event.release.prerelease == false) == false
steps:
- name: Skip Zenodo step unless publishing release
run: |
echo "Not a publication release, skipping Zenodo"
echo "github.ref: ${{ toJson(github.ref) }}"
echo "github.event_name: ${{ toJson(github.event_name) }}"
echo "github.event.action: ${{ toJson(github.event.action) }}"
publish_zenodo:
name: Production release, Zenodo publication
runs-on: ubuntu-latest
# "else" we *are* publishing something, only works for non-pre-releases on the main branch
# if: false
if: github.event.release.target_commitish == 'main' && github.event_name == 'release' && github.event.action == 'published' && github.event.release.prerelease == false
steps:
- name: Checkout the contents of your repository
uses: actions/checkout@v4
- name: Upsert the version and related ids in the metadata in .zenodo.json
run: |
echo "GITHUB_REF_NAME = $GITHUB_REF_NAME | GITHUB_REF = $GITHUB_REF | GITHUB_EVENT_PATH = $GITHUB_EVENT_PATH"
echo "github.event.release.target_commitish = ${{ github.event.release.target_commitish }}"
# convert semver to Python-style version
VERSION_PYTHON_STYLE=$(GITHUB_REF_NAME=$GITHUB_REF_NAME python -c 'import os; from packaging import version; print(str(version.Version(os.getenv("GITHUB_REF_NAME"))))')
echo "VERSION_PYTHON_STYLE = $VERSION_PYTHON_STYLE"
# cp .zenodo.json .zenodo.orig.json
# update the 'related_identifiers' to use exact tree and pypi packaage
cat .zenodo.json | jq "(.related_identifiers[] | (select (.relation == \"isSourceOf\" and .resource_type == \"software\")) | select (.identifier | startswith(\"https://pypi.org/project/scribl\")).identifier) |= \"https://pypi.org/project/scribl/$VERSION_PYTHON_STYLE\"" | jq "(.related_identifiers[] | (select (.relation == \"isSupplementTo\" and .resource_type == \"software\")) | select (.identifier | startswith(\"https://github.com/amberbiology/scribl\")).identifier) |= \"https://github.com/amberbiology/scribl/tree/$GITHUB_REF_NAME\"" > .zenodo.mod.json
# update the version number
echo "{\"version\": \"$GITHUB_REF_NAME\"}" | cat .zenodo.mod.json - | jq -s add > .zenodo.json
# debugging only
cat .zenodo.json
rm .zenodo.mod.json
- name: Upsert the version in the metadata in CITATION.cff
run: |
yq -i ".version = \"$GITHUB_REF_NAME\"" CITATION.cff
- name: Push changes back to repo files
run: |
git fetch origin
git config --global user.name "ci-scribl"
git config --global user.email "username@users.noreply.github.com"
git checkout -b $GITHUB_REF_NAME-with-upserting-changes-pre-upload
git add .zenodo.json CITATION.cff
git commit -m "Upserted version into CITATION.cff and into .zenodo.json"
git checkout ${{ github.event.release.target_commitish }}
git merge $GITHUB_REF_NAME-with-upserting-changes-pre-upload
git push origin ${{ github.event.release.target_commitish }}
# FIXME: removing files marked with 'export-ignore' 'set' in .gitattributes
# before zenodraft creates the zipball using https://stackoverflow.com/a/30573319
# to ensure that we handle filenames with potential ':' or newlines
git ls-files -z|git check-attr --stdin -z export-ignore |sed -zne 'x;n;n;s/^set$//;t print;b;:print;x;p'|xargs --null rm
# FIXME: need to also remove orphaned empty directories resulting from above
find . -empty -type d -delete
- name: Create a draft snapshot of your repository contents as a new
version in test collection on Zenodo using metadata
from repository file .zenodo.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ZENODO_ACCESS_TOKEN: ${{ secrets.ZENODO_SCRIBL_ACCESS_TOKEN }}
uses: zenodraft/action@0.13.3
# uses: alexlancaster/zenodraft-action@0.10.2
with:
concept: 12728362
metadata: .zenodo.json
publish: false
sandbox: false
compression: zip
upsert-doi: true
upsert-location: doi
verbose: true
ci_build:
needs: [publish_zenodo, skip_publish_zenodo]
if: always() && !cancelled() && !failure()
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.13", "3.12", "3.11", "3.10", "3.9"]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- name: Build wheel and sdist
run: |
python -m pip install --upgrade pip
python -m pip install build flake8 pytest
python -m build
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
# force use of bash on all platforms, so wildcard expansion works on Windows
# (otherwise would use powershell on Windows, which fails)
shell: bash
run: |
pip install --only-binary ":all:" --no-binary sgmllib3k,bibtexparser dist/scribl*.whl
pytest -v tests
# skip uploading separate files for each CI run (need just one wheel/dist done in 'dist' job, below)
#- name: Upload artifact
#
# uses: actions/upload-artifact@v4
# with:
# name: scribl-wheels-${{ matrix.os }}-python-${{ matrix.python-version }}
# path: dist/*.whl
dist:
name: Create wheel and sdist for distribution
needs: [ci_build] # make sure it passes the CI first
runs-on: ubuntu-latest
if: always() && !cancelled() && !failure()
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: hynek/build-and-inspect-python-package@v2
upload_test_pypi:
name: Upload to Test_PyPI
needs: [dist]
runs-on: ubuntu-latest
environment: test_pypi
permissions:
id-token: write
# drop requirement of 'main', release to test_pypi to test releases
#if: false
if: github.event_name == 'release' && github.event.action == 'published' && always() && !cancelled() && !failure()
steps:
- uses: actions/download-artifact@v4
with:
name: Packages
path: dist
merge-multiple: true
- uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
upload_pypi:
name: Upload to PyPI
needs: [dist]
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
if: github.event.release.target_commitish == 'main' && github.event_name == 'release' && github.event.action == 'published' && always() && !cancelled() && !failure()
steps:
- uses: actions/download-artifact@v4
with:
name: Packages
path: dist
merge-multiple: true
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1