2017-03-16 16:01:51 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-22 10:28:58 +00:00
|
|
|
import plac
|
2017-03-18 11:59:21 +00:00
|
|
|
from pathlib import Path
|
2017-05-22 10:28:58 +00:00
|
|
|
|
2017-05-07 21:25:29 +00:00
|
|
|
from ..compat import symlink_to, path2str
|
|
|
|
from ..util import prints
|
2017-03-18 14:14:48 +00:00
|
|
|
from .. import util
|
2017-03-16 16:01:51 +00:00
|
|
|
|
|
|
|
|
2017-05-22 10:28:58 +00: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),
|
|
|
|
force=("force overwriting of existing link", "flag", "f", bool)
|
|
|
|
)
|
2017-05-27 18:01:46 +00:00
|
|
|
def link(cmd, origin, link_name, force=False):
|
|
|
|
"""
|
|
|
|
Create a symlink for models within the spacy/data directory. Accepts
|
2017-05-22 10:28:58 +00: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-07 21:25:29 +00:00
|
|
|
if util.is_package(origin):
|
2017-05-27 22:22:00 +00:00
|
|
|
model_path = util.get_package_path(model)
|
2017-03-16 16:01:51 +00:00
|
|
|
else:
|
2017-05-07 21:25:29 +00:00
|
|
|
model_path = Path(origin)
|
2017-04-16 18:53:25 +00:00
|
|
|
if not model_path.exists():
|
2017-05-07 21:25:29 +00:00
|
|
|
prints("The data should be located in %s" % path2str(model_path),
|
2017-05-22 10:28:58 +00:00
|
|
|
title="Can't locate model data", exits=1)
|
2017-03-18 17:57:31 +00:00
|
|
|
link_path = util.get_data_path() / link_name
|
|
|
|
if link_path.exists() and not force:
|
2017-05-07 21:25:29 +00:00
|
|
|
prints("To overwrite an existing link, use the --force flag.",
|
2017-05-22 10:28:58 +00:00
|
|
|
title="Link %s already exists" % link_name, exits=1)
|
2017-03-18 17:57:31 +00:00
|
|
|
elif link_path.exists():
|
|
|
|
link_path.unlink()
|
2017-04-15 10:11:16 +00:00
|
|
|
try:
|
|
|
|
symlink_to(link_path, model_path)
|
|
|
|
except:
|
2017-05-07 21:25:29 +00:00
|
|
|
# This is quite dirty, but just making sure other errors are caught.
|
|
|
|
prints("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.",
|
|
|
|
"%s --> %s" % (path2str(model_path), path2str(link_path)),
|
|
|
|
title="Error: Couldn't link model to '%s'" % link_name)
|
2017-04-16 20:28:16 +00:00
|
|
|
raise
|
2017-05-07 21:25:29 +00:00
|
|
|
prints("%s --> %s" % (path2str(model_path), path2str(link_path)),
|
|
|
|
"You can now load the model via spacy.load('%s')." % link_name,
|
|
|
|
title="Linking successful")
|