-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters