Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add exaple how to build and serialize #397

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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='[email protected]',
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)