def _download_and_link(resource_alias, resource_fullname, compatibility, pip_args, verbose): version = get_resources_version(resource_fullname, resource_alias, compatibility) url_tail = '{r}-{v}/{r}-{v}.tar.gz#egg={r}=={v}'.format( r=resource_fullname, v=version) download_url = __about__.__download_url__ + '/' + url_tail exit_code = install_remote_package(download_url, pip_args) if exit_code != 0: sys.exit(exit_code) try: # Get package path here because link uses # pip.get_installed_distributions() to check if the resource is a # package, which fails if the resource was just installed via # subprocess package_path = get_package_path(resource_fullname) link_path, resources_dir = link_resources(resource_fullname, resource_alias, force=True, resources_path=package_path) if verbose: pretty_print("%s --> %s" % (str(resources_dir), str(link_path)), title="Linking successful", level=PrettyPrintLevel.SUCCESS) except OSError as e: # pylint:disable=bare-except pretty_print( "Creating a shortcut link for '%s' didn't work: %s" % (resource_alias, repr(e)), title="The language resources were successfully downloaded, " "however linking failed.", level=PrettyPrintLevel.ERROR)
def load_resources(name, required_resources=None): """Load language specific resources Args: name (str): Resource name as in ``snips-nlu download <name>``. Can also be the name of a python package or a directory path. required_resources (dict, optional): Resources requirement dict which, when provided, allows to limit the amount of resources to load. By default, all existing resources are loaded. """ if name in set(d.name for d in DATA_PATH.iterdir()): return load_resources_from_dir(DATA_PATH / name, required_resources) elif is_package(name): package_path = get_package_path(name) resources_sub_dir = get_resources_sub_directory(package_path) return load_resources_from_dir(resources_sub_dir, required_resources) elif Path(name).exists(): path = Path(name) if (path / "__init__.py").exists(): path = get_resources_sub_directory(path) return load_resources_from_dir(path, required_resources) else: raise MissingResource( "Language resource '{r}' not found. This may be " "solved by running " "'python -m snips_nlu download {r}'".format(r=name))
def _download_and_link_entity(long_resource_name, entity_name, language, compatibility, pip_args): import sys from builtins import str from snips_nlu_parsers import get_builtin_entity_shortname from snips_nlu.cli.link import link_resources from snips_nlu.cli.utils import (PrettyPrintLevel, get_json, get_resources_version, install_remote_package, pretty_print) from snips_nlu.common.utils import get_package_path full_resource_name = long_resource_name + "_" + language version = get_resources_version(full_resource_name, entity_name, compatibility) entity_alias = get_builtin_entity_shortname(str(entity_name)).lower() entity_base_url = _get_entity_base_url(language, entity_alias, version) latest = get_json(entity_base_url + "/latest", "Latest entity resources version") latest_url = "{b}/{n}#egg={r}=={v}".format(b=entity_base_url, n=latest["filename"], r=full_resource_name, v=latest["version"]) exit_code = install_remote_package(latest_url, pip_args) if exit_code != 0: sys.exit(exit_code) try: # Get package path here because link uses # pip.get_installed_distributions() to check if the resource is a # package, which fails if the resource was just installed via # subprocess package_path = get_package_path(full_resource_name) link_alias = entity_alias + "_" + language link_path, resources_dir = link_resources(full_resource_name, link_alias, force=True, resources_path=package_path) pretty_print("%s --> %s" % (str(resources_dir), str(link_path)), "You can now use the '%s' builtin entity" % entity_name, title="Linking successful", level=PrettyPrintLevel.SUCCESS) except: # pylint:disable=bare-except pretty_print( "Creating a shortcut link for '%s' didn't work." % entity_name, title="The builtin entity resources were successfully downloaded, " "however linking failed.", level=PrettyPrintLevel.ERROR)
def link_resources(origin, link_name, force, resources_path): if is_package(origin): resources_path = get_package_path(origin) else: resources_path = Path(origin) if resources_path is None \ else Path(resources_path) if not resources_path.exists(): raise OSError("%s not found" % str(resources_path)) link_path = DATA_PATH / str(link_name) if link_path.is_symlink() and not force: raise OSError("Symlink already exists: %s" % str(link_path)) elif link_path.is_symlink(): link_path.unlink() elif link_path.exists(): raise OSError("Symlink cannot be overwritten: %s" % str(link_path)) resources_sub_dir = get_resources_sub_directory(resources_path) create_symlink(link_path, resources_sub_dir) return link_path, resources_sub_dir
def link_resources(origin, link_name, force, resources_path): from pathlib import Path from snips_nlu.cli.compatibility import create_symlink from snips_nlu.common.utils import get_package_path, is_package from snips_nlu.constants import DATA_PATH from snips_nlu.resources import get_resources_sub_directory if is_package(origin): resources_path = get_package_path(origin) else: resources_path = Path(origin) if resources_path is None \ else Path(resources_path) if not resources_path.exists(): raise OSError("%s not found" % str(resources_path)) link_path = DATA_PATH / str(link_name) if link_path.is_symlink() and not force: raise OSError("Symlink already exists: %s" % str(link_path)) elif link_path.is_symlink(): link_path.unlink() elif link_path.exists(): raise OSError("Symlink cannot be overwritten: %s" % str(link_path)) resources_sub_dir = get_resources_sub_directory(resources_path) create_symlink(link_path, resources_sub_dir) return link_path, resources_sub_dir