Giter VIP home page Giter VIP logo

license-expression's Introduction

license-expression

license-expression is 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.4+

Build and tests status

Branch Linux (Travis) MacOSX (Travis) Windows (AppVeyor)
Master Linux 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

Parse an expression, then simplify and compare:

>>> from license_expression import Licensing
>>> l = Licensing()
>>> expr = l.parse(" GPL-2.0 or LGPL 2.1 and mit ")
>>> str(expr)
'GPL-2.0 OR (LGPL 2.1 AND mit)'
>>> l.license_symbols(expr)
[LicenseSymbol('GPL-2.0'), LicenseSymbol('LGPL 2.1'), LicenseSymbol('mit')]
>>> str(expr)
'GPL-2.0 OR (LGPL 2.1 AND mit)'
>>> print(expr.pretty())
OR(
  LicenseSymbol('GPL-2.0'),
  AND(
    LicenseSymbol('LGPL 2.1'),
    LicenseSymbol('mit')
  )
)
>>> expr2 = l.parse(" GPL-2.0 or (mit and LGPL 2.1) ")
>>> expr2.simplify() == expr.simplify()
True
>>> expr3 = l.parse("mit and LGPL 2.1")
>>> expr3 in expr2
True

An expression can be simplified:

>>> expr2 = l.parse(" GPL-2.0 or (mit and LGPL 2.1) or bsd Or GPL-2.0  or (mit and LGPL 2.1)")
>>> str(expr2.simplify())
'GPL-2.0 OR bsd OR (LGPL 2.1 AND mit)'

Two expressions can be compared for equivalence and containment:

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

An expression can be validated and normalized using a list of reference license keys (or ids), names and aliases:

>>> from license_expression import LicenseRef, Licensing
>>> license_refs = [
...    LicenseRef('gpl-2.0', 'GPL-2.0', ['The GNU GPL 20'], False),
...    LicenseRef('gpl-2.0+', 'GPL-2.0+', ['The GNU GPL 20 or later'], False),
...    LicenseRef('lgpl-2.1', 'LGPL-2.1', ['LGPL v2.1'], False),
...    LicenseRef('lgpl-2.1-plus', 'LGPL-2.1+', ['LGPL v2.1 or later', 'LGPL-2.1 or later'], False),
...    LicenseRef('mit', 'MIT', ['MIT license'], False),
...    LicenseRef('classpath-2.0', 'Classpath-2.0', ['Classpath-2.0 Exception'], True)
... ]
>>> l = Licensing(license_refs)
>>> expr = l.parse("The GNU GPL 20 or LGPL-2.1 and mit")
>>> str(expr)
'The GNU GPL 20 OR (LGPL-2.1 AND mit)'
>>> expr = l.resolve(expr)
>>> str(expr)
'GPL-2.0 OR (LGPL-2.1 AND MIT)'

The cases of a license with an exception or "or later version" are handled correctly:

>>> expr = l.parse("The GNU GPL 20 or later with Classpath-2.0 Exception or LGPL-2.1 or later and mit2")
>>> l.license_symbols(expr)
[LicenseSymbol('The GNU GPL 20 or later WITH Classpath-2.0 Exception'), LicenseSymbol('LGPL-2.1 or later'), LicenseSymbol('mit2')]
>>> expr = l.resolve(expr)
>>> l.unresolved_keys(expr) == ['mit2']
True
>>> str(expr)
'GPL-2.0+ WITH Classpath-2.0 OR (LGPL-2.1+ AND mit2)'

Here if we add mit2 as an alias, the expression resolves alright:

>>> license_refs = [
...    LicenseRef('gpl-2.0', 'GPL-2.0', ['The GNU GPL 20'], False),
...    LicenseRef('lgpl-2.1', 'LGPL-2.1', ['LGPL v2.1'], False),
...    LicenseRef('lgpl-2.1-plus', 'LGPL-2.1+', ['LGPL v2.1 or later', 'LGPL-2.1 or later'], False),
...    LicenseRef('mit', 'MIT', ['MIT license', 'mit2'], False),
...    LicenseRef('classpath-2.0', 'Classpath-2.0', ['Classpath-2.0 Exception'], True)
... ]
>>> l = Licensing(license_refs)
>>> expr = l.parse("The GNU GPL 20 with Classpath-2.0 Exception or LGPL-2.1 or later and mit2", resolve=True)
>>> l.resolution_errors(expr)
[]
>>> str(expr)
'GPL-2.0 WITH Classpath-2.0 OR (LGPL-2.1+ AND MIT)'

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

license-expression's People

Contributors

pombredanne avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.