Skip to content

Commit ebb8cc9

Browse files
committed
Use namespaced impors
* This help better understand what comes from where. * Also add missing ISC license Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 49df2cb commit ebb8cc9

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/python_inspector/resolution.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#
2-
# Copyright (c) nexB Inc. and others. All rights reserved.
3-
# ScanCode is a trademark of nexB Inc.
4-
# SPDX-License-Identifier: Apache-2.0
5-
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
2+
# Copyright (c) Tzu-ping Chung <uranusjr@gmail.com>, nexB Inc. and others.
3+
# SPDX-License-Identifier: ISC AND Apache-2.0
4+
# derived and heavily modified from https://github.com/sarugaku/resolvelib
5+
66
# See https://github.com/nexB/python-inspector for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
@@ -23,12 +23,7 @@
2323
from resolvelib.reporters import BaseReporter
2424

2525
from _packagedcode.pypi import PypiWheelHandler
26-
from python_inspector.utils_pypi import CACHE_THIRDPARTY_DIR
27-
from python_inspector.utils_pypi import PYPI_PUBLIC_REPO
28-
from python_inspector.utils_pypi import PYPI_SIMPLE_URL
29-
from python_inspector.utils_pypi import Environment
30-
from python_inspector.utils_pypi import PypiSimpleRepository
31-
from python_inspector.utils_pypi import download_wheel
26+
from python_inspector import utils_pypi
3227

3328
Candidate = collections.namedtuple("Candidate", "name version extras")
3429

@@ -63,6 +58,7 @@ def __init__(self, environment=None, repos=[]):
6358
self.dependencies_by_purl = {}
6459

6560
def identify(self, requirement_or_candidate):
61+
"""Given a requirement, return an identifier for it. Overridden."""
6662
name = packaging.utils.canonicalize_name(requirement_or_candidate.name)
6763
if requirement_or_candidate.extras:
6864
extras_str = ",".join(sorted(requirement_or_candidate.extras))
@@ -77,6 +73,8 @@ def get_preference(
7773
information,
7874
backtrack_causes,
7975
):
76+
"""Produce a sort key for given requirement based on preference. Overridden."""
77+
8078
transitive = all(p is not None for _, p in information[identifier])
8179
return transitive, identifier
8280

@@ -106,14 +104,16 @@ def get_requirements_for_package(self, purl, candidate):
106104
Yield requirements for a package.
107105
"""
108106
if self.repos and self.environment:
109-
wheels = download_wheel(
107+
wheels = utils_pypi.download_wheel(
110108
name=candidate.name,
111109
version=str(candidate.version),
112110
environment=self.environment,
113111
repos=self.repos,
114112
)
115113
for wheel in wheels:
116-
deps = list(PypiWheelHandler.parse(os.path.join(CACHE_THIRDPARTY_DIR, wheel)))
114+
deps = list(
115+
PypiWheelHandler.parse(os.path.join(utils_pypi.CACHE_THIRDPARTY_DIR, wheel))
116+
)
117117
assert len(deps) == 1
118118
deps = deps[0].dependencies
119119
for dep in deps:
@@ -133,7 +133,7 @@ def get_requirements_for_package(self, purl, candidate):
133133

134134
def get_candidates(self, all_versions, requirements, identifier, bad_versions, name, extras):
135135
"""
136-
Generate candidates for the given identifier.
136+
Generate candidates for the given identifier. Overridden.
137137
"""
138138
for version in all_versions:
139139
parsed_version = packaging.version.parse(version)
@@ -143,7 +143,7 @@ def get_candidates(self, all_versions, requirements, identifier, bad_versions, n
143143

144144
def _iter_matches(self, identifier, requirements, incompatibilities):
145145
"""
146-
Return a list of candidates for the given identifier.
146+
Yield candidates for the given identifier, requirements and incompatibilities
147147
"""
148148
name, _, _extras = identifier.partition("[")
149149
bad_versions = {c.version for c in incompatibilities[identifier]}
@@ -161,6 +161,7 @@ def _iter_matches(self, identifier, requirements, incompatibilities):
161161
)
162162

163163
def find_matches(self, identifier, requirements, incompatibilities):
164+
"""Find all possible candidates that satisfy given constraints. Overridden."""
164165
candidates = sorted(
165166
self._iter_matches(identifier, requirements, incompatibilities),
166167
key=operator.attrgetter("version"),
@@ -169,6 +170,7 @@ def find_matches(self, identifier, requirements, incompatibilities):
169170
return candidates
170171

171172
def is_satisfied_by(self, requirement, candidate):
173+
"""Whether the given requirement can be satisfied by a candidate. Overridden."""
172174
return candidate.version in requirement.specifier
173175

174176
def _iter_dependencies(self, candidate):
@@ -195,6 +197,7 @@ def _iter_dependencies(self, candidate):
195197
yield r
196198

197199
def get_dependencies(self, candidate):
200+
"""Get dependencies of a candidate. Overridden."""
198201
return list(self._iter_dependencies(candidate))
199202

200203

@@ -264,17 +267,17 @@ def format_resolution(results, as_tree=False):
264267
return dependencies
265268

266269

267-
def pypi_simple_repo_in_repos(repos: PypiSimpleRepository):
270+
def pypi_simple_repo_in_repos(repos: utils_pypi.PypiSimpleRepository):
268271
"""
269272
Return True if simple pypi index_url is present in any of the repos
270273
"""
271-
return any(repo.index_url == PYPI_SIMPLE_URL for repo in repos)
274+
return any(repo.index_url == utils_pypi.PYPI_SIMPLE_URL for repo in repos)
272275

273276

274277
def get_resolved_dependencies(
275278
requirements: List[Requirement],
276-
environment: Environment = None,
277-
repos: List[PypiSimpleRepository] = [],
279+
environment: utils_pypi.Environment = None,
280+
repos: List[utils_pypi.PypiSimpleRepository] = [],
278281
as_tree: bool = False,
279282
):
280283
"""
@@ -283,7 +286,7 @@ def get_resolved_dependencies(
283286
parent/children or a nested tree if ``as_tree`` is True
284287
"""
285288
if repos and not pypi_simple_repo_in_repos(repos):
286-
repos.append(PYPI_PUBLIC_REPO)
289+
repos.append(utils_pypi.PYPI_PUBLIC_REPO)
287290

288291
resolver = Resolver(
289292
provider=PythonInputProvider(environment, repos),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2018, Tzu-ping Chung <uranusjr@gmail.com>
2+
3+
Permission to use, copy, modify, and distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

0 commit comments

Comments
 (0)