From f9e939aaffd1c8b873c7cf8b7c812534d4895197 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 7 Jan 2021 09:43:20 +1000 Subject: [PATCH] Make orjson optional again While 32 bit platform support is probably not going to come back, this allows Anki to run on other architectures orjson doesn't support. --- docs/new-platform.md | 11 +++++++++-- pylib/anki/BUILD.bazel | 6 +++--- pylib/anki/rsbackend.py | 13 +++++++++---- pylib/orjson.bzl | 9 +++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 pylib/orjson.bzl diff --git a/docs/new-platform.md b/docs/new-platform.md index 777842287..20767cc84 100644 --- a/docs/new-platform.md +++ b/docs/new-platform.md @@ -34,8 +34,15 @@ https://github.com/ankitects/anki/commit/db3308e788f20b188e84add40d6a1dce5bf726a build --action_env=PYTHON_SITE_PACKAGES=/path/to/site-packages ``` -- Anki uses the Python 'orjson' module. If it's not available on your system, - you will need to [patch it out](https://github.com/ankitects/anki/pull/752#issuecomment-748861582), and remove references to it in the build scripts. +- Anki uses the Python 'orjson' module to speed up DB access. If you're on a + platform that can not build orjson, you can remove it from + pip/requirements.txt to skip it during running/building, but DB operations + will be slower. + + The py_wheel() rule in pylib/anki/BUILD.bazel adds an orjson requirement to + the generated Anki wheel on x86_64. If you have removed orjson, you'll want to + remove that line. If you have successfully built orjson for another platform, + you'll want to adjust that line to include your platform. ## Rust diff --git a/pylib/anki/BUILD.bazel b/pylib/anki/BUILD.bazel index 4dc4f721b..98fab4f67 100644 --- a/pylib/anki/BUILD.bazel +++ b/pylib/anki/BUILD.bazel @@ -5,6 +5,7 @@ load("//pylib:protobuf.bzl", "py_proto_library_typed") load("@rules_python//experimental/python:wheel.bzl", "py_package", "py_wheel") load("@bazel_skylib//lib:selects.bzl", "selects") load("//:defs.bzl", "anki_version") +load("//pylib:orjson.bzl", "orjson_if_available") copy_file( name = "buildinfo", @@ -93,10 +94,9 @@ py_library( requirement("beautifulsoup4"), requirement("decorator"), requirement("distro"), - requirement("orjson"), requirement("protobuf"), requirement("requests"), - ], + ] + orjson_if_available(), ) py_package( @@ -123,7 +123,7 @@ py_wheel( "requests[socks]", "decorator", "protobuf", - "orjson", + 'orjson; platform_machine == "x86_64"', 'psutil; sys_platform == "win32"', 'distro; sys_platform != "darwin" and sys_platform != "win32"', ], diff --git a/pylib/anki/rsbackend.py b/pylib/anki/rsbackend.py index e5c631875..1bba877fe 100644 --- a/pylib/anki/rsbackend.py +++ b/pylib/anki/rsbackend.py @@ -22,8 +22,6 @@ import os from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Union -import orjson - import anki._rsbridge import anki.backend_pb2 as pb import anki.buildinfo @@ -53,8 +51,15 @@ SyncOutput = pb.SyncCollectionOut SyncStatus = pb.SyncStatusOut CountsForDeckToday = pb.CountsForDeckTodayOut -to_json_bytes = orjson.dumps -from_json_bytes = orjson.loads +try: + import orjson + + to_json_bytes = orjson.dumps + from_json_bytes = orjson.loads +except: + print("orjson is missing; DB operations will be slower") + to_json_bytes = lambda obj: json.dumps(obj).encode("utf8") # type: ignore + from_json_bytes = json.loads class Interrupted(Exception): diff --git a/pylib/orjson.bzl b/pylib/orjson.bzl new file mode 100644 index 000000000..13d800ab4 --- /dev/null +++ b/pylib/orjson.bzl @@ -0,0 +1,9 @@ +load("@py_deps//:requirements.bzl", "requirement") + +def orjson_if_available(): + "Include orjson if it's listed in requirements.txt." + target = requirement("orjson") + if "not_found" in target: + return [] + else: + return [target]