Skip to content

Commit 9f80484

Browse files
Add generator plugins for all SPDX formats
This adds support for the other three SPDX formats: XML, YAML and RDF-XML Signed-off-by: Armin Tänzer <[email protected]>
1 parent 9a293b6 commit 9f80484

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ tern.formats =
5353
html = tern.formats.html.generator:HTML
5454
cyclonedxjson = tern.formats.cyclonedx.cyclonedxjson.generator:CycloneDXJSON
5555
spdxjson_new = tern.formats.spdx_new.spdxjson.generator:SpdxJSON
56+
spdxyaml_new = tern.formats.spdx_new.spdxyaml.generator:SpdxYAML
57+
spdxxml_new = tern.formats.spdx_new.spdxxml.generator:SpdxXML
58+
spdxrdf_new = tern.formats.spdx_new.spdxrdf.generator:SpdxRDF
5659
spdxtagvalue_new = tern.formats.spdx_new.spdxtagvalue.generator:SpdxTagValue
5760
tern.extensions =
5861
cve_bin_tool = tern.extensions.cve_bin_tool.executor:CveBinTool

tern/formats/spdx_new/spdxrdf/__init__.py

Whitespace-only changes.
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
4+
# SPDX-License-Identifier: BSD-2-Clause
5+
6+
"""
7+
SPDX RDF-XML document generator
8+
"""
9+
import io
10+
import logging
11+
from typing import List
12+
13+
from spdx_tools.spdx.model import Document
14+
from spdx_tools.spdx.writer.rdf.rdf_writer import write_document_to_stream
15+
16+
from tern.classes.image import Image
17+
from tern.classes.image_layer import ImageLayer
18+
from tern.formats import generator
19+
from tern.formats.spdx.spdx import SPDX
20+
from tern.formats.spdx_new.make_spdx_model import make_spdx_model, make_spdx_model_snapshot
21+
from tern.utils import constants
22+
23+
# global logger
24+
logger = logging.getLogger(constants.logger_name)
25+
26+
27+
class SpdxRDF(generator.Generate):
28+
def generate(self, image_obj_list: List[Image], print_inclusive=False) -> str:
29+
"""Generate an SPDX document
30+
WARNING: This assumes that the list consists of one image or the base
31+
image and a stub image, in which case, the information in the stub
32+
image is not applicable in the SPDX case as it is an empty image
33+
object with no metadata as nothing got built.
34+
35+
For the sake of SPDX, an image is a 'Package' which 'CONTAINS' each
36+
layer which is also a 'Package' which 'CONTAINS' the real Packages"""
37+
logger.debug("Generating SPDX RDF-XML document...")
38+
39+
spdx_document: Document = make_spdx_model(image_obj_list)
40+
41+
return get_serialized_rdf_document_string(spdx_document)
42+
43+
44+
def generate_layer(self, layer: ImageLayer) -> str:
45+
"""Generate an SPDX document containing package and file information
46+
at container build time"""
47+
logger.debug("Generating SPDX RDF-XML snapshot document...")
48+
template = SPDX()
49+
spdx_document: Document = make_spdx_model_snapshot(layer, template)
50+
51+
return get_serialized_rdf_document_string(spdx_document)
52+
53+
54+
def get_serialized_rdf_document_string(spdx_document):
55+
with io.BytesIO() as stream:
56+
write_document_to_stream(spdx_document, stream, validate=False)
57+
return stream.getvalue().decode("UTF-8")

tern/formats/spdx_new/spdxxml/__init__.py

Whitespace-only changes.
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
4+
# SPDX-License-Identifier: BSD-2-Clause
5+
6+
"""
7+
SPDX XML document generator
8+
"""
9+
import logging
10+
from typing import List
11+
12+
from spdx_tools.spdx.model import Document
13+
from spdx_tools.spdx.writer.xml.xml_writer import write_document_to_stream
14+
15+
from tern.classes.image import Image
16+
from tern.classes.image_layer import ImageLayer
17+
from tern.formats import generator
18+
from tern.formats.spdx.spdx import SPDX
19+
from tern.formats.spdx_new.general_helpers import get_serialized_document_string
20+
from tern.formats.spdx_new.make_spdx_model import make_spdx_model, make_spdx_model_snapshot
21+
from tern.utils import constants
22+
23+
# global logger
24+
logger = logging.getLogger(constants.logger_name)
25+
26+
27+
class SpdxXML(generator.Generate):
28+
def generate(self, image_obj_list: List[Image], print_inclusive=False) -> str:
29+
"""Generate an SPDX document
30+
WARNING: This assumes that the list consists of one image or the base
31+
image and a stub image, in which case, the information in the stub
32+
image is not applicable in the SPDX case as it is an empty image
33+
object with no metadata as nothing got built.
34+
35+
For the sake of SPDX, an image is a 'Package' which 'CONTAINS' each
36+
layer which is also a 'Package' which 'CONTAINS' the real Packages"""
37+
logger.debug("Generating SPDX XML document...")
38+
39+
spdx_document: Document = make_spdx_model(image_obj_list)
40+
41+
return get_serialized_document_string(spdx_document, write_document_to_stream)
42+
43+
def generate_layer(self, layer: ImageLayer) -> str:
44+
"""Generate an SPDX document containing package and file information
45+
at container build time"""
46+
logger.debug("Generating SPDX XML snapshot document...")
47+
template = SPDX()
48+
spdx_document: Document = make_spdx_model_snapshot(layer, template)
49+
50+
return get_serialized_document_string(spdx_document, write_document_to_stream)

tern/formats/spdx_new/spdxyaml/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
4+
# SPDX-License-Identifier: BSD-2-Clause
5+
6+
"""
7+
SPDX YAML document generator
8+
"""
9+
import logging
10+
from typing import List
11+
12+
from spdx_tools.spdx.model import Document
13+
from spdx_tools.spdx.writer.yaml.yaml_writer import write_document_to_stream
14+
15+
from tern.classes.image import Image
16+
from tern.classes.image_layer import ImageLayer
17+
from tern.formats import generator
18+
from tern.formats.spdx.spdx import SPDX
19+
from tern.formats.spdx_new.general_helpers import get_serialized_document_string
20+
from tern.formats.spdx_new.make_spdx_model import make_spdx_model, make_spdx_model_snapshot
21+
from tern.utils import constants
22+
23+
# global logger
24+
logger = logging.getLogger(constants.logger_name)
25+
26+
27+
class SpdxYAML(generator.Generate):
28+
def generate(self, image_obj_list: List[Image], print_inclusive=False) -> str:
29+
"""Generate an SPDX document
30+
WARNING: This assumes that the list consists of one image or the base
31+
image and a stub image, in which case, the information in the stub
32+
image is not applicable in the SPDX case as it is an empty image
33+
object with no metadata as nothing got built.
34+
35+
For the sake of SPDX, an image is a 'Package' which 'CONTAINS' each
36+
layer which is also a 'Package' which 'CONTAINS' the real Packages"""
37+
logger.debug("Generating SPDX YAML document...")
38+
39+
spdx_document: Document = make_spdx_model(image_obj_list)
40+
41+
return get_serialized_document_string(spdx_document, write_document_to_stream)
42+
43+
def generate_layer(self, layer: ImageLayer) -> str:
44+
"""Generate an SPDX document containing package and file information
45+
at container build time"""
46+
logger.debug("Generating SPDX YAML snapshot document...")
47+
template = SPDX()
48+
spdx_document: Document = make_spdx_model_snapshot(layer, template)
49+
50+
return get_serialized_document_string(spdx_document, write_document_to_stream)

0 commit comments

Comments
 (0)