mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00

* Migrate build system to uv Closes #3787, and is a step towards #3081 and #4022 This change breaks our PyOxidizer bundling process. While we probably could update it to work with the new venvs & lockfile, my intention is to use this as a base to try out a uv-based packager/installer. Some notes about the changes: - Use uv for python download + venv installation - Drop python/requirements* in favour of pyproject files / uv.lock - Bumped to latest Python 3.9 version. The move to 3.13 should be a fairly trivial change when we're ready. - Dropped the old write_wheel.py in favour of uv/hatchling. This has the unfortunate side-effect of dropping leading zeros in our wheels, which we could try hack around in the future. - Switch to Qt 6.7 for the dev repo, as it's the first PyQt version with a Linux/ARM WebEngine wheel. - Unified our macOS deployment target with minimum required for ARM. - Dropped unused fluent python files - Dropped unused python license generation - Dropped helpers to run under Qt 5, as our wheels were already requiring Qt 6 to install. * Build action to create universal uv binary * Drop some PyOxidizer-related files * Use Windows ARM64 cargo/node binaries during build We can't provide ARM64 wheels to users yet due to #4079, but we can at least speed up the build. The rustls -> native-tls change on Windows is because ring requires clang to compile for ARM64, and I figured it's best to keep our Windows deps consistent. We already built the wheels with native-tls. * Make libankihelper a universal library We were shipping a single arch library in a purelib, leading to breakages when running on a different platform. * Use Python wheel for mpv/lame on Windows/Mac This is convenient, but suboptimal on a Mac at the moment. The first run of mpv will take a number of seconds for security checks to run, and our mpv code ends up timing out, repeating the process each time. Our installer stub will need to invoke mpv once first to get it validated. We could address this by distributing the audio with the installer/stub, or perhaps by putting the binaries in a .pkg file that's notarized+stapled and then included in the wheel. * Add some helper scripts to build a fully-locked wheel * Initial macOS launcher prototype * Add a hidden env var to preload our libs and audio helpers on macOS * qt/bundle -> qt/launcher - remove more of the old bundling code - handle app icon * Fat binary, notarization & dmg * Publish wheels on testpypi for testing * Use our Python pin for the launcher too * Python cleanups * Extend launcher to other platforms + more - Switch to Qt 6.8 for repo default, as 6.7 depends on an older libwebp/tiff which is unavailable on newer installs - Drop tools/mac-x86, as we no longer need to test against Qt 5 - Add flags to cross compile wheels on Mac and Linux - Bump glibc target to 2_36, building on Debian Stable - Increase mpv timeout on macOS to allow for initial gatekeeper checks - Ship both arm64 and amd64 uv on Linux, with a bash stub to pick the appropriate arch. * Fix pylint on Linux * Fix failure to run from /usr/local/bin * Remove remaining pyoxidizer refs, and clean up duplicate release folder * Rust dep updates - Rust 1.87 for now (1.88 due out in around a week) - Nom looks involved, so I left it for now - prost-reflect depends on a new prost version that got yanked * Python 3.13 + dep updates Updated protoc binaries + add helper in order to try fix build breakage. Ended up being due to an AI-generated update to pip-system-certs that was not reviewed carefully enough: https://gitlab.com/alelec/pip-system-certs/-/issues/36 The updated mypy/black needed some tweaks to our files. * Windows compilation fixes * Automatically run Anki after installing on Windows * Touch pyproject.toml upon install, so we check for updates * Update Python deps - urllib3 for CVE - pip-system-certs got fixed - markdown/pytest also updated
353 lines
12 KiB
Python
353 lines
12 KiB
Python
# Copyright: Ankitects Pty Ltd and contributors
|
||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import re
|
||
import sys
|
||
import time
|
||
import traceback
|
||
from typing import TYPE_CHECKING, TextIO, cast
|
||
|
||
from markdown import markdown
|
||
|
||
import aqt
|
||
from anki.collection import HelpPage
|
||
from anki.errors import BackendError, Interrupted
|
||
from anki.utils import is_win
|
||
from aqt.addons import AddonManager, AddonMeta
|
||
from aqt.qt import *
|
||
from aqt.utils import openHelp, showWarning, supportText, tooltip, tr
|
||
|
||
if TYPE_CHECKING:
|
||
from aqt.main import AnkiQt
|
||
|
||
|
||
def show_exception(*, parent: QWidget, exception: Exception) -> None:
|
||
"Present a caught exception to the user using a pop-up."
|
||
if isinstance(exception, Interrupted):
|
||
# nothing to do
|
||
return
|
||
if isinstance(exception, BackendError):
|
||
if exception.context:
|
||
print(exception.context)
|
||
if exception.backtrace:
|
||
print(exception.backtrace)
|
||
showWarning(str(exception), parent=parent, help=exception.help_page)
|
||
else:
|
||
# if the error is not originating from the backend, dump
|
||
# a traceback to the console to aid in debugging
|
||
traceback.print_exception(
|
||
None, exception, exception.__traceback__, file=sys.stdout
|
||
)
|
||
showWarning(str(exception), parent=parent)
|
||
|
||
|
||
def is_chromium_cert_error(error: str) -> bool:
|
||
"""QtWebEngine sometimes spits out 'unknown error' messages to stderr on Windows.
|
||
|
||
They appear to be IDS_SETTINGS_CERTIFICATE_MANAGER_UNKNOWN_ERROR in
|
||
chrome/browser/ui/webui/certificates_handler.cc. At a guess, it's the
|
||
NetErrorToString() method.
|
||
|
||
The constant appears to get converted to an ID; the resources are found
|
||
in files like this:
|
||
|
||
chrome/app/resources/generated_resources_fr-CA.xtb
|
||
2258:<translation id="3380365263193509176">Erreur inconnue</translation>
|
||
|
||
List derived with:
|
||
qtwebengine-chromium% rg --no-heading --no-filename --no-line-number \
|
||
3380365263193509176 | perl -pe 's/.*>(.*)<.*/"$1",/' | sort | uniq
|
||
|
||
This list has been manually updated to add a different Japanese translation, as
|
||
the translations may change in different Chromium releases.
|
||
|
||
Judging by error reports, we can't assume the error falls on a separate line:
|
||
https://forums.ankiweb.net/t/topic/22036/
|
||
"""
|
||
if not is_win:
|
||
return False
|
||
for msg in (
|
||
"알 수 없는 오류가 발생했습니다.",
|
||
"Bilinmeyen hata",
|
||
"Eroare necunoscută",
|
||
"Erreur inconnue",
|
||
"Erreur inconnue.",
|
||
"Erro descoñecido",
|
||
"Erro desconhecido",
|
||
"Error desconegut",
|
||
"Error desconocido",
|
||
"Errore ezezaguna",
|
||
"Errore sconosciuto",
|
||
"Gabim i panjohur",
|
||
"Hindi kilalang error",
|
||
"Hitilafu isiyojulikana",
|
||
"Iphutha elingaziwa",
|
||
"Ismeretlen hiba",
|
||
"Kesalahan tidak dikenal",
|
||
"Lỗi không xác định",
|
||
"Naməlum xəta",
|
||
"Nepoznata greška",
|
||
"Nepoznata pogreška",
|
||
"Nezināma kļūda",
|
||
"Nežinoma klaida",
|
||
"Neznáma chyba",
|
||
"Neznámá chyba",
|
||
"Neznana napaka",
|
||
"Nieznany błąd",
|
||
"Noma’lum xatolik",
|
||
"Okänt fel",
|
||
"Onbekende fout",
|
||
"Óþekkt villa",
|
||
"Ralat tidak diketahui",
|
||
"Tundmatu viga",
|
||
"Tuntematon virhe",
|
||
"Ukendt fejl",
|
||
"Ukjent feil",
|
||
"Unbekannter Fehler",
|
||
"Unknown error",
|
||
"Άγνωστο σφάλμα",
|
||
"Белгисиз ката",
|
||
"Белгісіз қате",
|
||
"Невідома помилка",
|
||
"Невядомая памылка",
|
||
"Неизвестна грешка",
|
||
"Неизвестная ошибка",
|
||
"Непозната грешка",
|
||
"Үл мэдэгдэх алдаа",
|
||
"უცნობი შეცდომა",
|
||
"Անհայտ սխալ",
|
||
"שגיאה לא ידועה",
|
||
"خطأ غير معروف",
|
||
"خطای ناشناس",
|
||
"نامعلوم خرابی",
|
||
"ያልታወቀ ስህተት",
|
||
"अज्ञात एरर",
|
||
"अज्ञात गड़बड़ी",
|
||
"अज्ञात त्रुटि",
|
||
"অজানা ত্রুটি",
|
||
"অজ্ঞাত আসোঁৱাহ",
|
||
"ਅਗਿਆਤ ਗੜਬੜ",
|
||
"અજ્ઞાત ભૂલ",
|
||
"ଅଜଣା ତୃଟି",
|
||
"அறியப்படாத பிழை",
|
||
"తెలియని ఎర్రర్",
|
||
"ಅಪರಿಚಿತ ದೋಷ",
|
||
"അജ്ഞാതമായ പിശക്",
|
||
"නොදන්නා දෝෂය",
|
||
"ข้อผิดพลาดที่ไม่รู้จัก",
|
||
"ຄວາມຜິດພາດທີ່ບໍ່ຮູ້ຈັກ",
|
||
"မသိရ အမှား",
|
||
"កំហុសឆ្គងមិនស្គាល់",
|
||
"不明なエラー",
|
||
"未知のエラー",
|
||
"未知的錯誤",
|
||
"未知错误",
|
||
):
|
||
if error.startswith(msg):
|
||
return True
|
||
return False
|
||
|
||
|
||
if not os.environ.get("DEBUG"):
|
||
|
||
def excepthook(etype, val, tb) -> None: # type: ignore
|
||
sys.stderr.write("%s\n" % ("".join(traceback.format_exception(etype, val, tb))))
|
||
|
||
sys.excepthook = excepthook
|
||
|
||
# so we can be non-modal/non-blocking, without Python deallocating the message
|
||
# box ahead of time
|
||
_mbox: QMessageBox | None = None
|
||
|
||
|
||
class ErrorHandler(QObject):
|
||
"Catch stderr and write into buffer."
|
||
|
||
ivl = 100
|
||
fatal_error_encountered = False
|
||
|
||
errorTimer = pyqtSignal()
|
||
|
||
def __init__(self, mw: AnkiQt) -> None:
|
||
QObject.__init__(self, mw)
|
||
self.mw = mw
|
||
self.timer: QTimer | None = None
|
||
qconnect(self.errorTimer, self._setTimer)
|
||
self.pool = ""
|
||
self._oldstderr = sys.stderr
|
||
sys.stderr = cast(TextIO, self)
|
||
|
||
def unload(self) -> None:
|
||
sys.stderr = self._oldstderr
|
||
sys.excepthook = None
|
||
|
||
def write(self, data: str) -> None:
|
||
# dump to stdout
|
||
sys.stdout.write(data)
|
||
# save in buffer
|
||
self.pool += data
|
||
# and update timer
|
||
self.setTimer()
|
||
|
||
def setTimer(self) -> None:
|
||
# we can't create a timer from a different thread, so we post a
|
||
# message to the object on the main thread
|
||
self.errorTimer.emit() # type: ignore
|
||
|
||
def _setTimer(self) -> None:
|
||
if not self.timer:
|
||
self.timer = QTimer(self.mw)
|
||
qconnect(self.timer.timeout, self.onTimeout)
|
||
self.timer.setInterval(self.ivl)
|
||
self.timer.setSingleShot(True)
|
||
self.timer.start()
|
||
|
||
def tempFolderMsg(self) -> str:
|
||
return tr.qt_misc_unable_to_access_anki_media_folder()
|
||
|
||
def onTimeout(self) -> None:
|
||
if self.fatal_error_encountered:
|
||
# suppress follow-up errors caused by the poisoned lock
|
||
return
|
||
error = self.pool
|
||
self.pool = ""
|
||
self.mw.progress.clear()
|
||
if "AbortSchemaModification" in error:
|
||
return
|
||
if "DeprecationWarning" in error:
|
||
return
|
||
if "10013" in error:
|
||
showWarning(tr.qt_misc_your_firewall_or_antivirus_program_is())
|
||
return
|
||
if "invalidTempFolder" in error:
|
||
showWarning(self.tempFolderMsg())
|
||
return
|
||
if "Beautiful Soup is not an HTTP client" in error:
|
||
return
|
||
if "database or disk is full" in error or "Errno 28" in error:
|
||
showWarning(tr.qt_misc_your_computers_storage_may_be_full())
|
||
return
|
||
if "disk I/O error" in error:
|
||
showWarning(markdown(tr.errors_accessing_db()))
|
||
return
|
||
if "unable to get local issuer certificate" in error and is_win:
|
||
showWarning(tr.errors_windows_ssl_updates())
|
||
return
|
||
if is_chromium_cert_error(error):
|
||
return
|
||
|
||
debug_text = supportText() + "\n" + error
|
||
|
||
if "PanicException" in error:
|
||
self.fatal_error_encountered = True
|
||
# ensure no collection-related timers like backup fire
|
||
self.mw.col = None
|
||
user_text = "A fatal error occurred, and Anki must close. Please report this message on the forums."
|
||
else:
|
||
user_text = tr.errors_standard_popup2()
|
||
if self.mw.addonManager.dirty:
|
||
user_text += "\n\n" + self._addonText(error)
|
||
debug_text += addon_debug_info()
|
||
|
||
def show_troubleshooting():
|
||
openHelp(HelpPage.TROUBLESHOOTING)
|
||
|
||
def copy_debug_info():
|
||
QApplication.clipboard().setText(debug_text)
|
||
tooltip(tr.errors_copied_to_clipboard(), parent=_mbox)
|
||
|
||
global _mbox
|
||
_mbox = QMessageBox()
|
||
_mbox.setWindowTitle("Anki")
|
||
_mbox.setText(user_text)
|
||
_mbox.setIcon(QMessageBox.Icon.Warning)
|
||
_mbox.setTextFormat(Qt.TextFormat.PlainText)
|
||
|
||
troubleshooting = _mbox.addButton(
|
||
tr.errors_troubleshooting_button(), QMessageBox.ButtonRole.ActionRole
|
||
)
|
||
debug_info = _mbox.addButton(
|
||
tr.errors_copy_debug_info_button(), QMessageBox.ButtonRole.ActionRole
|
||
)
|
||
cancel = _mbox.addButton(QMessageBox.StandardButton.Cancel)
|
||
cancel.setText(tr.actions_close())
|
||
|
||
troubleshooting.disconnect()
|
||
troubleshooting.clicked.connect(show_troubleshooting)
|
||
debug_info.disconnect()
|
||
debug_info.clicked.connect(copy_debug_info)
|
||
|
||
if self.fatal_error_encountered:
|
||
_mbox.exec()
|
||
sys.exit(1)
|
||
else:
|
||
_mbox.show()
|
||
|
||
def _addonText(self, error: str) -> str:
|
||
matches = re.findall(r"addons21(/|\\)(.*?)(/|\\)", error)
|
||
if not matches:
|
||
return tr.errors_may_be_addon()
|
||
# reverse to list most likely suspect first, dict to deduplicate:
|
||
addons = [
|
||
aqt.mw.addonManager.addonName(i[1])
|
||
for i in dict.fromkeys(reversed(matches))
|
||
]
|
||
addons_str = ", ".join(addons)
|
||
return tr.addons_possibly_involved(addons=addons_str)
|
||
|
||
|
||
def addon_fmt(addmgr: AddonManager, addon: AddonMeta) -> str:
|
||
installed = "0"
|
||
if addon.installed_at:
|
||
try:
|
||
installed = time.strftime(
|
||
"%Y-%m-%dT%H:%M", time.localtime(addon.installed_at)
|
||
)
|
||
except (OverflowError, OSError):
|
||
print("invalid timestamp for", addon.provided_name)
|
||
if addon.provided_name:
|
||
name = addon.provided_name
|
||
else:
|
||
name = "''"
|
||
user = addmgr.getConfig(addon.dir_name)
|
||
default = addmgr.addonConfigDefaults(addon.dir_name)
|
||
if user == default:
|
||
modified = "''"
|
||
else:
|
||
modified = "mod"
|
||
return (
|
||
f"{name} ['{addon.dir_name}', {installed}, '{addon.human_version}', {modified}]"
|
||
)
|
||
|
||
|
||
def addon_debug_info() -> str:
|
||
from aqt import mw
|
||
|
||
addmgr = mw.addonManager
|
||
active = []
|
||
activeids = []
|
||
inactive = []
|
||
for addon in addmgr.all_addon_meta():
|
||
if addon.enabled:
|
||
active.append(addon_fmt(addmgr, addon))
|
||
if addon.ankiweb_id():
|
||
activeids.append(addon.dir_name)
|
||
else:
|
||
inactive.append(addon_fmt(addmgr, addon))
|
||
newline = "\n"
|
||
info = f"""\
|
||
===Add-ons (active)===
|
||
(add-on provided name [Add-on folder, installed at, version, is config changed])
|
||
{newline.join(sorted(active))}
|
||
|
||
===IDs of active AnkiWeb add-ons===
|
||
{" ".join(activeids)}
|
||
|
||
===Add-ons (inactive)===
|
||
(add-on provided name [Add-on folder, installed at, version, is config changed])
|
||
{newline.join(sorted(inactive))}
|
||
"""
|
||
return info
|