spdx_tools.spdx3.bump_from_spdx2.creation_info

 1# SPDX-FileCopyrightText: 2023 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from beartype.typing import List
 5from semantic_version import Version
 6
 7from spdx_tools.spdx3.bump_from_spdx2.actor import bump_actor
 8from spdx_tools.spdx3.bump_from_spdx2.external_document_ref import bump_external_document_ref
 9from spdx_tools.spdx3.bump_from_spdx2.message import print_missing_conversion
10from spdx_tools.spdx3.model import CreationInfo, ProfileIdentifierType, SpdxDocument
11from spdx_tools.spdx3.payload import Payload
12from spdx_tools.spdx.model.actor import ActorType
13from spdx_tools.spdx.model.document import CreationInfo as Spdx2_CreationInfo
14
15
16def bump_creation_info(spdx2_creation_info: Spdx2_CreationInfo, payload: Payload) -> SpdxDocument:
17    document_namespace = spdx2_creation_info.document_namespace
18    spdx_id = f"{document_namespace}#{spdx2_creation_info.spdx_id}"
19
20    print_missing_conversion("creation_info.document_namespace", 0, "https://github.com/spdx/spdx-3-model/issues/87")
21
22    namespaces, imports = (
23        zip(
24            *[
25                bump_external_document_ref(external_document_ref)
26                for external_document_ref in spdx2_creation_info.external_document_refs
27            ]
28        )
29        if spdx2_creation_info.external_document_refs
30        else ([], [])
31    )
32    namespaces = list(namespaces)
33    imports = list(imports)
34    print_missing_conversion(
35        "creation_info.license_list_version",
36        0,
37        "part of licensing profile, " "https://github.com/spdx/spdx-3-model/issues/131",
38    )
39    creation_info = CreationInfo(
40        spec_version=Version("3.0.0"),
41        created=spdx2_creation_info.created,
42        created_by=[],
43        profile=[ProfileIdentifierType.CORE, ProfileIdentifierType.SOFTWARE, ProfileIdentifierType.LICENSING],
44        data_license="https://spdx.org/licenses/" + spdx2_creation_info.data_license,
45    )
46
47    # due to creators having a creation_info themselves which inherits from the document's one,
48    # we have to add them after the creation_info has been initialized
49    creator_ids: List[str] = []
50    tool_ids: List[str] = []
51    for creator in spdx2_creation_info.creators:
52        bumped_actor_id = bump_actor(creator, payload, document_namespace, creation_info)
53        if creator.actor_type in [ActorType.PERSON, ActorType.ORGANIZATION]:
54            creator_ids.append(bumped_actor_id)
55        else:
56            tool_ids.append(bumped_actor_id)
57
58    if not creator_ids:
59        print_missing_conversion(
60            "Creators",
61            0,
62            "The SPDX2 creation_info does not contain creators of Type Person or Organization."
63            " This case leads to an invalid SPDX3 document and is currently not supported."
64            "https://github.com/spdx/spdx-3-model/issues/180",
65        )
66
67    creation_info.created_by = creator_ids
68    creation_info.created_using = tool_ids
69
70    return SpdxDocument(
71        spdx_id=spdx_id,
72        creation_info=creation_info,
73        name=spdx2_creation_info.name,
74        comment=spdx2_creation_info.document_comment,
75        element=[],
76        root_element=[],
77        imports=imports,
78        namespaces=namespaces,
79    )
17def bump_creation_info(spdx2_creation_info: Spdx2_CreationInfo, payload: Payload) -> SpdxDocument:
18    document_namespace = spdx2_creation_info.document_namespace
19    spdx_id = f"{document_namespace}#{spdx2_creation_info.spdx_id}"
20
21    print_missing_conversion("creation_info.document_namespace", 0, "https://github.com/spdx/spdx-3-model/issues/87")
22
23    namespaces, imports = (
24        zip(
25            *[
26                bump_external_document_ref(external_document_ref)
27                for external_document_ref in spdx2_creation_info.external_document_refs
28            ]
29        )
30        if spdx2_creation_info.external_document_refs
31        else ([], [])
32    )
33    namespaces = list(namespaces)
34    imports = list(imports)
35    print_missing_conversion(
36        "creation_info.license_list_version",
37        0,
38        "part of licensing profile, " "https://github.com/spdx/spdx-3-model/issues/131",
39    )
40    creation_info = CreationInfo(
41        spec_version=Version("3.0.0"),
42        created=spdx2_creation_info.created,
43        created_by=[],
44        profile=[ProfileIdentifierType.CORE, ProfileIdentifierType.SOFTWARE, ProfileIdentifierType.LICENSING],
45        data_license="https://spdx.org/licenses/" + spdx2_creation_info.data_license,
46    )
47
48    # due to creators having a creation_info themselves which inherits from the document's one,
49    # we have to add them after the creation_info has been initialized
50    creator_ids: List[str] = []
51    tool_ids: List[str] = []
52    for creator in spdx2_creation_info.creators:
53        bumped_actor_id = bump_actor(creator, payload, document_namespace, creation_info)
54        if creator.actor_type in [ActorType.PERSON, ActorType.ORGANIZATION]:
55            creator_ids.append(bumped_actor_id)
56        else:
57            tool_ids.append(bumped_actor_id)
58
59    if not creator_ids:
60        print_missing_conversion(
61            "Creators",
62            0,
63            "The SPDX2 creation_info does not contain creators of Type Person or Organization."
64            " This case leads to an invalid SPDX3 document and is currently not supported."
65            "https://github.com/spdx/spdx-3-model/issues/180",
66        )
67
68    creation_info.created_by = creator_ids
69    creation_info.created_using = tool_ids
70
71    return SpdxDocument(
72        spdx_id=spdx_id,
73        creation_info=creation_info,
74        name=spdx2_creation_info.name,
75        comment=spdx2_creation_info.document_comment,
76        element=[],
77        root_element=[],
78        imports=imports,
79        namespaces=namespaces,
80    )