|
| 1 | +# Defines the "License" recipe key that can be used to describe the package license. |
| 2 | +# If the package uses a vanilla license with a known spdx.id the license can be specified as: |
| 3 | +# |
| 4 | +# License: "GPL-3.0" |
| 5 | +# |
| 6 | +# and for custom licenses with a license file: |
| 7 | +# |
| 8 | +# License: |
| 9 | +# Id: "devel.m4" |
| 10 | +# Paths: "COPYING" |
| 11 | +# |
| 12 | +# The plugin also offers string functions to get these information back into the environment |
| 13 | +# to be used by the licenses class. |
| 14 | + |
| 15 | +# To enable the validation check of the license expression the license-expression module must be |
| 16 | +# installed. (e.g. 'pip install license-expression'). |
| 17 | + |
| 18 | +from bob.errors import ParseError |
| 19 | +from bob.input import PluginState, PluginProperty |
| 20 | +from bob.tty import Warn |
| 21 | +import schema |
| 22 | + |
| 23 | +def expression_check(license): |
| 24 | + try: |
| 25 | + get_spdx_licensing().parse(license, validate=True, strict=True) |
| 26 | + except ExpressionError as e: |
| 27 | + # a raised exception message is not shown (?) |
| 28 | + print(e) |
| 29 | + return False |
| 30 | + else: |
| 31 | + return True |
| 32 | + |
| 33 | +def dummyCheck (license): |
| 34 | + return True |
| 35 | + |
| 36 | +try: |
| 37 | + from license_expression import get_spdx_licensing, ExpressionError |
| 38 | +except ImportError as e: |
| 39 | + Warn ("Unable to import license_expression module. License expressions will not be checked!").warn() |
| 40 | + license_check = dummyCheck |
| 41 | +else: |
| 42 | + license_check = expression_check |
| 43 | + |
| 44 | +LICENSE_SHEMA = schema.Schema(schema.Or(str, schema.Schema({'Id' : str, 'Paths' : str}))) |
| 45 | + |
| 46 | +class LicenseProperty(PluginProperty): |
| 47 | + @staticmethod |
| 48 | + def validate(data): |
| 49 | + try: |
| 50 | + lic = LICENSE_SHEMA.validate(data) |
| 51 | + except schema.SchemaError as e: |
| 52 | + print(e) |
| 53 | + return False |
| 54 | + if isinstance(lic, str): |
| 55 | + return license_check(lic) |
| 56 | + return True |
| 57 | + |
| 58 | +def pkgLicense(args, env, recipe, **kwargs): |
| 59 | + lic = recipe.getPluginProperties().get('License').getValue() |
| 60 | + if lic is not None: |
| 61 | + if isinstance(lic, str): |
| 62 | + return lic |
| 63 | + else: |
| 64 | + return "LicenseRef-"+lic.get('Id') |
| 65 | + return "UNSET" |
| 66 | + #raise ParseError(f"{recipe.getName()} has no LICENSE") |
| 67 | + |
| 68 | +def pkgLicensePaths(args, env, recipe, **kwargs): |
| 69 | + lic = recipe.getPluginProperties().get('License').getValue() |
| 70 | + if lic and isinstance(lic, dict): |
| 71 | + return lic.get('Paths', "") |
| 72 | + return "" |
| 73 | + |
| 74 | +manifest = { |
| 75 | + 'apiVersion' : "1.1", |
| 76 | + 'properties' : { |
| 77 | + 'License' : LicenseProperty, |
| 78 | + }, |
| 79 | + 'stringFunctions' : { |
| 80 | + 'pkg_license' : pkgLicense, |
| 81 | + 'pkg_license_paths' : pkgLicensePaths |
| 82 | + } |
| 83 | +} |
0 commit comments