Skip to content

Commit

Permalink
Basic abstraction and one migration
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Jul 30, 2020
1 parent 5789e0f commit 200ed86
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/pip/_internal/metadata/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pip._internal.utils.compat import lru_cache
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import List, Optional

from .base import BaseEnvironment


@lru_cache(maxsize=None)
def get_environment(paths=None):
# type: (Optional[List[str]]) -> BaseEnvironment
from .pkg_resources import Environment
if paths is None:
return Environment.default()
return Environment.from_paths(paths)
35 changes: 35 additions & 0 deletions src/pip/_internal/metadata/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import abc

from pip._vendor.six import add_metaclass

from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import List, Optional


@add_metaclass(abc.ABCMeta)
class BaseDistribution(object):
@property
def installer(self):
# type: () -> str
raise NotImplementedError()


@add_metaclass(abc.ABCMeta)
class BaseEnvironment(object):
"""An environment containing distributions to introspect.
"""
@classmethod
def default(cls):
# type: () -> BaseEnvironment
raise NotImplementedError()

@classmethod
def from_paths(cls, paths):
# type: (List[str]) -> BaseEnvironment
raise NotImplementedError()

def get_distribution(self, name):
# type: (str) -> Optional[BaseDistribution]
raise NotImplementedError()
46 changes: 46 additions & 0 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from pip._vendor import pkg_resources

from pip._internal.utils.packaging import get_installer
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

from .base import BaseDistribution, BaseEnvironment

if MYPY_CHECK_RUNNING:
from typing import List, Optional


class Distribution(BaseDistribution):
def __init__(self, dist):
# type: (pkg_resources.Distribution) -> None
self._dist = dist

@property
def installer(self):
# type: () -> str
# TODO: Move get_installer() implementation here.
return get_installer(self._dist)


class Environment(BaseEnvironment):
def __init__(self, ws):
# type: (pkg_resources.WorkingSet) -> None
self._ws = ws

@classmethod
def default(cls):
# type: () -> BaseEnvironment
return cls(pkg_resources.working_set)

@classmethod
def from_paths(cls, paths):
# type: (List[str]) -> BaseEnvironment
return cls(pkg_resources.WorkingSet(paths))

def get_distribution(self, name):
# type: (str) -> Optional[BaseDistribution]
req = pkg_resources.Requirement(name)
try:
dist = self._ws.find(req)
except pkg_resources.DistributionNotFound:
return None
return Distribution(dist)
14 changes: 4 additions & 10 deletions src/pip/_internal/self_outdated_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@

from pip._internal.index.collector import LinkCollector
from pip._internal.index.package_finder import PackageFinder
from pip._internal.metadata import get_environment
from pip._internal.models.selection_prefs import SelectionPreferences
from pip._internal.utils.filesystem import (
adjacent_tmp_file,
check_path_owner,
replace,
)
from pip._internal.utils.misc import (
ensure_dir,
get_distribution,
get_installed_version,
)
from pip._internal.utils.packaging import get_installer
from pip._internal.utils.misc import ensure_dir, get_installed_version
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
Expand Down Expand Up @@ -113,10 +109,8 @@ def was_installed_by_pip(pkg):
This is used not to display the upgrade message when pip is in fact
installed by system package manager, such as dnf on Fedora.
"""
dist = get_distribution(pkg)
if not dist:
return False
return "pip" == get_installer(dist)
dist = get_environment().get_distribution(pkg)
return dist is not None and "pip" == dist.installer


def pip_self_version_check(session, options):
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/test_self_check_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def get_metadata_lines(self, name):
raise NotImplementedError('nope')


class MockEnvironment(object):
def __init__(self, installer):
self.installer = installer

def get_distribution(self, name):
return MockDistribution(self.installer)


def _options():
''' Some default options that we pass to
self_outdated_check.pip_self_version_check '''
Expand Down Expand Up @@ -97,8 +105,8 @@ def test_pip_self_version_check(monkeypatch, stored_time, installed_ver,
pretend.call_recorder(lambda *a, **kw: None))
monkeypatch.setattr(logger, 'debug',
pretend.call_recorder(lambda s, exc_info=None: None))
monkeypatch.setattr(self_outdated_check, 'get_distribution',
lambda name: MockDistribution(installer))
monkeypatch.setattr(self_outdated_check, 'get_environment',
lambda: MockEnvironment(installer))

fake_state = pretend.stub(
state={"last_check": stored_time, 'pypi_version': installed_ver},
Expand Down

0 comments on commit 200ed86

Please sign in to comment.