rspy -> pylib/rsbridge

This commit is contained in:
Damien Elmes 2020-11-02 15:02:30 +10:00
parent ef5cd9f551
commit 45ed97c56c
22 changed files with 117 additions and 136 deletions

View file

@ -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",

View file

@ -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 = [

View file

@ -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

View file

@ -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

View file

@ -1 +0,0 @@
mypy==0.790

View file

@ -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"]

View file

@ -1 +0,0 @@
protobuf==3.13.0

View file

@ -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

View file

@ -1,3 +1,2 @@
.build
target
Cargo.lock

View file

@ -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",
]),
)

View file

@ -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 <https://help.ankiweb.net>"]
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

View file

@ -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());
}
}
}

View file

@ -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<Backend> {
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::<Backend>()?;
m.add_wrapped(wrap_pyfunction!(buildhash)).unwrap();
m.add_wrapped(wrap_pyfunction!(open_backend)).unwrap();

View file

@ -29,7 +29,6 @@ py_test(
srcs = glob(["tests/*.py"]),
data = [
"//qt/aqt_data",
"//rspy:ankirspy",
],
main = "tests/run_pytest.py",
deps = [

View file

@ -41,10 +41,6 @@ py_library(
"py.typed",
":hooks_gen",
],
imports = [
# "..",
"../../rspy",
],
visibility = ["//visibility:public"],
deps = [
requirement("protobuf"),

View file

@ -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

View file

@ -1,2 +0,0 @@
[target.'cfg(target_os="macos")']
rustflags = ["-Clink-arg=-mmacosx-version-min=10.7"]

View file

@ -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"],
)

View file

@ -1 +0,0 @@
To build from scratch, please see https://github.com/ankitects/anki

View file

View file

@ -1 +0,0 @@
maturin>=0.8.0

View file

@ -1 +0,0 @@
stable