|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2021 Ericsson and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | + * with the GNU Classpath Exception which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + ********************************************************************************/ |
| 16 | +// @ts-check |
| 17 | + |
| 18 | +const cp = require('child_process'); |
| 19 | +const fs = require('fs'); |
| 20 | +const path = require('path'); |
| 21 | +const readline = require('readline'); |
| 22 | + |
| 23 | +const NO_COLOR = Boolean(process.env['NO_COLOR']); |
| 24 | +const dashLicensesJar = path.resolve(__dirname, 'download/dash-licenses.jar'); |
| 25 | +const dashLicensesSummary = path.resolve(__dirname, '../license-check-summary.txt'); |
| 26 | +const dashLicensesBaseline = path.resolve(__dirname, '../license-check-baseline.json'); |
| 27 | +const dashLicensesUrl = 'https://repo.eclipse.org/service/local/artifact/maven/redirect?r=dash-licenses&g=org.eclipse.dash&a=org.eclipse.dash.licenses&v=LATEST'; |
| 28 | + |
| 29 | +main().catch(error => { |
| 30 | + console.error(error); |
| 31 | + process.exit(1); |
| 32 | +}); |
| 33 | + |
| 34 | +async function main() { |
| 35 | + if (!fs.existsSync(dashLicensesJar)) { |
| 36 | + info('Fetching dash-licenses...'); |
| 37 | + fs.mkdirSync(path.dirname(dashLicensesJar), { recursive: true }); |
| 38 | + const curlError = getErrorFromStatus(spawn( |
| 39 | + 'curl', ['-L', dashLicensesUrl, '-o', dashLicensesJar], |
| 40 | + )); |
| 41 | + if (curlError) { |
| 42 | + error(curlError); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + } |
| 46 | + if (fs.existsSync(dashLicensesSummary)) { |
| 47 | + info('Backing up previous summary...'); |
| 48 | + fs.renameSync(dashLicensesSummary, `${dashLicensesSummary}.old`); |
| 49 | + } |
| 50 | + info('Running dash-licenses...'); |
| 51 | + const dashError = getErrorFromStatus(spawn( |
| 52 | + 'java', ['-jar', dashLicensesJar, 'yarn.lock', '-batch', '50', '-timeout', '240', '-summary', dashLicensesSummary], |
| 53 | + { stdio: ['ignore', 'ignore', 'inherit'] }, |
| 54 | + )); |
| 55 | + if (dashError) { |
| 56 | + warn(dashError); |
| 57 | + } |
| 58 | + const restricted = await getRestrictedDependenciesFromSummary(dashLicensesSummary); |
| 59 | + if (restricted.length > 0) { |
| 60 | + if (fs.existsSync(dashLicensesBaseline)) { |
| 61 | + info('Checking results against the baseline...'); |
| 62 | + const baseline = readBaseline(dashLicensesBaseline); |
| 63 | + const unmatched = new Set(baseline.keys()); |
| 64 | + const unhandled = restricted.filter(entry => { |
| 65 | + unmatched.delete(entry.dependency); |
| 66 | + return !baseline.has(entry.dependency); |
| 67 | + }); |
| 68 | + if (unmatched.size > 0) { |
| 69 | + warn('Some entries in the baseline did not match anything from dash-licences output:'); |
| 70 | + for (const dependency of unmatched) { |
| 71 | + console.log(magenta(`> ${dependency}`)); |
| 72 | + const data = baseline.get(dependency); |
| 73 | + if (data) { |
| 74 | + console.warn(`${dependency}:`, data); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + if (unhandled.length > 0) { |
| 79 | + error(`Found results that aren't part of the baseline!`); |
| 80 | + logRestrictedDashSummaryEntries(unhandled); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + } else { |
| 84 | + error(`Found unhandled restricted dependencies!`); |
| 85 | + logRestrictedDashSummaryEntries(restricted); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + } |
| 89 | + info('Done.'); |
| 90 | + process.exit(0); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * @param {Iterable<DashSummaryEntry>} entries |
| 95 | + * @return {void} |
| 96 | + */ |
| 97 | +function logRestrictedDashSummaryEntries(entries) { |
| 98 | + for (const { dependency: entry, license } of entries) { |
| 99 | + console.log(red(`X ${entry}, ${license}`)); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @param {string} summary path to the summary file. |
| 105 | + * @returns {Promise<DashSummaryEntry[]>} list of restriced dependencies. |
| 106 | + */ |
| 107 | +async function getRestrictedDependenciesFromSummary(summary) { |
| 108 | + const restricted = []; |
| 109 | + for await (const entry of readSummaryLines(summary)) { |
| 110 | + if (entry.status.toLocaleLowerCase() === 'restricted') { |
| 111 | + restricted.push(entry); |
| 112 | + } |
| 113 | + } |
| 114 | + return restricted.sort( |
| 115 | + (a, b) => a.dependency.localeCompare(b.dependency) |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Read each entry from dash's summary file and collect each entry. |
| 121 | + * This is essentially a cheap CSV parser. |
| 122 | + * @param {string} summary path to the summary file. |
| 123 | + * @returns {AsyncIterableIterator<DashSummaryEntry>} reading completed. |
| 124 | + */ |
| 125 | +async function* readSummaryLines(summary) { |
| 126 | + for await (const line of readline.createInterface(fs.createReadStream(summary))) { |
| 127 | + const [dependency, license, status, source] = line.split(', '); |
| 128 | + yield { dependency, license, status, source }; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Handle both list and object format for the baseline json file. |
| 134 | + * @param {string} baseline path to the baseline json file. |
| 135 | + * @returns {Map<string, any>} map of dependencies to ignore if restricted, value is an optional data field. |
| 136 | + */ |
| 137 | +function readBaseline(baseline) { |
| 138 | + const json = JSON.parse(fs.readFileSync(baseline, 'utf8')); |
| 139 | + if (Array.isArray(json)) { |
| 140 | + return new Map(json.map(element => [element, null])); |
| 141 | + } else if (typeof json === 'object' && json !== null) { |
| 142 | + return new Map(Object.entries(json)); |
| 143 | + } |
| 144 | + console.error(`ERROR: Invalid format for "${baseline}"`); |
| 145 | + process.exit(1); |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Spawn a process. Exits with code 1 on spawn error (e.g. file not found). |
| 150 | + * @param {string} bin |
| 151 | + * @param {string[]} args |
| 152 | + * @param {import('child_process').SpawnSyncOptions} [opts] |
| 153 | + * @returns {import('child_process').SpawnSyncReturns} |
| 154 | + */ |
| 155 | +function spawn(bin, args, opts = {}) { |
| 156 | + opts = { stdio: 'inherit', ...opts }; |
| 157 | + /** @type {any} */ |
| 158 | + const status = cp.spawnSync(bin, args, opts); |
| 159 | + // Add useful fields to the returned status object: |
| 160 | + status.bin = bin; |
| 161 | + status.args = args; |
| 162 | + status.opts = opts; |
| 163 | + // Abort on spawn error: |
| 164 | + if (status.error) { |
| 165 | + console.error(status.error); |
| 166 | + process.exit(1); |
| 167 | + } |
| 168 | + return status; |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * @param {import('child_process').SpawnSyncReturns} status |
| 173 | + * @returns {string | undefined} Error message if the process errored, `undefined` otherwise. |
| 174 | + */ |
| 175 | +function getErrorFromStatus(status) { |
| 176 | + if (typeof status.signal === 'string') { |
| 177 | + return `Command ${prettyCommand(status)} exited with signal: ${status.signal}`; |
| 178 | + } else if (status.status !== 0) { |
| 179 | + return `Command ${prettyCommand(status)} exited with code: ${status.status}`; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * @param {any} status |
| 185 | + * @param {number} [indent] |
| 186 | + * @returns {string} Pretty command with both bin and args as stringified JSON. |
| 187 | + */ |
| 188 | +function prettyCommand(status, indent = 2) { |
| 189 | + return JSON.stringify([status.bin, ...status.args], undefined, indent); |
| 190 | +} |
| 191 | + |
| 192 | +function info(text) { console.warn(cyan(`INFO: ${text}`)); } |
| 193 | +function warn(text) { console.warn(yellow(`WARN: ${text}`)); } |
| 194 | +function error(text) { console.error(red(`ERROR: ${text}`)); } |
| 195 | + |
| 196 | +function style(code, text) { return NO_COLOR ? text : `\x1b[${code}m${text}\x1b[0m`; } |
| 197 | +function cyan(text) { return style(96, text); } |
| 198 | +function magenta(text) { return style(95, text); } |
| 199 | +function yellow(text) { return style(93, text); } |
| 200 | +function red(text) { return style(91, text); } |
| 201 | + |
| 202 | +/** |
| 203 | + * @typedef {object} DashSummaryEntry |
| 204 | + * @property {string} dependency |
| 205 | + * @property {string} license |
| 206 | + * @property {string} status |
| 207 | + * @property {string} source |
| 208 | + */ |
0 commit comments