Skip to content

Commit cf5d2c7

Browse files
authored
feat!: this-builder (#649)
reworked `ThisTool` for #635 --------- Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 0172564 commit cf5d2c7

File tree

42 files changed

+884
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+884
-76
lines changed

cyclonedx/builder/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
#
13+
# SPDX-License-Identifier: Apache-2.0
14+
# Copyright (c) OWASP Foundation. All Rights Reserved.
15+
16+
"""
17+
Builders used in this library.
18+
"""

cyclonedx/builder/this.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
#
13+
# SPDX-License-Identifier: Apache-2.0
14+
# Copyright (c) OWASP Foundation. All Rights Reserved.
15+
16+
"""Representation of this very python library."""
17+
18+
__all__ = ['this_tool', 'this_component']
19+
20+
from typing import Iterable
21+
22+
from .. import __version__ as __ThisVersion # noqa: N812
23+
from ..model import ExternalReference, ExternalReferenceType, XsUri
24+
from ..model.component import Component, ComponentType
25+
from ..model.license import DisjunctiveLicense, LicenseAcknowledgement
26+
from ..model.tool import Tool
27+
28+
# !!! keep this file in sync with `pyproject.toml`
29+
30+
# !!!
31+
# things in here are built on demand, rather than using prepared frozen constants.
32+
# this is currently a draft and may change in the future.
33+
# !!!
34+
35+
36+
def __ext_refs() -> Iterable[ExternalReference]:
37+
return (
38+
ExternalReference(
39+
type=ExternalReferenceType.BUILD_SYSTEM,
40+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/actions')
41+
),
42+
ExternalReference(
43+
type=ExternalReferenceType.DISTRIBUTION,
44+
url=XsUri('https://pypi.org/project/cyclonedx-python-lib/')
45+
),
46+
ExternalReference(
47+
type=ExternalReferenceType.DOCUMENTATION,
48+
url=XsUri('https://cyclonedx-python-library.readthedocs.io/')
49+
),
50+
ExternalReference(
51+
type=ExternalReferenceType.ISSUE_TRACKER,
52+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/issues')
53+
),
54+
ExternalReference(
55+
type=ExternalReferenceType.LICENSE,
56+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSE')
57+
),
58+
ExternalReference(
59+
type=ExternalReferenceType.RELEASE_NOTES,
60+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md')
61+
),
62+
ExternalReference(
63+
type=ExternalReferenceType.VCS,
64+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib')
65+
),
66+
ExternalReference(
67+
type=ExternalReferenceType.WEBSITE,
68+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/#readme')
69+
),
70+
)
71+
72+
73+
def this_tool() -> Tool:
74+
"""Representation of this very python library as a :class:`Tool`."""
75+
76+
return Tool(
77+
vendor='CycloneDX',
78+
name='cyclonedx-python-lib',
79+
version=__ThisVersion or 'UNKNOWN',
80+
external_references=__ext_refs(),
81+
)
82+
83+
84+
def this_component() -> Component:
85+
"""Representation of this very python library as a :class:`Component`."""
86+
87+
return Component(
88+
type=ComponentType.LIBRARY,
89+
group='CycloneDX',
90+
name='cyclonedx-python-lib',
91+
version=__ThisVersion or 'UNKNOWN',
92+
description='Python library for CycloneDX',
93+
licenses=(DisjunctiveLicense(id='Apache-2.0',
94+
acknowledgement=LicenseAcknowledgement.DECLARED),),
95+
external_references=__ext_refs(),
96+
# to be expanded ...
97+
)

cyclonedx/model/__init__.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import serializable
3333
from sortedcontainers import SortedSet
3434

35-
from .. import __version__ as __ThisToolVersion # noqa: N812
3635
from .._internal.compare import ComparableTuple as _ComparableTuple
3736
from ..exception.model import (
3837
InvalidLocaleTypeException,
@@ -1262,47 +1261,3 @@ def __hash__(self) -> int:
12621261

12631262
def __repr__(self) -> str:
12641263
return f'<Copyright text={self.text}>'
1265-
1266-
1267-
# Importing here to avoid a circular import
1268-
from .tool import Tool # pylint: disable=wrong-import-position # noqa: E402
1269-
1270-
ThisTool = Tool(
1271-
vendor='CycloneDX',
1272-
name='cyclonedx-python-lib',
1273-
version=__ThisToolVersion or 'UNKNOWN',
1274-
external_references=[
1275-
ExternalReference(
1276-
type=ExternalReferenceType.BUILD_SYSTEM,
1277-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/actions')
1278-
),
1279-
ExternalReference(
1280-
type=ExternalReferenceType.DISTRIBUTION,
1281-
url=XsUri('https://pypi.org/project/cyclonedx-python-lib/')
1282-
),
1283-
ExternalReference(
1284-
type=ExternalReferenceType.DOCUMENTATION,
1285-
url=XsUri('https://cyclonedx-python-library.readthedocs.io/')
1286-
),
1287-
ExternalReference(
1288-
type=ExternalReferenceType.ISSUE_TRACKER,
1289-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/issues')
1290-
),
1291-
ExternalReference(
1292-
type=ExternalReferenceType.LICENSE,
1293-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSE')
1294-
),
1295-
ExternalReference(
1296-
type=ExternalReferenceType.RELEASE_NOTES,
1297-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md')
1298-
),
1299-
ExternalReference(
1300-
type=ExternalReferenceType.VCS,
1301-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib')
1302-
),
1303-
ExternalReference(
1304-
type=ExternalReferenceType.WEBSITE,
1305-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/#readme')
1306-
)
1307-
]
1308-
)

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ requires = ["poetry-core>=1.0.0"]
33
build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
6+
# keep in sync with `cyclonedx/builder/this.py`
67
name = "cyclonedx-python-lib"
78
# !! version is managed by semantic_release
89
version = "7.6.0"
@@ -63,13 +64,14 @@ keywords = [
6364
]
6465

6566
[tool.poetry.urls]
67+
# keep in sync with `cyclonedx/builder/this.py`
6668
"Bug Tracker" = "https://github.com/CycloneDX/cyclonedx-python-lib/issues"
6769
"Funding" = "https://owasp.org/donate/?reponame=www-project-cyclonedx&title=OWASP+CycloneDX"
6870

6971
[tool.poetry.dependencies]
7072
python = "^3.8"
7173
packageurl-python = ">=0.11, <2"
72-
py-serializable = "^1.1.0"
74+
py-serializable = "^1.1.1"
7375
sortedcontainers = "^2.4.0"
7476
license-expression = "^30"
7577
jsonschema = { version = "^4.18", extras=['format'], optional=true }

tests/_data/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# See https://github.com/package-url/packageurl-python/issues/65
2727
from packageurl import PackageURL
2828

29+
from cyclonedx.builder.this import this_component, this_tool
2930
from cyclonedx.model import (
3031
AttachedText,
3132
Copyright,
@@ -38,7 +39,6 @@
3839
Note,
3940
NoteText,
4041
Property,
41-
ThisTool,
4242
XsUri,
4343
)
4444
from cyclonedx.model.bom import Bom, BomMetaData
@@ -1052,7 +1052,7 @@ def get_bom_with_tools() -> Bom:
10521052
return _make_bom(
10531053
metadata=BomMetaData(
10541054
tools=(
1055-
ThisTool,
1055+
this_tool(),
10561056
Tool(name='test-tool-b'),
10571057
Tool(vendor='example',
10581058
name='test-tool-a',
@@ -1071,6 +1071,7 @@ def get_bom_with_tools_with_component_migrate() -> Bom:
10711071
metadata=BomMetaData(
10721072
tools=ToolsRepository(
10731073
components=(
1074+
this_component(),
10741075
Component(name='test-component', bom_ref='test-component'),
10751076
Component(type=ComponentType.APPLICATION,
10761077
bom_ref='other-component',
@@ -1108,6 +1109,7 @@ def get_bom_with_tools_with_component_and_service_migrate() -> Bom:
11081109
metadata=BomMetaData(
11091110
tools=ToolsRepository(
11101111
components=(
1112+
this_component(),
11111113
Component(name='test-component', bom_ref='test-component'),
11121114
Component(type=ComponentType.APPLICATION,
11131115
bom_ref='other-component',
@@ -1137,6 +1139,7 @@ def get_bom_with_tools_with_component_and_service_and_tools_irreversible_migrate
11371139
tserv = tools.services
11381140
ttools = tools.tools
11391141
tcomp.update((
1142+
this_component(),
11401143
Component(name='test-component', bom_ref='test-component'),
11411144
Component(type=ComponentType.APPLICATION,
11421145
bom_ref='other-component',
@@ -1156,7 +1159,7 @@ def get_bom_with_tools_with_component_and_service_and_tools_irreversible_migrate
11561159
),
11571160
))
11581161
ttools.update((
1159-
ThisTool,
1162+
this_tool(),
11601163
Tool(name='test-tool-b'),
11611164
Tool(vendor='example',
11621165
name='test-tool-a',

tests/_data/snapshots/get_bom_with_tools_with_component_and_service_and_tools_irreversible_migrate-1.2.json.bin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
"name": "other-component",
3232
"vendor": "acme"
3333
},
34+
{
35+
"name": "cyclonedx-python-lib",
36+
"vendor": "CycloneDX",
37+
"version": "TESTING"
38+
},
3439
{
3540
"name": "test-component"
3641
},

tests/_data/snapshots/get_bom_with_tools_with_component_and_service_and_tools_irreversible_migrate-1.2.xml.bin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
<hash alg="SHA-256">49b420bd8d8182542a76d4422e0c7890dcc88a3d8ddad04da06366d8c40ac8ca</hash>
2727
</hashes>
2828
</tool>
29+
<tool>
30+
<vendor>CycloneDX</vendor>
31+
<name>cyclonedx-python-lib</name>
32+
<version>TESTING</version>
33+
</tool>
2934
<tool>
3035
<name>test-component</name>
3136
</tool>

tests/_data/snapshots/get_bom_with_tools_with_component_and_service_and_tools_irreversible_migrate-1.3.json.bin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
"name": "other-component",
3232
"vendor": "acme"
3333
},
34+
{
35+
"name": "cyclonedx-python-lib",
36+
"vendor": "CycloneDX",
37+
"version": "TESTING"
38+
},
3439
{
3540
"name": "test-component"
3641
},

tests/_data/snapshots/get_bom_with_tools_with_component_and_service_and_tools_irreversible_migrate-1.3.xml.bin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
<hash alg="SHA-256">49b420bd8d8182542a76d4422e0c7890dcc88a3d8ddad04da06366d8c40ac8ca</hash>
2727
</hashes>
2828
</tool>
29+
<tool>
30+
<vendor>CycloneDX</vendor>
31+
<name>cyclonedx-python-lib</name>
32+
<version>TESTING</version>
33+
</tool>
2934
<tool>
3035
<name>test-component</name>
3136
</tool>

tests/_data/snapshots/get_bom_with_tools_with_component_and_service_and_tools_irreversible_migrate-1.4.json.bin

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,45 @@
9191
"name": "other-component",
9292
"vendor": "acme"
9393
},
94+
{
95+
"externalReferences": [
96+
{
97+
"type": "build-system",
98+
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/actions"
99+
},
100+
{
101+
"type": "distribution",
102+
"url": "https://pypi.org/project/cyclonedx-python-lib/"
103+
},
104+
{
105+
"type": "documentation",
106+
"url": "https://cyclonedx-python-library.readthedocs.io/"
107+
},
108+
{
109+
"type": "issue-tracker",
110+
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/issues"
111+
},
112+
{
113+
"type": "license",
114+
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSE"
115+
},
116+
{
117+
"type": "release-notes",
118+
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md"
119+
},
120+
{
121+
"type": "vcs",
122+
"url": "https://github.com/CycloneDX/cyclonedx-python-lib"
123+
},
124+
{
125+
"type": "website",
126+
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/#readme"
127+
}
128+
],
129+
"name": "cyclonedx-python-lib",
130+
"vendor": "CycloneDX",
131+
"version": "TESTING"
132+
},
94133
{
95134
"name": "test-component"
96135
},

0 commit comments

Comments
 (0)