Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 11 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,22 @@ This is the result of an initial GSoC contribution by @[ah450](https://github.co

## Command-line usage:

1. **PARSER** (for parsing any format):
1. **PARSING/VALIDATING** (for parsing any format):

* Use `pyspdxtools_parser --file <filename>` where `<filename>` is the location of the file.
If you are using a source distribution, try running: `pyspdxtools_parser --file tests/data/formats/SPDXRdfExample.rdf`.
* Use `pyspdxtools -i <filename>` where `<filename>` is the location of the file.
If you are using a source distribution, try running: `pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json`.

* Or you can use `pyspdxtools_parser` only, and it will automatically prompt/ask for `filename`.
* Or you can use `pyspdxtools` only, and it will automatically prompt/ask for the `input file path`.

* For help use `pyspdxtools_parser --help`
2. **CONVERTING** (for converting one format to another):

* Use `pyspdxtools -i <input_file> -o <output_file>` where `<input_file>` is the location of the file to be converted
and `<output_file>` is the location of the output file. The output format is inferred automatically from the file ending.
If you are using a source distribution, try running : `pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json -o output.tag`

2. **CONVERTOR** (for converting one format to another):

* If I/O formats are known:

* Use `pyspdxtools_convertor --infile/-i <input_file> --outfile/-o <output_file>` where `<input_file>` is the location of the file to be converted
and `<output_file>` is the location of the output file.
If you are using a source distribution, try running : `pyspdxtools_convertor --infile tests/data/formats/SPDXRdfExample.rdf --outfile output.json`

* If I/O formats are not known:

* Use `pyspdxtools_convertor --from/-f <input_format> <input_file> --to/-t <output_format> <output_file>` where `<input_format>` is the manually entered format of the input file
and `<out_format>` is the manually entered format of the output file.
If you are using a source distribution, try running : `pyspdxtools_convertor --from tag tests/data/formats/SPDXTagExample.in --to yaml output.out`

* If one of the formats is known and the other is not, you can use a mixture of the above two points.
Example (if you are using a source distribution): `pyspdxtools_convertor -f rdf tests/data/formats/SPDXRdfExample.xyz -o output.xml`

* For help use `pyspdxtools_convertor --help`
* If you want to skip the validation process, provide the `--novalidation` flag, like so:
`pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json -o output.tag --novalidation`
* For help use `pyspdxtools --help`

# Installation

Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ dynamic = ["version"]
test = ["pytest"]

[project.scripts]
pyspdxtools_convertor = "src.clitools.convertor:main"
pyspdxtools_parser = "src.clitools.parser:main"
pyspdxtools = "src.clitools.pyspdxtools:main"

[tool.setuptools]
zip-safe = false # because of the uses of __file__: https://github.com/spdx/tools-python/issues/257
Expand Down
110 changes: 0 additions & 110 deletions src/clitools/convertor.py

This file was deleted.

40 changes: 0 additions & 40 deletions src/clitools/parser.py

This file was deleted.

67 changes: 67 additions & 0 deletions src/clitools/pyspdxtools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

# Copyright (c) 2020 Yash Varshney
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from typing import List

import click

from src.model.document import Document
from src.parser.parse_anything import parse_file
from src.validation.document_validator import validate_full_spdx_document
from src.validation.validation_message import ValidationMessage
from src.writer.tagvalue import tagvalue_writer
from src.writer.write_anything import write_file


@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("--novalidation", is_flag=True, help="Don't validate the provided document.")
def main(infile: str, outfile: str, version: str, novalidation: bool):
"""
CLI-tool for validating SPDX documents and converting between RDF, TAG-VALUE, JSON, YAML and XML formats.
Formats are determined by the file endings.
To use, run: 'pyspdxtools --infile <input file name> --outfile <output file name>'
"""
try:
document: Document = parse_file(infile)

if outfile == "-":
tagvalue_writer.write_document(document, sys.stdout)
print("")

if not novalidation:
validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, version)
if validation_messages:
print("The document is invalid. The following issues have been found:")
for message in validation_messages:
print(message.validation_message)
sys.exit(1)
else:
print("The document is valid.")

if outfile and outfile != "-":
write_file(document, outfile, validate=False)

except NotImplementedError as err:
print(err.args[0])
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.")
sys.exit(1)


if __name__ == "__main__":
main()
36 changes: 36 additions & 0 deletions src/formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2022 spdx contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from enum import Enum, auto

from src.parser.error import SPDXParsingError


class FileFormat(Enum):
JSON = auto()
YAML = auto()
XML = auto()
TAG_VALUE = auto()
RDF_XML = auto()


def file_name_to_format(file_name: str) -> FileFormat:
if file_name.endswith(".rdf") or file_name.endswith(".rdf.xml"):
return FileFormat.RDF_XML
elif file_name.endswith(".tag") or file_name.endswith(".spdx"):
return FileFormat.TAG_VALUE
elif file_name.endswith(".json"):
return FileFormat.JSON
elif file_name.endswith(".xml"):
return FileFormat.XML
elif file_name.endswith(".yaml") or file_name.endswith(".yml"):
return FileFormat.YAML
else:
raise SPDXParsingError(["Unsupported SPDX file type: " + str(file_name)])
26 changes: 26 additions & 0 deletions src/parser/parse_anything.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) spdx contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from src.formats import file_name_to_format, FileFormat
from src.parser.json.json_parser import JsonParser


def parse_file(file_name: str):
input_format = file_name_to_format(file_name)
if input_format == FileFormat.RDF_XML:
raise NotImplementedError("Currently, the rdf parser is not implemented")
elif input_format == FileFormat.TAG_VALUE:
raise NotImplementedError("Currently, the tag-value parser is not implemented")
elif input_format == FileFormat.JSON:
return JsonParser().parse(file_name)
elif input_format == FileFormat.XML:
raise NotImplementedError("Currently, the xml parser is not implemented")
elif input_format == FileFormat.YAML:
raise NotImplementedError("Currently, the yaml parser is not implemented")
28 changes: 28 additions & 0 deletions src/writer/write_anything.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2022 spdx contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from src.formats import file_name_to_format, FileFormat
from src.model.document import Document
from src.writer.json import json_writer
from src.writer.tagvalue import tagvalue_writer


def write_file(document: Document, file_name: str, validate: bool = True):
output_format = file_name_to_format(file_name)
if output_format == FileFormat.JSON:
json_writer.write_document(document, file_name, validate)
elif output_format == FileFormat.YAML:
raise NotImplementedError("Currently, the yaml writer is not implemented")
elif output_format == FileFormat.XML:
raise NotImplementedError("Currently, the xml writer is not implemented")
elif output_format == FileFormat.TAG_VALUE:
tagvalue_writer.write_document_to_file(document, file_name)
elif output_format == FileFormat.RDF_XML:
raise NotImplementedError("Currently, the rdf writer is not implemented")
Empty file removed tests/clitools/__init__.py
Empty file.
Loading