spdx_tools.spdx3.clitools.pyspdxtools3

 1# SPDX-FileCopyrightText: 2023 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4import sys
 5
 6import click
 7from beartype.typing import List
 8
 9from spdx_tools.spdx3.bump_from_spdx2.spdx_document import bump_spdx_document
10from spdx_tools.spdx3.payload import Payload
11from spdx_tools.spdx3.writer.console.payload_writer import write_payload as write_payload_to_console
12from spdx_tools.spdx3.writer.json_ld.json_ld_writer import write_payload
13from spdx_tools.spdx.model.document import Document
14from spdx_tools.spdx.parser.parse_anything import parse_file
15from spdx_tools.spdx.validation.document_validator import validate_full_spdx_document
16from spdx_tools.spdx.validation.validation_message import ValidationMessage
17
18
19@click.command()
20@click.option(
21    "--infile", "-i", prompt="input file path", help="The file containing the document to be validated or converted."
22)
23@click.option(
24    "--outfile",
25    "-o",
26    help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion)."
27    "For now only a prototype serialization to json-ld is available. The provided file will therefore be extended "
28    "with `.jsonld`.",
29)
30@click.option(
31    "--version",
32    help='The SPDX version to be used during parsing and validation (format "SPDX-2.3").',
33    default="SPDX-2.3",
34)
35@click.option("--novalidation", is_flag=True, help="Don't validate the provided document.")
36def main(infile: str, outfile: str, version: str, novalidation: bool):
37    """
38    CLI-tool to parse and validate a SPDX 2.x document and migrate it into the prototype of SPDX 3.0. As there is no
39    definition for a serialization yet output can only be written to stdout.
40    To use, run: 'pyspdxtools3 --infile <input file name> -o -'
41    """
42    try:
43        document: Document = parse_file(infile)
44
45        if not novalidation:
46            validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, version)
47            if validation_messages:
48                print("The document is invalid. The following issues have been found:", file=sys.stderr)
49                for message in validation_messages:
50                    print(message.validation_message, file=sys.stderr)
51                sys.exit(1)
52            else:
53                print("The document is valid.", file=sys.stderr)
54        if outfile:
55            payload: Payload = bump_spdx_document(document)
56            if outfile == "-":
57                write_payload_to_console(payload, sys.stdout)
58            else:
59                write_payload(payload, outfile)
60
61    except NotImplementedError as err:
62        print(err.args[0])
63        sys.exit(1)
64
65
66if __name__ == "__main__":
67    main()
main = <Command main>

CLI-tool to parse and validate a SPDX 2.x document and migrate it into the prototype of SPDX 3.0. As there is no definition for a serialization yet output can only be written to stdout. To use, run: 'pyspdxtools3 --infile -o -'