Debian Codegraph @

maven-packager-utils/src/main/java/org/debian/maven/packager/util/LicensesScanner.java

raw
1 /*
2 * Copyright 2012 Ludovic Claude.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.debian.maven.packager.util;
18
19 import org.apache.maven.model.License;
20 import org.debian.maven.packager.interaction.SimpleQuestion;
21
22 import java.util.List;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 public class LicensesScanner {
27
28 public Set<String> discoverLicenses(List<License> projectLicenses) {
29 Set<String> licenses = new TreeSet<String>();
30 for (License license : projectLicenses) {
31 String licenseName = license.getName() != null ? license.getName() + " " : "";
32 String licenseUrl = license.getUrl() != null ? license.getUrl() : "";
33
34 if (!recognizeLicense(licenses, licenseName, licenseUrl)) {
35 String s = new SimpleQuestion("License " + licenseName + licenseUrl + " was not recognized, " +
36 "please enter a license name preferably in one of: " + getAvailableLicenses()).ask();
37 if (s.length() > 0) {
38 licenses.add(s);
39 }
40 }
41 }
42
43 System.out.println();
44 System.out.println("Checking licenses in the upstream sources...");
45 LicenseCheckResult licenseResult = new LicenseCheckResult();
46 IOUtil.executeProcess(new String[]{"/bin/sh", "-c", "licensecheck `find . -type f`"}, licenseResult);
47 for (String license : licenseResult.getLicenses()) {
48 if (!recognizeLicense(licenses, license, "")) {
49 String s = new SimpleQuestion("License " + license + " was not recognized, " +
50 "please enter a license name preferably in one of:" + getAvailableLicenses()).ask();
51 if (s.length() > 0) {
52 licenses.add(s);
53 }
54 }
55 }
56
57 if (licenses.isEmpty()) {
58 String s = new SimpleQuestion("License was not found, please enter a license name preferably in one of:" + getAvailableLicenses()).ask();
59 if (s.length() > 0) {
60 licenses.add(s);
61 }
62 }
63 return licenses;
64 }
65
66 private String getAvailableLicenses() {
67 return "Apache-2.0 Artistic BSD-2-clause BSD-3-clause BSD-4-clause ISC CC-BY CC-BY-SA\n"
68 + "CC-BY-ND CC-BY-NC CC-BY-NC-SA CC-BY-NC-ND CC0 CDDL CDDL-1.1 CPL Eiffel EPL-1.0 EPL-2.0 Expat\n"
69 + "GPL-2 GPL-3 LGPL-2 LGPL-2.1 LGPL-3 GFDL-1.2 GFDL-1.3 GFDL-NIV LPPL MPL-1.1\n"
70 + "MPL-2.0 Perl PSF QPL W3C-Software ZLIB Zope";
71 }
72
73 boolean recognizeLicense(Set<String> licenses, String licenseName, String licenseUrl) {
74 boolean recognized = false;
75 licenseName = licenseName.toLowerCase();
76 licenseUrl = licenseUrl.toLowerCase();
77 if (licenseName.contains("mit ") || licenseUrl.contains("mit-license")) {
78 licenses.add("MIT");
79 recognized = true;
80 } else if (licenseName.matches(".*bsd \\(?2[ -]clause\\)?.*") || licenseUrl.contains("bsd-2-clause")) {
81 licenses.add("BSD-2-clause");
82 recognized = true;
83 } else if (licenseName.matches(".*bsd \\(?3[ -]clause\\)?.*") || licenseUrl.contains("bsd-3-clause")) {
84 licenses.add("BSD-3-clause");
85 recognized = true;
86 } else if (licenseName.matches(".*bsd \\(?4[ -]clause\\)?.*") || licenseUrl.contains("bsd-4-clause")) {
87 licenses.add("BSD-4-clause");
88 recognized = true;
89 } else if (licenseName.contains("bsd ") || licenseUrl.contains("bsd-license")) {
90 licenses.add("BSD");
91 recognized = true;
92 } else if (licenseName.contains("artistic ") || licenseUrl.contains("artistic-license")) {
93 licenses.add("Artistic");
94 recognized = true;
95 } else if (licenseName.contains("apache ") || licenseUrl.contains("apache")) {
96 if (licenseName.contains("2.") || licenseUrl.contains("2.")) {
97 licenses.add("Apache-2.0");
98 recognized = true;
99 } else if (licenseName.contains("1.0") || licenseUrl.contains("1.0")) {
100 licenses.add("Apache-1.0");
101 recognized = true;
102 } else if (licenseName.contains("1.1") || licenseUrl.contains("1.1")) {
103 licenses.add("Apache-1.1");
104 recognized = true;
105 }
106 } else if (licenseName.contains("eclipse") || licenseName.contains("epl") || licenseUrl.contains("epl")) {
107 if (licenseName.contains("1.0") || licenseUrl.contains("v10")) {
108 licenses.add("EPL-1.0");
109 recognized = true;
110 } else if (licenseName.contains("2.0") || licenseUrl.contains("2.0")) {
111 licenses.add("EPL-2.0");
112 recognized = true;
113 }
114 } else if (licenseName.contains("lgpl ") || licenseUrl.contains("lgpl")) {
115 if (licenseName.contains("2.1") || licenseUrl.contains("2.1")) {
116 licenses.add("LGPL-2.1");
117 recognized = true;
118 } else if (licenseName.contains("2") || licenseUrl.contains("2")) {
119 licenses.add("LGPL-2");
120 recognized = true;
121 } else if (licenseName.contains("3") || licenseUrl.contains("3")) {
122 licenses.add("LGPL-2");
123 recognized = true;
124 }
125 } else if (licenseName.contains("gpl ") || licenseUrl.contains("gpl")) {
126 if (licenseName.contains("2") || licenseUrl.contains("2")) {
127 licenses.add("GPL-2");
128 recognized = true;
129 } else if (licenseName.contains("3") || licenseUrl.contains("3")) {
130 licenses.add("GPL-3");
131 recognized = true;
132 }
133 } else if (licenseName.contains("mpl") || licenseUrl.contains("mpl")) {
134 if (licenseName.contains("1.1") || licenseUrl.contains("1.1")) {
135 licenses.add("MPL-1.1");
136 recognized = true;
137 } else if (licenseName.contains("2.0") || licenseUrl.contains("2.0")) {
138 licenses.add("MPL-2.0");
139 recognized = true;
140 }
141 } else if (licenseName.contains("cddl") || licenseUrl.contains("cddl")) {
142 if (licenseName.contains("1.1") || licenseUrl.contains("1.1")) {
143 licenses.add("CDDL-1.1");
144 recognized = true;
145 } else if (licenseName.contains("1.0") || licenseUrl.contains("1.0")) {
146 licenses.add("CDDL");
147 recognized = true;
148 }
149 } else if (licenseUrl.contains("http://creativecommons.org/licenses/by-sa/3.0")) {
150 licenses.add("CC-BY-SA-3.0");
151 recognized = true;
152 } else if (licenseName.contains("generated file")) {
153 // ignore the files reported as generated by licensecheck
154 recognized = true;
155 }
156 return recognized;
157 }
158
159 }