Skip to content

Commit 7dcbff5

Browse files
committed
Add a "Inspect Manifest" pipeline #284
Signed-off-by: Thomas Druez <[email protected]>
1 parent bace29e commit 7dcbff5

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

docs/built-in-pipelines.rst

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ Docker Windows Image Analysis
3131
:members:
3232
:member-order: bysource
3333

34+
.. _pipeline_inspect_manifest:
35+
36+
Inspect Manifest
37+
----------------
38+
.. autoclass:: scanpipe.pipelines.inspect_manifest.InspectManifest()
39+
:members:
40+
:member-order: bysource
41+
42+
.. _pipeline_load_inventory:
43+
3444
Load Inventory From Scan
3545
------------------------
3646
.. autoclass:: scanpipe.pipelines.load_inventory.LoadInventory()
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# http://nexb.com and https://github.com/nexB/scancode.io
4+
# The ScanCode.io software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode.io is provided as-is without warranties.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
16+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
17+
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
18+
# for any legal advice.
19+
#
20+
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/scancode.io for support and download.
22+
23+
from packagedcode import APPLICATION_PACKAGE_DATAFILE_HANDLERS
24+
from python_inspector.resolve_cli import resolver_api
25+
26+
from scanpipe.pipelines import Pipeline
27+
from scanpipe.pipes import update_or_create_package
28+
29+
30+
def resolve_pypi_packages(input_location):
31+
"""
32+
https://github.com/nexB/scancode-toolkit/blob/develop/requirements.txt
33+
https://raw.githubusercontent.com/nexB/python-inspector/main/requirements.txt
34+
"""
35+
inspector_output = resolver_api(requirement_files=[input_location])
36+
resolved_packages = inspector_output.packages
37+
return resolved_packages
38+
39+
40+
# `default_package_type`: resolver callable
41+
resolver_registry = {
42+
"pypi": resolve_pypi_packages,
43+
}
44+
45+
46+
def get_default_package_type(input_location):
47+
for handler in APPLICATION_PACKAGE_DATAFILE_HANDLERS:
48+
if handler.is_datafile(input_location):
49+
return handler.default_package_type
50+
51+
52+
class InspectManifest(Pipeline):
53+
"""
54+
A pipeline to inspect one or more manifest files and resolve its packages.
55+
"""
56+
57+
@classmethod
58+
def steps(cls):
59+
return (
60+
cls.get_manifest_inputs,
61+
cls.create_packages_from_manifest,
62+
)
63+
64+
def get_manifest_inputs(self):
65+
"""
66+
Locates all the manifest files from the project's input/ directory.
67+
"""
68+
self.input_locations = [
69+
str(input.absolute()) for input in self.project.inputs()
70+
]
71+
72+
def create_packages_from_manifest(self):
73+
"""
74+
Resolves manifest files into packages.
75+
"""
76+
for input_location in self.input_locations:
77+
default_package_type = get_default_package_type(input_location)
78+
79+
resolver = resolver_registry.get(default_package_type)
80+
if not resolver:
81+
raise Exception(f"No resolver for {default_package_type}")
82+
83+
resolved_packages = resolver(input_location=input_location)
84+
85+
if not resolved_packages:
86+
raise Exception("No packages could be resolved.")
87+
88+
for package_data in resolved_packages:
89+
update_or_create_package(self.project, package_data)

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ install_requires =
7575
commoncode==31.0.0
7676
# FetchCode
7777
fetchcode-container==1.2.3.210512; sys_platform == "linux"
78+
# Python-inspector
79+
python-inspector==0.8.2
7880
# Utilities
7981
XlsxWriter==3.0.3
8082
requests==2.28.1
@@ -106,6 +108,7 @@ console_scripts =
106108
scancodeio_pipelines =
107109
docker = scanpipe.pipelines.docker:Docker
108110
docker_windows = scanpipe.pipelines.docker_windows:DockerWindows
111+
inspect_manifest = scanpipe.pipelines.inspect_manifest:InspectManifest
109112
load_inventory = scanpipe.pipelines.load_inventory:LoadInventory
110113
root_filesystems = scanpipe.pipelines.root_filesystems:RootFS
111114
scan_codebase = scanpipe.pipelines.scan_codebase:ScanCodebase

0 commit comments

Comments
 (0)