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
6 changes: 5 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[flake8]
max-line-length = 88
ignore = E501, E203, W503
ignore = E501, E203, W503
# Exact type comparisons on purpose (pycodestyle 2.11, which Python 3.12 needs, reports them)
per-file-ignores =
pywhat/filter.py: E721
tests/test_regex_identifier.py: E721
16 changes: 9 additions & 7 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ jobs:
id: python
with:
python-version: ${{ matrix.python-version }}
# The CI tooling runs on a current Python so it can be installed from
# wheels only (cffi, which Poetry needs on macOS, has no Python 3.8 arm64
# wheels). The nox sessions still use the matrix Python (--force-python).
# nox runs on a current Python, and its sessions on the matrix Python
# (--force-python).
- uses: actions/setup-python@v6
with:
python-version: '3.12'
# Pinned: Poetry 2.x removed `install --no-dev` and the bundled `export`
# command. packaging 26.3+ no longer imports on Python 3.8, and Poetry
# loads its own copy inside the session's Python 3.8 environment.
# The package's extension module (py/) is Rust, which pip builds with
# maturin when the session installs the package
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2
# virtualenv 21, which nox creates the sessions' environments with, no
# longer creates Python 3.8 ones
- run: |
python -m pip install --only-binary :all: nox==2025.5.1 poetry==1.8.5 packaging==26.2
python -m pip install --only-binary :all: nox==2025.5.1 virtualenv==20.39.1
- run: python -m nox --force-python "${{ steps.python.outputs.python-path }}"
env:
OS: ${{ matrix.os }}
Expand Down
287 changes: 287 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
# The wheels and the sdist of the Python package, for every platform, and their upload to PyPI.
#
# Generated by maturin 1.16.0 (`maturin generate-ci github -o .github/workflows/release.yml`),
# then changed:
# - Wheels are built for pull requests that can change how they build (paths below) and when
# the workflow is run by hand, and kept as the run's artifacts. Version tags (like 5.1.0)
# build them too, and only tags publish them.
# - The wheels are abi3 (pyo3's abi3-py38): one wheel per platform for CPython 3.8 and newer.
# The generated free-threaded (python3.14t) builds are left out: free-threaded Python can't
# load abi3 wheels, and builds from the sdist.
# - Every wheel that the runner can run is installed and tried, and so is the sdist.
# - Actions are pinned to commits.
# - Publishing needs a PYPI_API_TOKEN secret, or PyPI trusted publishing for this workflow and
# the environment "pypi" with the repository variable PYPI_TRUSTED_PUBLISHING set to true.
# Without either, the release job only says so.
name: Release

on:
push:
tags:
- '[0-9]*'
pull_request:
paths:
- 'Cargo.toml'
- 'Cargo.lock'
- 'src/**'
- 'py/**'
- 'pyproject.toml'
- '.github/workflows/release.yml'
workflow_dispatch:

concurrency:
group: release-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
contents: read

env:
# What the "Try the wheel" steps run, outside the checkout (so the installed package is used).
# LICENCE_FILES are the licence files of the checkout, which the package must carry.
SMOKE_TEST: |
import os
from importlib.metadata import distribution
from pywhat import Identifier, _pywhat
found = Identifier().identify("thm{a_flag}\nbee@skerritt.blog")["Regexes"]["text"]
names = [match["Regex Pattern"]["Name"] for match in found]
assert names == ["TryHackMe Flag Format", "Email Address"], names
assert _pywhat.finditer("[0-9]+", "\u00e9 12 \u20ac345") == [(2, 4), (6, 9)]
files = distribution("pywhat").files or []
licences = sorted(file.name for file in files if file.parent.name == "licenses")
assert licences == sorted(os.environ["LICENCE_FILES"].split()), licences
print("OK", names, licences)

jobs:
linux:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: ubuntu-22.04
target: x86_64
- runner: ubuntu-22.04
target: x86
- runner: ubuntu-22.04
target: aarch64
- runner: ubuntu-22.04
target: armv7
- runner: ubuntu-22.04
target: s390x
- runner: ubuntu-22.04
target: ppc64le
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.8"
- name: Build wheels
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
manylinux: auto
- name: Try the wheel
if: ${{ matrix.platform.target == 'x86_64' }}
shell: bash
run: |
# The wheel that was just built, then its dependencies (wheels only)
version=$(basename dist/*.whl | cut -d- -f2)
python -m pip install --only-binary :all: --no-index --no-deps --find-links dist "pywhat==$version"
python -m pip install --only-binary :all: "pywhat==$version"
export LICENCE_FILES="$(ls LICENSE NOTICE 2> /dev/null || true)"
cd "$RUNNER_TEMP"
python -c "$SMOKE_TEST"
pywhat --version
- name: Upload wheels
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6
with:
name: wheels-linux-${{ matrix.platform.target }}
path: dist

musllinux:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: ubuntu-22.04
target: x86_64
- runner: ubuntu-22.04
target: x86
- runner: ubuntu-22.04
target: aarch64
- runner: ubuntu-22.04
target: armv7
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.8"
- name: Build wheels
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
manylinux: musllinux_1_2
- name: Upload wheels
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6
with:
name: wheels-musllinux-${{ matrix.platform.target }}
path: dist

windows:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: windows-latest
target: x64
python_arch: x64
- runner: windows-latest
target: x86
python_arch: x86
- runner: windows-11-arm
target: aarch64
python_arch: arm64
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.13"
architecture: ${{ matrix.platform.python_arch }}
- name: Build wheels
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
- name: Try the wheel
shell: bash
run: |
# The wheel that was just built, then its dependencies (wheels only)
version=$(basename dist/*.whl | cut -d- -f2)
python -m pip install --only-binary :all: --no-index --no-deps --find-links dist "pywhat==$version"
python -m pip install --only-binary :all: "pywhat==$version"
export LICENCE_FILES="$(ls LICENSE NOTICE 2> /dev/null || true)"
cd "$RUNNER_TEMP"
python -c "$SMOKE_TEST"
pywhat --version
- name: Upload wheels
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6
with:
name: wheels-windows-${{ matrix.platform.target }}
path: dist

macos:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: macos-15-intel
target: x86_64
- runner: macos-latest
target: aarch64
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.8"
- name: Build wheels
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
- name: Try the wheel
shell: bash
run: |
# The wheel that was just built, then its dependencies (wheels only)
version=$(basename dist/*.whl | cut -d- -f2)
python -m pip install --only-binary :all: --no-index --no-deps --find-links dist "pywhat==$version"
python -m pip install --only-binary :all: "pywhat==$version"
export LICENCE_FILES="$(ls LICENSE NOTICE 2> /dev/null || true)"
cd "$RUNNER_TEMP"
python -c "$SMOKE_TEST"
pywhat --version
- name: Upload wheels
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6
with:
name: wheels-macos-${{ matrix.platform.target }}
path: dist

sdist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
- name: Build sdist
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1
with:
command: sdist
args: --out dist
# pip builds the extension from the sdist, with Rust
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.12"
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- name: Try the sdist
shell: bash
run: |
# pip builds the wheel from the sdist (with maturin, without build isolation, so
# that the build runs with this maturin), then installs the dependencies
version=$(basename dist/*.tar.gz .tar.gz | cut -d- -f2)
python -m pip install --only-binary :all: maturin==1.16.0
python -m pip install --only-binary :all: --no-binary pywhat --no-build-isolation --no-index --no-deps --find-links dist "pywhat==$version"
python -m pip install --only-binary :all: "pywhat==$version"
export LICENCE_FILES="$(ls LICENSE NOTICE 2> /dev/null || true)"
cd "$RUNNER_TEMP"
python -c "$SMOKE_TEST"
pywhat --version
- name: Upload sdist
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6
with:
name: wheels-sdist
path: dist

release:
name: Release
runs-on: ubuntu-latest
if: ${{ startsWith(github.ref, 'refs/tags/') }}
needs: [linux, musllinux, windows, macos, sdist]
# Protection rules (such as required reviewers) of the environment apply to publishing
environment: pypi
permissions:
# Trusted publishing, and signing the attestations
id-token: write
# Used to generate artifact attestation
attestations: write
steps:
- name: Check that publishing is set up
id: setup
shell: bash
env:
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
PYPI_TRUSTED_PUBLISHING: ${{ vars.PYPI_TRUSTED_PUBLISHING }}
run: |
if [ -n "$PYPI_API_TOKEN" ] || [ "$PYPI_TRUSTED_PUBLISHING" = true ]; then
echo "publish=true" >> "$GITHUB_OUTPUT"
else
echo "::warning::Not published: add a PYPI_API_TOKEN secret, or set up trusted publishing and the repository variable PYPI_TRUSTED_PUBLISHING=true"
fi
- uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7
if: ${{ steps.setup.outputs.publish == 'true' }}
- name: Generate artifact attestation
if: ${{ steps.setup.outputs.publish == 'true' }}
uses: actions/attest@1e69f48acb82d1966a394da916b4c1698aa569d6 # v4
with:
subject-path: 'wheels-*/*'
- name: Install uv
if: ${{ steps.setup.outputs.publish == 'true' }}
uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7
- name: Publish to PyPI
if: ${{ steps.setup.outputs.publish == 'true' }}
run: uv publish 'wheels-*/*'
env:
# Empty without the secret: uv then uses trusted publishing
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ __pycache__/
*$py.class
tests/__pycache__/

# C extensions
# C extensions (and the Rust one, pywhat/_pywhat.*, which pip install -e . builds)
*.so
*.pyd

# Distribution / packaging
.Python
Expand Down
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ Secret scanners take fake API keys, like a Stripe or Shopify key, for leaked sec
]
]
```

## Running the tests

The Python package searches with the regex engine of the Rust crate at the root of the repository, through its extension module `pywhat._pywhat` (`py/`). Building it needs [Rust](https://rustup.rs):

```console
$ pip install -r requirements.txt
$ pip install -e . # builds the extension into pywhat/; run it again after changing Rust code
$ pytest
```

`nox` runs the tests the way CI does. The Rust crate has tests of its own (`cargo test`), see [docs/rust.md](docs/rust.md).
Loading
Loading