Skip to content

Commit e467e67

Browse files
authored
Enhance license detection for various licenses (#31198)
This commit enhances the license detection that we have for various licenses. Here we improve the detection for all licenses (especially the Apache 2.0 License), the BSD 2-clause license, the MIT (with attribution) license, and we add detection for the BSD 3-clause license. One way that we achieved this improvement is by changing how the license files are read so that rather than reading them as a multi-line string which ended up represented as "[line1, line2, line3, ...]" internally, we read the full bytes of the license text and replace all whitespace with a single space so the license text is now loaded as "line1 line2 line3". For the MIT license we add the actual license text and remove the "MIT" string as not all copies of the license clearly indicate that the text is the MIT license. We take a similar strategy for the BSD-2 and BSD-3 clause licenses. With this change, we reduce the number of "custom" licenses in the codebase from 31 to 2. The two remaining appear to be truly custom licenses, not carrying licenses identifiable by SPDX. A follow-up will address "unknown" licenses.
1 parent 8d4f09f commit e467e67

File tree

1 file changed

+79
-4
lines changed

1 file changed

+79
-4
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/DependenciesInfoTask.groovy

+79-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public class DependenciesInfoTask extends DefaultTask {
109109
}
110110

111111
if (license) {
112-
final String content = license.readLines("UTF-8").toString()
112+
// replace * because they are sometimes used at the beginning lines as if the license was a multi-line comment
113+
final String content = new String(license.readBytes(), "UTF-8").replaceAll("\\s+", " ").replaceAll("\\*", " ")
113114
final String spdx = checkSPDXLicense(content)
114115
if (spdx == null) {
115116
// License has not be identified as SPDX.
@@ -133,13 +134,84 @@ public class DependenciesInfoTask extends DefaultTask {
133134
private String checkSPDXLicense(final String licenseText) {
134135
String spdx = null
135136

136-
final String APACHE_2_0 = "Apache.*License.*(v|V)ersion 2.0"
137-
final String BSD_2 = "BSD 2-clause.*License"
137+
final String APACHE_2_0 = "Apache.*License.*(v|V)ersion.*2\\.0"
138+
139+
final String BSD_2 = """
140+
Redistribution and use in source and binary forms, with or without
141+
modification, are permitted provided that the following conditions
142+
are met:
143+
144+
1\\. Redistributions of source code must retain the above copyright
145+
notice, this list of conditions and the following disclaimer\\.
146+
2\\. Redistributions in binary form must reproduce the above copyright
147+
notice, this list of conditions and the following disclaimer in the
148+
documentation and/or other materials provided with the distribution\\.
149+
150+
THIS SOFTWARE IS PROVIDED BY .+ (``|''|")AS IS(''|") AND ANY EXPRESS OR
151+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
152+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\.
153+
IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT,
154+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\(INCLUDING, BUT
155+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
156+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\) HOWEVER CAUSED AND ON ANY
157+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
158+
\\(INCLUDING NEGLIGENCE OR OTHERWISE\\) ARISING IN ANY WAY OUT OF THE USE OF
159+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\.
160+
""".replaceAll("\\s+", "\\\\s*")
161+
162+
final String BSD_3 = """
163+
Redistribution and use in source and binary forms, with or without
164+
modification, are permitted provided that the following conditions
165+
are met:
166+
167+
(1\\.)? Redistributions of source code must retain the above copyright
168+
notice, this list of conditions and the following disclaimer\\.
169+
(2\\.)? Redistributions in binary form must reproduce the above copyright
170+
notice, this list of conditions and the following disclaimer in the
171+
documentation and/or other materials provided with the distribution\\.
172+
((3\\.)? The name of .+ may not be used to endorse or promote products
173+
derived from this software without specific prior written permission\\.|
174+
(3\\.)? Neither the name of .+ nor the names of its
175+
contributors may be used to endorse or promote products derived from
176+
this software without specific prior written permission\\.)
177+
178+
THIS SOFTWARE IS PROVIDED BY .+ (``|''|")AS IS(''|") AND ANY EXPRESS OR
179+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
180+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\.
181+
IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT,
182+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\(INCLUDING, BUT
183+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
184+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\) HOWEVER CAUSED AND ON ANY
185+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
186+
\\(INCLUDING NEGLIGENCE OR OTHERWISE\\) ARISING IN ANY WAY OUT OF THE USE OF
187+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\.
188+
""".replaceAll("\\s+", "\\\\s*")
189+
138190
final String CDDL_1_0 = "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.0"
139191
final String CDDL_1_1 = "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.1"
140192
final String ICU = "ICU License - ICU 1.8.1 and later"
141193
final String LGPL_3 = "GNU LESSER GENERAL PUBLIC LICENSE.*Version 3"
142-
final String MIT = "MIT License"
194+
195+
final String MIT = """
196+
Permission is hereby granted, free of charge, to any person obtaining a copy of
197+
this software and associated documentation files \\(the "Software"\\), to deal in
198+
the Software without restriction, including without limitation the rights to
199+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
200+
of the Software, and to permit persons to whom the Software is furnished to do
201+
so, subject to the following conditions:
202+
203+
The above copyright notice and this permission notice shall be included in all
204+
copies or substantial portions of the Software\\.
205+
206+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
207+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\\. IN NO EVENT SHALL THE
209+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
210+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
212+
SOFTWARE\\.
213+
""".replaceAll("\\s+", "\\\\s*")
214+
143215
final String MOZILLA_1_1 = "Mozilla Public License.*Version 1.1"
144216

145217
switch (licenseText) {
@@ -152,6 +224,9 @@ public class DependenciesInfoTask extends DefaultTask {
152224
case ~/.*${BSD_2}.*/:
153225
spdx = 'BSD-2-Clause'
154226
break
227+
case ~/.*${BSD_3}.*/:
228+
spdx = 'BSD-3-Clause'
229+
break
155230
case ~/.*${LGPL_3}.*/:
156231
spdx = 'LGPL-3.0'
157232
break

0 commit comments

Comments
 (0)