|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# http://nexb.com and https://github.com/aboutcode-org/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/aboutcode-org/scancode.io for support and download. |
| 22 | + |
| 23 | +import difflib |
| 24 | + |
| 25 | + |
| 26 | +def get_expected_purls(project): |
| 27 | + """ |
| 28 | + Load the expected Package URLs (PURLs) from the project's input files. |
| 29 | +
|
| 30 | + A file is considered an expected PURLs source if: |
| 31 | + - Its filename ends with ``*purls.txt``, or |
| 32 | + - Its download URL includes the "#purls" tag. |
| 33 | +
|
| 34 | + Each line in the file should contain one PURL. Returns a sorted, |
| 35 | + deduplicated list of PURLs. Raises an exception if no input is found. |
| 36 | + """ |
| 37 | + purls_files = list(project.inputs("*purls.txt")) |
| 38 | + purls_files.extend( |
| 39 | + [input.path for input in project.inputsources.filter(tag="purls")] |
| 40 | + ) |
| 41 | + |
| 42 | + expected_purls = [] |
| 43 | + for file_path in purls_files: |
| 44 | + expected_purls.extend(file_path.read_text().splitlines()) |
| 45 | + |
| 46 | + if not expected_purls: |
| 47 | + raise Exception("Expected PURLs not provided.") |
| 48 | + |
| 49 | + return sorted(set(expected_purls)) |
| 50 | + |
| 51 | + |
| 52 | +def compare_purls(project, expected_purls): |
| 53 | + """ |
| 54 | + Compare discovered project PURLs against the expected PURLs. |
| 55 | +
|
| 56 | + Returns only the differences: |
| 57 | + - Lines starting with '-' are missing from the project. |
| 58 | + - Lines starting with '+' are unexpected in the project. |
| 59 | + """ |
| 60 | + project_packages = project.discoveredpackages.only_package_url_fields() |
| 61 | + sorted_unique_purls = sorted({package.purl for package in project_packages}) |
| 62 | + |
| 63 | + diff_result = difflib.ndiff(sorted_unique_purls, expected_purls) |
| 64 | + |
| 65 | + # Keep only lines that are diffs (- or +) |
| 66 | + filtered_diff = [line for line in diff_result if line.startswith(("-", "+"))] |
| 67 | + |
| 68 | + return filtered_diff |
0 commit comments