Skip to content

Commit

Permalink
feat(python): add Python bindings for Autonomi
Browse files Browse the repository at this point in the history
This commit introduces Python bindings for the Autonomi crate using PyO3,
making the Autonomi network client accessible from Python applications.

Key changes:
- Add autonomi-py crate with PyO3 bindings
- Configure workspace to include Python package
- Set up maturin build system for Python package
- Add GitHub Actions workflow for building and publishing Python wheels
- Configure cross-platform builds for Linux, macOS, and Windows
- Add Python 3.8-3.12 support

The Python package provides bindings for core Autonomi functionality including:
- Network client connection
- Data upload/download
- Wallet management
- Payment handling

Build artifacts will be published to PyPI when a new version is tagged.
  • Loading branch information
dirvine committed Nov 7, 2024
1 parent a7a96b5 commit ff20962
Show file tree
Hide file tree
Showing 15 changed files with 1,372 additions and 1 deletion.
190 changes: 190 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
name: Build and Publish Python Package

on:
push:
tags:
- 'XXX*'

permissions:
id-token: write
contents: read

jobs:
macos:
runs-on: macos-latest
permissions:
id-token: write
contents: read
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
target: [x86_64, aarch64]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Create Python module structure
run: |
mkdir -p autonomi/python/autonomi_client
cat > autonomi/python/autonomi_client/__init__.py << EOL
from ._autonomi import *
__version__ = "0.2.33"
EOL
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist
sccache: 'true'
working-directory: ./autonomi
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: autonomi/dist/*.whl
if-no-files-found: error

windows:
runs-on: windows-latest
permissions:
id-token: write
contents: read
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
target: [x64]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.target }}
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Create Python module structure
shell: cmd
run: |
mkdir autonomi\python\autonomi_client
echo from ._autonomi import * > autonomi\python\autonomi_client\__init__.py
echo __version__ = "0.2.33" >> autonomi\python\autonomi_client\__init__.py
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
args: --release --out dist
sccache: 'true'
working-directory: ./autonomi
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: autonomi/dist/*.whl
if-no-files-found: error

linux:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
target: [x86_64]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
target: x86_64-unknown-linux-gnu
- name: Install dependencies
run: |
python -m pip install --user cffi
python -m pip install --user patchelf
rustup component add rustfmt
- name: Create Python module structure
run: |
mkdir -p autonomi/python/autonomi_client
cat > autonomi/python/autonomi_client/__init__.py << EOL
from ._autonomi import *
__version__ = "0.2.33"
EOL
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
manylinux: auto
args: --release --out dist
sccache: 'true'
working-directory: ./autonomi
before-script-linux: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
rustup component add rustfmt
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: autonomi/dist/*.whl
if-no-files-found: error

sdist:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Create Python module structure
run: |
mkdir -p autonomi/python/autonomi_client
cat > autonomi/python/autonomi_client/__init__.py << EOL
from ._autonomi import *
__version__ = "0.2.33"
EOL
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
working-directory: ./autonomi
- name: Upload sdist
uses: actions/upload-artifact@v3
with:
name: wheels
path: autonomi/dist/*.tar.gz
if-no-files-found: error

release:
name: Release
runs-on: ubuntu-latest
needs: [macos, windows, linux, sdist]
permissions:
id-token: write
contents: read
steps:
- uses: actions/download-artifact@v3
with:
name: wheels
path: dist
- name: Display structure of downloaded files
run: ls -R dist
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
verbose: true
print-hash: true
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ metrics/prometheus/prometheus.yml
*.dot

sn_node_manager/.vagrant

# Python
.venv/
uv.lock
*.so
*.pyc

*.pyc
*.swp

91 changes: 91 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "README.md"
repository = "https://github.com/maidsafe/safe_network"

[lib]
name = "autonomi"
crate-type = ["cdylib", "rlib"]

[features]
Expand All @@ -22,6 +23,7 @@ local = ["sn_networking/local", "sn_evm/local"]
registers = ["data"]
loud = []
external-signer = ["sn_evm/external-signer", "data"]
extension-module = ["pyo3/extension-module"]

[dependencies]
bip39 = "2.0.0"
Expand Down Expand Up @@ -55,6 +57,7 @@ serde-wasm-bindgen = "0.6.5"
sha2 = "0.10.6"
blst = "0.3.13"
blstrs = "0.7.1"
pyo3 = { version = "0.20", optional = true, features = ["extension-module", "abi3-py38"] }

[dev-dependencies]
alloy = { version = "0.5.3", default-features = false, features = ["std", "reqwest-rustls-tls", "provider-anvil-node", "sol-types", "json", "signers", "contract", "signer-local", "network"] }
Expand Down
Loading

0 comments on commit ff20962

Please sign in to comment.