spdx_tools.spdx3.bump_from_spdx2.annotation

 1# SPDX-FileCopyrightText: 2023 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from copy import deepcopy
 5
 6from spdx_tools.spdx3.bump_from_spdx2.actor import bump_actor
 7from spdx_tools.spdx3.bump_from_spdx2.message import print_missing_conversion
 8from spdx_tools.spdx3.model import Annotation, AnnotationType, CreationInfo
 9from spdx_tools.spdx3.payload import Payload
10from spdx_tools.spdx.model.actor import ActorType
11from spdx_tools.spdx.model.annotation import Annotation as Spdx2_Annotation
12
13
14def bump_annotation(
15    spdx2_annotation: Spdx2_Annotation,
16    payload: Payload,
17    creation_info: CreationInfo,
18    document_namespace: str,
19    counter: int,
20):
21    spdx_id: str = "#".join([document_namespace, f"SPDXRef-Annotation-{counter}"])
22    creation_info = deepcopy(creation_info)
23    creation_info.created = spdx2_annotation.annotation_date
24
25    # caution: the annotator and the annotation will only share the same creation_info if the actor
26    #          has not been previously defined
27    annotator = spdx2_annotation.annotator
28    creator_id: str = bump_actor(annotator, payload, document_namespace, creation_info)
29    if annotator.actor_type in [ActorType.PERSON, ActorType.ORGANIZATION]:
30        creation_info.created_by = [creator_id]
31    else:
32        print_missing_conversion(
33            "Annotator",
34            0,
35            "The SPDX2 annotation is not of Type Person or Organization."
36            " This case leads to an invalid SPDX3 document and is currently not supported."
37            "https://github.com/spdx/spdx-3-model/issues/180",
38        )
39    annotation_type: AnnotationType = AnnotationType[spdx2_annotation.annotation_type.name]
40
41    payload.add_element(
42        Annotation(
43            spdx_id,
44            annotation_type,
45            creation_info=creation_info,
46            subject=f"{document_namespace}#{spdx2_annotation.spdx_id}",
47            statement=spdx2_annotation.annotation_comment,
48        )
49    )
def bump_annotation( spdx2_annotation: spdx_tools.spdx.model.annotation.Annotation, payload: spdx_tools.spdx3.payload.Payload, creation_info: spdx_tools.spdx3.model.creation_info.CreationInfo, document_namespace: str, counter: int):
15def bump_annotation(
16    spdx2_annotation: Spdx2_Annotation,
17    payload: Payload,
18    creation_info: CreationInfo,
19    document_namespace: str,
20    counter: int,
21):
22    spdx_id: str = "#".join([document_namespace, f"SPDXRef-Annotation-{counter}"])
23    creation_info = deepcopy(creation_info)
24    creation_info.created = spdx2_annotation.annotation_date
25
26    # caution: the annotator and the annotation will only share the same creation_info if the actor
27    #          has not been previously defined
28    annotator = spdx2_annotation.annotator
29    creator_id: str = bump_actor(annotator, payload, document_namespace, creation_info)
30    if annotator.actor_type in [ActorType.PERSON, ActorType.ORGANIZATION]:
31        creation_info.created_by = [creator_id]
32    else:
33        print_missing_conversion(
34            "Annotator",
35            0,
36            "The SPDX2 annotation is not of Type Person or Organization."
37            " This case leads to an invalid SPDX3 document and is currently not supported."
38            "https://github.com/spdx/spdx-3-model/issues/180",
39        )
40    annotation_type: AnnotationType = AnnotationType[spdx2_annotation.annotation_type.name]
41
42    payload.add_element(
43        Annotation(
44            spdx_id,
45            annotation_type,
46            creation_info=creation_info,
47            subject=f"{document_namespace}#{spdx2_annotation.spdx_id}",
48            statement=spdx2_annotation.annotation_comment,
49        )
50    )