File: _index.py

package info (click to toggle)
python-rjsmin 1.2.5%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,256 kB
  • sloc: javascript: 8,503; python: 5,315; ansic: 821; sh: 100; makefile: 19
file content (306 lines) | stat: -rw-r--r-- 8,680 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# -*- coding: ascii -*-
#
# Copyright 2018 - 2025
# Andr\xe9 Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Inspect package versions in index
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"""

import json as _json
import logging as _logging

import pkg_resources as _pkg_resources

try:
    from setuptools import package_index as _pkg_index
except ImportError:
    _pkg_index = None
import invoke as _invoke

from ... import pypi as _pypi
from ..._inv import tasks as _tasks
from .. import _parse

# pylint: disable = import-outside-toplevel

logger = _logging.getLogger("deps.inspect.index")


class PipIndex(_pkg_resources.Environment):
    """Query the index using pip"""

    _is_available = None

    def __init__(self, *args, **kwargs):
        super(PipIndex, self).__init__(*args, **kwargs)
        self._ctx = _tasks.new_context()
        self._cache = set()

    @classmethod
    def is_available(cls):
        """Is the pip index available?"""
        if cls._is_available is not None:
            return cls._is_available

        ctx = _tasks.new_context()
        try:
            ctx.run(
                ctx.c(
                    "pip index -i %s versions --json pip",
                    _pypi.index_url(ctx),
                ),
                hide=True,
                echo=False,
            )
        except _invoke.UnexpectedExit:
            cls._is_available = False
        else:
            cls._is_available = True

        return cls._is_available

    def find_packages(self, requirement):
        """Find packages"""
        if requirement.key in self._cache:
            return

        ctx = self._ctx
        index_info = _json.loads(
            ctx.run(
                ctx.c(
                    [
                        "pip",
                        "index",
                        "-i",
                        _pypi.index_url(ctx),
                        "versions",
                        "--json",
                        requirement.project_name,
                    ]
                ),
                hide=True,
            ).stdout.strip()
        )
        for version in index_info.get("versions", ()):
            self.add(
                _pkg_resources.Distribution(
                    project_name=requirement.project_name,
                    version=version,
                    precedence=_pkg_resources.EGG_DIST + 1,
                )
            )
        self._cache.add(requirement.key)

    def obtain(self, requirement, installer=None):
        self.find_packages(requirement)
        for dist in self[requirement.key]:
            if dist in requirement:
                return dist
        return super().obtain(requirement, installer)


class Index(object):
    """Python package index container"""

    def __init__(self):
        """Initialization"""
        if PipIndex.is_available():
            self._index = PipIndex(search_path=[])
        elif _pkg_index is not None:
            self._index = _pkg_index.PackageIndex(search_path=[])
        else:
            raise RuntimeError("No index access tool available")

        self._wset = _pkg_resources.WorkingSet([])

    def lookup(self, req):
        """
        Find the best-fitting requirement in the index

        Parameters:
          req (Requirement):
            The requirement to look up

        Returns:
          Distribution: The found distribution or ``None``
        """
        dist = self._index.best_match(req, self._wset)
        logger.debug("Index lookup result of %r: %r", str(req), dist)
        return dist

    def scanner(self, version):
        """
        Create a new package scanner

        Parameters:
          version (str):
            The package version to compare to

        Returns:
          PackageScanner: New package scanner
        """
        return PackageScanner(self, version)


class PackageScanner(object):
    """Container for scanning a single package"""

    def __init__(self, index, package_version):
        """
        Initialization

        Parameters:
          index (Index):
            The index container

          package_version (str):
            The installed package version
        """
        self._index = index
        self._version = package_version and _parse.version(package_version)
        self._result = {}

    def result(self):
        """
        Get the aggregated result

        Returns:
          dict: The result
        """
        final = dict(self._result)
        if (
            final.get("latest") is not None
            and final.get("compatible") is not None
            and final["latest"] == final["compatible"]
        ):
            del final["latest"]

        if (
            final.get("compatible") is not None
            and final.get("unchanged") is not None
            and final["compatible"] == final["unchanged"]
        ):
            del final["compatible"]

        # if (
        #     final.get("unchanged") is not None
        #     and final["unchanged"] == self._version
        # ):
        #     del final["unchanged"]

        return {
            key: str(value)
            for key, value in final.items()
            if value is not None
        }

    def inspect(self, req):
        """
        Lookup requirement versions in index

        The aggregated result can be retrieved by calling ``.result()``

        Parameters:
          req (Requirement):
            The requirement to look up
        """
        # we want a specifically typed copy
        if PipIndex.is_available():
            req = _pkg_resources.Requirement.parse(str(req))
        else:
            req = _pkg_index.Requirement.parse(str(req))

        self._update_field("unchanged", req)
        self._find_compat(req)
        self._find_latest(req)

    def _find_compat(self, req):
        """
        Find compatible version

        Parameters:
          req (Requirement):
            The requirement to look up
        """
        req = req.parse(str(req))  # copy

        # We turn == into ~=, which finds compatible versions
        # (last digit update)
        specs = []
        for spec in req.specifier:
            operator = spec.operator
            if operator == "==":
                operator = "~="
            specs.append("%s%s" % (operator, spec.version))

        try:
            # pylint: disable = unnecessary-dunder-call
            req.specifier.__init__(",".join(specs), prereleases=False)
        except _pkg_resources.packaging.specifiers.InvalidSpecifier:
            logger.debug("Invalid compat %r", ",".join(specs))
            return

        logger.debug("Valid compat %r", str(req))
        self._update_field("compatible", req)

    def _find_latest(self, req):
        """
        Find latest version

        Parameters:
          req (Requirement):
            The requirement to look up
        """
        req = req.parse(str(req))  # copy

        spec = None if self._version is None else ">=%s" % (self._version,)
        # pylint: disable = unnecessary-dunder-call
        req.specifier.__init__(spec or "", prereleases=False)

        correction = self._update_field("latest", req)
        if correction:
            correction = "< %s" % (correction,)
            spec = ", ".join((spec, correction)) if spec else correction
            req.specifier.__init__(spec or "", prereleases=False)
            self._update_field("latest", req)

    def _update_field(self, field, req):
        """
        Update version info field

        Parameters:
          field (str):
            The field to update

          req (Requirement):
            The requirement to evaluate
        """
        dist = self._index.lookup(req.parse(str(req)))
        if dist is None:
            return None

        dist_version = _parse.version(dist.version)
        if dist_version.is_prerelease:
            return dist_version

        if (self._version is None or dist_version >= self._version) and (
            self._result.get(field) is None
            or dist_version > self._result[field]
        ):
            self._result[field] = dist_version

        return None