SPDX Tools Python Library

0.8.5 · active · verified Fri Apr 10

The `spdx-tools` library is a Python implementation providing parsers, converters, validators, and handlers for SPDX (Software Package Data Exchange) documents. It supports SPDX specification versions 2.2 and 2.3, with experimental, write-only support for the upcoming SPDX v3.0 specification. Maintained by a community of SPDX adopters, it helps users with security, compliance, and understanding software supply chain dependencies. The current version is 0.8.5, with an active development pace that includes significant refactoring in recent major releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a simple SPDX Tag/Value document from a string using a temporary file. The `parse_file` function is the primary entry point for parsing, capable of handling various SPDX formats.

import io
import os
import tempfile
from spdx_tools.spdx.parser.parse_anything import parse_file
from spdx_tools.spdx.model import Document

# Example SPDX Tag/Value content
spdx_content = """SPDXVersion: SPDX-2.3
DataLicense: CC0-1.0
SPDXID: SPDXRef-DOCUMENT
DocumentName: example-software-bill-of-materials
DocumentNamespace: http://spdx.org/spdxdocs/spdx-example-document-1.0
Creator: Tool: spdx-tools-python-0.8.5
Created: 2026-04-10T12:00:00Z
"""

temp_file_name = None
try:
    # Write content to a temporary file for parsing
    with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=".spdx") as temp_file:
        temp_file.write(spdx_content)
        temp_file_name = temp_file.name
    
    # Parse the SPDX file
    document: Document = parse_file(temp_file_name)
    
    print(f"Successfully parsed SPDX document.")
    print(f"  Document Name: {document.name}")
    print(f"  SPDX Version: {document.spdx_version}")
    print(f"  Data License: {document.data_license}")
    
except Exception as e:
    print(f"An error occurred during parsing: {e}")
finally:
    # Clean up the temporary file
    if temp_file_name and os.path.exists(temp_file_name):
        os.remove(temp_file_name)

view raw JSON →