2017-03-16 19:01:51 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
import plac
|
2017-03-18 14:59:21 +03:00
|
|
|
from pathlib import Path
|
2019-11-04 04:38:45 +03:00
|
|
|
from wasabi import msg
|
2017-05-22 13:28:58 +03:00
|
|
|
|
2017-05-08 00:25:29 +03:00
|
|
|
from ..compat import symlink_to, path2str
|
2017-03-18 17:14:48 +03:00
|
|
|
from .. import util
|
2017-03-16 19:01:51 +03:00
|
|
|
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
@plac.annotations(
|
|
|
|
origin=("package name or local path to model", "positional", None, str),
|
|
|
|
link_name=("name of shortuct link to create", "positional", None, str),
|
2018-11-30 22:16:14 +03:00
|
|
|
force=("force overwriting of existing link", "flag", "f", bool),
|
|
|
|
)
|
2018-01-04 23:33:47 +03:00
|
|
|
def link(origin, link_name, force=False, model_path=None):
|
2017-05-27 21:01:46 +03:00
|
|
|
"""
|
|
|
|
Create a symlink for models within the spacy/data directory. Accepts
|
2017-05-22 13:28:58 +03:00
|
|
|
either the name of a pip package, or the local path to the model data
|
|
|
|
directory. Linking models allows loading them via spacy.load(link_name).
|
|
|
|
"""
|
2017-05-08 00:25:29 +03:00
|
|
|
if util.is_package(origin):
|
2017-06-04 14:45:50 +03:00
|
|
|
model_path = util.get_package_path(origin)
|
2017-03-16 19:01:51 +03:00
|
|
|
else:
|
2017-08-09 12:52:38 +03:00
|
|
|
model_path = Path(origin) if model_path is None else Path(model_path)
|
2017-04-16 21:53:25 +03:00
|
|
|
if not model_path.exists():
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.fail(
|
2018-12-08 13:49:43 +03:00
|
|
|
"Can't locate model data",
|
|
|
|
"The data should be located in {}".format(path2str(model_path)),
|
|
|
|
exits=1,
|
2018-11-30 22:16:14 +03:00
|
|
|
)
|
2017-10-19 01:53:49 +03:00
|
|
|
data_path = util.get_data_path()
|
|
|
|
if not data_path or not data_path.exists():
|
|
|
|
spacy_loc = Path(__file__).parent.parent
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail(
|
|
|
|
"Can't find the spaCy data path to create model symlink",
|
|
|
|
"Make sure a directory `/data` exists within your spaCy "
|
|
|
|
"installation and try again. The data directory should be located "
|
|
|
|
"here:".format(path=spacy_loc),
|
|
|
|
exits=1,
|
|
|
|
)
|
2017-03-18 20:57:31 +03:00
|
|
|
link_path = util.get_data_path() / link_name
|
2018-01-03 19:39:36 +03:00
|
|
|
if link_path.is_symlink() and not force:
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail(
|
|
|
|
"Link '{}' already exists".format(link_name),
|
|
|
|
"To overwrite an existing link, use the --force flag",
|
|
|
|
exits=1,
|
|
|
|
)
|
2018-01-03 19:39:36 +03:00
|
|
|
elif link_path.is_symlink(): # does a symlink exist?
|
|
|
|
# NB: It's important to check for is_symlink here and not for exists,
|
|
|
|
# because invalid/outdated symlinks would return False otherwise.
|
2017-03-18 20:57:31 +03:00
|
|
|
link_path.unlink()
|
2018-11-30 22:16:14 +03:00
|
|
|
elif link_path.exists(): # does it exist otherwise?
|
2018-01-03 19:39:36 +03:00
|
|
|
# NB: Check this last because valid symlinks also "exist".
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail(
|
|
|
|
"Can't overwrite symlink '{}'".format(link_name),
|
|
|
|
"This can happen if your data directory contains a directory or "
|
|
|
|
"file of the same name.",
|
|
|
|
exits=1,
|
|
|
|
)
|
2018-11-30 22:16:14 +03:00
|
|
|
details = "%s --> %s" % (path2str(model_path), path2str(link_path))
|
2017-04-15 13:11:16 +03:00
|
|
|
try:
|
|
|
|
symlink_to(link_path, model_path)
|
2018-11-30 22:16:14 +03:00
|
|
|
except: # noqa: E722
|
2017-05-08 00:25:29 +03:00
|
|
|
# This is quite dirty, but just making sure other errors are caught.
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail(
|
|
|
|
"Couldn't link model to '{}'".format(link_name),
|
|
|
|
"Creating a symlink in spacy/data failed. Make sure you have the "
|
|
|
|
"required permissions and try re-running the command as admin, or "
|
|
|
|
"use a virtualenv. You can still import the model as a module and "
|
|
|
|
"call its load() method, or create the symlink manually.",
|
|
|
|
)
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.text(details)
|
2017-04-16 23:28:16 +03:00
|
|
|
raise
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.good("Linking successful", details)
|
|
|
|
msg.text("You can now load the model via spacy.load('{}')".format(link_name))
|