# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org># SPDX-FileCopyrightText: 2022 Pietro Albini <pietro.albini@ferrous-systems.com>## SPDX-License-Identifier: GPL-3.0-or-later"""Compilation of the SPDX Document."""importcontextlibimportloggingimportsysfromargparseimportArgumentParser,Namespacefromgettextimportgettextas_fromtypingimportIOfrom.import_IGNORE_SPDX_PATTERNSfrom._utilimportPathTypefrom.projectimportProjectfrom.reportimportProjectReport_LOGGER=logging.getLogger(__name__)
[docs]defadd_arguments(parser:ArgumentParser)->None:"""Add arguments to the parser."""parser.add_argument("--output","-o",dest="file",action="store",type=PathType("w"))parser.add_argument("--add-license-concluded",action="store_true",help=_("populate the LicenseConcluded field; note that reuse cannot ""guarantee the field is accurate"),)parser.add_argument("--creator-person",metavar="NAME",help=_("name of the person signing off on the SPDX report"),)parser.add_argument("--creator-organization",metavar="NAME",help=_("name of the organization signing off on the SPDX report"),)
[docs]defrun(args:Namespace,project:Project,out:IO[str]=sys.stdout)->int:"""Print the project's bill of materials."""# The SPDX spec mandates that a creator must be specified when a license# conclusion is made, so here we enforce that. More context:# https://github.com/fsfe/reuse-tool/issues/586#issuecomment-1310425706if(args.add_license_concludedandargs.creator_personisNoneandargs.creator_organizationisNone):args.parser.error(_("error: --creator-person=NAME or --creator-organization=NAME"" required when --add-license-concluded is provided"),)withcontextlib.ExitStack()asstack:ifargs.file:out=stack.enter_context(args.file.open("w",encoding="utf-8"))ifnotany(pattern.match(args.file.name)forpatternin_IGNORE_SPDX_PATTERNS):# pylint: disable=line-too-long_LOGGER.warning(_("'{path}' does not match a common SPDX file pattern. Find"" the suggested naming conventions here:"" https://spdx.github.io/spdx-spec/conformance/#44-standard-data-format-requirements").format(path=out.name))report=ProjectReport.generate(project,multiprocessing=notargs.no_multiprocessing,add_license_concluded=args.add_license_concluded,)out.write(report.bill_of_materials(creator_person=args.creator_person,creator_organization=args.creator_organization,))return0