summaryrefslogtreecommitdiff
path: root/src/pip/_internal/metadata/base.py
blob: fb1f8579a38d91f9754058eb7ce34cf05e1e1c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from pip._internal.utils.misc import stdlib_pkgs  # TODO: Move definition here.
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
    from typing import Container, Iterator, List, Optional

    from pip._vendor.packaging.version import _BaseVersion


class BaseDistribution:
    @property
    def metadata_version(self):
        # type: () -> Optional[str]
        """Value of "Metadata-Version:" in the distribution, if available."""
        raise NotImplementedError()

    @property
    def canonical_name(self):
        # type: () -> str
        raise NotImplementedError()

    @property
    def version(self):
        # type: () -> _BaseVersion
        raise NotImplementedError()

    @property
    def installer(self):
        # type: () -> str
        raise NotImplementedError()

    @property
    def editable(self):
        # type: () -> bool
        raise NotImplementedError()

    @property
    def local(self):
        # type: () -> bool
        raise NotImplementedError()

    @property
    def in_usersite(self):
        # type: () -> bool
        raise NotImplementedError()


class BaseEnvironment:
    """An environment containing distributions to introspect."""

    @classmethod
    def default(cls):
        # type: () -> BaseEnvironment
        raise NotImplementedError()

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

    def get_distribution(self, name):
        # type: (str) -> Optional[BaseDistribution]
        """Given a requirement name, return the installed distributions."""
        raise NotImplementedError()

    def iter_distributions(self):
        # type: () -> Iterator[BaseDistribution]
        """Iterate through installed distributions."""
        raise NotImplementedError()

    def iter_installed_distributions(
        self,
        local_only=True,  # type: bool
        skip=stdlib_pkgs,  # type: Container[str]
        include_editables=True,  # type: bool
        editables_only=False,  # type: bool
        user_only=False,  # type: bool
    ):
        # type: (...) -> Iterator[BaseDistribution]
        """Return a list of installed distributions.

        :param local_only: If True (default), only return installations
        local to the current virtualenv, if in a virtualenv.
        :param skip: An iterable of canonicalized project names to ignore;
            defaults to ``stdlib_pkgs``.
        :param include_editables: If False, don't report editables.
        :param editables_only: If True, only report editables.
        :param user_only: If True, only report installations in the user
        site directory.
        """
        it = self.iter_distributions()
        if local_only:
            it = (d for d in it if d.local)
        if not include_editables:
            it = (d for d in it if not d.editable)
        if editables_only:
            it = (d for d in it if d.editable)
        if user_only:
            it = (d for d in it if d.in_usersite)
        return (d for d in it if d.canonical_name not in skip)