mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 07:22:23 -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
385 lines
12 KiB
Rust
385 lines
12 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use std::collections::HashMap;
|
|
use std::collections::HashSet;
|
|
|
|
pub use anki_proto::scheduler::schedule_cards_as_new_request::Context as ScheduleAsNewContext;
|
|
pub use anki_proto::scheduler::RepositionDefaultsResponse;
|
|
pub use anki_proto::scheduler::ScheduleCardsAsNewDefaultsResponse;
|
|
use rand::seq::SliceRandom;
|
|
|
|
use crate::card::CardQueue;
|
|
use crate::card::CardType;
|
|
use crate::config::BoolKey;
|
|
use crate::config::SchedulerVersion;
|
|
use crate::deckconfig::NewCardInsertOrder;
|
|
use crate::prelude::*;
|
|
use crate::search::JoinSearches;
|
|
use crate::search::SearchNode;
|
|
use crate::search::SortMode;
|
|
use crate::search::StateKind;
|
|
|
|
impl Card {
|
|
pub(crate) fn original_or_current_due(&self) -> i32 {
|
|
if self.is_filtered() {
|
|
self.original_due
|
|
} else {
|
|
self.due
|
|
}
|
|
}
|
|
|
|
pub(crate) fn last_position(&self) -> Option<u32> {
|
|
if self.ctype == CardType::New {
|
|
Some(self.original_or_current_due() as u32)
|
|
} else {
|
|
self.original_position
|
|
}
|
|
}
|
|
|
|
/// True if the provided position has been used.
|
|
/// (Always true, if restore_position is false.)
|
|
pub(crate) fn schedule_as_new(
|
|
&mut self,
|
|
position: u32,
|
|
reset_counts: bool,
|
|
restore_position: bool,
|
|
) -> bool {
|
|
let last_position = restore_position.then(|| self.last_position()).flatten();
|
|
self.remove_from_filtered_deck_before_reschedule();
|
|
self.due = last_position.unwrap_or(position) as i32;
|
|
self.ctype = CardType::New;
|
|
self.queue = CardQueue::New;
|
|
self.interval = 0;
|
|
self.ease_factor = 0;
|
|
self.original_position = None;
|
|
if reset_counts {
|
|
self.reps = 0;
|
|
self.lapses = 0;
|
|
}
|
|
self.memory_state = None;
|
|
|
|
last_position.is_none()
|
|
}
|
|
|
|
/// If the card is new, change its position, and return true.
|
|
fn set_new_position(&mut self, position: u32) -> bool {
|
|
if self.ctype == CardType::New {
|
|
if self.is_filtered() {
|
|
self.original_due = position as i32;
|
|
} else {
|
|
self.due = position as i32;
|
|
}
|
|
true
|
|
} else if self.queue == CardQueue::New {
|
|
self.due = position as i32;
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
pub(crate) struct NewCardSorter {
|
|
position: HashMap<NoteId, u32>,
|
|
}
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
pub enum NewCardDueOrder {
|
|
NoteId,
|
|
Random,
|
|
Preserve,
|
|
}
|
|
|
|
impl NewCardSorter {
|
|
pub(crate) fn new(
|
|
cards: &[Card],
|
|
starting_from: u32,
|
|
step: u32,
|
|
order: NewCardDueOrder,
|
|
) -> Self {
|
|
let nids = nids_in_desired_order(cards, order);
|
|
|
|
NewCardSorter {
|
|
position: nids
|
|
.into_iter()
|
|
.enumerate()
|
|
.map(|(i, nid)| (nid, ((i as u32) * step) + starting_from))
|
|
.collect(),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn position(&self, card: &Card) -> u32 {
|
|
self.position
|
|
.get(&card.note_id)
|
|
.cloned()
|
|
.unwrap_or_default()
|
|
}
|
|
}
|
|
|
|
fn nids_in_desired_order(cards: &[Card], order: NewCardDueOrder) -> Vec<NoteId> {
|
|
if order == NewCardDueOrder::Preserve {
|
|
nids_in_preserved_order(cards)
|
|
} else {
|
|
let nids: HashSet<_> = cards.iter().map(|c| c.note_id).collect();
|
|
let mut nids: Vec<_> = nids.into_iter().collect();
|
|
match order {
|
|
NewCardDueOrder::NoteId => {
|
|
nids.sort_unstable();
|
|
}
|
|
NewCardDueOrder::Random => {
|
|
nids.shuffle(&mut rand::rng());
|
|
}
|
|
NewCardDueOrder::Preserve => unreachable!(),
|
|
}
|
|
nids
|
|
}
|
|
}
|
|
|
|
fn nids_in_preserved_order(cards: &[Card]) -> Vec<NoteId> {
|
|
let mut seen = HashSet::new();
|
|
cards
|
|
.iter()
|
|
.filter_map(|card| {
|
|
if seen.insert(card.note_id) {
|
|
Some(card.note_id)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
impl Collection {
|
|
pub fn reschedule_cards_as_new(
|
|
&mut self,
|
|
cids: &[CardId],
|
|
log: bool,
|
|
restore_position: bool,
|
|
reset_counts: bool,
|
|
context: Option<ScheduleAsNewContext>,
|
|
) -> Result<OpOutput<()>> {
|
|
let usn = self.usn()?;
|
|
let mut position = self.get_next_card_position();
|
|
self.transact(Op::ScheduleAsNew, |col| {
|
|
let cards = col.all_cards_for_ids(cids, true)?;
|
|
for mut card in cards {
|
|
let original = card.clone();
|
|
if card.schedule_as_new(position, reset_counts, restore_position) {
|
|
position += 1;
|
|
}
|
|
if log {
|
|
col.log_manually_scheduled_review(&card, original.interval, usn)?;
|
|
}
|
|
col.update_card_inner(&mut card, original, usn)?;
|
|
}
|
|
col.set_next_card_position(position)?;
|
|
|
|
match context {
|
|
Some(ScheduleAsNewContext::Browser) => {
|
|
col.set_config_bool_inner(BoolKey::RestorePositionBrowser, restore_position)?;
|
|
col.set_config_bool_inner(BoolKey::ResetCountsBrowser, reset_counts)?;
|
|
}
|
|
Some(ScheduleAsNewContext::Reviewer) => {
|
|
col.set_config_bool_inner(BoolKey::RestorePositionReviewer, restore_position)?;
|
|
col.set_config_bool_inner(BoolKey::ResetCountsReviewer, reset_counts)?;
|
|
}
|
|
None => (),
|
|
}
|
|
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
pub fn reschedule_cards_as_new_defaults(
|
|
&self,
|
|
context: ScheduleAsNewContext,
|
|
) -> ScheduleCardsAsNewDefaultsResponse {
|
|
match context {
|
|
ScheduleAsNewContext::Browser => ScheduleCardsAsNewDefaultsResponse {
|
|
restore_position: self.get_config_bool(BoolKey::RestorePositionBrowser),
|
|
reset_counts: self.get_config_bool(BoolKey::ResetCountsBrowser),
|
|
},
|
|
ScheduleAsNewContext::Reviewer => ScheduleCardsAsNewDefaultsResponse {
|
|
restore_position: self.get_config_bool(BoolKey::RestorePositionReviewer),
|
|
reset_counts: self.get_config_bool(BoolKey::ResetCountsReviewer),
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn sort_cards(
|
|
&mut self,
|
|
cids: &[CardId],
|
|
starting_from: u32,
|
|
step: u32,
|
|
order: NewCardDueOrder,
|
|
shift: bool,
|
|
) -> Result<OpOutput<usize>> {
|
|
let usn = self.usn()?;
|
|
self.transact(Op::SortCards, |col| {
|
|
col.set_config_bool_inner(
|
|
BoolKey::RandomOrderReposition,
|
|
order == NewCardDueOrder::Random,
|
|
)?;
|
|
col.set_config_bool_inner(BoolKey::ShiftPositionOfExistingCards, shift)?;
|
|
col.sort_cards_inner(cids, starting_from, step, order, shift, usn)
|
|
})
|
|
}
|
|
|
|
fn sort_cards_inner(
|
|
&mut self,
|
|
cids: &[CardId],
|
|
starting_from: u32,
|
|
step: u32,
|
|
order: NewCardDueOrder,
|
|
shift: bool,
|
|
usn: Usn,
|
|
) -> Result<usize> {
|
|
if self.scheduler_version() == SchedulerVersion::V1 {
|
|
return Err(AnkiError::SchedulerUpgradeRequired);
|
|
}
|
|
if shift {
|
|
self.shift_existing_cards(starting_from, step * cids.len() as u32, usn)?;
|
|
}
|
|
let cards = self.all_cards_for_ids(cids, true)?;
|
|
let sorter = NewCardSorter::new(&cards, starting_from, step, order);
|
|
let mut count = 0;
|
|
for mut card in cards {
|
|
let original = card.clone();
|
|
if card.set_new_position(sorter.position(&card)) {
|
|
count += 1;
|
|
self.update_card_inner(&mut card, original, usn)?;
|
|
}
|
|
}
|
|
Ok(count)
|
|
}
|
|
|
|
pub fn reposition_defaults(&self) -> RepositionDefaultsResponse {
|
|
RepositionDefaultsResponse {
|
|
random: self.get_config_bool(BoolKey::RandomOrderReposition),
|
|
shift: self.get_config_bool(BoolKey::ShiftPositionOfExistingCards),
|
|
}
|
|
}
|
|
|
|
/// This is handled by update_deck_configs() now; this function has been
|
|
/// kept around for now to support the old deck config screen.
|
|
pub fn sort_deck_legacy(&mut self, deck: DeckId, random: bool) -> Result<OpOutput<usize>> {
|
|
self.transact(Op::SortCards, |col| {
|
|
col.sort_deck(
|
|
deck,
|
|
if random {
|
|
NewCardInsertOrder::Random
|
|
} else {
|
|
NewCardInsertOrder::Due
|
|
},
|
|
col.usn()?,
|
|
)
|
|
})
|
|
}
|
|
|
|
pub(crate) fn sort_deck(
|
|
&mut self,
|
|
deck: DeckId,
|
|
order: NewCardInsertOrder,
|
|
usn: Usn,
|
|
) -> Result<usize> {
|
|
let cids = self.search_cards(
|
|
SearchNode::DeckIdsWithoutChildren(deck.to_string()).and(StateKind::New),
|
|
SortMode::NoOrder,
|
|
)?;
|
|
self.sort_cards_inner(&cids, 1, 1, order.into(), false, usn)
|
|
}
|
|
|
|
fn shift_existing_cards(&mut self, start: u32, by: u32, usn: Usn) -> Result<()> {
|
|
for mut card in self.storage.all_cards_at_or_above_position(start)? {
|
|
let original = card.clone();
|
|
card.set_new_position(card.due as u32 + by);
|
|
self.update_card_inner(&mut card, original, usn)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl From<NewCardInsertOrder> for NewCardDueOrder {
|
|
fn from(o: NewCardInsertOrder) -> Self {
|
|
match o {
|
|
NewCardInsertOrder::Due => NewCardDueOrder::NoteId,
|
|
NewCardInsertOrder::Random => NewCardDueOrder::Random,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn new_order() {
|
|
let mut c1 = Card::new(NoteId(6), 0, DeckId(0), 0);
|
|
c1.id.0 = 2;
|
|
let mut c2 = Card::new(NoteId(5), 0, DeckId(0), 0);
|
|
c2.id.0 = 3;
|
|
let mut c3 = Card::new(NoteId(4), 0, DeckId(0), 0);
|
|
c3.id.0 = 1;
|
|
let cards = vec![c1.clone(), c2.clone(), c3.clone()];
|
|
|
|
// Preserve
|
|
let sorter = NewCardSorter::new(&cards, 0, 1, NewCardDueOrder::Preserve);
|
|
assert_eq!(sorter.position(&c1), 0);
|
|
assert_eq!(sorter.position(&c2), 1);
|
|
assert_eq!(sorter.position(&c3), 2);
|
|
|
|
// NoteId/step/starting
|
|
let sorter = NewCardSorter::new(&cards, 3, 2, NewCardDueOrder::NoteId);
|
|
assert_eq!(sorter.position(&c3), 3);
|
|
assert_eq!(sorter.position(&c2), 5);
|
|
assert_eq!(sorter.position(&c1), 7);
|
|
|
|
// Random
|
|
let mut c1_positions = HashSet::new();
|
|
for _ in 1..100 {
|
|
let sorter = NewCardSorter::new(&cards, 0, 1, NewCardDueOrder::Random);
|
|
c1_positions.insert(sorter.position(&c1));
|
|
if c1_positions.len() == cards.len() {
|
|
return;
|
|
}
|
|
}
|
|
unreachable!("not random");
|
|
}
|
|
|
|
#[test]
|
|
fn last_position() {
|
|
// new card
|
|
let mut card = Card::new(NoteId(0), 0, DeckId(1), 42);
|
|
assert_eq!(card.last_position(), Some(42));
|
|
// in filtered deck
|
|
card.original_deck_id.0 = 1;
|
|
card.deck_id.0 = 2;
|
|
card.original_due = 42;
|
|
card.due = 123456789;
|
|
card.queue = CardQueue::Review;
|
|
assert_eq!(card.last_position(), Some(42));
|
|
|
|
// graduated card
|
|
let mut card = Card::new(NoteId(0), 0, DeckId(1), 42);
|
|
card.queue = CardQueue::Review;
|
|
card.ctype = CardType::Review;
|
|
card.due = 123456789;
|
|
// only recent clients remember the original position
|
|
assert_eq!(card.last_position(), None);
|
|
card.original_position = Some(42);
|
|
assert_eq!(card.last_position(), Some(42));
|
|
}
|
|
|
|
#[test]
|
|
fn scheduling_as_new() {
|
|
let mut card = Card::new(NoteId(0), 0, DeckId(1), 42);
|
|
card.reps = 4;
|
|
card.lapses = 2;
|
|
// keep counts and position
|
|
card.schedule_as_new(1, false, true);
|
|
assert_eq!((card.due, card.reps, card.lapses), (42, 4, 2));
|
|
// complete reset
|
|
card.schedule_as_new(1, true, false);
|
|
assert_eq!((card.due, card.reps, card.lapses), (1, 0, 0));
|
|
}
|
|
}
|