|
| 1 | +# This file is part of CycloneDX Python Library |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | +# Copyright (c) OWASP Foundation. All Rights Reserved. |
| 17 | + |
| 18 | +""" |
| 19 | + This set of classes represents the lifecycles types in the CycloneDX standard. |
| 20 | +
|
| 21 | +.. note:: |
| 22 | + Introduced in CycloneDX v1.5 |
| 23 | +
|
| 24 | +.. note:: |
| 25 | + See the CycloneDX Schema for lifecycles: https://cyclonedx.org/docs/1.5/#metadata_lifecycles |
| 26 | +""" |
| 27 | + |
| 28 | +from enum import Enum |
| 29 | +from json import loads as json_loads |
| 30 | +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union |
| 31 | +from xml.etree.ElementTree import Element # nosec B405 |
| 32 | + |
| 33 | +import serializable |
| 34 | +from serializable.helpers import BaseHelper |
| 35 | +from sortedcontainers import SortedSet |
| 36 | + |
| 37 | +from .._internal.compare import ComparableTuple as _ComparableTuple |
| 38 | +from ..exception.serialization import CycloneDxDeserializationException |
| 39 | + |
| 40 | +if TYPE_CHECKING: # pragma: no cover |
| 41 | + from serializable import ViewType |
| 42 | + |
| 43 | + |
| 44 | +@serializable.serializable_enum |
| 45 | +class Phase(str, Enum): |
| 46 | + DESIGN = 'design' |
| 47 | + PREBUILD = 'pre-build' |
| 48 | + BUILD = 'build' |
| 49 | + POSTBUILD = 'post-build' |
| 50 | + OPERATIONS = 'operations' |
| 51 | + DISCOVERY = 'discovery' |
| 52 | + DECOMISSION = 'decommission' |
| 53 | + |
| 54 | + |
| 55 | +@serializable.serializable_class |
| 56 | +class PredefinedPhase: |
| 57 | + """ |
| 58 | + Object that defines pre-defined phases in the product lifecycle. |
| 59 | +
|
| 60 | + .. note:: |
| 61 | + See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.5/#metadata_lifecycles |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, phase: Phase) -> None: |
| 65 | + self._phase = phase |
| 66 | + |
| 67 | + @property |
| 68 | + def phase(self) -> Phase: |
| 69 | + return self._phase |
| 70 | + |
| 71 | + @phase.setter |
| 72 | + def phase(self, phase: Phase) -> None: |
| 73 | + self._phase = phase |
| 74 | + |
| 75 | + def __hash__(self) -> int: |
| 76 | + return hash(self._phase) |
| 77 | + |
| 78 | + def __eq__(self, other: object) -> bool: |
| 79 | + if isinstance(other, PredefinedPhase): |
| 80 | + return hash(other) == hash(self) |
| 81 | + return False |
| 82 | + |
| 83 | + def __lt__(self, other: Any) -> bool: |
| 84 | + if isinstance(other, PredefinedPhase): |
| 85 | + return self._phase < other._phase |
| 86 | + if isinstance(other, CustomPhase): |
| 87 | + return True # put PredefinedPhase before any CustomPhase |
| 88 | + return NotImplemented |
| 89 | + |
| 90 | + def __repr__(self) -> str: |
| 91 | + return f'<PredefinedPhase name={self._phase}>' |
| 92 | + |
| 93 | + |
| 94 | +@serializable.serializable_class |
| 95 | +class CustomPhase: |
| 96 | + def __init__(self, name: str, description: Optional[str] = None) -> None: |
| 97 | + self._name = name |
| 98 | + self._description = description |
| 99 | + |
| 100 | + @property |
| 101 | + @serializable.xml_sequence(1) |
| 102 | + @serializable.xml_string(serializable.XmlStringSerializationType.NORMALIZED_STRING) |
| 103 | + def name(self) -> str: |
| 104 | + """ |
| 105 | + Name of the lifecycle phase. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + `str` |
| 109 | + """ |
| 110 | + return self._name |
| 111 | + |
| 112 | + @name.setter |
| 113 | + def name(self, name: str) -> None: |
| 114 | + self._name = name |
| 115 | + |
| 116 | + @property |
| 117 | + @serializable.xml_sequence(2) |
| 118 | + @serializable.xml_string(serializable.XmlStringSerializationType.NORMALIZED_STRING) |
| 119 | + def description(self) -> Optional[str]: |
| 120 | + """ |
| 121 | + Description of the lifecycle phase. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + `str` |
| 125 | + """ |
| 126 | + return self._description |
| 127 | + |
| 128 | + @description.setter |
| 129 | + def description(self, description: Optional[str]) -> None: |
| 130 | + self._description = description |
| 131 | + |
| 132 | + def __hash__(self) -> int: |
| 133 | + return hash((self._name, self._description)) |
| 134 | + |
| 135 | + def __eq__(self, other: object) -> bool: |
| 136 | + if isinstance(other, CustomPhase): |
| 137 | + return hash(other) == hash(self) |
| 138 | + return False |
| 139 | + |
| 140 | + def __lt__(self, other: Any) -> bool: |
| 141 | + if isinstance(other, CustomPhase): |
| 142 | + return _ComparableTuple((self._name, self._description)) < _ComparableTuple( |
| 143 | + (other._name, other._description) |
| 144 | + ) |
| 145 | + if isinstance(other, PredefinedPhase): |
| 146 | + return False # put CustomPhase after any PredefinedPhase |
| 147 | + return NotImplemented |
| 148 | + |
| 149 | + def __repr__(self) -> str: |
| 150 | + return f'<CustomPhase name={self._name}>' |
| 151 | + |
| 152 | + |
| 153 | +Lifecycle = Union[PredefinedPhase, CustomPhase] |
| 154 | +"""TypeAlias for a union of supported lifecycle models. |
| 155 | +
|
| 156 | +- :class:`PredefinedPhase` |
| 157 | +- :class:`CustomPhase` |
| 158 | +""" |
| 159 | + |
| 160 | +if TYPE_CHECKING: # pragma: no cover |
| 161 | + # workaround for https://github.com/python/mypy/issues/5264 |
| 162 | + # this code path is taken when static code analysis or documentation tools runs through. |
| 163 | + class LifecycleRepository(SortedSet[Lifecycle]): |
| 164 | + """Collection of :class:`Lifecycle`. |
| 165 | +
|
| 166 | + This is a `set`, not a `list`. Order MUST NOT matter here. |
| 167 | + """ |
| 168 | + |
| 169 | +else: |
| 170 | + |
| 171 | + class LifecycleRepository(SortedSet): |
| 172 | + """Collection of :class:`Lifecycle`. |
| 173 | +
|
| 174 | + This is a `set`, not a `list`. Order MUST NOT matter here. |
| 175 | + """ |
| 176 | + |
| 177 | + |
| 178 | +class _LifecycleRepositoryHelper(BaseHelper): |
| 179 | + @classmethod |
| 180 | + def json_normalize(cls, o: LifecycleRepository, *, |
| 181 | + view: Optional[Type['ViewType']], |
| 182 | + **__: Any) -> Any: |
| 183 | + if len(o) == 0: |
| 184 | + return None |
| 185 | + |
| 186 | + return [json_loads(li.as_json( # type:ignore[union-attr] |
| 187 | + view_=view)) for li in o] |
| 188 | + |
| 189 | + @classmethod |
| 190 | + def json_denormalize(cls, o: List[Dict[str, Any]], |
| 191 | + **__: Any) -> LifecycleRepository: |
| 192 | + repo = LifecycleRepository() |
| 193 | + for li in o: |
| 194 | + if 'phase' in li: |
| 195 | + repo.add(PredefinedPhase.from_json(li)) # type:ignore[attr-defined] |
| 196 | + elif 'name' in li: |
| 197 | + repo.add(CustomPhase.from_json(li)) # type:ignore[attr-defined] |
| 198 | + else: |
| 199 | + raise CycloneDxDeserializationException(f'unexpected: {li!r}') |
| 200 | + |
| 201 | + return repo |
| 202 | + |
| 203 | + @classmethod |
| 204 | + def xml_normalize(cls, o: LifecycleRepository, *, |
| 205 | + element_name: str, |
| 206 | + view: Optional[Type['ViewType']], |
| 207 | + xmlns: Optional[str], |
| 208 | + **__: Any) -> Optional[Element]: |
| 209 | + if len(o) == 0: |
| 210 | + return None |
| 211 | + |
| 212 | + elem = Element(element_name) |
| 213 | + for li in o: |
| 214 | + elem.append(li.as_xml( # type:ignore[union-attr] |
| 215 | + view_=view, as_string=False, element_name='lifecycle', xmlns=xmlns)) |
| 216 | + |
| 217 | + return elem |
| 218 | + |
| 219 | + @classmethod |
| 220 | + def xml_denormalize(cls, o: Element, |
| 221 | + default_ns: Optional[str], |
| 222 | + **__: Any) -> LifecycleRepository: |
| 223 | + repo = LifecycleRepository() |
| 224 | + |
| 225 | + for li in o: |
| 226 | + tag = li.tag if default_ns is None else li.tag.replace(f'{{{default_ns}}}', '') |
| 227 | + |
| 228 | + if tag == 'lifecycle': |
| 229 | + stages = list(li) |
| 230 | + |
| 231 | + predefined_phase = next((el for el in stages if 'phase' in el.tag), None) |
| 232 | + custom_phase = next((el for el in stages if 'name' in el.tag), None) |
| 233 | + if predefined_phase is not None: |
| 234 | + repo.add(PredefinedPhase.from_xml(li, default_ns)) # type:ignore[attr-defined] |
| 235 | + elif custom_phase is not None: |
| 236 | + repo.add(CustomPhase.from_xml(li, default_ns)) # type:ignore[attr-defined] |
| 237 | + else: |
| 238 | + raise CycloneDxDeserializationException(f'unexpected: {li!r}') |
| 239 | + |
| 240 | + return repo |
0 commit comments