Skip to content

Commit 1364f81

Browse files
committed
add license helpers
1 parent ee0ce4d commit 1364f81

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

classes/licenses.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
metaEnvironment:
2+
PKG_LICENSE: "$(pkg_license)"
3+
4+
privateEnvironment:
5+
PKG_LICENSE_PATH: "$(pkg_license_paths)"
6+
7+
buildVars: [PKG_LICENSE, PKG_LICENSE_PATH]
8+
buildScript: |
9+
if [ -z ${PKG_LICENSE:-} ]; then
10+
echo "PKG_LICENSE is missing!" 1>&2
11+
exit 1
12+
fi
13+
mkdir -p .bob
14+
if [[ ${PKG_LICENSE} == LicenseRef* ]]; then
15+
ID=${PKG_LICENSE#LicenseRef-}
16+
if [[ $ID =~ ^[A-Za-z0-9.\-]+$ ]]; then
17+
mkdir -p .bob/licenses/$ID
18+
else
19+
echo "LicenseRef contains invalid characters: ${PKG_LICENSE}"
20+
false
21+
fi
22+
while read -r lic; do
23+
_path=""
24+
_file=${lic}
25+
if [[ ${lic} == *:* ]]; then
26+
IFS=: read -r _path _file <<< ${lic}
27+
fi
28+
mkdir -p .bob/licenses/$ID/${_path:-}
29+
cp $1/$_file .bob/licenses/$ID/${_path:-}
30+
done <<< ${PKG_LICENSE_PATH}
31+
else
32+
echo "${PKG_LICENSE}" >> .bob/license
33+
fi
34+
35+
packageScript: |
36+
cp -r $1/.bob .

config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ plugins:
66
- vsenv
77
- msbuild
88
- config
9+
- licenses

plugins/licenses.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)