1 package org.apache.lucene.validation;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 import java.io.FileFilter;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
35 public class DependencyChecker {
36 private static Set<String> excludes = new HashSet<String>();
37 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
40 //Collections.addAll(excludes, );
43 public static void main(String[] args) throws IOException {
44 String dumpFile = null;
45 List<String> dirs = new ArrayList<String>();
46 for (int i = 0; i < args.length; i++) {
47 if (args[i].equalsIgnoreCase("--dump") || args[i].equalsIgnoreCase("-d")) {
50 } else if (args[i].equalsIgnoreCase("--check") || args[i].equalsIgnoreCase("-c")) {
56 FileWriter writer = null;
58 if (dumpFile != null) {
59 File out = new File(dumpFile);
60 System.out.println("Dumping to " + out);
61 writer = new FileWriter(out);
64 //TODO: put in NOTICE checks
65 for (String checkDir : dirs) {
66 File dir = new File(checkDir);
68 System.out.println("----------------------");
69 System.out.println("Starting on dir: " + dir);
71 File[] list = dir.listFiles();
72 File[] licFiles = dir.listFiles(new FileFilter() {
73 public boolean accept(File file) {
74 return file.getName().indexOf("-LICENSE") != -1 && file.getName().endsWith(".txt");//check for a consistent end, so that we aren't fooled by emacs ~ files or other temp files
77 File[] noticeFiles = dir.listFiles(new FileFilter() {
78 public boolean accept(File file) {
79 return file.getName().indexOf("-NOTICE") != -1 && file.getName().endsWith(".txt");
82 File[] jarFiles = dir.listFiles(new FileFilter() {
83 public boolean accept(File file) {
84 return file.getName().endsWith(".jar");
87 if (licFiles.length == 0 && jarFiles.length != 0) {
88 System.out.println("No license files found: " + dir);
91 if (jarFiles.length != licFiles.length) {
92 System.out.println("WARNING: There are missing LICENSE files in: " + dir + " Jar file count: " + jarFiles.length + " License Count: " + licFiles.length);
93 printDiffs(jarFiles, licFiles);
96 if (jarFiles.length != noticeFiles.length) {
97 System.out.println("WARNING: There may be missing NOTICE files in: " + dir + ". Note, not all files require a NOTICE. Jar file count: " + jarFiles.length + " Notice Count: " + noticeFiles.length);
98 //printDiffs(jarFiles, noticeFiles);
100 Map<String, UpdateableInt> licenseNames = new HashMap<String, UpdateableInt>();
101 for (int i = 0; i < licFiles.length; i++) {
102 licenseNames.put(licFiles[i].getName(), new UpdateableInt());
104 Map<String, UpdateableInt> noticeNames = new HashMap<String, UpdateableInt>();
105 for (int i = 0; i < noticeFiles.length; i++) {
106 noticeNames.put(noticeFiles[i].getName(), new UpdateableInt());
110 for (int i = 0; i < list.length; i++) {
112 String fileName = file.getName();
113 if (fileName.endsWith(".jar") && excludes.contains(fileName) == false) {
114 File licFile = getLicenseFile(file, licenseNames);
115 if (licFile != null && licFile.exists()) {
116 String licName = licFile.getName();
117 LicenseType[] types = getLicenseTypes(licName);
118 if (types != null && types.length > 0) {
119 for (int j = 0; j < types.length; j++) {
120 LicenseType type = types[j];
122 writer.write(file.getName() + "," + type.getDisplay() + LINE_SEPARATOR);
124 if (type.isNoticeRequired()) {
125 File noticeFile = getNoticeFile(file, noticeNames);
126 if (noticeFile != null && noticeFile.exists()) {
129 System.out.println("!!!!!! Missing NOTICE file for " + file + " and license type: " + type.getDisplay());
131 writer.write("Missing NOTICE file for " + file + LINE_SEPARATOR);
138 System.out.println("!!!!!! Couldn't determine license type for file: " + file);
140 writer.write("Invalid license for file: " + file + LINE_SEPARATOR);
145 System.out.println("!!!!!!! Couldn't get license file for " + file);
147 writer.write("Couldn't get license file for " + file + LINE_SEPARATOR);
154 writer.write(LINE_SEPARATOR + LINE_SEPARATOR);
155 writer.write("Other Licenses (installer, javascript, etc." + LINE_SEPARATOR);
159 for (Map.Entry<String, UpdateableInt> entry : licenseNames.entrySet()) {
160 if (entry.getValue().theInt == 0) {
161 LicenseType[] types = getLicenseTypes(entry.getKey());
162 if (types != null && types.length > 0) {
163 for (int i = 0; i < types.length; i++) {
164 writer.write(entry.getKey() + "," + types[i].getDisplay() + LINE_SEPARATOR);
167 System.out.println("Couldn't determine license for: " + entry.getKey());
172 if (writer != null) {
176 System.out.println("At least one file does not have a license, or it's license name is not in the proper format. See the logs.");
179 System.out.println("Found a license for every file in " + dir);
182 System.out.println("Could not find directory:" + dir);
189 * Sort the two lists and then print them out for visual comparison
195 private static void printDiffs(File[] left, File[] right) {
198 System.out.println("Left\t\t\tRight");
199 System.out.println("----------------");
200 StringBuilder bldr = new StringBuilder();
202 for (; i < left.length; i++) {
203 bldr.append(left[i]).append("\t\t\t");
204 if (i < right.length) {
205 bldr.append(right[i]);
207 bldr.append(LINE_SEPARATOR);
209 if (i < right.length) {
210 for (; i < right.length; i++) {
211 bldr.append("--- N/A ---\t\t\t").append(right[i]).append(LINE_SEPARATOR);
214 System.out.println(bldr.toString());
215 System.out.println("----------------");
218 private static LicenseType[] getLicenseTypes(String licName) {
219 LicenseType[] result = new LicenseType[0];
220 int idx = licName.lastIndexOf("-");
222 String licAbbrev = licName.substring(idx + 1, licName.length() - ".txt".length());
223 String[] lics = licAbbrev.split("__");
224 result = new LicenseType[lics.length];
225 for (int j = 0; j < lics.length; j++) {
227 result[j] = LicenseType.valueOf(lics[j].toUpperCase());
228 } catch (IllegalArgumentException e) {
229 System.out.println("Invalid license: " + lics[j].toUpperCase() + " for " + licName);
236 private static File getLicenseFile(File file, Map<String, UpdateableInt> licenseNames) {
238 String filename = file.getName();
240 for (String licName : licenseNames.keySet()) {
241 String prefix = licName.substring(0, licName.indexOf("-LICENSE"));
243 //System.out.println("prefix: " + prefix + " lic name: " + licName);
244 if (filename.toLowerCase().startsWith(prefix.toLowerCase())) {
245 result = new File(file.getParentFile(), licName);
246 UpdateableInt ui = licenseNames.get(licName);
252 //System.out.println("License File: " + result + " for file: " + file);
257 private static File getNoticeFile(File file, Map<String, UpdateableInt> noticeNames) {
259 String filename = file.getName();
261 for (String noticeName : noticeNames.keySet()) {
262 String prefix = noticeName.substring(0, noticeName.indexOf("-NOTICE"));
264 //System.out.println("prefix: " + prefix + " lic name: " + licName);
265 if (filename.toLowerCase().startsWith(prefix.toLowerCase())) {
266 result = new File(file.getParentFile(), noticeName);
267 UpdateableInt ui = noticeNames.get(noticeName);
273 //System.out.println("License File: " + result + " for file: " + file);
280 class UpdateableInt {