From ab93a367c3804da4f5800119ec7d97c7a06a6732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armin=20T=C3=A4nzer?= Date: Wed, 25 Jan 2023 15:35:18 +0100 Subject: [PATCH 1/2] [issue-442] change CLI to read version from document MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit also print validation output to stderr Signed-off-by: Armin Tänzer --- src/spdx/clitools/pyspdxtools.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/spdx/clitools/pyspdxtools.py b/src/spdx/clitools/pyspdxtools.py index 1f98b624a..d292d004f 100644 --- a/src/spdx/clitools/pyspdxtools.py +++ b/src/spdx/clitools/pyspdxtools.py @@ -26,7 +26,7 @@ @click.command() @click.option("--infile", "-i", prompt="input file path", help="The file containing the document to be validated or converted.") @click.option("--outfile", "-o", help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion).") -@click.option("--version", help='The SPDX version to be used during parsing and validation (format "SPDX-2.3").', default="SPDX-2.3") +@click.option("--version", help='The SPDX version to be used during parsing and validation (format "SPDX-2.3"). Will be read from the document if not provided.', default=None) @click.option("--novalidation", is_flag=True, help="Don't validate the provided document.") def main(infile: str, outfile: str, version: str, novalidation: bool): """ @@ -39,27 +39,29 @@ def main(infile: str, outfile: str, version: str, novalidation: bool): if outfile == "-": tagvalue_writer.write_document(document, sys.stdout) - print("") if not novalidation: + if not version: + version = document.creation_info.spdx_version + validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, version) if validation_messages: - print("The document is invalid. The following issues have been found:") + print("The document is invalid. The following issues have been found:", file=sys.stderr) for message in validation_messages: - print(message.validation_message) + print(message.validation_message, file=sys.stderr) sys.exit(1) else: - print("The document is valid.") + print("The document is valid.", file=sys.stderr) if outfile and outfile != "-": write_file(document, outfile, validate=False) except NotImplementedError as err: - print(err.args[0]) + print(err.args[0], file=sys.stderr) print("Please note that this project is currently undergoing a major refactoring and therefore missing " "a few features which will be added in time (refer to https://github.com/spdx/tools-python/issues " "for insights into the current status).\n" - "In the meantime, please use the PyPI release version 0.7.0.") + "In the meantime, please use the PyPI release version 0.7.0.", file=sys.stderr) sys.exit(1) From c85a0252638193e4d3243541cf575f39fd2026b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armin=20T=C3=A4nzer?= Date: Thu, 26 Jan 2023 08:52:54 +0100 Subject: [PATCH 2/2] [issue-442, review] except SPDXParsingError and delete CLI prompt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Armin Tänzer --- src/spdx/clitools/pyspdxtools.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/spdx/clitools/pyspdxtools.py b/src/spdx/clitools/pyspdxtools.py index d292d004f..695cf99e8 100644 --- a/src/spdx/clitools/pyspdxtools.py +++ b/src/spdx/clitools/pyspdxtools.py @@ -16,6 +16,7 @@ import click from spdx.model.document import Document +from spdx.parser.error import SPDXParsingError from spdx.parser.parse_anything import parse_file from spdx.validation.document_validator import validate_full_spdx_document from spdx.validation.validation_message import ValidationMessage @@ -24,7 +25,7 @@ @click.command() -@click.option("--infile", "-i", prompt="input file path", help="The file containing the document to be validated or converted.") +@click.option("--infile", "-i", help="The file containing the document to be validated or converted.") @click.option("--outfile", "-o", help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion).") @click.option("--version", help='The SPDX version to be used during parsing and validation (format "SPDX-2.3"). Will be read from the document if not provided.', default=None) @click.option("--novalidation", is_flag=True, help="Don't validate the provided document.") @@ -64,6 +65,12 @@ def main(infile: str, outfile: str, version: str, novalidation: bool): "In the meantime, please use the PyPI release version 0.7.0.", file=sys.stderr) sys.exit(1) + except SPDXParsingError as err: + print("There have been issues while parsing the provided document:", file=sys.stderr) + for message in err.get_messages(): + print(message, file=sys.stderr) + sys.exit(1) + if __name__ == "__main__": main()