2017-03-16 19:01:51 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import pip
|
|
|
|
import site
|
2017-03-18 14:59:21 +03:00
|
|
|
from pathlib import Path
|
2017-03-18 17:14:48 +03:00
|
|
|
from .. import util
|
2017-03-16 19:01:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
def link(origin, link_name, force=False):
|
|
|
|
if is_package(origin):
|
2017-03-18 14:59:21 +03:00
|
|
|
link_package(origin, link_name, force)
|
2017-03-16 19:01:51 +03:00
|
|
|
else:
|
|
|
|
symlink(origin, link_name, force)
|
|
|
|
|
|
|
|
|
2017-03-17 23:35:51 +03:00
|
|
|
def link_package(origin, link_name, force=False):
|
|
|
|
package_path = site.getsitepackages()[0]
|
|
|
|
meta = get_meta(package_path, origin)
|
|
|
|
data_dir = origin + '-' + meta['version']
|
2017-03-18 14:59:21 +03:00
|
|
|
model_path = Path(package_path) / origin / data_dir
|
2017-03-17 23:35:51 +03:00
|
|
|
symlink(model_path, link_name, force)
|
|
|
|
|
|
|
|
|
2017-03-16 19:01:51 +03:00
|
|
|
def symlink(model_path, link_name, force):
|
2017-03-18 14:59:21 +03:00
|
|
|
if not model_path.exists():
|
2017-03-16 19:08:58 +03:00
|
|
|
util.sys_exit(
|
|
|
|
"The data should be located in {p}".format(p=model_path),
|
|
|
|
title="Can't locate model data")
|
2017-03-16 19:01:51 +03:00
|
|
|
|
|
|
|
data_path = str(util.get_data_path())
|
2017-03-18 14:59:21 +03:00
|
|
|
link_path = Path(__file__).parent.parent / data_path / link_name
|
2017-03-16 19:01:51 +03:00
|
|
|
|
2017-03-18 14:59:21 +03:00
|
|
|
if link_path.exists():
|
2017-03-16 19:01:51 +03:00
|
|
|
if force:
|
2017-03-18 14:59:21 +03:00
|
|
|
os.unlink(str(link_path))
|
2017-03-16 19:01:51 +03:00
|
|
|
else:
|
2017-03-16 19:08:58 +03:00
|
|
|
util.sys_exit(
|
|
|
|
"To overwrite an existing link, use the --force flag.",
|
|
|
|
title="Link {l} already exists".format(l=link_name))
|
2017-03-18 14:59:21 +03:00
|
|
|
|
|
|
|
os.symlink(str(model_path), str(link_path))
|
2017-03-16 19:08:58 +03:00
|
|
|
util.print_msg(
|
2017-03-18 14:59:21 +03:00
|
|
|
"{a} --> {b}".format(a=str(model_path), b=str(link_path)),
|
2017-03-16 19:08:58 +03:00
|
|
|
"You can now load the model via spacy.load('{l}').".format(l=link_name),
|
|
|
|
title="Linking successful")
|
|
|
|
|
2017-03-16 19:01:51 +03:00
|
|
|
|
|
|
|
def get_meta(package_path, package):
|
|
|
|
meta = util.parse_package_meta(package_path, package)
|
|
|
|
return meta
|
|
|
|
|
|
|
|
|
|
|
|
def is_package(origin):
|
|
|
|
packages = pip.get_installed_distributions()
|
|
|
|
for package in packages:
|
|
|
|
if package.project_name.replace('-', '_') == origin:
|
|
|
|
return True
|
|
|
|
return False
|