Support for reading and writing SPDX 3.0.1 documents using spdx-python-model#898
Open
bd-jrobson wants to merge 1 commit into
Open
Support for reading and writing SPDX 3.0.1 documents using spdx-python-model#898bd-jrobson wants to merge 1 commit into
bd-jrobson wants to merge 1 commit into
Conversation
Signed-off-by: Johnathan Robson <jrobson@blackduck.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds reading and writing SPDX 3.0.1 documents to this library by using the spdx-python-model.
There are a lot of changes here, most of them are in the
src/spdx_tools/spdx3/modeldirectory. These are to make the model match SPDX 3.0.1, either by deleting classes that do not exist in it, adding new ones, or adjusting fields e.g.creation_info: Optional[CreationInfo]tocreation_info: CreationInfosince CreationInfo is mandatory orhomepagetohome_pageso that snake case to camel case conversion will work when converting between python naming and SPDX 3 naming.Much like #897, this uses the spdx-python-model for serialization and deserialization. It's quite a coincidence that we ended up creating MRs within a day or so of each other! That
SpdxObjectSetis a good idea!The code for converting between the models in this library and spdx-python-model is in the
src/spdx_tools/spdx3/bindingdirectory.shacl_to_spdx3_converter.pyhas aconvert_to_payloadmethod that accepts av3_0_1.SHACLObjectSetand converts it into aPayload.The
Spdx3ToSHACLConverterclass inspdx3_to_shacl_converter.pyhas aconvertmethod that accepts a Payload and converts it into av3_0_1.SHACLObjectSetThe
helpers.pyfile contains code used by both classes.How to use
Reading a file
To read a file, use
jsonld_parser.parse_from_fileto either parse from a string, or a file path. To use a sample of test code...This produces a
Payloadwhich already existed in the codebase.Writing a file
To write a SPDX 3 file in the jsonld format, which is the only one supported, use
json_ld_writer.write_payloadThis is the same way it would be done as the current code onmain. Another sample of test code..This code generates a
Payload, and useswrite_payloadto write it to a file.Creating a Payload via code
This is done the much the same way as it would be on
mainright now. Create your objects using the classes insrc/spdx_tools/spdx3/modeland add them to aPayload, with elements referencing other elements by SPDX ID.Bump and console writer
The existing "bump" classes and tests were updated to match the new SPDX 3 models.
The console writer code in
src/spdx_tools/spdx/writer/consolewas barely updated. A few writers that matched classes that no longer exist were removed, as were references to now removed fields, but new ones were not created to include the new classes, so these are incomplete.SPDX 3 test files
The three new SPDX 3 test documents in
tests/spdx3/datawere sourced from:With the last one being modified slightly. These are all CC0-1.0 according to the repository's README.
SPDX 2.3 RDF change
The change to
src/spdx_tools/spdx/writer/rdf/writer_utils.pyis because of what appears to be a bug with the Java SPDX library. This could be split off into its own MR, or directed towards the Java SPDX maintainers.This library was serializing a download location with a value of
noassertionas<spdx:downloadLocation rdf:resource="http://spdx.org/rdf/terms#noassertion"/>, which according an example in the the spec here at 7.7.3 https://spdx.github.io/spdx-spec/v2.3/package-information/#773-examples and this library, is a valid representation. This library allows a downloadLocation to have its noassertion/none value in either the rdf:resource attribute as a URI as above, or as the literal value of an element. i.e.<spdx:downloadLocation>NOASSERTION</spdx:downloadLocation>The problem is that the Java SPDX library only accepts
<spdx:downloadLocation>NOASSERTION</spdx:downloadLocation>, so I made this library serialize it in that manner.This can be verified by using this library (not on this branch) to convert a SPDX 2.3 file containing download locations with noassertion values to JSON format, then RDF, and then having the SPDX Java library/the SPDX online tool validate it. It will fail because the conversion to RDF will be using the
rdf:resourceformatting. Doing the same process with this branch should result in no errors when validating.