Utility library to parse, normalize and compare License expressions for Python using a boolean logic engine. For expressions using SPDX or any other license id scheme.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
etc
src/license_expression Add simple tokenizer and improve error checks #29 Aug 6, 2018
tests Add simple tokenizer and improve error checks #29 Aug 6, 2018
thirdparty Update and correct ABOUT files #29 Aug 6, 2018
.gitignore Update third-party packages May 24, 2018
.travis.yml
AUTHORS.rst Fix authors Dec 20, 2016
MANIFEST.in Do not include thirdparty in sdist Aug 24, 2016
NOTICE
README.rst Support nested license names #29 Aug 3, 2018
apache-2.0.LICENSE Initial commit Aug 23, 2016
appveyor.yml Add Python 3.6 to CI #22 Nov 1, 2017
configure Add failing tests for #29 Aug 2, 2018
configure.bat Enable multiple Python version in Appveyor test matrix Dec 20, 2016
license-expression.ABOUT Update ABOUT files for compatibility with latest SPEC #24 Feb 13, 2018
setup.cfg
setup.py
tox.ini

README.rst

license-expression

license-expression is a small utility library to parse, compare, simplify and normalize license expressions (e.g. SPDX license expressions) using boolean logic such as: GPL-2.0 or later WITH Classpath Exception AND MIT.

See also for details: https://spdx.org/sites/cpstandard/files/pages/files/spdxversion2.1.pdf#page=95&zoom=auto

license: apache-2.0

Python: 2.7 and 3.5+

Build and tests status

Branch Linux (Travis) MacOSX (Travis) Windows (AppVeyor)
Master MacOSX Master branch tests status MacOSX Master branch tests status Windows Master branch tests status

Source code and download

Support

Submit bugs and questions at:

Description

This module defines a mini language to parse, validate, simplify, normalize and compare license expressions using a boolean logic engine.

This supports SPDX license expressions and also accepts other license naming conventions and license identifiers aliases to resolve and normalize licenses.

Using boolean logic, license expressions can be tested for equality, containment, equivalence and can be normalized or simplified.

The main entry point is the Licensing object.

Usage examples

For example:

>>> from license_expression import Licensing, LicenseSymbol
>>> licensing = Licensing()
>>> expression = ' GPL-2.0 or LGPL-2.1 and mit '
>>> parsed = licensing.parse(expression)
>>> expected = 'GPL-2.0 OR (LGPL-2.1 AND mit)'
>>> assert expected == parsed.render('{symbol.key}')

>>> expected = [
...   LicenseSymbol('GPL-2.0'),
...   LicenseSymbol('LGPL-2.1'),
...   LicenseSymbol('mit')
... ]
>>> assert expected == licensing.license_symbols(expression)
>>> assert expected == licensing.license_symbols(parsed)

>>> symbols = ['GPL-2.0+', 'Classpath', 'BSD']
>>> licensing = Licensing(symbols)
>>> expression = 'GPL-2.0+ with Classpath or (bsd)'
>>> parsed = licensing.parse(expression)
>>> expected = 'GPL-2.0+ WITH Classpath OR BSD'
>>> assert expected == parsed.render('{symbol.key}')

>>> expected = [
...   LicenseSymbol('GPL-2.0+'),
...   LicenseSymbol('Classpath'),
...   LicenseSymbol('BSD')
... ]
>>> assert expected == licensing.license_symbols(parsed)
>>> assert expected == licensing.license_symbols(expression)

And expression can be simplified:

>>> expression2 = ' GPL-2.0 or (mit and LGPL 2.1) or bsd Or GPL-2.0  or (mit and LGPL 2.1)'
>>> parsed2 = licensing.parse(expression2)
>>> str(parsed2)
'GPL-2.0 OR (mit AND LGPL 2.1) OR BSD OR GPL-2.0 OR (mit AND LGPL 2.1)'
>>> assert str(parsed2.simplify()) == 'BSD OR GPL-2.0 OR (LGPL 2.1 AND mit)'

Two expressions can be compared for equivalence and containment:

>>> expr1 = licensing.parse(' GPL-2.0 or (LGPL 2.1 and mit) ')
>>> expr2 = licensing.parse(' (mit and LGPL 2.1)  or GPL-2.0 ')
>>> licensing.is_equivalent(expr1, expr2)
True
>>> licensing.is_equivalent(' GPL-2.0 or (LGPL 2.1 and mit) ',
...                         ' (mit and LGPL 2.1)  or GPL-2.0 ')
True
>>> expr1.simplify() == expr2.simplify()
True
>>> expr3 = licensing.parse(' GPL-2.0 or mit or LGPL 2.1')
>>> licensing.is_equivalent(expr2, expr3)
False
>>> expr4 = licensing.parse('mit and LGPL 2.1')
>>> expr4.simplify() in expr2.simplify()
True
>>> licensing.contains(expr2, expr4)
True

Development

  • Checkout a clone from https://github.com/nexB/license-expression.git
  • Then run ./configure (or configure.bat) and then source bin/activate. This will install all vendored dependencies in a local virtualenv, including development deps.
  • To run the tests, run py.test -vvs