From bd4f3e9be8d0d5772ee63f15cd75edc07aa844df Mon Sep 17 00:00:00 2001 From: Ben Bolte Date: Fri, 22 Nov 2024 21:16:37 -0800 Subject: [PATCH] ci changes (#2) * ci changes * things build... * python build magic * update publish flow * update minor version * remove version * correct versions * asdf * fix cargo --- .cargo/config.toml | 6 + .github/workflows/publish.yml | 106 ++- .github/workflows/test.yml | 19 +- .gitignore | 5 + Cargo.lock | 909 ----------------------- Cargo.toml | 10 +- krec/__init__.py | 3 + krec/bindings/Cargo.toml | 16 + {python => krec/bindings}/pyproject.toml | 4 +- krec/bindings/src/bin/stub_gen.rs | 7 + {python => krec/bindings}/src/lib.rs | 95 ++- krec/py.typed | 0 pyproject.toml | 86 +++ python/Cargo.toml | 13 - setup.py | 63 ++ 15 files changed, 338 insertions(+), 1004 deletions(-) create mode 100644 .cargo/config.toml delete mode 100644 Cargo.lock create mode 100644 krec/__init__.py create mode 100644 krec/bindings/Cargo.toml rename {python => krec/bindings}/pyproject.toml (87%) create mode 100644 krec/bindings/src/bin/stub_gen.rs rename {python => krec/bindings}/src/lib.rs (93%) create mode 100644 krec/py.typed create mode 100644 pyproject.toml delete mode 100644 python/Cargo.toml create mode 100644 setup.py diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..f648169 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,6 @@ +[target.aarch64-apple-darwin] + +rustflags = [ + "-C", "link-arg=-undefined", + "-C", "link-arg=dynamic_lookup", +] diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 1ba47ac..3f5e105 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -14,40 +14,27 @@ concurrency: cancel-in-progress: true jobs: - build-rust: - name: Build and publish Rust package - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Install Rust - uses: dtolnay/rust-toolchain@stable - - - name: Build and publish to crates.io - env: - CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} - run: | - cargo publish --package krec-rs - build-wheels: - needs: build-rust strategy: matrix: os: [ubuntu-latest, macos-latest] - name: Build Python wheels (${{ matrix.os }}) + name: Build and publish Python package (${{ matrix.os }}) timeout-minutes: 10 runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 - - - name: Install Rust - uses: dtolnay/rust-toolchain@stable + - name: Checkout code + uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: - python-version: "3.11" + python-version: "3.10" + + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable - name: Install dependencies run: | @@ -55,34 +42,48 @@ jobs: pip install cibuildwheel shell: bash - - name: Build wheels + - name: Build package env: CIBW_SKIP: "pp* *-musllinux*" # Skip PyPy and musllinux builds - CIBW_BEFORE_BUILD: "pip install maturin" - CIBW_BUILD_BACKEND: "maturin" + CIBW_BEFORE_ALL_LINUX: | + yum install -y libudev-devel pkgconfig + CIBW_BEFORE_ALL_MACOS: | + brew install openssl pkg-config + CIBW_BEFORE_BUILD: | + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y + source $HOME/.cargo/env + pip install setuptools-rust + CIBW_ENVIRONMENT: | + PATH="/usr/local/bin:$HOME/.cargo/bin:$PATH" + CARGO_NET_GIT_FETCH_WITH_CLI=true run: | - cd python - cibuildwheel --output-dir ../dist + cibuildwheel --output-dir dist - name: Upload wheel artifacts uses: actions/upload-artifact@v3 with: name: wheels-${{ matrix.os }} - path: dist/*.whl + path: | + dist/*.whl build-source-dist: - needs: build-rust - name: Build Python source distribution + name: Build and publish Python package (source distribution) timeout-minutes: 10 runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: Checkout code + uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: - python-version: "3.11" + python-version: "3.10" + + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable - name: Install dependencies run: | @@ -123,3 +124,44 @@ jobs: uses: pypa/gh-action-pypi-publish@release/v1 with: packages-dir: final_dist/ + + publish-rust: + name: Build and publish Rust package + timeout-minutes: 10 + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y libudev-dev pkg-config + + - name: Cache Cargo registry + uses: actions/cache@v2 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry + restore-keys: | + ${{ runner.os }}-cargo-registry + + - name: Cache Cargo index + uses: actions/cache@v2 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index + restore-keys: | + ${{ runner.os }}-cargo-index + + - name: Publish K-Rec package to crates.io + env: + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} + run: | + cargo publish -p krec diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04aa24d..35c7b88 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,6 +30,14 @@ jobs: with: python-version: "3.11" + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get install -y libudev-dev pkg-config + + - name: Install protoc + uses: arduino/setup-protoc@v3 + - name: Restore cache id: restore-cache uses: actions/cache/restore@v3 @@ -44,16 +52,15 @@ jobs: - name: Install package run: | - pip install --upgrade --upgrade-strategy eager --extra-index-url https://download.pytorch.org/whl/cpu -e '.[dev]' + pip install --upgrade --upgrade-strategy eager -e '.' - - name: Run static checks + - name: Check Rust lint run: | - mkdir -p .mypy_cache - make static-checks + cargo fmt --check - - name: Run unit tests + - name: Run Rust tests run: | - make test + cargo test - name: Save cache uses: actions/cache/save@v3 diff --git a/.gitignore b/.gitignore index ae8e28a..8c07eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,8 @@ build/ dist/ *.so out*/ +*.pyi + +# Rust +Cargo.lock +target/ diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 10a0817..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,909 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "addr2line" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - -[[package]] -name = "anyhow" -version = "1.0.93" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" - -[[package]] -name = "autocfg" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" - -[[package]] -name = "backtrace" -version = "0.3.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" -dependencies = [ - "addr2line", - "cc", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", -] - -[[package]] -name = "bitflags" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" - -[[package]] -name = "bytes" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" - -[[package]] -name = "cc" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" -dependencies = [ - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "color-eyre" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5" -dependencies = [ - "backtrace", - "color-spantrace", - "eyre", - "indenter", - "once_cell", - "owo-colors", - "tracing-error", -] - -[[package]] -name = "color-spantrace" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" -dependencies = [ - "once_cell", - "owo-colors", - "tracing-core", - "tracing-error", -] - -[[package]] -name = "either" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" - -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - -[[package]] -name = "errno" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "eyre" -version = "0.6.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" -dependencies = [ - "indenter", - "once_cell", -] - -[[package]] -name = "fastrand" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" - -[[package]] -name = "fixedbitset" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" - -[[package]] -name = "gimli" -version = "0.28.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" - -[[package]] -name = "hashbrown" -version = "0.15.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" - -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "indenter" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" - -[[package]] -name = "indexmap" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "indoc" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" - -[[package]] -name = "itertools" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" -dependencies = [ - "either", -] - -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - -[[package]] -name = "krec" -version = "0.1.0" -dependencies = [ - "bytes", - "color-eyre", - "eyre", - "prost 0.13.3", - "prost-build", - "thiserror", - "tracing", - "tracing-subscriber", -] - -[[package]] -name = "krec-py" -version = "0.1.0" -dependencies = [ - "krec", - "pyo3", - "tracing", -] - -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - -[[package]] -name = "libc" -version = "0.2.164" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" - -[[package]] -name = "linux-raw-sys" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" - -[[package]] -name = "lock_api" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" -dependencies = [ - "autocfg", - "scopeguard", -] - -[[package]] -name = "log" -version = "0.4.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" - -[[package]] -name = "matchers" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" -dependencies = [ - "regex-automata 0.1.10", -] - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - -[[package]] -name = "memoffset" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" -dependencies = [ - "autocfg", -] - -[[package]] -name = "miniz_oxide" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" -dependencies = [ - "adler", -] - -[[package]] -name = "multimap" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" - -[[package]] -name = "nu-ansi-term" -version = "0.46.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" -dependencies = [ - "overload", - "winapi", -] - -[[package]] -name = "object" -version = "0.32.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" -dependencies = [ - "memchr", -] - -[[package]] -name = "once_cell" -version = "1.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" - -[[package]] -name = "overload" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" - -[[package]] -name = "owo-colors" -version = "3.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" - -[[package]] -name = "parking_lot" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets", -] - -[[package]] -name = "petgraph" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" -dependencies = [ - "fixedbitset", - "indexmap", -] - -[[package]] -name = "pin-project-lite" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" - -[[package]] -name = "prettyplease" -version = "0.2.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" -dependencies = [ - "proc-macro2", - "syn 2.0.89", -] - -[[package]] -name = "proc-macro2" -version = "1.0.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "prost" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29" -dependencies = [ - "bytes", - "prost-derive 0.12.6", -] - -[[package]] -name = "prost" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b0487d90e047de87f984913713b85c601c05609aad5b0df4b4573fbf69aa13f" -dependencies = [ - "bytes", - "prost-derive 0.13.3", -] - -[[package]] -name = "prost-build" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22505a5c94da8e3b7c2996394d1c933236c4d743e81a410bcca4e6989fc066a4" -dependencies = [ - "bytes", - "heck", - "itertools 0.12.1", - "log", - "multimap", - "once_cell", - "petgraph", - "prettyplease", - "prost 0.12.6", - "prost-types", - "regex", - "syn 2.0.89", - "tempfile", -] - -[[package]] -name = "prost-derive" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1" -dependencies = [ - "anyhow", - "itertools 0.12.1", - "proc-macro2", - "quote", - "syn 2.0.89", -] - -[[package]] -name = "prost-derive" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" -dependencies = [ - "anyhow", - "itertools 0.13.0", - "proc-macro2", - "quote", - "syn 2.0.89", -] - -[[package]] -name = "prost-types" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9091c90b0a32608e984ff2fa4091273cbdd755d54935c51d520887f4a1dbd5b0" -dependencies = [ - "prost 0.12.6", -] - -[[package]] -name = "pyo3" -version = "0.19.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e681a6cfdc4adcc93b4d3cf993749a4552018ee0a9b65fc0ccfad74352c72a38" -dependencies = [ - "cfg-if", - "indoc", - "libc", - "memoffset", - "parking_lot", - "pyo3-build-config", - "pyo3-ffi", - "pyo3-macros", - "unindent", -] - -[[package]] -name = "pyo3-build-config" -version = "0.19.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "076c73d0bc438f7a4ef6fdd0c3bb4732149136abd952b110ac93e4edb13a6ba5" -dependencies = [ - "once_cell", - "target-lexicon", -] - -[[package]] -name = "pyo3-ffi" -version = "0.19.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e53cee42e77ebe256066ba8aa77eff722b3bb91f3419177cf4cd0f304d3284d9" -dependencies = [ - "libc", - "pyo3-build-config", -] - -[[package]] -name = "pyo3-macros" -version = "0.19.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfeb4c99597e136528c6dd7d5e3de5434d1ceaf487436a3f03b2d56b6fc9efd1" -dependencies = [ - "proc-macro2", - "pyo3-macros-backend", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "pyo3-macros-backend" -version = "0.19.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "947dc12175c254889edc0c02e399476c2f652b4b9ebd123aa655c224de259536" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "quote" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "redox_syscall" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" -dependencies = [ - "bitflags", -] - -[[package]] -name = "regex" -version = "1.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata 0.4.9", - "regex-syntax 0.8.5", -] - -[[package]] -name = "regex-automata" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" -dependencies = [ - "regex-syntax 0.6.29", -] - -[[package]] -name = "regex-automata" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax 0.8.5", -] - -[[package]] -name = "regex-syntax" -version = "0.6.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" - -[[package]] -name = "regex-syntax" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" - -[[package]] -name = "rustc-demangle" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" - -[[package]] -name = "rustix" -version = "0.38.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.52.0", -] - -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "sharded-slab" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "smallvec" -version = "1.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" - -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "syn" -version = "2.0.89" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "target-lexicon" -version = "0.12.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" - -[[package]] -name = "tempfile" -version = "3.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" -dependencies = [ - "cfg-if", - "fastrand", - "once_cell", - "rustix", - "windows-sys 0.59.0", -] - -[[package]] -name = "thiserror" -version = "1.0.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", -] - -[[package]] -name = "thread_local" -version = "1.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" -dependencies = [ - "cfg-if", - "once_cell", -] - -[[package]] -name = "tracing" -version = "0.1.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" -dependencies = [ - "pin-project-lite", - "tracing-attributes", - "tracing-core", -] - -[[package]] -name = "tracing-attributes" -version = "0.1.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", -] - -[[package]] -name = "tracing-core" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" -dependencies = [ - "once_cell", - "valuable", -] - -[[package]] -name = "tracing-error" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" -dependencies = [ - "tracing", - "tracing-subscriber", -] - -[[package]] -name = "tracing-log" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" -dependencies = [ - "log", - "once_cell", - "tracing-core", -] - -[[package]] -name = "tracing-subscriber" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" -dependencies = [ - "matchers", - "nu-ansi-term", - "once_cell", - "regex", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", -] - -[[package]] -name = "unicode-ident" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" - -[[package]] -name = "unindent" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" - -[[package]] -name = "valuable" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 7ae510b..40f2f34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "krec" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true description = "K-Scale robot action format" license = "MIT" authors = ["Denys Bezmenov "] @@ -19,4 +19,8 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } prost-build = "0.12" [workspace] -members = ["python"] +members = ["krec/bindings"] + +[workspace.package] +version = "0.2.0" +edition = "2021" diff --git a/krec/__init__.py b/krec/__init__.py new file mode 100644 index 0000000..f0a572e --- /dev/null +++ b/krec/__init__.py @@ -0,0 +1,3 @@ +"""Python bindings for K-Scale recordings.""" + +from .bindings import * diff --git a/krec/bindings/Cargo.toml b/krec/bindings/Cargo.toml new file mode 100644 index 0000000..b5861bd --- /dev/null +++ b/krec/bindings/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "bindings" +version.workspace = true +edition.workspace = true + +[lib] +name = "bindings" +crate-type = ["cdylib", "rlib"] + +[dependencies] +pyo3 = { version = ">= 0.21", features = ["extension-module"] } +pyo3-stub-gen = ">= 0.6" +tracing = "0.1" + +# Workspace packages. +krec = { path = "../.." } diff --git a/python/pyproject.toml b/krec/bindings/pyproject.toml similarity index 87% rename from python/pyproject.toml rename to krec/bindings/pyproject.toml index 8d94a9d..d4d3c3c 100644 --- a/python/pyproject.toml +++ b/krec/bindings/pyproject.toml @@ -3,7 +3,7 @@ requires = ["maturin>=1.0,<2.0"] build-backend = "maturin" [project] -name = "krec" +name = "bindings" version = "0.1.4" description = "Python bindings for KRec" authors = [{ name = "Denys Bezmenov", email = "denys@kscale.dev" }] @@ -15,4 +15,4 @@ classifiers = [ ] [tool.maturin] -features = ["pyo3/extension-module"] \ No newline at end of file +features = ["pyo3/extension-module"] diff --git a/krec/bindings/src/bin/stub_gen.rs b/krec/bindings/src/bin/stub_gen.rs new file mode 100644 index 0000000..ceb1044 --- /dev/null +++ b/krec/bindings/src/bin/stub_gen.rs @@ -0,0 +1,7 @@ +use pyo3_stub_gen::Result; + +fn main() -> Result<()> { + let stub = bindings::stub_info()?; + stub.generate()?; + Ok(()) +} diff --git a/python/src/lib.rs b/krec/bindings/src/lib.rs similarity index 93% rename from python/src/lib.rs rename to krec/bindings/src/lib.rs index 863d46d..2937292 100644 --- a/python/src/lib.rs +++ b/krec/bindings/src/lib.rs @@ -1,33 +1,37 @@ -use ::krec::{ +use krec::{ ActuatorCommand, ActuatorConfig, ActuatorState, ImuQuaternion, ImuValues, KRec, KRecFrame, KRecHeader, Vec3, }; use pyo3::exceptions::{PyIndexError, PyValueError}; use pyo3::prelude::*; use pyo3::types::PyIterator; +use pyo3_stub_gen::define_stub_info_gatherer; +use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pyfunction, gen_stub_pymethods}; use tracing::{info, instrument}; /// A 3D vector with x, y, z components +#[gen_stub_pyclass] #[pyclass(name = "Vec3")] #[derive(Debug, Clone)] struct PyVec3 { inner: Vec3, } +#[gen_stub_pymethods] #[pymethods] impl PyVec3 { #[new] - #[pyo3(text_signature = "(x=0.0, y=0.0, z=0.0, /, values=None)")] + #[pyo3(signature = (x=None, y=None, z=None, values=None))] fn new( - py: Python<'_>, + _py: Python<'_>, x: Option, y: Option, z: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { // Try to convert from iterable - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut coords: Vec = Vec::new(); for item in iter { let value: f64 = item?.extract()?; @@ -76,26 +80,28 @@ impl PyVec3 { } /// A quaternion representing 3D rotation +#[gen_stub_pyclass] #[pyclass(name = "IMUQuaternion")] #[derive(Debug, Clone)] struct PyIMUQuaternion { inner: ImuQuaternion, } +#[gen_stub_pymethods] #[pymethods] impl PyIMUQuaternion { #[new] - #[pyo3(text_signature = "(x=0.0, y=0.0, z=0.0, w=1.0, /, values=None)")] + #[pyo3(signature = (x=None, y=None, z=None, w=None, values=None))] fn new( - py: Python<'_>, + _py: Python<'_>, x: Option, y: Option, z: Option, w: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut coords: Vec = Vec::new(); for item in iter { let value: f64 = item?.extract()?; @@ -149,26 +155,28 @@ impl PyIMUQuaternion { } /// IMU sensor values including acceleration, gyroscope, and orientation data +#[gen_stub_pyclass] #[pyclass(name = "IMUValues")] #[derive(Debug, Clone)] struct PyIMUValues { inner: ImuValues, } +#[gen_stub_pymethods] #[pymethods] impl PyIMUValues { #[new] - #[pyo3(text_signature = "(accel=None, gyro=None, mag=None, quaternion=None, /, values=None)")] + #[pyo3(signature = (accel=None, gyro=None, mag=None, quaternion=None, values=None))] fn new( py: Python<'_>, accel: Option, gyro: Option, mag: Option, quaternion: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut items = Vec::new(); for item in iter { let item = item?; @@ -270,18 +278,18 @@ impl PyIMUValues { } /// State information for a single actuator +#[gen_stub_pyclass] #[pyclass(name = "ActuatorState")] #[derive(Debug, Clone)] struct PyActuatorState { inner: ActuatorState, } +#[gen_stub_pymethods] #[pymethods] impl PyActuatorState { #[new] - #[pyo3( - text_signature = "(actuator_id, online=False, position=None, velocity=None, torque=None, temperature=None, voltage=None, current=None, /, values=None)" - )] + #[pyo3(signature = (actuator_id, online=None, position=None, velocity=None, torque=None, temperature=None, voltage=None, current=None, values=None))] fn new( py: Python<'_>, actuator_id: u32, @@ -292,10 +300,10 @@ impl PyActuatorState { temperature: Option, voltage: Option, current: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut items = Vec::new(); for item in iter { let item = item?; @@ -439,18 +447,18 @@ impl PyActuatorState { } /// Configuration for an actuator +#[gen_stub_pyclass] #[pyclass(name = "ActuatorConfig")] #[derive(Debug, Clone)] struct PyActuatorConfig { inner: ActuatorConfig, } +#[gen_stub_pymethods] #[pymethods] impl PyActuatorConfig { #[new] - #[pyo3( - text_signature = "(actuator_id, kp=None, kd=None, ki=None, max_torque=None, name=None, /, values=None)" - )] + #[pyo3(signature = (actuator_id, kp=None, kd=None, ki=None, max_torque=None, name=None, values=None))] fn new( py: Python<'_>, actuator_id: u32, @@ -459,10 +467,10 @@ impl PyActuatorConfig { ki: Option, max_torque: Option, name: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut items = Vec::new(); for item in iter { let item = item?; @@ -578,28 +586,28 @@ impl PyActuatorConfig { } /// Command for an actuator +#[gen_stub_pyclass] #[pyclass(name = "ActuatorCommand")] #[derive(Debug, Clone)] struct PyActuatorCommand { inner: ActuatorCommand, } +#[gen_stub_pymethods] #[pymethods] impl PyActuatorCommand { #[new] - #[pyo3( - text_signature = "(actuator_id, position=0.0, velocity=0.0, effort=0.0, /, values=None)" - )] + #[pyo3(signature = (actuator_id, position=None, velocity=None, torque=None, values=None))] fn new( - py: Python<'_>, + _py: Python<'_>, actuator_id: u32, position: Option, velocity: Option, torque: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut coords: Vec = Vec::new(); for item in iter { let value: f32 = item?.extract()?; @@ -667,12 +675,14 @@ impl PyActuatorCommand { } } +#[gen_stub_pyclass] #[pyclass(name = "KRec")] #[derive(Debug, Clone)] struct PyKRec { inner: KRec, } +#[gen_stub_pymethods] #[pymethods] impl PyKRec { #[new] @@ -981,6 +991,7 @@ impl PyKRec { } } +#[gen_stub_pyclass] #[pyclass(name = "KRecHeader")] #[derive(Debug, Clone)] struct PyKRecHeader { @@ -988,11 +999,10 @@ struct PyKRecHeader { } #[pymethods] +#[gen_stub_pymethods] impl PyKRecHeader { #[new] - #[pyo3( - text_signature = "(uuid=None, task=None, robot_platform=None, robot_serial=None, start_timestamp=None, end_timestamp=None, /, values=None)" - )] + #[pyo3(signature = (uuid=None, task=None, robot_platform=None, robot_serial=None, start_timestamp=None, end_timestamp=None, values=None))] fn new( py: Python<'_>, uuid: Option, @@ -1001,10 +1011,10 @@ impl PyKRecHeader { robot_serial: Option, start_timestamp: Option, end_timestamp: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut items = Vec::new(); for item in iter { let item = item?; @@ -1123,6 +1133,7 @@ impl PyKRecHeader { } } +#[gen_stub_pyclass] #[pyclass(name = "KRecFrame")] #[derive(Debug, Clone)] struct PyKRecFrame { @@ -1130,20 +1141,19 @@ struct PyKRecFrame { } #[pymethods] +#[gen_stub_pymethods] impl PyKRecFrame { #[new] - #[pyo3( - text_signature = "(video_timestamp=None, frame_number=None, inference_step=None, /, values=None)" - )] + #[pyo3(signature = (video_timestamp=None, frame_number=None, inference_step=None, values=None))] fn new( py: Python<'_>, video_timestamp: Option, frame_number: Option, inference_step: Option, - values: Option<&PyAny>, + values: Option>, ) -> PyResult { if let Some(values) = values { - if let Ok(iter) = PyIterator::from_object(py, values) { + if let Ok(iter) = PyIterator::from_bound_object(&values) { let mut items = Vec::new(); for item in iter { let item = item?; @@ -1258,6 +1268,7 @@ impl PyKRecFrame { } // Methods for IMU values + #[pyo3(signature = (imu=None))] fn set_imu_values(&mut self, imu: Option<&PyIMUValues>) { self.inner.imu_values = imu.map(|imu| imu.inner.clone()); } @@ -1285,12 +1296,14 @@ impl PyKRecFrame { } /// Iterator for frames +#[gen_stub_pyclass] #[pyclass] struct FrameIterator { frames: Vec, index: usize, } +#[gen_stub_pymethods] #[pymethods] impl FrameIterator { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { @@ -1310,12 +1323,14 @@ impl FrameIterator { } } +#[gen_stub_pyfunction] #[pyfunction] fn combine_with_video(video_path: &str, krec_path: &str, output_path: &str) -> PyResult<()> { ::krec::combine_with_video(video_path, krec_path, output_path) .map_err(|e| PyErr::new::(e.to_string())) } +#[gen_stub_pyfunction] #[pyfunction] fn extract_from_video(video_path: &str, output_path: &str) -> PyResult<()> { ::krec::extract_from_video(video_path, output_path) @@ -1323,7 +1338,7 @@ fn extract_from_video(video_path: &str, output_path: &str) -> PyResult<()> { } #[pymodule] -fn krec(_py: Python, m: &PyModule) -> PyResult<()> { +fn bindings(m: &Bound) -> PyResult<()> { let _ = ::krec::init(); m.add_class::()?; m.add_class::()?; @@ -1340,3 +1355,5 @@ fn krec(_py: Python, m: &PyModule) -> PyResult<()> { Ok(()) } + +define_stub_info_gatherer!(stub_info); diff --git a/krec/py.typed b/krec/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..6a8d2f7 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,86 @@ +[tool.black] + +line-length = 120 +target-version = ["py311"] +include = '\.pyi?$' + +[tool.pytest.ini_options] + +addopts = "-rx -rf -x -q --full-trace" +testpaths = ["tests"] + +markers = [ + "slow: Marks test as being slow", +] + +[tool.mypy] + +pretty = true +show_column_numbers = true +show_error_context = true +show_error_codes = true +show_traceback = true +disallow_untyped_defs = true +strict_equality = true +allow_redefinition = true + +warn_unused_ignores = true +warn_redundant_casts = true + +incremental = true +namespace_packages = false + +[[tool.mypy.overrides]] + +module = [ + "setuptools", + "setuptools_rust", +] +ignore_missing_imports = true + +[[tool.mypy.overrides]] + +module = ["krec.bindings"] + +disable_error_code = ["no-untyped-def"] + +[tool.isort] + +profile = "black" + +[tool.ruff] + +line-length = 120 +target-version = "py310" + +[tool.ruff.lint] + +select = ["ANN", "D", "E", "F", "I", "N", "PGH", "PLC", "PLE", "PLR", "PLW", "W"] + +ignore = [ + "ANN101", "ANN102", + "D101", "D102", "D103", "D104", "D105", "D106", "D107", + "N812", "N817", + "PLR0911", "PLR0912", "PLR0913", "PLR0915", "PLR2004", + "PLW0603", "PLW2901", +] + +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[tool.ruff.lint.per-file-ignores] + +"__init__.py" = ["E402", "F401", "F403", "F811"] + +[tool.ruff.lint.isort] + +known-first-party = ["krec", "tests"] +combine-as-imports = true + +[tool.ruff.lint.pydocstyle] + +convention = "google" + +[build-system] + +requires = ["setuptools>=42", "wheel", "setuptools-rust>=1.5.2"] +build-backend = "setuptools.build_meta" diff --git a/python/Cargo.toml b/python/Cargo.toml deleted file mode 100644 index c4159d0..0000000 --- a/python/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "krec-py" -version = "0.1.0" -edition = "2021" - -[lib] -name = "krec" -crate-type = ["cdylib"] - -[dependencies] -krec = { version = "0.1.0", path = ".." } -pyo3 = { version = "0.19", features = ["extension-module"] } -tracing = "0.1" \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1f0c555 --- /dev/null +++ b/setup.py @@ -0,0 +1,63 @@ +# mypy: disable-error-code="import-untyped" +#!/usr/bin/env python +"""Setup script for the project.""" + +import os +import re +import shutil +import subprocess + +from setuptools import find_packages, setup +from setuptools.command.build_ext import build_ext +from setuptools_rust import Binding, RustExtension + +with open("README.md", "r", encoding="utf-8") as f: + long_description: str = f.read() + + +with open("Cargo.toml", "r", encoding="utf-8") as fh: + version_re = re.search(r"^version = \"([^\"]*)\"", fh.read(), re.MULTILINE) +assert version_re is not None, "Could not find version in Cargo.toml" +version: str = version_re.group(1) + + +class RustBuildExt(build_ext): + def run(self) -> None: + # Generate the stub file + subprocess.run(["cargo", "run", "--bin", "stub_gen", "--package", "bindings"], check=True) + + # Move the generated stub file to parent directory + src_file = "krec/bindings/bindings.pyi" + dst_file = "krec/bindings.pyi" + if os.path.exists(src_file) and not os.path.exists(dst_file): + shutil.move(src_file, dst_file) + if not os.path.exists(dst_file): + raise RuntimeError(f"Failed to generate {dst_file}") + if os.path.exists(src_file): + os.remove(src_file) + + super().run() + + +setup( + name="krec", + version=version, + description="Python bindings for K-Scale recordingss", + author="K-Scale Labs", + url="https://github.com/kscalelabs/krec", + rust_extensions=[ + RustExtension( + target="krec.bindings", + path="krec/bindings/Cargo.toml", + binding=Binding.PyO3, + ), + ], + setup_requires=["setuptools-rust"], + zip_safe=False, + long_description=long_description, + long_description_content_type="text/markdown", + python_requires=">=3.11", + include_package_data=True, + packages=find_packages(include=["krec"]), + cmdclass={"build_ext": RustBuildExt}, +)