From 1d6e370ea17544132c8989a7a65b1b4f7bcaa480 Mon Sep 17 00:00:00 2001 From: AranMesquita Date: Sun, 6 Jul 2025 11:11:40 +0200 Subject: [PATCH] setup python --- .github/workflows/CI.yml | 181 +++++++++++++++++++++++++++++++++++++++ .gitignore | 2 + Cargo.toml | 6 ++ README.md | 16 +++- pyproject.toml | 15 ++++ requirements.txt | Bin 0 -> 34 bytes src-py/example.py | 7 ++ src/lib.rs | 22 ++--- 8 files changed, 237 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/CI.yml create mode 100644 pyproject.toml create mode 100644 requirements.txt create mode 100644 src-py/example.py diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..3de9ce6 --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,181 @@ +# This file is autogenerated by maturin v1.9.0 +# To update, run +# +# maturin generate-ci github +# +name: CI + +on: + push: + branches: + - main + - master + tags: + - '*' + pull_request: + workflow_dispatch: + +permissions: + contents: read + +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@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} + manylinux: auto + - name: Upload wheels + uses: actions/upload-artifact@v4 + 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@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} + manylinux: musllinux_1_2 + - name: Upload wheels + uses: actions/upload-artifact@v4 + with: + name: wheels-musllinux-${{ matrix.platform.target }} + path: dist + + windows: + runs-on: ${{ matrix.platform.runner }} + strategy: + matrix: + platform: + - runner: windows-latest + target: x64 + - runner: windows-latest + target: x86 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + architecture: ${{ matrix.platform.target }} + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} + - name: Upload wheels + uses: actions/upload-artifact@v4 + with: + name: wheels-windows-${{ matrix.platform.target }} + path: dist + + macos: + runs-on: ${{ matrix.platform.runner }} + strategy: + matrix: + platform: + - runner: macos-13 + target: x86_64 + - runner: macos-14 + target: aarch64 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} + - name: Upload wheels + uses: actions/upload-artifact@v4 + with: + name: wheels-macos-${{ matrix.platform.target }} + path: dist + + sdist: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build sdist + uses: PyO3/maturin-action@v1 + with: + command: sdist + args: --out dist + - name: Upload sdist + uses: actions/upload-artifact@v4 + with: + name: wheels-sdist + path: dist + + release: + name: Release + runs-on: ubuntu-latest + if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }} + needs: [linux, musllinux, windows, macos, sdist] + permissions: + # Use to sign the release artifacts + id-token: write + # Used to upload release artifacts + contents: write + # Used to generate artifact attestation + attestations: write + steps: + - uses: actions/download-artifact@v4 + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v2 + with: + subject-path: 'wheels-*/*' + - name: Publish to PyPI + if: ${{ startsWith(github.ref, 'refs/tags/') }} + uses: PyO3/maturin-action@v1 + env: + MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} + with: + command: upload + args: --non-interactive --skip-existing wheels-*/* diff --git a/.gitignore b/.gitignore index ea8c4bf..198dc01 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +/.venv +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 6cfa556..800692a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,10 @@ name = "test_rust_in_python" version = "0.1.0" edition = "2021" +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "test_rust_in_python" +crate-type = ["cdylib"] + [dependencies] +pyo3 = "0.25.0" diff --git a/README.md b/README.md index 0195f1b..32063b0 100644 --- a/README.md +++ b/README.md @@ -1 +1,15 @@ -A repo where I use rust in python and vice versa to test the capabilities of using rust and python code togethe +A repo where I use rust in python and vice versa to test the capabilities of using rust and python code together + + + +\[setup steps] + +1. python -m venv .venv +2. activate the venv +3. pip install maturin (build system for building and publishing rust crates as python packages) +4. maturin init \& select --> pyo3 +5. create the folder in the root directory where all the python files will be kept e.g. mkdir src-py +6. create a python file in the python directory +7. import and run the functions, with the rust #\[pymodule] attribute, that is located in the .\\src\\lib.rs file +8. then run maturin develop --release (which builds the lib.rs file and installs it as a package in the .venv) +9. then run the python file with the imported rust functions diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..6f61b15 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[build-system] +requires = ["maturin>=1.9,<2.0"] +build-backend = "maturin" + +[project] +name = "test_rust_in_python" +requires-python = ">=3.8" +classifiers = [ + "Programming Language :: Rust", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] +dynamic = ["version"] +[tool.maturin] +features = ["pyo3/extension-module"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..b7546dc174bbd1f11e2da650303887e26f962069 GIT binary patch literal 34 mcmezWFP9;ap@gB7p@<=qA&g literal 0 HcmV?d00001 diff --git a/src-py/example.py b/src-py/example.py new file mode 100644 index 0000000..18d23bc --- /dev/null +++ b/src-py/example.py @@ -0,0 +1,7 @@ +import test_rust_in_python + +RUST_FUNCTIONS = test_rust_in_python + +result = RUST_FUNCTIONS.sum_as_string(1, 2) + +print(f"Result from Rust: {result}") diff --git a/src/lib.rs b/src/lib.rs index b93cf3f..a492c32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,14 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right -} +use pyo3::prelude::*; -#[cfg(test)] -mod tests { - use super::*; +/// Formats the sum of two numbers as string. +#[pyfunction] +fn sum_as_string(a: usize, b: usize) -> PyResult { + Ok((a + b).to_string()) +} - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +/// A Python module implemented in Rust. +#[pymodule] +fn test_rust_in_python(m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + Ok(()) }