Skip to content

Commit

Permalink
Check LICENSE containing valid SPDX like code
Browse files Browse the repository at this point in the history
Closes #426

Signed-off-by: Konrad Weihmann <[email protected]>
  • Loading branch information
priv-kweihmann committed Aug 23, 2023
1 parent c05e695 commit 7611388
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Rules marked with **[S]** can have multiple sub-IDs
* oelint.var.filesoverride - FILES:*(FILES_*) variables should not be overridden
* oelint.var.improperinherit - Warn about improperly named inherits
* oelint.var.licenseremotefile - License shall be a file in remote source not a local file
* oelint.var.licensesdpx - Check for correct SPDX syntax in licenses
* oelint.var.mandatoryvar - Check for mandatory variables **[S]**
* oelint.var.multiinclude - Warn on including the same file more than once
* oelint.var.multiinherit - Warn on inherit the same file more than once
Expand Down Expand Up @@ -189,8 +190,7 @@ Rules marked with **[S]** can have multiple sub-IDs
* oelint.vars.pbpusage - \$\{BP\} should be used instead of \$\{P\} **[F]**
* oelint.vars.pkgspecific - Variable is package-specific, but isn't set in that way **[S]**
* oelint.vars.pnbpnusage - \$\{BPN\} should be used instead of \$\{PN\} **[F]**
* oelint.vars.pnusagediscouraged - Variable shouldn't contain \$\{PN\} or
* \$\{BPN\}
* oelint.vars.pnusagediscouraged - Variable shouldn't contain \$\{PN\} or \$\{BPN\}
* oelint.vars.sectionlowercase - SECTION should be lowercase only **[F]**
* oelint.vars.spacesassignment - ' = ' should be correct variable assignment
* oelint.vars.specific - Variable is specific to an unknown identifier
Expand Down
19 changes: 19 additions & 0 deletions oelint_adv/rule_base/rule_var_license_spdx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from oelint_parser.cls_item import Variable
from oelint_adv.cls_rule import Rule


class LicenseSDPX(Rule):
def __init__(self):
super().__init__(id='oelint.vars.licensesdpx',
severity='warning',
message='LICENSE is not a valid OpenEmbedded SPDX expression')

def check(self, _file, stash):
res = []
items = stash.GetItemsFor(filename=_file, classifier=Variable.CLASSIFIER,
attribute=Variable.ATTR_VAR)
for i in [x for x in items if x.VarName == 'LICENSE']:
if (('|' in i.VarValueStripped and ' | ' not in i.VarValueStripped) or
('&' in i.VarValueStripped and ' & ' not in i.VarValueStripped)):
res += self.finding(i.Origin, i.InFileLine)
return res
47 changes: 47 additions & 0 deletions tests/test_class_oelint_var_licensespdx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest # noqa: I900

from .base import TestBaseClass


class TestClassOelintVarLicenseSPDX(TestBaseClass):

@pytest.mark.parametrize('id_', ['oelint.vars.licensesdpx'])
@pytest.mark.parametrize('occurrence', [1])
@pytest.mark.parametrize('input_',
[
{
'oelint_adv_test.bb':
'''
LICENSE:a = "ISC &MIT"
''',
},
{
'oelint_adv_test.bb':
'''
LICENSE:e = "(ISC|MIT) & Apache-2.0 & BSD-3-Clause"
''',
},
],
)
def test_bad(self, input_, id_, occurrence):
self.check_for_id(self._create_args(input_), id_, occurrence)

@pytest.mark.parametrize('id_', ['oelint.var.licensesdpx'])
@pytest.mark.parametrize('occurrence', [0])
@pytest.mark.parametrize('input_',
[
{
'oelint_adv_test.bb':
'''
LICENSE:${PN} = "BSD-2-Clause"
LICENSE:b = "(ISC | MIT)"
LICENSE:c = "(BSD-2-Clause | MIT)"
LICENSE:d = "(BSD-2-Clause | MIT) & MIT"
LICENSE:f = "BSD-3-Clause"
LICENSE = "Unknown"
''',
},
],
)
def test_good(self, input_, id_, occurrence):
self.check_for_id(self._create_args(input_), id_, occurrence)

0 comments on commit 7611388

Please sign in to comment.