Skip to content

Commit 27cd03f

Browse files
committed
fixed spacy manager errors on windows (#348)
1 parent 1675102 commit 27cd03f

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

ankimorphs/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
from .generators.generators_window import GeneratorWindow
5858
from .highlighting.highlight_just_in_time import highlight_morphs_jit
5959
from .known_morphs_exporter import KnownMorphsExporterDialog
60+
from .morphemizers import spacy_wrapper
6061
from .progression.progression_window import ProgressionWindow
6162
from .recalc import recalc_main
6263
from .settings import settings_dialog
@@ -88,6 +89,7 @@ def main() -> None:
8889
gui_hooks.profile_did_open.append(init_browser_menus_and_actions)
8990
gui_hooks.profile_did_open.append(replace_card_reviewer)
9091
gui_hooks.profile_did_open.append(text_preprocessing.update_translation_table)
92+
gui_hooks.profile_did_open.append(spacy_wrapper.maybe_delete_spacy_venv)
9193

9294
gui_hooks.sync_will_start.append(recalc_on_sync)
9395

ankimorphs/morphemizers/spacy_wrapper.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import subprocess
77
import sys
8+
from pathlib import Path
89
from types import ModuleType
910
from typing import Any
1011

@@ -153,7 +154,7 @@ def get_installed_models() -> list[str]:
153154

154155
def _get_am_spacy_venv_python() -> str:
155156
if is_win:
156-
return os.path.join(_get_am_spacy_venv_path(), "Scripts", "python", ".exe")
157+
return os.path.join(_get_am_spacy_venv_path(), "Scripts", "python.exe")
157158
return os.path.join(_get_am_spacy_venv_path(), "bin", "python")
158159

159160

@@ -168,17 +169,21 @@ def create_spacy_venv() -> None:
168169
"""
169170

170171
spacy_venv_path = _get_am_spacy_venv_path()
172+
173+
# delete in case it already exists from previously failed attempts
174+
shutil.rmtree(spacy_venv_path, ignore_errors=True)
175+
171176
python_path: str | None = venv_binary("python")
172177
assert python_path is not None
173178

174179
subprocess.run([python_path, "-m", "venv", spacy_venv_path], check=True)
175180

176181
if is_win:
177-
spacy_venv_python = os.path.join(spacy_venv_path, "Scripts", "python", ".exe")
182+
spacy_venv_python = os.path.join(spacy_venv_path, "Scripts", "python.exe")
178183
else:
179184
spacy_venv_python = os.path.join(spacy_venv_path, "bin", "python")
180185

181-
# make sure pip, setuptools, and wheel are up to date
186+
# make sure pip, setuptools, and wheel are up-to-date
182187
subprocess.run(
183188
[
184189
spacy_venv_python,
@@ -201,7 +206,21 @@ def create_spacy_venv() -> None:
201206

202207

203208
def delete_spacy_venv() -> None:
204-
shutil.rmtree(_get_am_spacy_venv_path())
209+
spacy_venv_path = _get_am_spacy_venv_path()
210+
try:
211+
shutil.rmtree(spacy_venv_path)
212+
except PermissionError:
213+
# windows does not like deleting files in use, so we add this flag file
214+
# and we delete the venv on startup if the file exists
215+
(Path(spacy_venv_path) / ".delete_me").touch()
216+
217+
218+
def maybe_delete_spacy_venv() -> None:
219+
# gracefully delete spacy venv on windows
220+
spacy_venv_path = _get_am_spacy_venv_path()
221+
flag = Path(spacy_venv_path, ".delete_me")
222+
if flag.exists():
223+
shutil.rmtree(spacy_venv_path)
205224

206225

207226
def install_model(model_name: str) -> None:

docs/src/contributors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Vilhelm-Ian, CodeWithMa, ashprice, aleksejrs, HQYang1979, soliviantar, buster-bl
1818
cocowash, asayake-b5, quietmansoath, MichaelPetre, xofm31, knoebelja, xuiqzy, Jcuhfehl, fuquasteve, pallas42, syfgk,
1919
jahnke, jsteel44, iwouldrathernotusegithub, tanhoaian01, drkthomp, Kirchheim, zeroeightysix, Gardengul, wolearyc,
2020
Pedrubik2000, RyanMcEntire, BobvanSchendel, khanguyenwk, buqamura, Rct567, rwmpelstilzchen, bie-zheng, IncontinentCell,
21-
mdraves91, dae.
21+
mdraves91, dae, AtilioA.
2222

2323
### MorphMan (v5.0-qt6-alpha.1)
2424

0 commit comments

Comments
 (0)