pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / tools / java / org / apache / lucene / validation / DependencyChecker.java
1 package org.apache.lucene.validation;
2 /**
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import java.io.File;
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;
28 import java.util.Map;
29 import java.util.Set;
30
31 /**
32  *
33  *
34  **/
35 public class DependencyChecker {
36   private static Set<String> excludes = new HashSet<String>();
37   private static final String LINE_SEPARATOR = System.getProperty("line.separator");
38
39   static {
40     //Collections.addAll(excludes, );
41   }
42
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")) {
48         dumpFile = args[++i];
49
50       } else if (args[i].equalsIgnoreCase("--check") || args[i].equalsIgnoreCase("-c")) {
51         dirs.add(args[++i]);
52       }
53     }
54
55
56     FileWriter writer = null;
57     boolean dump = false;
58     if (dumpFile != null) {
59       File out = new File(dumpFile);
60       System.out.println("Dumping to " + out);
61       writer = new FileWriter(out);
62       dump = true;
63     }
64     //TODO: put in NOTICE checks
65     for (String checkDir : dirs) {
66       File dir = new File(checkDir);
67       if (dir.exists()) {
68         System.out.println("----------------------");
69         System.out.println("Starting on dir: " + dir);
70         int numFailed = 0;
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
75           }
76         });
77         File[] noticeFiles = dir.listFiles(new FileFilter() {
78           public boolean accept(File file) {
79             return file.getName().indexOf("-NOTICE") != -1 && file.getName().endsWith(".txt");
80           }
81         });
82         File[] jarFiles = dir.listFiles(new FileFilter() {
83           public boolean accept(File file) {
84             return file.getName().endsWith(".jar");
85           }
86         });
87         if (licFiles.length == 0 && jarFiles.length != 0) {
88           System.out.println("No license files found: " + dir);
89           numFailed++;
90         }
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);
94           numFailed++;
95         }
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);
99         }
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());
103         }
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());
107         }
108
109
110         for (int i = 0; i < list.length; i++) {
111           File file = list[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];
121                   if (dump == true) {
122                     writer.write(file.getName() + "," + type.getDisplay() + LINE_SEPARATOR);
123                   }
124                   if (type.isNoticeRequired()) {
125                     File noticeFile = getNoticeFile(file, noticeNames);
126                     if (noticeFile != null && noticeFile.exists()) {
127
128                     } else {
129                       System.out.println("!!!!!! Missing NOTICE file for " + file + " and license type: " + type.getDisplay());
130                       if (dump) {
131                         writer.write("Missing NOTICE file for " + file + LINE_SEPARATOR);
132                       }
133                       numFailed++;
134                     }
135                   }
136                 }
137               } else {
138                 System.out.println("!!!!!! Couldn't determine license type for file: " + file);
139                 if (dump == true) {
140                   writer.write("Invalid license for file: " + file + LINE_SEPARATOR);
141                 }
142                 numFailed++;
143               }
144             } else {
145               System.out.println("!!!!!!! Couldn't get license file for " + file);
146               if (dump == true) {
147                 writer.write("Couldn't get license file for " + file + LINE_SEPARATOR);
148               }
149               numFailed++;
150             }
151           }
152         }
153         if (dump == true) {
154           writer.write(LINE_SEPARATOR + LINE_SEPARATOR);
155           writer.write("Other Licenses (installer, javascript, etc." + LINE_SEPARATOR);
156         }
157
158         if (dump == true) {
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);
165                 }
166               } else {
167                 System.out.println("Couldn't determine license for: " + entry.getKey());
168               }
169             }
170           }
171         }
172         if (writer != null) {
173           writer.close();
174         }
175         if (numFailed > 0) {
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.");
177           System.exit(-1);
178         } else {
179           System.out.println("Found a license for every file in " + dir);
180         }
181       } else {
182         System.out.println("Could not find directory:" + dir);
183       }
184     }
185   }
186
187
188   /**
189    * Sort the two lists and then print them out for visual comparison
190    *
191    * @param left
192    * @param right
193    */
194
195   private static void printDiffs(File[] left, File[] right) {
196     Arrays.sort(left);
197     Arrays.sort(right);
198     System.out.println("Left\t\t\tRight");
199     System.out.println("----------------");
200     StringBuilder bldr = new StringBuilder();
201     int i = 0;
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]);
206       }
207       bldr.append(LINE_SEPARATOR);
208     }
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);
212       }
213     }
214     System.out.println(bldr.toString());
215     System.out.println("----------------");
216   }
217
218   private static LicenseType[] getLicenseTypes(String licName) {
219     LicenseType[] result = new LicenseType[0];
220     int idx = licName.lastIndexOf("-");
221     if (idx != -1) {
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++) {
226         try {
227           result[j] = LicenseType.valueOf(lics[j].toUpperCase());
228         } catch (IllegalArgumentException e) {
229           System.out.println("Invalid license: " + lics[j].toUpperCase() + " for " + licName);
230         }
231       }
232     }
233     return result;
234   }
235
236   private static File getLicenseFile(File file, Map<String, UpdateableInt> licenseNames) {
237     File result = null;
238     String filename = file.getName();
239     int length = 0;
240     for (String licName : licenseNames.keySet()) {
241       String prefix = licName.substring(0, licName.indexOf("-LICENSE"));
242       String name = null;
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);
247         ui.theInt++;
248       } else {
249       }
250
251     }
252     //System.out.println("License File: " + result + " for file: " + file);
253
254     return result;
255   }
256
257   private static File getNoticeFile(File file, Map<String, UpdateableInt> noticeNames) {
258     File result = null;
259     String filename = file.getName();
260     int length = 0;
261     for (String noticeName : noticeNames.keySet()) {
262       String prefix = noticeName.substring(0, noticeName.indexOf("-NOTICE"));
263       String name = null;
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);
268         ui.theInt++;
269       } else {
270       }
271
272     }
273     //System.out.println("License File: " + result + " for file: " + file);
274
275     return result;
276   }
277
278 }
279
280 class UpdateableInt {
281   public int theInt;
282 }