-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically generate adblock component LICENSE
The generated LICENSE includes a preamble which clarifies that these licenses apply to lists which are downloaded at run time and are separate from the Brave Browser (fixes #7150).
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* Copyright (c) 2019 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
const path = require('path') | ||
const config = require('./config') | ||
const fs = require('fs-extra') | ||
|
||
const knownMissing = new Set([ | ||
// Emailed author (henrik@schack.dk) on 2019-11-05. | ||
path.join('components', 'third_party', 'adblock', 'lists', 'adblock_dk'), | ||
// https://github.com/gfmaster/adblock-korea-contrib/issues/47 | ||
path.join('components', 'third_party', 'adblock', 'lists', 'adblock-korea-contrib'), | ||
// https://github.com/tcptomato/ROad-Block/issues/55 | ||
path.join('components', 'third_party', 'adblock', 'lists', 'road-block') | ||
]) | ||
|
||
const extractLicenseInfo = (directory) => { | ||
const readmePath = path.join(directory, 'README.chromium') | ||
if (!fs.existsSync(readmePath)) { | ||
console.error('Missing README.chromium in ' + directory) | ||
process.exit(1) | ||
} | ||
|
||
const readmeContents = fs.readFileSync(readmePath).toString().split('\n') | ||
|
||
let metadata = { | ||
'slug': path.basename(directory), | ||
'Name': null, | ||
'URL': null, | ||
'License': null, | ||
'License File': null, | ||
'License Text': null | ||
} | ||
|
||
for (const line of readmeContents) { | ||
for (let field in metadata) { | ||
if (line.indexOf(field + ':') > -1) { | ||
metadata[field] = line.substr(field.length + 1).trim() | ||
break | ||
} | ||
} | ||
} | ||
|
||
if (!metadata['License File']) { | ||
const licensePath = path.join(directory, 'LICENSE') | ||
if (fs.existsSync(licensePath)) { | ||
metadata['License Text'] = fs.readFileSync(licensePath) | ||
} else if (metadata['License'] === 'unknown') { | ||
const relativeDir = directory.substr(config.projects['brave-core'].dir.length + 1) | ||
if (!knownMissing.has(relativeDir)) { | ||
console.log(knownMissing) | ||
console.error('Unknown license is not whitelisted: ' + relativeDir) | ||
process.exit(1) | ||
} | ||
} else { | ||
console.error('Missing LICENSE in ' + directory) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
return metadata | ||
} | ||
|
||
const readLicenseText = (filename) => { | ||
// README.chromium files assume UNIX path separators | ||
let fullPath = config.srcDir | ||
for (const piece of filename.split('/')) { | ||
fullPath = path.join(fullPath, piece) | ||
} | ||
|
||
if (!fs.existsSync(fullPath)) { | ||
console.error('License file not found: ' + fullPath) | ||
process.exit(1) | ||
} | ||
|
||
return fs.readFileSync(fullPath) | ||
} | ||
|
||
const generateExternalComponentLicenseFile = (preamble, components) => { | ||
let componentNotices = '' | ||
let licenseText = '' | ||
|
||
let licenses = {} | ||
for (let component of components) { | ||
if (componentNotices.length != 0) { | ||
componentNotices += '\n' | ||
} | ||
|
||
let licenseId = component['License'] | ||
const licenseText = component['License Text'] | ||
if (licenseText) { | ||
// Custom license | ||
licenseId += '-' + component['slug'] | ||
} | ||
|
||
componentNotices += 'Name: ' + component['Name'] + '\n' + | ||
'URL: ' + component['URL'] + '\n' + | ||
'License: ' + licenseId + '\n' | ||
|
||
if (licenseId === 'unknown') { | ||
continue | ||
} | ||
|
||
if (!licenses.hasOwnProperty(licenseId)) { | ||
if (licenseText) { | ||
licenses[licenseId] = licenseText | ||
} else { | ||
licenses[licenseId] = readLicenseText(component['License File']) | ||
} | ||
} | ||
} | ||
|
||
for (const id of Object.keys(licenses).sort()) { | ||
licenseText += '--------------------------------------------------------------------------------\n' | ||
licenseText += id + ':\n\n' | ||
licenseText += licenses[id] + '\n' | ||
} | ||
|
||
return preamble + '\n\n' + componentNotices + '\n' + licenseText | ||
} | ||
|
||
const licensing = { | ||
updateLicenses: () => { | ||
console.log('regenerating LICENSE files...') | ||
const braveComponentsThirdPartyDir = path.join(config.projects['brave-core'].dir, 'components', 'third_party') | ||
|
||
// Brave Ad Block component | ||
const braveAdBlockDir = path.join(braveComponentsThirdPartyDir, 'adblock') | ||
const braveAdBlockListsDir = path.join(braveAdBlockDir, 'lists') | ||
|
||
let adblockComponents = [] | ||
fs.readdirSync(braveAdBlockListsDir).forEach(file => { | ||
const dirPath = path.join(braveAdBlockListsDir, file) | ||
if (fs.statSync(dirPath).isDirectory()) { | ||
adblockComponents.push(extractLicenseInfo(dirPath)) | ||
} | ||
}) | ||
|
||
let adblockPreamble = 'These licenses do not apply to any of the code shipped with the Brave Browser, but may apply to lists downloaded after installation for use with the Brave Shields feature. The Brave Browser and such lists are separate and independent works.' | ||
let adblockLicense = generateExternalComponentLicenseFile(adblockPreamble, adblockComponents) | ||
fs.writeFileSync(path.join(braveComponentsThirdPartyDir, 'adblock', 'LICENSE'), adblockLicense) | ||
console.log(' - ' + adblockComponents.length + ' sub-components added in adblock/LICENSE') | ||
} | ||
} | ||
|
||
module.exports = licensing |