-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds new options to the -f parameter like "spdxjson22", "spdxjson23", "spdxrdf22" etc. Also extracts common code from the generators of all the different formats. Signed-off-by: Armin Tänzer <armin.taenzer@tngtech.com>
- Loading branch information
1 parent
0f1ff2c
commit a226cec
Showing
29 changed files
with
318 additions
and
283 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021 VMware, Inc. All Rights Reserved. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
""" | ||
Handle imports and logging for different SPDX formats | ||
""" | ||
import io | ||
import logging | ||
from typing import Callable, IO, List | ||
|
||
from spdx_tools.spdx.model import Document | ||
|
||
from tern.classes.image import Image | ||
from tern.classes.image_layer import ImageLayer | ||
from tern.formats.spdx.spdx import SPDX | ||
from tern.formats.spdx_new.make_spdx_model import make_spdx_model, make_spdx_model_snapshot | ||
from tern.utils import constants | ||
|
||
logger = logging.getLogger(constants.logger_name) | ||
|
||
|
||
def get_spdx_from_image_list(image_obj_list: List[Image], spdx_format: str, spdx_version: str) -> str: | ||
"""Generate an SPDX document | ||
WARNING: This assumes that the list consists of one image or the base | ||
image and a stub image, in which case, the information in the stub | ||
image is not applicable in the SPDX case as it is an empty image | ||
object with no metadata as nothing got built. | ||
For the sake of SPDX, an image is a 'Package' which 'CONTAINS' each | ||
layer which is also a 'Package' which 'CONTAINS' the real Packages""" | ||
logger.debug(f"Generating SPDX {spdx_format} document...") | ||
|
||
spdx_document: Document = make_spdx_model(image_obj_list, spdx_version) | ||
|
||
return convert_document_to_serialized_string(spdx_document, spdx_format) | ||
|
||
|
||
def get_spdx_from_layer(layer: ImageLayer, spdx_format: str, spdx_version: str) -> str: | ||
"""Generate an SPDX document containing package and file information | ||
at container build time""" | ||
logger.debug(f"Generating SPDX {spdx_format} snapshot document...") | ||
|
||
template = SPDX() | ||
spdx_document: Document = make_spdx_model_snapshot(layer, template, spdx_version) | ||
|
||
return convert_document_to_serialized_string(spdx_document, spdx_format) | ||
|
||
|
||
def convert_document_to_serialized_string(spdx_document: Document, spdx_format: str) -> str: | ||
if spdx_format == "JSON": | ||
from spdx_tools.spdx.writer.json.json_writer import write_document_to_stream | ||
return get_serialized_document_string(spdx_document, write_document_to_stream) | ||
if spdx_format == "YAML": | ||
from spdx_tools.spdx.writer.yaml.yaml_writer import write_document_to_stream | ||
return get_serialized_document_string(spdx_document, write_document_to_stream) | ||
if spdx_format == "XML": | ||
from spdx_tools.spdx.writer.xml.xml_writer import write_document_to_stream | ||
return get_serialized_document_string(spdx_document, write_document_to_stream) | ||
if spdx_format == "RDF-XML": | ||
return get_serialized_rdf_document_string(spdx_document) | ||
if spdx_format == "Tag-Value": | ||
from spdx_tools.spdx.writer.tagvalue.tagvalue_writer import write_document_to_stream | ||
return get_serialized_document_string(spdx_document, write_document_to_stream) | ||
|
||
|
||
def get_serialized_document_string(spdx_document: Document, writer_function: Callable[[Document, IO[str]], str]) -> str: | ||
with io.StringIO() as stream: | ||
writer_function(spdx_document, stream, validate=False) | ||
return stream.getvalue() | ||
|
||
|
||
def get_serialized_rdf_document_string(spdx_document: Document) -> str: | ||
from spdx_tools.spdx.writer.rdf.rdf_writer import write_document_to_stream | ||
with io.BytesIO() as stream: | ||
write_document_to_stream(spdx_document, stream, validate=False) | ||
return stream.getvalue().decode("UTF-8") |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021 VMware, Inc. All Rights Reserved. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
""" | ||
SPDX-2.2 JSON document generator | ||
""" | ||
from typing import List | ||
|
||
from tern.classes.image import Image | ||
from tern.classes.image_layer import ImageLayer | ||
from tern.formats import generator | ||
from tern.formats.spdx_new.spdx_formats_helper import get_spdx_from_image_list, get_spdx_from_layer | ||
|
||
|
||
class SpdxJSON22(generator.Generate): | ||
def generate(self, image_obj_list: List[Image], print_inclusive=False) -> str: | ||
return get_spdx_from_image_list(image_obj_list, "JSON", "SPDX-2.2") | ||
|
||
def generate_layer(self, layer: ImageLayer) -> str: | ||
return get_spdx_from_layer(layer, "JSON", "SPDX-2.2") |
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021 VMware, Inc. All Rights Reserved. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
""" | ||
SPDX-2.3 JSON document generator | ||
""" | ||
from typing import List | ||
|
||
from tern.classes.image import Image | ||
from tern.classes.image_layer import ImageLayer | ||
from tern.formats import generator | ||
from tern.formats.spdx_new.spdx_formats_helper import get_spdx_from_image_list, get_spdx_from_layer | ||
|
||
|
||
class SpdxJSON22(generator.Generate): | ||
def generate(self, image_obj_list: List[Image], print_inclusive=False) -> str: | ||
return get_spdx_from_image_list(image_obj_list, "JSON", "SPDX-2.3") | ||
|
||
def generate_layer(self, layer: ImageLayer) -> str: | ||
return get_spdx_from_layer(layer, "JSON", "SPDX-2.3") |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021 VMware, Inc. All Rights Reserved. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
""" | ||
SPDX-2.2 RDF-XML document generator | ||
""" | ||
from typing import List | ||
|
||
from tern.classes.image import Image | ||
from tern.classes.image_layer import ImageLayer | ||
from tern.formats import generator | ||
from tern.formats.spdx_new.spdx_formats_helper import get_spdx_from_image_list, get_spdx_from_layer | ||
|
||
|
||
class SpdxRDF22(generator.Generate): | ||
def generate(self, image_obj_list: List[Image], print_inclusive=False) -> str: | ||
return get_spdx_from_image_list(image_obj_list, "RDF-XML", "SPDX-2.2") | ||
|
||
def generate_layer(self, layer: ImageLayer) -> str: | ||
return get_spdx_from_layer(layer, "RDF-XML", "SPDX-2.2") |
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021 VMware, Inc. All Rights Reserved. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
""" | ||
SPDX-2.3 RDF-XML document generator | ||
""" | ||
from typing import List | ||
|
||
from tern.classes.image import Image | ||
from tern.classes.image_layer import ImageLayer | ||
from tern.formats import generator | ||
from tern.formats.spdx_new.spdx_formats_helper import get_spdx_from_image_list, get_spdx_from_layer | ||
|
||
|
||
class SpdxRDF22(generator.Generate): | ||
def generate(self, image_obj_list: List[Image], print_inclusive=False) -> str: | ||
return get_spdx_from_image_list(image_obj_list, "RDF-XML", "SPDX-2.3") | ||
|
||
def generate_layer(self, layer: ImageLayer) -> str: | ||
return get_spdx_from_layer(layer, "RDF-XML", "SPDX-2.3") |
Oops, something went wrong.