|
| 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) |
0 commit comments