|
| 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 pathlib import Path |
| 24 | +from timeit import default_timer as timer |
| 25 | + |
| 26 | +from django.core.management import CommandError |
| 27 | +from django.core.management.base import BaseCommand |
| 28 | + |
| 29 | +import xlsxwriter |
| 30 | + |
| 31 | +from aboutcode.pipeline import humanize_time |
| 32 | +from scanpipe.models import Project |
| 33 | +from scanpipe.pipes import filename_now |
| 34 | +from scanpipe.pipes import output |
| 35 | + |
| 36 | + |
| 37 | +class Command(BaseCommand): |
| 38 | + help = "Report of selected projects." |
| 39 | + |
| 40 | + def add_arguments(self, parser): |
| 41 | + super().add_arguments(parser) |
| 42 | + parser.add_argument( |
| 43 | + "--output-directory", |
| 44 | + help=( |
| 45 | + "The path to the directory where the report file will be created. " |
| 46 | + "If not provided, the report file will be created in the current " |
| 47 | + "working directory." |
| 48 | + ), |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "--sheet", |
| 52 | + required=True, |
| 53 | + choices=list(output.object_type_to_model_name.keys()), |
| 54 | + help="Specifies the sheet to include in the XLSX report.", |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "--search", |
| 58 | + help="Select projects searching for the provided string in their name.", |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "--label", |
| 62 | + action="append", |
| 63 | + dest="labels", |
| 64 | + default=list(), |
| 65 | + help=( |
| 66 | + "Filter projects by the provided label(s). Multiple labels can be " |
| 67 | + "provided by using this argument multiple times." |
| 68 | + ), |
| 69 | + ) |
| 70 | + |
| 71 | + def handle(self, *args, **options): |
| 72 | + start_time = timer() |
| 73 | + self.verbosity = options["verbosity"] |
| 74 | + |
| 75 | + output_directory = options["output_directory"] |
| 76 | + labels = options["labels"] |
| 77 | + search = options["search"] |
| 78 | + sheet = options["sheet"] |
| 79 | + model_name = output.object_type_to_model_name.get(sheet) |
| 80 | + |
| 81 | + if not (labels or search): |
| 82 | + raise CommandError( |
| 83 | + "You must provide either --label or --search to select projects." |
| 84 | + ) |
| 85 | + |
| 86 | + project_qs = Project.objects.all() |
| 87 | + if labels: |
| 88 | + project_qs = project_qs.filter(labels__name__in=labels) |
| 89 | + if search: |
| 90 | + project_qs = project_qs.filter(name__icontains=search) |
| 91 | + project_count = project_qs.count() |
| 92 | + |
| 93 | + if not project_count: |
| 94 | + raise CommandError("No projects found for the provided criteria.") |
| 95 | + |
| 96 | + if self.verbosity > 0: |
| 97 | + msg = f"{project_count} project(s) will be included in the report." |
| 98 | + self.stdout.write(msg, self.style.SUCCESS) |
| 99 | + |
| 100 | + worksheet_queryset = output.get_queryset(project=None, model_name=model_name) |
| 101 | + worksheet_queryset = worksheet_queryset.filter(project__in=project_qs) |
| 102 | + |
| 103 | + filename = f"scancodeio-report-{filename_now()}.xlsx" |
| 104 | + if output_directory: |
| 105 | + output_file = Path(f"{output_directory}/{filename}") |
| 106 | + else: |
| 107 | + output_file = Path(filename) |
| 108 | + |
| 109 | + with xlsxwriter.Workbook(output_file) as workbook: |
| 110 | + output.queryset_to_xlsx_worksheet( |
| 111 | + worksheet_queryset, |
| 112 | + workbook, |
| 113 | + exclude_fields=output.XLSX_EXCLUDE_FIELDS, |
| 114 | + prepend_fields=["project"], |
| 115 | + worksheet_name="TODOS", |
| 116 | + ) |
| 117 | + |
| 118 | + run_time = timer() - start_time |
| 119 | + if self.verbosity > 0: |
| 120 | + msg = f"Report generated at {output_file} in {humanize_time(run_time)}." |
| 121 | + self.stdout.write(msg, self.style.SUCCESS) |
0 commit comments