|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import org.gradle.api.GradleException |
| 21 | +import org.gradle.api.file.ArchiveOperations |
| 22 | +import java.io.File |
| 23 | + |
| 24 | +/** The jars bundled in a binary distribution tarball and the LICENSE and NOTICE it ships. */ |
| 25 | +internal class BinaryDistributionContents( |
| 26 | + val bundledJars: Set<String>, |
| 27 | + val license: String, |
| 28 | + val notice: String, |
| 29 | +) |
| 30 | + |
| 31 | +/** |
| 32 | + * The rules shared by [CheckBinaryLicenseTask] and [UpdateBinaryLicenseTask], so that the update |
| 33 | + * task produces exactly what the check task accepts. |
| 34 | + */ |
| 35 | +internal object BinaryLicenseSupport { |
| 36 | + private val licenseEntryRegex = Regex("^[^/]+/LICENSE$") |
| 37 | + private val noticeEntryRegex = Regex("^[^/]+/NOTICE$") |
| 38 | + private val nameExclusionSubstrings = listOf( |
| 39 | + "pulsar-client", |
| 40 | + "pulsar-cli-utils", |
| 41 | + "pulsar-common", |
| 42 | + "pulsar-http-client-api", |
| 43 | + "pulsar-package", |
| 44 | + "pulsar-tls-factory-api", |
| 45 | + "pulsar-websocket", |
| 46 | + ) |
| 47 | + |
| 48 | + /** The NOTICE reference that does not need to be bundled. */ |
| 49 | + const val NOTICE_EXEMPT_JAR = "checker-qual.jar" |
| 50 | + |
| 51 | + private val jarReferenceRegex = Regex(""".* (.*\.jar).*""") |
| 52 | + private const val JAR_NAME_CHARS = "A-Za-z0-9_.+-" |
| 53 | + private val digitRunRegex = Regex("""\d+""") |
| 54 | + |
| 55 | + fun readDistribution(archiveOperations: ArchiveOperations, tarFile: File): BinaryDistributionContents { |
| 56 | + val bundledJars = sortedSetOf<String>() |
| 57 | + var licenseContent: String? = null |
| 58 | + var noticeContent: String? = null |
| 59 | + |
| 60 | + archiveOperations.tarTree(tarFile).visit { |
| 61 | + if (isDirectory) return@visit |
| 62 | + val path = relativePath.pathString |
| 63 | + when { |
| 64 | + path.endsWith(".jar") -> { |
| 65 | + val inExcludedDir = path.contains("/examples/") || path.contains("/instances/") |
| 66 | + val nameExcluded = nameExclusionSubstrings.any { name.contains(it) } |
| 67 | + if (!inExcludedDir && !nameExcluded) { |
| 68 | + bundledJars.add(name) |
| 69 | + } |
| 70 | + } |
| 71 | + licenseEntryRegex.matches(path) -> licenseContent = file.readText() |
| 72 | + noticeEntryRegex.matches(path) -> noticeContent = file.readText() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return BinaryDistributionContents( |
| 77 | + bundledJars, |
| 78 | + licenseContent |
| 79 | + ?: throw GradleException("Could not find a top-level LICENSE entry in ${tarFile.name}"), |
| 80 | + noticeContent |
| 81 | + ?: throw GradleException("Could not find a top-level NOTICE entry in ${tarFile.name}"), |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + /** The jar referenced on each line of a LICENSE or NOTICE text: the last one on the line. */ |
| 86 | + fun extractJarReferences(content: String): List<String> = |
| 87 | + content.lines().mapNotNull { line -> jarReferenceRegex.matchEntire(line)?.groupValues?.get(1) } |
| 88 | + |
| 89 | + /** Whether LICENSE has to mention [jar]; the Pulsar jars themselves need no entry. */ |
| 90 | + fun needsLicenseEntry(jar: String): Boolean = !jar.contains("org.apache.pulsar") |
| 91 | + |
| 92 | + /** |
| 93 | + * Whether a LICENSE or NOTICE reference matches a bundled jar. A reference may contain |
| 94 | + * wildcards like "org.rocksdb.*.jar", so it is treated as a regex, matching the legacy bash |
| 95 | + * `grep -q $J` semantics. |
| 96 | + */ |
| 97 | + fun isBundled(reference: String, bundledJars: Collection<String>): Boolean { |
| 98 | + val pattern = Regex(reference) |
| 99 | + return bundledJars.any { pattern.containsMatchIn(it) } |
| 100 | + } |
| 101 | + |
| 102 | + /** Replaces each whole-word occurrence of a key of [renames] in [content] with its value. */ |
| 103 | + fun replaceJarNames(content: String, renames: Map<String, String>): String = |
| 104 | + renames.entries.fold(content) { text, (from, to) -> |
| 105 | + Regex("(?<![$JAR_NAME_CHARS])${Regex.escape(from)}(?![$JAR_NAME_CHARS])") |
| 106 | + .replace(text, Regex.escapeReplacement(to)) |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Pairs each [stale] reference, which no longer matches a bundled jar, with the jar in |
| 111 | + * [candidates] that is the same artifact at another version. |
| 112 | + * |
| 113 | + * A candidate is the same artifact when both names share a prefix that ends with "-" and is |
| 114 | + * followed by a digit in both, as in "netty-codec-" + "4.2.20.Final.jar". Of these, the |
| 115 | + * candidate whose remainder has the same shape, with each run of digits treated as equal, is |
| 116 | + * chosen, which keeps classifiers apart: "...-4.2.21.Final-linux-x86_64.jar" replaces |
| 117 | + * "...-4.2.20.Final-linux-x86_64.jar" and not "...-4.2.20.Final-linux-aarch_64.jar". When no |
| 118 | + * candidate has the same shape, as when "1.80.jar" becomes "1.80.1.jar", a single candidate |
| 119 | + * is still taken. Anything else is left for a human to resolve. |
| 120 | + */ |
| 121 | + fun matchVersionChanges(stale: Collection<String>, candidates: Collection<String>): JarVersionMatches { |
| 122 | + val chosen = mutableMapOf<String, String>() |
| 123 | + val ambiguous = mutableMapOf<String, List<String>>() |
| 124 | + val unmatched = mutableListOf<String>() |
| 125 | + |
| 126 | + for (reference in stale) { |
| 127 | + val sameArtifact = candidates.mapNotNull { jar -> |
| 128 | + versionStart(reference, jar)?.let { start -> jar to start } |
| 129 | + } |
| 130 | + val sameShape = sameArtifact.filter { (jar, start) -> |
| 131 | + shape(reference.substring(start)) == shape(jar.substring(start)) |
| 132 | + } |
| 133 | + val match = sameShape.singleOrNull() ?: if (sameShape.isEmpty()) sameArtifact.singleOrNull() else null |
| 134 | + when { |
| 135 | + match != null -> chosen[reference] = match.first |
| 136 | + sameArtifact.isEmpty() -> unmatched.add(reference) |
| 137 | + else -> ambiguous[reference] = (sameShape.ifEmpty { sameArtifact }).map { it.first } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // A jar can replace only one reference; when several claim it, a human has to decide. |
| 142 | + val claimedTwice = chosen.values.groupingBy { it }.eachCount().filterValues { it > 1 }.keys |
| 143 | + val renames = chosen.filterValues { it !in claimedTwice } |
| 144 | + chosen.filterValues { it in claimedTwice }.forEach { (reference, jar) -> ambiguous[reference] = listOf(jar) } |
| 145 | + |
| 146 | + return JarVersionMatches(renames, ambiguous, unmatched) |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * The length of the longest shared prefix of [a] and [b] that ends with "-" and is followed |
| 151 | + * by a digit in both, or null when there is none. |
| 152 | + */ |
| 153 | + private fun versionStart(a: String, b: String): Int? { |
| 154 | + if (a == b) return null |
| 155 | + val common = a.commonPrefixWith(b).length |
| 156 | + for (i in common downTo 1) { |
| 157 | + if (a[i - 1] == '-' && i < a.length && i < b.length && a[i].isDigit() && b[i].isDigit()) { |
| 158 | + return i |
| 159 | + } |
| 160 | + } |
| 161 | + return null |
| 162 | + } |
| 163 | + |
| 164 | + private fun shape(versionAndRest: String): String = digitRunRegex.replace(versionAndRest, "#") |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * The result of [BinaryLicenseSupport.matchVersionChanges]: the [renames] to apply, the |
| 169 | + * references with several possible replacements, and the references that match no candidate. |
| 170 | + */ |
| 171 | +internal data class JarVersionMatches( |
| 172 | + val renames: Map<String, String>, |
| 173 | + val ambiguous: Map<String, List<String>>, |
| 174 | + val unmatched: List<String>, |
| 175 | +) |