summaryrefslogtreecommitdiff
path: root/src/pip/_internal/metadata/pkg_resources.py
blob: f39a39ebebfb913cc6fc30c307d53ffca1052dba (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import zipfile
from typing import Iterator, List, Optional

from pip._vendor import pkg_resources
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import parse as parse_version

from pip._internal.utils import misc  # TODO: Move definition here.
from pip._internal.utils.packaging import get_installer
from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel

from .base import BaseDistribution, BaseEnvironment, DistributionVersion


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

    @classmethod
    def from_wheel(cls, path, name):
        # type: (str, str) -> Distribution
        with zipfile.ZipFile(path, allowZip64=True) as zf:
            dist = pkg_resources_distribution_for_wheel(zf, name, path)
        return cls(dist)

    @property
    def location(self):
        # type: () -> Optional[str]
        return self._dist.location

    @property
    def metadata_version(self):
        # type: () -> Optional[str]
        for line in self._dist.get_metadata_lines(self._dist.PKG_INFO):
            if line.lower().startswith("metadata-version:"):
                return line.split(":", 1)[-1].strip()
        return None

    @property
    def canonical_name(self):
        # type: () -> str
        return canonicalize_name(self._dist.project_name)

    @property
    def version(self):
        # type: () -> DistributionVersion
        return parse_version(self._dist.version)

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

    @property
    def editable(self):
        # type: () -> bool
        return misc.dist_is_editable(self._dist)

    @property
    def local(self):
        # type: () -> bool
        return misc.dist_is_local(self._dist)

    @property
    def in_usersite(self):
        # type: () -> bool
        return misc.dist_in_usersite(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: (Optional[List[str]]) -> BaseEnvironment
        return cls(pkg_resources.WorkingSet(paths))

    def _search_distribution(self, name):
        # type: (str) -> Optional[BaseDistribution]
        """Find a distribution matching the ``name`` in the environment.

        This searches from *all* distributions available in the environment, to
        match the behavior of ``pkg_resources.get_distribution()``.
        """
        canonical_name = canonicalize_name(name)
        for dist in self.iter_distributions():
            if dist.canonical_name == canonical_name:
                return dist
        return None

    def get_distribution(self, name):
        # type: (str) -> Optional[BaseDistribution]

        # Search the distribution by looking through the working set.
        dist = self._search_distribution(name)
        if dist:
            return dist

        # If distribution could not be found, call working_set.require to
        # update the working set, and try to find the distribution again.
        # This might happen for e.g. when you install a package twice, once
        # using setup.py develop and again using setup.py install. Now when
        # running pip uninstall twice, the package gets removed from the
        # working set in the first uninstall, so we have to populate the
        # working set again so that pip knows about it and the packages gets
        # picked up and is successfully uninstalled the second time too.
        try:
            # We didn't pass in any version specifiers, so this can never
            # raise pkg_resources.VersionConflict.
            self._ws.require(name)
        except pkg_resources.DistributionNotFound:
            return None
        return self._search_distribution(name)

    def _iter_distributions(self):
        # type: () -> Iterator[BaseDistribution]
        for dist in self._ws:
            yield Distribution(dist)