License-Expression

30.4.4 · active · verified Sun Apr 05

license-expression is a comprehensive Python utility library designed to parse, compare, simplify, and normalize license expressions, particularly SPDX license expressions, using boolean logic. It actively maintains and updates its bundled license keys from the SPDX License List and ScanCode LicenseDB, with frequent patch releases for these updates and minor releases for broader changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the SPDX licensing engine and use it to parse, pretty-print, check equivalence, and validate license expressions. The `get_spdx_licensing()` call can be computationally intensive, so its result should be cached and reused.

from license_expression import get_spdx_licensing

# Get a licensing object. This can be slow and should be reused.
licensing = get_spdx_licensing()

# Parse a license expression
expression_str = 'GPL-2.0-only OR LGPL-2.1-only AND MIT'
parsed_expression = licensing.parse(expression_str)

print(f"Original: {expression_str}")
print(f"Parsed (pretty): {parsed_expression.pretty()}")

# Check for equivalence
expression_str_2 = 'MIT AND (GPL-2.0-only OR LGPL-2.1-only)'
parsed_expression_2 = licensing.parse(expression_str_2)
print(f"Is '{expression_str}' equivalent to '{expression_str_2}'? {licensing.is_equivalent(parsed_expression, parsed_expression_2)}")

# Validate an expression
valid = licensing.validate(expression_str)
print(f"Is '{expression_str}' a valid expression? {valid.errors if valid.errors else True}")

invalid_expression = 'Invalid-License AND Other-License'
invalid_valid = licensing.validate(invalid_expression)
print(f"Is '{invalid_expression}' a valid expression? {invalid_valid.errors if invalid_valid.errors else True}")

view raw JSON →