From c510be9c0eda61c0e006b633ccd8f5f690507082 Mon Sep 17 00:00:00 2001 From: Orion Montoya Date: Sat, 17 Mar 2018 22:57:52 -0700 Subject: [PATCH] Backport 2.x's avoidance of 'import pip' --- requirements.txt | 1 - spacy/cli/link.py | 14 +++++++++----- spacy/tests/test_cli.py | 7 +++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9d6f34133..d7b9245b6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,3 @@ requests>=2.11.0,<3.0.0 regex>=2017.4.1,<2017.12.1 ftfy>=4.4.2,<5.0.0 pytest>=3.0.6,<4.0.0 -pip>=9.0.0,<10.0.0 diff --git a/spacy/cli/link.py b/spacy/cli/link.py index f2d2fd436..8922306fa 100644 --- a/spacy/cli/link.py +++ b/spacy/cli/link.py @@ -1,7 +1,7 @@ # coding: utf8 from __future__ import unicode_literals -import pip +import pkg_resources from pathlib import Path import importlib from ..compat import unicode_, symlink_to @@ -69,10 +69,14 @@ 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() +def is_package(name): + """Check if string maps to a package installed via pip. + name (unicode): Name of package. + RETURNS (bool): True if installed package, False if not. + """ + name = name.lower() # compare package name against lowercase name + packages = pkg_resources.working_set.by_key.keys() for package in packages: - if package.project_name.replace('-', '_') == origin: + if package.lower().replace('-', '_') == name: return True return False diff --git a/spacy/tests/test_cli.py b/spacy/tests/test_cli.py index 189a238fc..d4a2ab4b6 100644 --- a/spacy/tests/test_cli.py +++ b/spacy/tests/test_cli.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from ..cli.download import download, get_compatibility, get_version, check_error_depr +from ..cli.link import is_package import pytest @@ -24,3 +25,9 @@ def test_cli_download_get_matching_version_fails(model): def test_cli_download_no_model_depr_error(model): with pytest.raises(SystemExit): check_error_depr(model) + + +@pytest.mark.parametrize('package', ['numpy']) +def test_cli_link_is_package(package): + """Test that an installed package via pip is recognised by link.is_package.""" + assert is_package(package)