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 Apr 23, 2020
1 parent d617783 commit 7c86fd6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 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)
10 changes: 3 additions & 7 deletions src/pip/_internal/self_outdated_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import os.path
import sys

from pip._vendor import pkg_resources
from pip._vendor.packaging import version as packaging_version
from pip._vendor.six import ensure_binary

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.search_scope import SearchScope
from pip._internal.models.selection_prefs import SelectionPreferences
from pip._internal.utils.filesystem import (
Expand All @@ -28,7 +28,6 @@
get_installed_version,
redact_auth_from_url,
)
from pip._internal.utils.packaging import get_installer
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
Expand Down Expand Up @@ -149,11 +148,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.
"""
try:
dist = pkg_resources.get_distribution(pkg)
return "pip" == get_installer(dist)
except pkg_resources.DistributionNotFound:
return False
dist = get_environment().get_distribution(pkg)
return dist is not None and "pip" == dist.installer


def pip_self_version_check(session, options):
Expand Down

0 comments on commit 7c86fd6

Please sign in to comment.