Skip to content

Commit

Permalink
docs: add exaple how to build and serialize (#397)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck authored Jun 28, 2023
1 parent 2180d31 commit 65e22bd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

cyclonedx/schema/** linguist-language
cyclonedx/schema/** linguist-vendored

23 changes: 23 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
Examples
========

Build & Serialize
-----------------

.. literalinclude:: ../examples/build_and_serialize.py
:language: python
:linenos:
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ programmatically generate SBOMs.

install
architecture
examples
contributing
support
changelog
Expand All @@ -48,4 +49,4 @@ programmatically generate SBOMs.
.. _CycloneDX Python: https://pypi.org/project/cyclonedx-bom/
.. _Jake: https://pypi.org/project/jake
.. _CycloneDX Tool Center: https://cyclonedx.org/tool-center/
.. _official examples: https://cyclonedx.org/capabilities/bomlink/#linking-external-vex-to-bom-inventory
.. _official examples: https://cyclonedx.org/capabilities/bomlink/#linking-external-vex-to-bom-inventory
46 changes: 46 additions & 0 deletions examples/build_and_serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from cyclonedx.factory.license import LicenseFactory
from cyclonedx.model import OrganizationalEntity, XsUri
from cyclonedx.model.bom import Bom, LicenseChoice
from cyclonedx.model.component import Component, ComponentType
from cyclonedx.model.dependency import Dependency
from cyclonedx.output.json import JsonV1Dot4
from cyclonedx.output.xml import XmlV1Dot4
from packageurl import PackageURL

lFac = LicenseFactory()

# region build the BOM

bom = Bom()
bom.metadata.component = rootComponent = Component(
name='myApp',
type=ComponentType.APPLICATION,
licenses=[LicenseChoice(license=lFac.make_from_string('MIT'))],
bom_ref='myApp',
)

component = Component(
type=ComponentType.LIBRARY,
name='some-component',
group='acme',
version='1.33.7-beta.1',
licenses=[LicenseChoice(license=lFac.make_from_string('(c) 2021 Acme inc.'))],
supplier=OrganizationalEntity(
name='Acme Inc',
urls=[XsUri('https://www.acme.org')]
),
bom_ref='myComponent@1.33.7-beta.1',
purl=PackageURL('generic', 'acme', 'some-component', '1.33.7-beta.1')
)

bom.components.add(component)
bom.dependencies.add(Dependency(rootComponent.bom_ref, [Dependency(component.bom_ref)]))

# endregion build the BOM


serializedJSON = JsonV1Dot4(bom).output_as_string()
print(serializedJSON)

serializedXML = XmlV1Dot4(bom).output_as_string()
print(serializedXML)

0 comments on commit 65e22bd

Please sign in to comment.