Skip to content

Commit d596d5b

Browse files
committed
[review] use --graph as flag and save the generated graph to --outfile
Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
1 parent e1ee8d2 commit d596d5b

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ instead of `bin`.
8686
relationships. The graph can be rendered to a picture. Below is an example for the file `tests/data/formats/SPDXJSONExample-v2.3.spdx.json`:
8787
![SPDXJSONExample-v2.3.spdx.png](assets/SPDXJSONExample-v2.3.spdx.png)
8888
* Make sure you install the optional dependencies `networkx` and `pygraphviz`. To do so run `pip install ".[graph_generation]"`.
89-
* Use `pyspdxtools -i <input_file> --generate_graph <output_file>` where `<output_file>` is an output file name with valid format for `pygraphviz` (check
89+
* Use `pyspdxtools -i <input_file> --graph -o <output_file>` where `<output_file>` is an output file name with valid format for `pygraphviz` (check
9090
the documentation [here](https://pygraphviz.github.io/documentation/stable/reference/agraph.html#pygraphviz.AGraph.draw)).
9191
* If you are using a source distribution, try running
92-
`pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json --generate_graph SPDXJSONExample-v2.3.spdx.png` to generate
92+
`pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json --graph -o SPDXJSONExample-v2.3.spdx.png` to generate
9393
a png with an overview of the structure of the example file.
9494

9595
## Library usage

src/spdx/clitools/pyspdxtools.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
@click.option(
3434
"--outfile",
3535
"-o",
36-
help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion).",
36+
help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion). "
37+
"If you add the option --graph to the command the generated graph will be written to this file.",
3738
)
3839
@click.option(
3940
"--version",
@@ -43,12 +44,14 @@
4344
)
4445
@click.option("--novalidation", is_flag=True, help="Don't validate the provided document.")
4546
@click.option(
46-
"--generate_graph",
47-
default="",
48-
help="The file to write the structure of the spdx document as a pygraphviz AGraph to. Note: You need to install"
49-
" the optional dependencies 'networkx' and 'pygraphviz' for this feature.",
47+
"--graph",
48+
is_flag=True,
49+
default=False,
50+
help="Generate a relationship graph from the input file. "
51+
"The generated graph is saved to the file specified with --outfile. "
52+
"Note: You need to install the optional dependencies 'networkx' and 'pygraphviz' for this feature.",
5053
)
51-
def main(infile: str, outfile: str, version: str, novalidation: bool, generate_graph: str):
54+
def main(infile: str, outfile: str, version: str, novalidation: bool, graph: bool):
5255
"""
5356
CLI-tool for validating SPDX documents and converting between RDF, TAG-VALUE, JSON, YAML and XML formats.
5457
Formats are determined by the file endings.
@@ -57,9 +60,6 @@ def main(infile: str, outfile: str, version: str, novalidation: bool, generate_g
5760
try:
5861
document: Document = parse_file(infile)
5962

60-
if outfile == "-":
61-
tagvalue_writer.write_document(document, sys.stdout)
62-
6363
if not novalidation:
6464
if not version:
6565
version = document.creation_info.spdx_version
@@ -79,19 +79,22 @@ def main(infile: str, outfile: str, version: str, novalidation: bool, generate_g
7979
else:
8080
logging.info("The document is valid.")
8181

82-
if outfile and outfile != "-":
83-
write_file(document, outfile, validate=False)
82+
if outfile == "-":
83+
tagvalue_writer.write_document(document, sys.stdout)
8484

85-
if generate_graph:
85+
elif graph:
8686
try:
87-
export_graph_from_document(document, generate_graph)
87+
export_graph_from_document(document, outfile)
8888
except ImportError:
8989
logging.error(
9090
"To be able to draw a relationship graph of the parsed document "
9191
"you need to install 'networkx' and 'pygraphviz'. Run 'pip install \".[graph_generation]\"'."
9292
)
9393
sys.exit(1)
9494

95+
elif outfile:
96+
write_file(document, outfile, validate=False)
97+
9598
except NotImplementedError as err:
9699
logging.error(
97100
err.args[0]

0 commit comments

Comments
 (0)