Skip to content

Commit f66e306

Browse files
authored
Refine readability in resolve_pypi_packages and add logging #1598 (#1647)
Signed-off-by: tdruez <[email protected]>
1 parent 6b8871a commit f66e306

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

scanpipe/pipes/resolve.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121
# Visit https://github.com/aboutcode-org/scancode.io for support and download.
2222

2323
import json
24+
import logging
2425
import sys
2526
import uuid
2627
from pathlib import Path
2728

2829
from django.core.exceptions import MultipleObjectsReturned
2930
from django.core.exceptions import ObjectDoesNotExist
3031

32+
import python_inspector.api as python_inspector
3133
from attributecode.model import About
3234
from packagedcode import APPLICATION_PACKAGE_DATAFILE_HANDLERS
3335
from packagedcode.licensing import get_license_detections_and_expression
3436
from packageurl import PackageURL
35-
from python_inspector.api import resolve_dependencies
3637

3738
from scanpipe.models import DiscoveredDependency
3839
from scanpipe.models import DiscoveredPackage
@@ -46,6 +47,8 @@
4647
Resolve packages from manifest, lockfile, and SBOM.
4748
"""
4849

50+
logger = logging.getLogger("scanpipe.pipes")
51+
4952

5053
def resolve_manifest_resources(resource, package_registry):
5154
"""Get package data from resource."""
@@ -164,17 +167,22 @@ def get_packages_from_manifest(input_location, package_registry=None):
164167
Resolve packages or get packages data from a package manifest file/
165168
lockfile/SBOM at `input_location`.
166169
"""
170+
logger.info(f"> Get packages from manifest: {input_location}")
167171
default_package_type = get_default_package_type(input_location)
168172
# we only try to resolve packages if file at input_location is
169173
# a package manifest, and ignore for other files
170174
if not default_package_type:
175+
logger.info(" Package type not found.")
171176
return
172177

173178
# Get resolvers for available packages/SBOMs in the registry
174179
resolver = package_registry.get(default_package_type)
175180
if resolver:
181+
logger.info(f" Using resolver={resolver.__name__}")
176182
resolved_packages = resolver(input_location=input_location)
177183
return resolved_packages
184+
else:
185+
logger.info(f" No resolvers available for type={default_package_type}")
178186

179187

180188
def get_manifest_resources(project):
@@ -188,19 +196,23 @@ def get_manifest_resources(project):
188196

189197

190198
def resolve_pypi_packages(input_location):
191-
"""Resolve the PyPI packages from the `input_location` requirements file."""
199+
"""Resolve the PyPI packages from the ``input_location`` requirements file."""
192200
python_version = f"{sys.version_info.major}{sys.version_info.minor}"
193201
operating_system = "linux"
194202

195-
inspector_output = resolve_dependencies(
203+
resolution_output = python_inspector.resolve_dependencies(
196204
requirement_files=[input_location],
197205
python_version=python_version,
198206
operating_system=operating_system,
207+
# Prefer source distributions over binary distributions,
208+
# if no source distribution is available then binary distributions are used.
199209
prefer_source=True,
210+
# Activate the verbosity and send it to the logger.
211+
verbose=True,
212+
printer=logger.info,
200213
)
201214

202-
packages = inspector_output.packages
203-
215+
packages = resolution_output.packages
204216
# python-inspector returns the `extracted_license_statement` under the
205217
# `declared_license` field.
206218
for package in packages:

scanpipe/tests/pipes/test_resolve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_scanpipe_pipes_resolve_get_packages_from_manifest(self):
113113
}
114114
self.assertEqual([expected], packages)
115115

116-
@mock.patch("scanpipe.pipes.resolve.resolve_dependencies")
116+
@mock.patch("scanpipe.pipes.resolve.python_inspector.resolve_dependencies")
117117
def test_scanpipe_pipes_resolve_resolve_pypi_packages(self, mock_resolve):
118118
# Generated with:
119119
# $ python-inspector --python-version 3.12 --operating-system linux \

scanpipe/tests/test_pipelines.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ def test_scanpipe_resolve_dependencies_pipeline_integration_empty_manifest(self)
13021302
expected = "No packages could be resolved"
13031303
self.assertIn(expected, message.description)
13041304

1305-
@mock.patch("scanpipe.pipes.resolve.resolve_dependencies")
1305+
@mock.patch("scanpipe.pipes.resolve.python_inspector.resolve_dependencies")
13061306
def test_scanpipe_resolve_dependencies_pipeline_integration_misc(
13071307
self, mock_resolve_dependencies
13081308
):
@@ -1323,7 +1323,7 @@ def test_scanpipe_resolve_dependencies_pipeline_integration_misc(
13231323
self.assertEqual(0, exitcode, msg=out)
13241324
self.assertEqual(1, project1.discoveredpackages.count())
13251325

1326-
@mock.patch("scanpipe.pipes.resolve.resolve_dependencies")
1326+
@mock.patch("scanpipe.pipes.resolve.python_inspector.resolve_dependencies")
13271327
def test_scanpipe_resolve_dependencies_pipeline_pypi_integration(
13281328
self, mock_resolve_dependencies
13291329
):

0 commit comments

Comments
 (0)