Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Sep 20, 2024
1 parent 670ee4c commit 33e9b9e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
5 changes: 3 additions & 2 deletions actuator/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ edition = "2021"

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

[dependencies]
pyo3 = { version = "0.17", features = ["extension-module"] }
pyo3 = { version = "0.21", features = ["extension-module"] }
pyo3-stub-gen = "0.6.0"
7 changes: 7 additions & 0 deletions actuator/rust/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use pyo3_stub_gen::Result;

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

#[pymodule]
fn lib(_py: Python, _m: &PyModule) -> PyResult<()> {
#[pyfunction]
fn hello_world() {
println!("Hello, world!");
}

#[pyfunction]
fn add(a: i64, b: i64) -> i64 {
a + b
}

#[pymodule]
fn lib(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello_world, m)?)?;
m.add_function(wrap_pyfunction!(add, m)?)?;
Ok(())
}
35 changes: 20 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@
#!/usr/bin/env python
"""Setup script for the project."""

import os
import re
import subprocess

from setuptools import setup
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools import Command, setup
from setuptools_rust import Binding, RustExtension


class BuildExtWithStubgen(_build_ext):
def run(self) -> None:
super().run()

module_name = "actuator.rust.lib"
output_dir = os.path.dirname(__file__)
os.makedirs(output_dir, exist_ok=True)
subprocess.run(["stubgen", "-m", module_name, "-o", output_dir], check=False)


with open("README.md", "r", encoding="utf-8") as f:
long_description: str = f.read()

Expand All @@ -39,6 +26,22 @@ def run(self) -> None:
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 @@ -52,7 +55,6 @@ def run(self) -> None:
binding=Binding.PyO3,
)
],
cmdclass={"build_ext": BuildExtWithStubgen},
setup_requires=[
"setuptools-rust",
"mypy", # For stubgen
Expand All @@ -65,4 +67,7 @@ def run(self) -> None:
install_requires=requirements,
tests_require=requirements_dev,
extras_require={"dev": requirements_dev},
cmdclass={
"post_install": PostInstallCommand,
},
)

0 comments on commit 33e9b9e

Please sign in to comment.