use anyhow::Result;
use super::{GeneratedFile, Mechanism, MechanismContext, StepResult};
pub struct Pypi;
impl Mechanism for Pypi {
fn id(&self) -> &'static str {
"pypi"
}
fn description(&self) -> &'static str {
"maturin bin wheels + publish-pypi.yml (Trusted Publishing)"
}
fn generate(&self, ctx: &MechanismContext<'_>) -> Result<StepResult> {
let p = &ctx.config.project;
let name = ctx
.config
.org
.pypi_package
.clone()
.unwrap_or_else(|| p.binary_name.clone());
let manifest = &p.cargo_manifest;
let pyproject = format!(
r#"[build-system]
requires = ["maturin>=1.7,<2.0"]
build-backend = "maturin"
[project]
name = "{name}"
version = "{version}"
description = {desc}
readme = "README.md"
requires-python = ">=3.8"
license = "{license}"
license-files = ["LICENSE"]
authors = [{{ name = "{owner}" }}]
classifiers = [
"Environment :: Console",
"License :: OSI Approved :: MIT License",
"Programming Language :: Rust",
"Programming Language :: Python :: 3",
]
urls = {{ Homepage = "{homepage}", Repository = "https://github.com/{owner}/{repo}" }}
[tool.maturin]
bindings = "bin"
manifest-path = "{manifest}"
module-name = "{name}"
strip = true
include = [
{{ path = "LICENSE", format = "sdist" }},
{{ path = "README.md", format = "sdist" }},
]
"#,
version = p.version,
desc = toml_string(&p.description),
license = p.license,
owner = p.github_owner,
repo = p.github_repo,
homepage = ctx.config.homepage(),
);
let workflow = r#"name: Publish PyPI wheels
on:
workflow_dispatch:
push:
tags:
- 'v*'
permissions:
contents: read
jobs:
wheels:
name: Wheel (${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
target: x86_64
manylinux: auto
- os: ubuntu-24.04-arm
target: aarch64
manylinux: auto
- os: macos-latest
target: aarch64
- os: macos-latest
target: x86_64
- os: windows-latest
target: x64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist
manylinux: ${{ matrix.manylinux }}
maturin-version: v1.8.3
- uses: actions/upload-artifact@v4
with:
name: wheels-${{ matrix.os }}-${{ matrix.target }}
path: dist
sdist:
name: Source distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
maturin-version: v1.8.3
- uses: actions/upload-artifact@v4
with:
name: wheels-sdist
path: dist
publish:
name: Publish to PyPI
needs: [wheels, sdist]
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch'
environment: pypi
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
path: dist
merge-multiple: true
- name: Publish
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
skip-existing: true
"#
.to_string();
let notes_md = format!(
r#"# PyPI (`{name}`)
Trusted Publishing (no API token):
1. Create GitHub Environment **`pypi`**
2. On pypi.org → Publishing → pending publisher:
- Owner: `{owner}`
- Repository: `{repo}`
- Workflow: `publish-pypi.yml`
- Environment: `pypi`
```bash
pip install {name}
{bin} --version
```
"#,
owner = p.github_owner,
repo = p.github_repo,
bin = p.binary_name,
);
let wf_path = format!(
"{}/publish-pypi.yml",
ctx.config.paths.workflows_dir.trim_end_matches('/')
);
let base = format!(
"{}/pypi",
ctx.config.paths.packaging_dir.trim_end_matches('/')
);
Ok(StepResult {
files: vec![
GeneratedFile {
relative_path: "pyproject.toml".into(),
contents: pyproject,
executable: false,
},
GeneratedFile {
relative_path: wf_path,
contents: workflow,
executable: false,
},
GeneratedFile {
relative_path: format!("{base}/README.md"),
contents: notes_md,
executable: false,
},
],
notes: vec![
"Configure PyPI Trusted Publisher before first upload.".into(),
"Ensure LICENSE exists at repo root for the sdist.".into(),
],
})
}
}
fn toml_string(s: &str) -> String {
format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
}