Python library to parse, validate and create SPDX documents
—
Comprehensive writing functionality for SPDX documents supporting all major output formats with automatic format detection, optional validation, and flexible output options.
Write SPDX documents to files with automatic format detection based on file extension.
def write_file(document: Document, file_name: str, validate: bool = True):
"""
Write SPDX document to file in any supported format.
Automatically detects format from file extension:
- .spdx, .tag -> Tag/Value format
- .json -> JSON format
- .yaml, .yml -> YAML format
- .xml -> XML format
- .rdf, .rdf.xml -> RDF/XML format
Args:
document: SPDX document to write
file_name: Output file path (format determined by extension)
validate: Whether to validate document before writing (default: True)
Raises:
ValidationError: If validation fails and validate=True
FileNotFoundError: If output directory doesn't exist
PermissionError: If cannot write to file
"""Write SPDX documents to JSON format files.
def write_document_to_file(document: Document, file_name: str, validate: bool = True) -> None:
"""
Write SPDX document to JSON file.
Args:
document: SPDX document to write
file_name: Output JSON file path
validate: Whether to validate before writing
Raises:
ValidationError: If validation fails and validate=True
"""Write SPDX documents to XML format files.
def write_document_to_file(document: Document, file_name: str, validate: bool = True) -> None:
"""
Write SPDX document to XML file.
Args:
document: SPDX document to write
file_name: Output XML file path
validate: Whether to validate before writing
Raises:
ValidationError: If validation fails and validate=True
"""Write SPDX documents to YAML format files.
def write_document_to_file(document: Document, file_name: str, validate: bool = True) -> None:
"""
Write SPDX document to YAML file.
Args:
document: SPDX document to write
file_name: Output YAML file path
validate: Whether to validate before writing
Raises:
ValidationError: If validation fails and validate=True
"""Write SPDX documents to Tag-Value format files.
def write_document_to_file(document: Document, file_name: str, validate: bool = True) -> None:
"""
Write SPDX document to Tag-Value file.
Args:
document: SPDX document to write
file_name: Output Tag-Value file path (.spdx extension)
validate: Whether to validate before writing
Raises:
ValidationError: If validation fails and validate=True
"""
def write_document(document: Document, stream) -> None:
"""
Write SPDX document to Tag-Value format stream.
Args:
document: SPDX document to write
stream: Output stream (e.g., sys.stdout, file object)
"""Write SPDX documents to RDF/XML format files.
def write_document_to_file(document: Document, file_name: str, validate: bool = True) -> None:
"""
Write SPDX document to RDF/XML file.
Args:
document: SPDX document to write
file_name: Output RDF file path (.rdf or .rdf.xml extension)
validate: Whether to validate before writing
Raises:
ValidationError: If validation fails and validate=True
"""from spdx_tools.spdx.writer.write_anything import write_file
from spdx_tools.spdx.parser.parse_anything import parse_file
# Parse a document and convert to different formats
document = parse_file("input.spdx")
# Write to different formats (format auto-detected from extension)
write_file(document, "output.json") # JSON format
write_file(document, "output.yaml") # YAML format
write_file(document, "output.xml") # XML format
write_file(document, "output.rdf") # RDF/XML format
write_file(document, "output.spdx") # Tag-Value format# Skip validation for faster writing (not recommended for production)
write_file(document, "output.json", validate=False)from spdx_tools.spdx.writer.json import json_writer
from spdx_tools.spdx.writer.xml import xml_writer
from spdx_tools.spdx.writer.yaml import yaml_writer
# Use format-specific writers directly
json_writer.write_document_to_file(document, "output.json")
xml_writer.write_document_to_file(document, "output.xml")
yaml_writer.write_document_to_file(document, "output.yaml")import sys
from spdx_tools.spdx.writer.tagvalue import tagvalue_writer
# Write Tag-Value format to stdout
tagvalue_writer.write_document(document, sys.stdout)
# Write to custom stream
with open("output.spdx", "w") as f:
tagvalue_writer.write_document(document, f)from spdx_tools.spdx.writer.write_anything import write_file
try:
write_file(document, "output.json")
print("Document written successfully")
except Exception as e:
print(f"Writing failed: {e}")import os
from spdx_tools.spdx.parser.parse_anything import parse_file
from spdx_tools.spdx.writer.write_anything import write_file
def convert_spdx_files(input_dir: str, output_dir: str, target_format: str):
"""Convert all SPDX files in directory to target format."""
for filename in os.listdir(input_dir):
if filename.endswith((".spdx", ".json", ".yaml", ".xml", ".rdf")):
input_path = os.path.join(input_dir, filename)
output_filename = os.path.splitext(filename)[0] + f".{target_format}"
output_path = os.path.join(output_dir, output_filename)
try:
document = parse_file(input_path)
write_file(document, output_path)
print(f"Converted {filename} -> {output_filename}")
except Exception as e:
print(f"Failed to convert {filename}: {e}")
# Convert all files to JSON
convert_spdx_files("input_docs/", "output_docs/", "json")from typing import TextIO
# All writer functions accept Document objects and file paths
# Stream writers accept any text stream (TextIO)Install with Tessl CLI
npx tessl i tessl/pypi-spdx-tools