Skip to content

Commit 65e22bd

Browse files
authored
docs: add exaple how to build and serialize (#397)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 2180d31 commit 65e22bd

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

.gitattributes

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
cyclonedx/schema/** linguist-language
2+
cyclonedx/schema/** linguist-vendored
33

docs/examples.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
15+
Examples
16+
========
17+
18+
Build & Serialize
19+
-----------------
20+
21+
.. literalinclude:: ../examples/build_and_serialize.py
22+
:language: python
23+
:linenos:

docs/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ programmatically generate SBOMs.
4040

4141
install
4242
architecture
43+
examples
4344
contributing
4445
support
4546
changelog
@@ -48,4 +49,4 @@ programmatically generate SBOMs.
4849
.. _CycloneDX Python: https://pypi.org/project/cyclonedx-bom/
4950
.. _Jake: https://pypi.org/project/jake
5051
.. _CycloneDX Tool Center: https://cyclonedx.org/tool-center/
51-
.. _official examples: https://cyclonedx.org/capabilities/bomlink/#linking-external-vex-to-bom-inventory
52+
.. _official examples: https://cyclonedx.org/capabilities/bomlink/#linking-external-vex-to-bom-inventory

examples/build_and_serialize.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from cyclonedx.factory.license import LicenseFactory
2+
from cyclonedx.model import OrganizationalEntity, XsUri
3+
from cyclonedx.model.bom import Bom, LicenseChoice
4+
from cyclonedx.model.component import Component, ComponentType
5+
from cyclonedx.model.dependency import Dependency
6+
from cyclonedx.output.json import JsonV1Dot4
7+
from cyclonedx.output.xml import XmlV1Dot4
8+
from packageurl import PackageURL
9+
10+
lFac = LicenseFactory()
11+
12+
# region build the BOM
13+
14+
bom = Bom()
15+
bom.metadata.component = rootComponent = Component(
16+
name='myApp',
17+
type=ComponentType.APPLICATION,
18+
licenses=[LicenseChoice(license=lFac.make_from_string('MIT'))],
19+
bom_ref='myApp',
20+
)
21+
22+
component = Component(
23+
type=ComponentType.LIBRARY,
24+
name='some-component',
25+
group='acme',
26+
version='1.33.7-beta.1',
27+
licenses=[LicenseChoice(license=lFac.make_from_string('(c) 2021 Acme inc.'))],
28+
supplier=OrganizationalEntity(
29+
name='Acme Inc',
30+
urls=[XsUri('https://www.acme.org')]
31+
),
32+
bom_ref='[email protected]',
33+
purl=PackageURL('generic', 'acme', 'some-component', '1.33.7-beta.1')
34+
)
35+
36+
bom.components.add(component)
37+
bom.dependencies.add(Dependency(rootComponent.bom_ref, [Dependency(component.bom_ref)]))
38+
39+
# endregion build the BOM
40+
41+
42+
serializedJSON = JsonV1Dot4(bom).output_as_string()
43+
print(serializedJSON)
44+
45+
serializedXML = XmlV1Dot4(bom).output_as_string()
46+
print(serializedXML)

0 commit comments

Comments
 (0)