|
| 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 | +from django.core.management import CommandError |
| 24 | + |
| 25 | +from scanpipe.management.commands import ProjectCommand |
| 26 | + |
| 27 | + |
| 28 | +class Command(ProjectCommand): |
| 29 | + help = "Verify project analysis results against expected counts" |
| 30 | + |
| 31 | + def add_arguments(self, parser): |
| 32 | + super().add_arguments(parser) |
| 33 | + parser.add_argument( |
| 34 | + "--packages", |
| 35 | + type=int, |
| 36 | + default=0, |
| 37 | + help="Minimum number of packages expected (default: 0)", |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + "--vulnerable-packages", |
| 41 | + type=int, |
| 42 | + default=0, |
| 43 | + help="Minimum number of vulnerable packages expected (default: 0)", |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--dependencies", |
| 47 | + type=int, |
| 48 | + default=0, |
| 49 | + help="Minimum number of dependencies expected (default: 0)", |
| 50 | + ) |
| 51 | + parser.add_argument( |
| 52 | + "--vulnerable-dependencies", |
| 53 | + type=int, |
| 54 | + default=0, |
| 55 | + help="Minimum number of vulnerable dependencies expected (default: 0)", |
| 56 | + ) |
| 57 | + |
| 58 | + def handle(self, *args, **options): |
| 59 | + super().handle(*args, **options) |
| 60 | + |
| 61 | + expected_packages = options["packages"] |
| 62 | + expected_vulnerable_packages = options["vulnerable_packages"] |
| 63 | + expected_dependencies = options["dependencies"] |
| 64 | + expected_vulnerable_dependencies = options["vulnerable_dependencies"] |
| 65 | + |
| 66 | + project = self.project |
| 67 | + packages = project.discoveredpackages |
| 68 | + package_count = packages.count() |
| 69 | + vulnerable_package_count = packages.vulnerable().count() |
| 70 | + dependencies = project.discovereddependencies.all() |
| 71 | + dependency_count = dependencies.count() |
| 72 | + vulnerable_dependency_count = dependencies.vulnerable().count() |
| 73 | + |
| 74 | + errors = [] |
| 75 | + |
| 76 | + if package_count < expected_packages: |
| 77 | + errors.append( |
| 78 | + f"Expected at least {expected_packages} packages, found {package_count}" |
| 79 | + ) |
| 80 | + if vulnerable_package_count < expected_vulnerable_packages: |
| 81 | + errors.append( |
| 82 | + f"Expected at least {expected_vulnerable_packages} vulnerable packages," |
| 83 | + f" found {vulnerable_package_count}" |
| 84 | + ) |
| 85 | + if dependency_count < expected_dependencies: |
| 86 | + errors.append( |
| 87 | + f"Expected at least {expected_dependencies} dependencies, " |
| 88 | + f"found {dependency_count}" |
| 89 | + ) |
| 90 | + if vulnerable_dependency_count < expected_vulnerable_dependencies: |
| 91 | + errors.append( |
| 92 | + f"Expected at least {expected_vulnerable_dependencies} " |
| 93 | + f"vulnerable dependencies, found {vulnerable_dependency_count}" |
| 94 | + ) |
| 95 | + |
| 96 | + if errors: |
| 97 | + raise CommandError("Project verification failed:\n" + "\n".join(errors)) |
| 98 | + |
| 99 | + self.stdout.write("Project verification passed.", self.style.SUCCESS) |
0 commit comments