Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-442] change CLI to read version from document #446

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/spdx/clitools/pyspdxtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,9 +25,9 @@


@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").', 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):
"""
Expand All @@ -39,27 +40,35 @@ 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)

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)


Expand Down