Lib4SBOM

0.10.3 · active · verified Thu Apr 16

Lib4SBOM is a Python library designed for parsing, generating, and validating Software Bills of Materials (SBOMs). It supports both SPDX and CycloneDX formats, offering a generic abstraction for SBOM data regardless of the underlying specification. Currently at version 0.10.3, the library maintains an active development pace with frequent minor releases and regular feature updates, addressing new specification versions and user-reported issues.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an existing SBOM file using the `SBOMParser` class. It creates a simple SPDX 2.3 TagValue file, parses it, and then extracts package information. The `sbom_type` parameter can be set to 'spdx', 'cyclonedx', or 'auto' for automatic detection.

import os
from lib4sbom.parser import SBOMParser

# Create a dummy SPDX SBOM file for demonstration
sbom_content = """
SPDXVersion: SPDX-2.3
DataLicense: CC0-1.0
SPDXID: SPDXRef-DOCUMENT
DocumentName: example-sbom
DocumentNamespace: https://spdx.org/spdxdocs/spdx-example-44455566-31b3-40e1-b4f0-4660f9450c26
Creator: Tool: lib4sbom-example
Created: 2026-04-16T12:00:00Z

PackageName: SamplePackage
SPDXID: SPDXRef-Package-Sample
PackageVersion: 1.0.0
PackageSupplier: Organization: Example Org (contact@example.org)
PackageDownloadLocation: NOASSERTION
PackageLicenseConcluded: MIT
PackageLicenseDeclared: MIT
PackageCopyrightText: NOASSERTION
"""

example_sbom_file = "example.spdx"
with open(example_sbom_file, "w") as f:
    f.write(sbom_content)

# Initialize the SBOM parser
sbom_parser = SBOMParser(sbom_type='spdx') # 'auto' can also be used, or 'cyclonedx'

# Parse the SBOM file
try:
    sbom_parser.parse_file(example_sbom_file)
    print(f"Successfully parsed SBOM type: {sbom_parser.get_type()}")

    # Retrieve packages and print their names
    packages = sbom_parser.get_packages()
    if packages:
        print("Packages found:")
        for pkg in packages:
            print(f"  - {pkg.get_name()} ({pkg.get_version()})")
    else:
        print("No packages found in SBOM.")

except FileNotFoundError:
    print(f"Error: SBOM file '{example_sbom_file}' not found.")
except Exception as e:
    print(f"An error occurred during parsing: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(example_sbom_file):
        os.remove(example_sbom_file)

view raw JSON →