|
| 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 | +import sys |
| 24 | +from collections import defaultdict |
| 25 | + |
| 26 | +from scanpipe.management.commands import ProjectCommand |
| 27 | +from scanpipe.models import PACKAGE_URL_FIELDS |
| 28 | + |
| 29 | + |
| 30 | +class Command(ProjectCommand): |
| 31 | + help = ( |
| 32 | + "Check for compliance issues in Project. Exit with a non-zero status if " |
| 33 | + "compliance issues are present in the project." |
| 34 | + "The compliance alert indicates how the license expression complies with " |
| 35 | + "provided policies." |
| 36 | + ) |
| 37 | + |
| 38 | + def add_arguments(self, parser): |
| 39 | + super().add_arguments(parser) |
| 40 | + parser.add_argument( |
| 41 | + "--fail-level", |
| 42 | + default="ERROR", |
| 43 | + choices=["ERROR", "WARNING", "MISSING"], |
| 44 | + help=( |
| 45 | + "Compliance alert level that will cause the command to exit with a " |
| 46 | + "non-zero status. Default is ERROR." |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + def handle(self, *args, **options): |
| 51 | + super().handle(*args, **options) |
| 52 | + fail_level = options["fail_level"] |
| 53 | + total_compliance_issues_count = 0 |
| 54 | + |
| 55 | + package_qs = self.project.discoveredpackages.compliance_issues( |
| 56 | + severity=fail_level |
| 57 | + ).only(*PACKAGE_URL_FIELDS, "compliance_alert") |
| 58 | + |
| 59 | + resource_qs = self.project.codebaseresources.compliance_issues( |
| 60 | + severity=fail_level |
| 61 | + ).only("path", "compliance_alert") |
| 62 | + |
| 63 | + queryset_mapping = { |
| 64 | + "Package": package_qs, |
| 65 | + "Resource": resource_qs, |
| 66 | + } |
| 67 | + |
| 68 | + results = {} |
| 69 | + for label, queryset in queryset_mapping.items(): |
| 70 | + compliance_issues = defaultdict(list) |
| 71 | + for instance in queryset: |
| 72 | + compliance_issues[instance.compliance_alert].append(str(instance)) |
| 73 | + total_compliance_issues_count += 1 |
| 74 | + if compliance_issues: |
| 75 | + results[label] = dict(compliance_issues) |
| 76 | + |
| 77 | + if not total_compliance_issues_count: |
| 78 | + sys.exit(0) |
| 79 | + |
| 80 | + if self.verbosity > 0: |
| 81 | + msg = [ |
| 82 | + f"{total_compliance_issues_count} compliance issues detected on " |
| 83 | + f"this project." |
| 84 | + ] |
| 85 | + for label, issues in results.items(): |
| 86 | + msg.append(f"{label}:") |
| 87 | + for severity, entries in issues.items(): |
| 88 | + msg.append(f" - {severity}: {len(entries)}") |
| 89 | + |
| 90 | + self.stderr.write("\n".join(msg)) |
| 91 | + |
| 92 | + sys.exit(1) |
0 commit comments