From 45ed97c56c99f7378af240f2b47889c55af88c4c Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 2 Nov 2020 15:02:30 +1000 Subject: [PATCH] rspy -> pylib/rsbridge --- pylib/BUILD.bazel | 4 +- pylib/anki/BUILD.bazel | 30 +++++++++++-- pylib/anki/rsbackend.py | 7 ++- pylib/mypy.ini | 2 +- pylib/mypy_version.txt | 1 - pylib/pyproject.toml | 4 -- pylib/requirement.protobuf | 1 - pylib/requirements.dev | 8 ---- {rspy => pylib/rsbridge}/.gitignore | 1 - pylib/rsbridge/BUILD.bazel | 50 +++++++++++++++++++++ {rspy => pylib/rsbridge}/Cargo.toml | 22 +++++----- {rspy => pylib/rsbridge}/build.rs | 2 +- {rspy/src => pylib/rsbridge}/lib.rs | 42 +++++++++--------- qt/BUILD.bazel | 1 - qt/aqt/BUILD.bazel | 4 -- qt/mypy.ini | 2 +- rspy/.cargo/config | 2 - rspy/BUILD.bazel | 67 ----------------------------- rspy/README.md | 1 - rspy/empty.rs | 0 rspy/requirements.txt | 1 - rspy/rust-toolchain | 1 - 22 files changed, 117 insertions(+), 136 deletions(-) delete mode 100644 pylib/mypy_version.txt delete mode 100644 pylib/pyproject.toml delete mode 100644 pylib/requirement.protobuf delete mode 100644 pylib/requirements.dev rename {rspy => pylib/rsbridge}/.gitignore (72%) create mode 100644 pylib/rsbridge/BUILD.bazel rename {rspy => pylib/rsbridge}/Cargo.toml (58%) rename {rspy => pylib/rsbridge}/build.rs (99%) rename {rspy/src => pylib/rsbridge}/lib.rs (69%) delete mode 100644 rspy/.cargo/config delete mode 100644 rspy/BUILD.bazel delete mode 100644 rspy/README.md delete mode 100644 rspy/empty.rs delete mode 100644 rspy/requirements.txt delete mode 100644 rspy/rust-toolchain diff --git a/pylib/BUILD.bazel b/pylib/BUILD.bazel index c3919939b..a77bc575c 100644 --- a/pylib/BUILD.bazel +++ b/pylib/BUILD.bazel @@ -42,9 +42,7 @@ py_binary( py_test( name = "pytest", srcs = glob(["tests/*.py"]), - data = glob(["tests/support/**"]) + [ - "//rspy:ankirspy", - ], + data = glob(["tests/support/**"]), main = "tests/run_pytest.py", deps = [ "//pylib/anki", diff --git a/pylib/anki/BUILD.bazel b/pylib/anki/BUILD.bazel index 6b101a3bd..46c55ecd7 100644 --- a/pylib/anki/BUILD.bazel +++ b/pylib/anki/BUILD.bazel @@ -3,6 +3,8 @@ load("@rules_python//python:defs.bzl", "py_library") load("@py_deps//:requirements.bzl", "requirement") load("//proto:defs.bzl", "py_proto_library_typed") load("@rules_python//experimental/python:wheel.bzl", "py_package", "py_wheel") +load("@bazel_skylib//lib:selects.bzl", "selects") +load("@bazel_skylib//rules:copy_file.bzl", "copy_file") copy_file( name = "buildinfo", @@ -40,6 +42,30 @@ py_proto_library_typed( ], ) +copy_file( + name = "rsbridge_unix", + src = "//pylib/rsbridge", + out = "_rsbridge.so", +) + +copy_file( + name = "rsbridge_win", + src = "//pylib/rsbridge", + out = "_rsbridge.pyd", +) + +alias( + name = "rsbridge", + actual = selects.with_or({ + ( + "@io_bazel_rules_rust//rust/platform:x86_64-pc-windows-msvc", + "@io_bazel_rules_rust//rust/platform:i686-pc-windows-msvc", + ): ":rsbridge_win", + "//conditions:default": ":rsbridge_unix", + }), + visibility = ["//visibility:public"], +) + py_library( name = "anki", srcs = glob([ @@ -52,11 +78,10 @@ py_library( ":fluent_pb2", ":hooks_gen", ":rsbackend_gen", - "//rspy:ankirspy", + ":rsbridge", ], imports = [ "..", - "../../rspy", ], visibility = ["//visibility:public"], deps = [ @@ -80,7 +105,6 @@ py_wheel( distribution = "anki", python_tag = "py3", requires = [ - "ankirspy (==2.1.35)", "distro ; sys_platform != \"darwin\" and sys_platform != \"win32\"", ], strip_path_prefixes = [ diff --git a/pylib/anki/rsbackend.py b/pylib/anki/rsbackend.py index ff817c903..ec78d831e 100644 --- a/pylib/anki/rsbackend.py +++ b/pylib/anki/rsbackend.py @@ -22,8 +22,7 @@ import os from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Union -import ankirspy # pytype: disable=import-error - +import anki._rsbridge import anki.backend_pb2 as pb import anki.buildinfo from anki import hooks @@ -45,7 +44,7 @@ if TYPE_CHECKING: FormatTimeSpanContextValue = pb.FormatTimespanIn.ContextValue -assert ankirspy.buildhash() == anki.buildinfo.buildhash +assert anki._rsbridge.buildhash() == anki.buildinfo.buildhash SchedTimingToday = pb.SchedTimingTodayOut BuiltinSortKind = pb.BuiltinSearchOrder.BuiltinSortKind @@ -216,7 +215,7 @@ class RustBackend(RustBackendGenerated): preferred_langs=langs, server=server, ) - self._backend = ankirspy.open_backend(init_msg.SerializeToString()) + self._backend = anki._rsbridge.open_backend(init_msg.SerializeToString()) def db_query( self, sql: str, args: Sequence[ValueForDB], first_row_only: bool diff --git a/pylib/mypy.ini b/pylib/mypy.ini index 3b3754b19..b823d67aa 100644 --- a/pylib/mypy.ini +++ b/pylib/mypy.ini @@ -29,7 +29,7 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-bs4] ignore_missing_imports = True -[mypy-ankirspy] +[mypy-anki._rsbridge] ignore_missing_imports = True [mypy-stringcase] ignore_missing_imports = True diff --git a/pylib/mypy_version.txt b/pylib/mypy_version.txt deleted file mode 100644 index 45278fa49..000000000 --- a/pylib/mypy_version.txt +++ /dev/null @@ -1 +0,0 @@ -mypy==0.790 diff --git a/pylib/pyproject.toml b/pylib/pyproject.toml deleted file mode 100644 index 754ed04e1..000000000 --- a/pylib/pyproject.toml +++ /dev/null @@ -1,4 +0,0 @@ -[build-system] -# Minimum requirements for the build system to execute. -# https://stackoverflow.com/questions/48048745/setup-py-require-a-recent-version-of-setuptools-before-trying-to-install -requires = ["setuptools", "wheel"] diff --git a/pylib/requirement.protobuf b/pylib/requirement.protobuf deleted file mode 100644 index 911bae084..000000000 --- a/pylib/requirement.protobuf +++ /dev/null @@ -1 +0,0 @@ -protobuf==3.13.0 diff --git a/pylib/requirements.dev b/pylib/requirements.dev deleted file mode 100644 index 889eb2588..000000000 --- a/pylib/requirements.dev +++ /dev/null @@ -1,8 +0,0 @@ -wheel -mypy -mypy_protobuf>=1.21 -black>=20.1b0 -pytest>=6.0.1 -pylint>=2.6.0 -isort>=5.4.2 -stringcase==1.2.0 diff --git a/rspy/.gitignore b/pylib/rsbridge/.gitignore similarity index 72% rename from rspy/.gitignore rename to pylib/rsbridge/.gitignore index c8baed871..a9d37c560 100644 --- a/rspy/.gitignore +++ b/pylib/rsbridge/.gitignore @@ -1,3 +1,2 @@ -.build target Cargo.lock diff --git a/pylib/rsbridge/BUILD.bazel b/pylib/rsbridge/BUILD.bazel new file mode 100644 index 000000000..8bc9adc0f --- /dev/null +++ b/pylib/rsbridge/BUILD.bazel @@ -0,0 +1,50 @@ +load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_test") +load("@io_bazel_rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") +load("@bazel_skylib//lib:selects.bzl", "selects") +load("//rslib:rustfmt.bzl", "rustfmt_fix", "rustfmt_test") + +cargo_build_script( + name = "build_script", + srcs = ["build.rs"], +) + +rust_library( + name = "rsbridge", + srcs = ["lib.rs"], + crate_type = "cdylib", + data = [ + "//:buildinfo.txt", + ], + rustc_flags = selects.with_or({ + ( + "@io_bazel_rules_rust//rust/platform:x86_64-apple-darwin", + ): [ + "-Clink-arg=-undefined", + "-Clink-arg=dynamic_lookup", + "-Clink-arg=-mmacosx-version-min=10.7", + ], + "//conditions:default": [], + }), + visibility = [ + "//pylib:__subpackages__", + ], + deps = [ + ":build_script", + "//cargo:pyo3", + "//rslib:anki", + ], +) + +rustfmt_test( + name = "format", + srcs = glob([ + "*.rs", + ]), +) + +rustfmt_fix( + name = "format_fix", + srcs = glob([ + "*.rs", + ]), +) diff --git a/rspy/Cargo.toml b/pylib/rsbridge/Cargo.toml similarity index 58% rename from rspy/Cargo.toml rename to pylib/rsbridge/Cargo.toml index 1ebbe1204..4153532c8 100644 --- a/rspy/Cargo.toml +++ b/pylib/rsbridge/Cargo.toml @@ -1,14 +1,13 @@ [package] -name = "ankirspy" -version = "2.1.36" # automatically updated +name = "rsbackend" +version = "0.0.0" edition = "2018" authors = ["Ankitects Pty Ltd and contributors "] license = "AGPL-3.0-or-later" description = "Anki's Rust library code Python bindings" -readme = "README.md" [dependencies] -anki = { path = "../rslib" } +anki = { path = "../../rslib" } [dependencies.pyo3] version = "0.11.0" @@ -17,12 +16,13 @@ features = ["extension-module"] [lib] name = "ankirspy" crate-type = ["cdylib"] +path = "lib.rs" -[profile.dev] -opt-level = 2 -debug = 0 -codegen-units = 256 +#[profile.dev] +#opt-level = 2 +#debug = 0 +#codegen-units = 256 -[profile.dev.package."*"] -opt-level = 3 -codegen-units = 16 +#[profile.dev.package."*"] +#opt-level = 3 +#codegen-units = 16 diff --git a/rspy/build.rs b/pylib/rsbridge/build.rs similarity index 99% rename from rspy/build.rs rename to pylib/rsbridge/build.rs index 155bc72f8..330deb59b 100644 --- a/rspy/build.rs +++ b/pylib/rsbridge/build.rs @@ -6,4 +6,4 @@ fn main() { let path = std::path::Path::new(&path).with_file_name("libs"); println!("cargo:rustc-link-search={}", path.to_str().unwrap()); } -} \ No newline at end of file +} diff --git a/rspy/src/lib.rs b/pylib/rsbridge/lib.rs similarity index 69% rename from rspy/src/lib.rs rename to pylib/rsbridge/lib.rs index bb04ea049..747593cbc 100644 --- a/rspy/src/lib.rs +++ b/pylib/rsbridge/lib.rs @@ -16,7 +16,7 @@ struct Backend { backend: RustBackend, } -create_exception!(ankirspy, BackendError, Exception); +create_exception!(_rsbridge, BackendError, Exception); #[pyfunction] fn buildhash() -> &'static str { @@ -33,25 +33,27 @@ fn open_backend(init_msg: &PyBytes) -> PyResult { fn want_release_gil(method: u32) -> bool { if let Ok(method) = BackendMethod::try_from(method) { - !matches!(method, + !matches!( + method, BackendMethod::ExtractAVTags - | BackendMethod::ExtractLatex - | BackendMethod::RenderExistingCard - | BackendMethod::RenderUncommittedCard - | BackendMethod::StripAVTags - | BackendMethod::LocalMinutesWest - | BackendMethod::SchedTimingToday - | BackendMethod::AddOrUpdateDeckLegacy - | BackendMethod::NewDeckLegacy - | BackendMethod::NewDeckConfigLegacy - | BackendMethod::GetStockNotetypeLegacy - | BackendMethod::SetLocalMinutesWest - | BackendMethod::StudiedToday - | BackendMethod::TranslateString - | BackendMethod::FormatTimespan - | BackendMethod::LatestProgress - | BackendMethod::SetWantsAbort - | BackendMethod::I18nResources) + | BackendMethod::ExtractLatex + | BackendMethod::RenderExistingCard + | BackendMethod::RenderUncommittedCard + | BackendMethod::StripAVTags + | BackendMethod::LocalMinutesWest + | BackendMethod::SchedTimingToday + | BackendMethod::AddOrUpdateDeckLegacy + | BackendMethod::NewDeckLegacy + | BackendMethod::NewDeckConfigLegacy + | BackendMethod::GetStockNotetypeLegacy + | BackendMethod::SetLocalMinutesWest + | BackendMethod::StudiedToday + | BackendMethod::TranslateString + | BackendMethod::FormatTimespan + | BackendMethod::LatestProgress + | BackendMethod::SetWantsAbort + | BackendMethod::I18nResources + ) } else { false } @@ -92,7 +94,7 @@ impl Backend { ////////////////////////////////// #[pymodule] -fn ankirspy(_py: Python, m: &PyModule) -> PyResult<()> { +fn _rsbridge(_py: Python, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_wrapped(wrap_pyfunction!(buildhash)).unwrap(); m.add_wrapped(wrap_pyfunction!(open_backend)).unwrap(); diff --git a/qt/BUILD.bazel b/qt/BUILD.bazel index 9eba196da..864fce01b 100644 --- a/qt/BUILD.bazel +++ b/qt/BUILD.bazel @@ -29,7 +29,6 @@ py_test( srcs = glob(["tests/*.py"]), data = [ "//qt/aqt_data", - "//rspy:ankirspy", ], main = "tests/run_pytest.py", deps = [ diff --git a/qt/aqt/BUILD.bazel b/qt/aqt/BUILD.bazel index b0d23ce7d..05ae65c82 100644 --- a/qt/aqt/BUILD.bazel +++ b/qt/aqt/BUILD.bazel @@ -41,10 +41,6 @@ py_library( "py.typed", ":hooks_gen", ], - imports = [ - # "..", - "../../rspy", - ], visibility = ["//visibility:public"], deps = [ requirement("protobuf"), diff --git a/qt/mypy.ini b/qt/mypy.ini index d72efe6e8..db4e0ae9a 100644 --- a/qt/mypy.ini +++ b/qt/mypy.ini @@ -42,7 +42,7 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-jsonschema.*] ignore_missing_imports = True -[mypy-ankirspy] +[mypy-anki._rsbridge] ignore_missing_imports = True [mypy-PyQt5.sip] ignore_missing_imports = True diff --git a/rspy/.cargo/config b/rspy/.cargo/config deleted file mode 100644 index 70ce4d5c7..000000000 --- a/rspy/.cargo/config +++ /dev/null @@ -1,2 +0,0 @@ -[target.'cfg(target_os="macos")'] -rustflags = ["-Clink-arg=-mmacosx-version-min=10.7"] diff --git a/rspy/BUILD.bazel b/rspy/BUILD.bazel deleted file mode 100644 index 5f7e38c14..000000000 --- a/rspy/BUILD.bazel +++ /dev/null @@ -1,67 +0,0 @@ -load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_test") -load("@bazel_skylib//lib:selects.bzl", "selects") -load("@io_bazel_rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") -load("@bazel_skylib//rules:copy_file.bzl", "copy_file") - -cargo_build_script( - name = "build_script", - srcs = ["build.rs"], -) - -rust_library( - name = "ankirspy_lib", - srcs = glob([ - "src/**/*.rs", - ]), - crate_type = "cdylib", - data = [ - "//:buildinfo.txt", - ], - rustc_flags = selects.with_or({ - ( - "@io_bazel_rules_rust//rust/platform:x86_64-apple-darwin", - ): [ - "-Clink-arg=-undefined", - "-Clink-arg=dynamic_lookup", - "-Clink-arg=-mmacosx-version-min=10.7", - ], - "//conditions:default": [], - }), - deps = [ - ":build_script", - "//cargo:pyo3", - "//rslib:anki", - ], -) - -# a binary target so that test //rspy/... runs clippy -rust_test( - name = "rspy_test", - srcs = ["empty.rs"], - deps = [":ankirspy_lib"], -) - -# rename lib file for Python -copy_file( - name = "ankirspy_unix", - src = ":ankirspy_lib", - out = "ankirspy.so", -) - -copy_file( - name = "ankirspy_win", - src = ":ankirspy_lib", - out = "ankirspy.pyd", -) - -alias( - name = "ankirspy", - actual = selects.with_or({ - ( - "@io_bazel_rules_rust//rust/platform:x86_64-pc-windows-msvc", - "@io_bazel_rules_rust//rust/platform:i686-pc-windows-msvc", - ): ":ankirspy_win", - "//conditions:default": ":ankirspy_unix", - }), - visibility = ["//visibility:public"], -) diff --git a/rspy/README.md b/rspy/README.md deleted file mode 100644 index a318d199f..000000000 --- a/rspy/README.md +++ /dev/null @@ -1 +0,0 @@ -To build from scratch, please see https://github.com/ankitects/anki diff --git a/rspy/empty.rs b/rspy/empty.rs deleted file mode 100644 index e69de29bb..000000000 diff --git a/rspy/requirements.txt b/rspy/requirements.txt deleted file mode 100644 index 2e268c221..000000000 --- a/rspy/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -maturin>=0.8.0 diff --git a/rspy/rust-toolchain b/rspy/rust-toolchain deleted file mode 100644 index 2bf5ad044..000000000 --- a/rspy/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -stable