Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use licenseRefLookup to normalize license expression #1202

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 9 additions & 36 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,49 +416,22 @@ function joinExpressions(expressions) {
return SPDX.normalize(joinedExpressionString)
}

function normalizeLicenseExpression(rawLicenseExpression, logger) {
function normalizeLicenseExpression(
rawLicenseExpression,
logger,
licenseRefLookup = token => token && scancodeMap.get(token)
) {
if (!rawLicenseExpression) return null

const licenseVisitor = licenseExpression => {
return scancodeMap.get(licenseExpression) || SPDX.normalizeSingle(licenseExpression)
}

// parse() checks for LicenseRef- and other special types of expressions before calling the visitor
// therefore use the mapped license expression as an argument if it was found
const mappedLicenseExpression = scancodeMap.get(rawLicenseExpression)
const parsed = SPDX.parse(mappedLicenseExpression || rawLicenseExpression || '', licenseVisitor)

// normalize the parsed license expression will recursively normalize the parsed license expression
const normalizedParsed = _normalizeParsedLicenseExpression(parsed, logger)

const result = SPDX.stringify(normalizedParsed)
const licenseVisitor = licenseExpression =>
scancodeMap.get(licenseExpression) || SPDX.normalizeSingle(licenseExpression)
const parsed = SPDX.parse(rawLicenseExpression, licenseVisitor, licenseRefLookup)
const result = SPDX.stringify(parsed)
if (result === 'NOASSERTION') logger.info(`ScanCode NOASSERTION from ${rawLicenseExpression}`)

return result
}

function _normalizeParsedLicenseExpression(parsedLicenseExpression, logger) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good to see this move into the parser. Thanks for making this happen!

if (parsedLicenseExpression.left) {
if (parsedLicenseExpression.left.hasOwnProperty('left')) {
parsedLicenseExpression.left = _normalizeParsedLicenseExpression(parsedLicenseExpression.left, logger)
} else if (parsedLicenseExpression.left.hasOwnProperty('noassertion')) {
const new_left = normalizeLicenseExpression(parsedLicenseExpression.left['noassertion'], logger)
if (new_left.toLowerCase() === 'noassertion') parsedLicenseExpression.right = { 'noassertion': new_left }
else parsedLicenseExpression.left = { license: new_left }
}
}
if (parsedLicenseExpression.right) {
if (parsedLicenseExpression.right.hasOwnProperty('left')) {
parsedLicenseExpression.right = _normalizeParsedLicenseExpression(parsedLicenseExpression.right, logger)
} else if (parsedLicenseExpression.right.hasOwnProperty('noassertion')) {
const new_right = normalizeLicenseExpression(parsedLicenseExpression.right['noassertion'], logger)
if (new_right.toLowerCase() === 'noassertion') parsedLicenseExpression.right = { 'noassertion': new_right }
else parsedLicenseExpression.right = { license: new_right }
}
}
return parsedLicenseExpression
}

function _normalizeVersion(version) {
if (version == '1') return '1.0.0' // version '1' is not semver valid see https://github.com/clearlydefined/crawler/issues/124
return semver.valid(version) ? version : null
Expand Down
4 changes: 2 additions & 2 deletions providers/summary/scancode/legacy-summarizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ScanCodeLegacySummarizer {

_readLicenseExpressionFromSummary(harvested) {
const licenseExpression = get(harvested, 'content.summary.packages[0].license_expression')
const result = licenseExpression && normalizeLicenseExpression(licenseExpression, this.logger)
const result = licenseExpression && normalizeLicenseExpression(licenseExpression, this.logger, null)
return result?.includes('NOASSERTION') ? null : result
}

Expand Down Expand Up @@ -196,7 +196,7 @@ class ScanCodeLegacySummarizer {
_createExpressionFromLicense(license) {
const rule = license.matched_rule
if (!rule || !rule.license_expression) return SPDX.normalize(license.spdx_license_key)
return normalizeLicenseExpression(rule.license_expression, this.logger)
return normalizeLicenseExpression(rule.license_expression, this.logger, null)
}
}

Expand Down