Skip to content

Commit

Permalink
rust + python package first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Sep 21, 2024
1 parent 33e9b9e commit 0a552db
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 44 deletions.
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[target.aarch64-apple-darwin]

rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ build/
dist/
*.so
out*/

# Rust
target/
Cargo.lock
20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[workspace]
members = [
"actuator/rust",
]
resolver = "2"

[workspace.package]
version = "0.1.0"
edition = "2021"

description = "Actuator Python bindings"
authors = ["K-Scale Labs <[email protected]>"]
repository = "https://github.com/kscalelabs/actuator"
license = "MIT"
readme = "README.md"

[workspace.dependencies]
pyo3 = { version = ">= 0.21.0", features = ["extension-module"] }
pyo3-stub-gen = ">= 0.6.0"
env_logger = "0.11.5"
5 changes: 0 additions & 5 deletions actuator/.gitignore

This file was deleted.

13 changes: 13 additions & 0 deletions actuator/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Defines the CLI for the actuator project."""

from actuator.rust.lib import hello_world, sum


def main() -> None:
hello_world()
print(sum([1, 2, 3, 4, 5]))


if __name__ == "__main__":
# python -m actuator.cli
main()
4 changes: 0 additions & 4 deletions actuator/rust/.gitignore

This file was deleted.

13 changes: 9 additions & 4 deletions actuator/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
[package]
name = "lib"
version = "0.1.0"
edition = "2021"
version.workspace = true
edition.workspace = true

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

[dependencies]
pyo3 = { version = "0.21", features = ["extension-module"] }
pyo3-stub-gen = "0.6.0"
env_logger.workspace = true
pyo3-stub-gen.workspace = true
pyo3.workspace = true

[[bin]]
name = "stub_gen"
doc = false
Empty file added actuator/rust/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions actuator/rust/lib.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is automatically generated by pyo3_stub_gen
# ruff: noqa: E501, F401

import typing

def hello_world() -> None:
...

def sum(v:typing.Sequence[int]) -> int:
...

10 changes: 10 additions & 0 deletions actuator/rust/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = ["maturin>=1.1,<2.0"]
build-backend = "maturin"

[project]
name = "lib"
requires-python = ">=3.9"

[project.optional-dependencies]
test = ["pytest", "pyright", "ruff"]
2 changes: 1 addition & 1 deletion actuator/rust/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pyo3_stub_gen::Result;

fn main() -> Result<()> {
let stub = pure::stub_info()?;
let stub = lib::stub_info()?;
stub.generate()?;
Ok(())
}
12 changes: 8 additions & 4 deletions actuator/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use pyo3::prelude::*;
use pyo3_stub_gen::{define_stub_info_gatherer, derive::gen_stub_pyfunction};

#[gen_stub_pyfunction]
#[pyfunction]
fn hello_world() {
println!("Hello, world!");
}

#[gen_stub_pyfunction]
#[pyfunction]
fn add(a: i64, b: i64) -> i64 {
a + b
fn sum(v: Vec<u32>) -> u32 {
v.iter().sum()
}

#[pymodule]
fn lib(py: Python, m: &PyModule) -> PyResult<()> {
fn lib(m: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello_world, m)?)?;
m.add_function(wrap_pyfunction!(add, m)?)?;
m.add_function(wrap_pyfunction!(sum, m)?)?;
Ok(())
}

define_stub_info_gatherer!(stub_info);
29 changes: 3 additions & 26 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
"""Setup script for the project."""

import re
import subprocess

from setuptools import Command, setup
from setuptools import setup
from setuptools_rust import Binding, RustExtension

with open("README.md", "r", encoding="utf-8") as f:
Expand All @@ -26,22 +25,6 @@
version: str = version_re.group(1)


class PostInstallCommand(Command):
"""Post-installation for installation mode."""

description = "Run stub_gen after installation"
user_options = []

def initialize_options(self) -> None:
pass

def finalize_options(self) -> None:
pass

def run(self) -> None:
subprocess.check_call(["cargo", "run", "--bin", "stub_gen"], cwd="actuator/rust")


setup(
name="actuator",
version=version,
Expand All @@ -53,12 +36,9 @@ def run(self) -> None:
target="actuator.rust.lib",
path="actuator/rust/Cargo.toml",
binding=Binding.PyO3,
)
],
setup_requires=[
"setuptools-rust",
"mypy", # For stubgen
),
],
setup_requires=["setuptools-rust"],
include_package_data=True,
zip_safe=False,
long_description=long_description,
Expand All @@ -67,7 +47,4 @@ def run(self) -> None:
install_requires=requirements,
tests_require=requirements_dev,
extras_require={"dev": requirements_dev},
cmdclass={
"post_install": PostInstallCommand,
},
)

0 comments on commit 0a552db

Please sign in to comment.