Mercurial > hg > truffle
annotate mxtool/CheckCopyright.java @ 21527:07b088d61d5d
added HotSpotJVMCIRuntime* classes, replaced references to HotSpotGraalRuntime in VM with HotSpotJVMCIRuntime (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Fri, 22 May 2015 23:26:20 +0200 |
parents | c190ed6b84bf |
children |
rev | line source |
---|---|
21472
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
1 /* |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
4 * |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
8 * |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
14 * |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
18 * |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
21 * questions. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
22 */ |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
23 import java.io.*; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
24 import java.net.URISyntaxException; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
25 import java.net.URL; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
26 import java.util.*; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
27 import java.util.regex.*; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
28 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
29 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
30 /** |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
31 * A program to check the existence and correctness of the copyright notice on a given set of sources. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
32 * Sources are defined to be those under management by Mercurial and various options are available |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
33 * to limit the set of sources scanned. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
34 */ |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
35 public class CheckCopyright { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
36 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
37 static class YearInfo { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
38 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
39 final int firstYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
40 final int lastYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
41 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
42 YearInfo(int firstYear, int lastYear) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
43 this.firstYear = firstYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
44 this.lastYear = lastYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
45 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
46 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
47 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
48 public boolean equals(Object other) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
49 final YearInfo yearInfo = (YearInfo) other; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
50 return yearInfo.firstYear == firstYear && yearInfo.lastYear == lastYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
51 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
52 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
53 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
54 public int hashCode() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
55 return firstYear ^ lastYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
56 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
57 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
58 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
59 static class Info extends YearInfo { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
60 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
61 final String fileName; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
62 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
63 Info(String fileName, int firstYear, int lastYear) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
64 super(firstYear, lastYear); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
65 this.fileName = fileName; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
66 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
67 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
68 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
69 public String toString() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
70 return fileName + " " + firstYear + ", " + lastYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
71 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
72 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
73 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
74 private static abstract class CopyrightHandler { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
75 enum CommentType{ |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
76 STAR, HASH |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
77 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
78 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
79 private static Map<String, CopyrightHandler> copyrightMap; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
80 private static String copyrightFiles = ".*/makefile|.*/Makefile|.*\\.sh|.*\\.bash|.*\\.mk|.*\\.java|.*\\.c|.*\\.h|.*\\.py|.*\\.g|.*\\.r"; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
81 private static Pattern copyrightFilePattern; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
82 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
83 protected final String suffix; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
84 private CopyrightHandler customHandler; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
85 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
86 CopyrightHandler(CommentType commentType) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
87 this.suffix = commentType.name().toLowerCase(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
88 initCopyrightMap(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
89 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
90 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
91 void addCustomhandler(CopyrightHandler copyrightHandler) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
92 this.customHandler = copyrightHandler; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
93 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
94 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
95 /** |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
96 * Add @code extension to files handled by this {@code CopyrightKind} |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
97 */ |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
98 protected void updateMap(String extension) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
99 copyrightMap.put(extension, this); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
100 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
101 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
102 static void addCopyrightFilesPattern(String pattern) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
103 copyrightFiles += "|" + pattern; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
104 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
105 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
106 protected abstract void readCopyrights() throws IOException; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
107 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
108 protected abstract Matcher getMatcher(String fileName, String fileContent) throws IOException; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
109 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
110 protected abstract String getText(String fileName) throws IOException ; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
111 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
112 protected abstract boolean handlesFile(String fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
113 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
114 /** |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
115 * Checks that the Oracle copyright year info was correct. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
116 * @return {@code false} if the year info was incorrect and was not fixed otherwise return {@code true} |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
117 * @throws IOException |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
118 */ |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
119 protected abstract boolean checkYearInfo(String fileName, String fileContent, Matcher matcher, Info info) throws IOException; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
120 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
121 static String getCopyrightText(String fileName) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
122 return getCopyrightHandler(fileName).getText(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
123 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
124 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
125 private static CopyrightHandler getCopyrightHandler(String fileName) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
126 initCopyrightMap(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
127 if (!copyrightFilePattern.matcher(fileName).matches()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
128 return null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
129 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
130 CopyrightHandler ck = getDefaultHandler(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
131 if (ck.customHandler != null && ck.customHandler.handlesFile(fileName)) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
132 return ck.customHandler; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
133 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
134 return ck; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
135 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
136 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
137 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
138 private static void initCopyrightMap() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
139 if (copyrightMap == null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
140 copyrightMap = new HashMap<String, CopyrightHandler>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
141 copyrightFilePattern = Pattern.compile(copyrightFiles); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
142 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
143 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
144 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
145 static CopyrightHandler getDefaultHandler(String fileName) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
146 int index = fileName.lastIndexOf(File.separatorChar); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
147 if (index > 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
148 fileName = fileName.substring(index + 1); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
149 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
150 String ext = ""; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
151 index = fileName.lastIndexOf('.'); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
152 if (index > 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
153 ext = fileName.substring(index + 1); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
154 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
155 if (fileName.equals("makefile")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
156 ext = "mk"; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
157 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
158 CopyrightHandler ck = copyrightMap.get(ext); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
159 assert ck != null : fileName; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
160 return ck; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
161 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
162 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
163 protected String readCopyright(InputStream is) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
164 byte[] b = new byte[16384]; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
165 int n = is.read(b); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
166 is.close(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
167 return new String(b, 0, n); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
168 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
169 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
170 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
171 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
172 private static class DefaultCopyrightHandler extends CopyrightHandler { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
173 private static String ORACLE_COPYRIGHT = "oracle.copyright"; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
174 private static String ORACLE_COPYRIGHT_REGEX = "oracle.copyright.regex"; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
175 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
176 private String copyrightRegex; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
177 private String copyright; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
178 Pattern copyrightPattern; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
179 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
180 DefaultCopyrightHandler(CopyrightHandler.CommentType commentType) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
181 super(commentType); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
182 if (commentType == CopyrightHandler.CommentType.STAR) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
183 updateMap("java"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
184 updateMap("c"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
185 updateMap("h"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
186 updateMap("g"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
187 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
188 updateMap("r"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
189 updateMap("R"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
190 updateMap("py"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
191 updateMap("sh"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
192 updateMap("mk"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
193 updateMap("bash"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
194 updateMap(""); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
195 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
196 readCopyrights(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
197 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
198 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
199 private String readCopyright(String name) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
200 String copyRightDir = COPYRIGHT_DIR.getValue(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
201 String fileName = "copyrights/" + name + "." + suffix; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
202 String copyrightPath; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
203 if (copyRightDir != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
204 copyrightPath = new File(new File(copyRightDir), fileName).getAbsolutePath(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
205 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
206 URL url = CheckCopyright.class.getResource(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
207 try { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
208 copyrightPath = url.toURI().getPath(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
209 } catch (URISyntaxException ex) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
210 throw new IOException(ex); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
211 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
212 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
213 InputStream is = new FileInputStream(copyrightPath); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
214 return readCopyright(is); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
215 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
216 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
217 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
218 protected void readCopyrights() throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
219 copyright = readCopyright(ORACLE_COPYRIGHT); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
220 copyrightRegex = readCopyright(ORACLE_COPYRIGHT_REGEX); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
221 copyrightPattern = Pattern.compile(copyrightRegex, Pattern.DOTALL); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
222 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
223 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
224 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
225 protected Matcher getMatcher(String fileName, String fileContent) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
226 return copyrightPattern.matcher(fileContent); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
227 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
228 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
229 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
230 protected String getText(String fileName) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
231 return copyright; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
232 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
233 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
234 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
235 protected boolean handlesFile(String fileName) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
236 return true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
237 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
238 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
239 /** |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
240 * Check the year info against the copyright header. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
241 * N.B. In the case of multiple matching groups, only the last group is checked. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
242 * I.e., only the last lines containing year info is checked/updated. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
243 */ |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
244 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
245 protected boolean checkYearInfo(String fileName, String fileContent, Matcher matcher, Info info) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
246 int yearInCopyright; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
247 int yearInCopyrightIndex; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
248 int groupCount = matcher.groupCount(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
249 String yearInCopyrightString = matcher.group(groupCount); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
250 yearInCopyright = Integer.parseInt(yearInCopyrightString); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
251 yearInCopyrightIndex = matcher.start(groupCount); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
252 if (yearInCopyright != info.lastYear) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
253 System.out.println(fileName + " copyright last modified year " + yearInCopyright + ", hg last modified year " + info.lastYear); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
254 if (FIX.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
255 // Use currentYear as that is what it will be when it's checked in! |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
256 System.out.println("updating last modified year of " + fileName + " to " + info.lastYear); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
257 // If the previous copyright only specified a single (initial) year, we convert it to the pair form |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
258 String newContent = fileContent.substring(0, yearInCopyrightIndex); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
259 if (matcher.group(groupCount - 1) == null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
260 // single year form |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
261 newContent += yearInCopyrightString + ", "; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
262 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
263 newContent += info.lastYear + fileContent.substring(yearInCopyrightIndex + 4); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
264 final FileOutputStream os = new FileOutputStream(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
265 os.write(newContent.getBytes()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
266 os.close(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
267 return true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
268 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
269 return false; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
270 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
271 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
272 return true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
273 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
274 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
275 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
276 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
277 private static class CustomCopyrightHandler extends CopyrightHandler { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
278 private Map<String, String> overrides = new HashMap<String, String>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
279 private CopyrightHandler defaultHandler; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
280 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
281 CustomCopyrightHandler(CopyrightHandler.CommentType commentType, CopyrightHandler defaultHandler) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
282 super(commentType); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
283 this.defaultHandler = defaultHandler; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
284 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
285 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
286 void addFile(String fileName, String copyright) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
287 overrides.put(fileName, copyright); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
288 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
289 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
290 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
291 protected void readCopyrights() throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
292 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
293 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
294 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
295 protected Matcher getMatcher(String fileName, String fileContent) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
296 String copyright = overrides.get(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
297 assert copyright != null : fileName; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
298 try (InputStream fs = new FileInputStream(copyright + "." + suffix + ".regex")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
299 return Pattern.compile(readCopyright(fs), Pattern.DOTALL).matcher(fileContent); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
300 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
301 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
302 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
303 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
304 protected String getText(String fileName) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
305 String copyright = overrides.get(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
306 assert copyright != null : fileName; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
307 try (InputStream fs = new FileInputStream(copyright + "." + suffix)) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
308 return readCopyright(fs); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
309 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
310 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
311 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
312 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
313 protected boolean handlesFile(String fileName) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
314 return overrides.get(fileName) != null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
315 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
316 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
317 @Override |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
318 protected boolean checkYearInfo(String fileName, String fileContent, Matcher matcher, Info info) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
319 // This is a bit tacky |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
320 String copyright = overrides.get(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
321 if (copyright.endsWith("no.copyright")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
322 return true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
323 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
324 return defaultHandler.checkYearInfo(fileName, fileContent, matcher, info); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
325 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
326 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
327 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
328 private static void initCopyrightKinds() throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
329 CopyrightHandler starHandler = new DefaultCopyrightHandler(CopyrightHandler.CommentType.STAR); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
330 CopyrightHandler hashHandler = new DefaultCopyrightHandler(CopyrightHandler.CommentType.HASH); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
331 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
332 String customCopyrightDir = CUSTOM_COPYRIGHT_DIR.getValue(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
333 if (customCopyrightDir != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
334 CustomCopyrightHandler customStarHandler = new CustomCopyrightHandler(CopyrightHandler.CommentType.STAR, starHandler); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
335 CustomCopyrightHandler customHashHandler = new CustomCopyrightHandler(CopyrightHandler.CommentType.HASH, hashHandler); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
336 starHandler.addCustomhandler(customStarHandler); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
337 hashHandler.addCustomhandler(customHashHandler); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
338 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
339 File overrides = new File(new File(customCopyrightDir), "overrides"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
340 if (overrides.exists()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
341 ArrayList<String> lines = new ArrayList<>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
342 boolean changed = false; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
343 try (BufferedReader br = new BufferedReader(new FileReader( |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
344 overrides))) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
345 while (true) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
346 String line = br.readLine(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
347 if (line == null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
348 break; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
349 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
350 if (line.length() == 0 || line.startsWith("#")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
351 lines.add(line); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
352 continue; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
353 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
354 String[] parts = line.split(","); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
355 // filename,copyright-file |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
356 CopyrightHandler defaultHandler = CopyrightHandler.getDefaultHandler(parts[0]); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
357 if (defaultHandler == null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
358 System.err.println("no default copyright handler for: " + parts[0]); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
359 System.exit(1); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
360 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
361 if (!new File(parts[0]).exists()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
362 System.err.printf("file %s in overrides file does not exist", parts[0]); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
363 if (FIX.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
364 System.err.print(" - removing"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
365 line = null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
366 changed = true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
367 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
368 System.err.println(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
369 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
370 if (line != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
371 lines.add(line); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
372 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
373 CustomCopyrightHandler customhandler = (CustomCopyrightHandler) defaultHandler.customHandler; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
374 customhandler.addFile(parts[0], new File(new File(customCopyrightDir), parts[1]).getAbsolutePath()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
375 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
376 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
377 if (changed) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
378 try (BufferedWriter bw = new BufferedWriter(new FileWriter( |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
379 overrides))) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
380 for (String line : lines) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
381 bw.write(line); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
382 bw.write('\n'); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
383 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
384 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
385 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
386 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
387 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
388 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
389 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
390 private static int currentYear = Calendar.getInstance().get(Calendar.YEAR); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
391 private static Options options = new Options(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
392 private static Option<Boolean> help = options.newBooleanOption("help", false, "Show help message and exit."); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
393 private static Option<String> COPYRIGHT_DIR = options.newStringOption("copyright-dir", null, "override default location of copyright files"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
394 private static Option<List<String>> FILES_TO_CHECK = options.newStringListOption("files", null, "list of files to check"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
395 private static Option<String> FILE_LIST = options.newStringOption("file-list", null, "file containing list of files to check"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
396 private static Option<Boolean> DIR_WALK = options.newBooleanOption("list-dir", false, "check all files in directory tree requiring a copyright (ls -R)"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
397 private static Option<Boolean> HG_ALL = options.newBooleanOption("hg-all", false, "check all hg managed files requiring a copyright (hg status --all)"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
398 private static Option<Boolean> HG_MODIFIED = options.newBooleanOption("hg-modified", false, "check all modified hg managed files requiring a copyright (hg status)"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
399 private static Option<Boolean> HG_OUTGOING = options.newBooleanOption("hg-outgoing", false, "check outgoing hg managed files requiring a copyright (hg outgoing)"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
400 private static Option<String> HG_LOG = options.newStringOption("hg-last", "0", "check hg managed files requiring a copyright in last N changesets (hg log -l N)"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
401 private static Option<List<String>> PROJECT = options.newStringListOption("projects", null, "filter files to specific projects"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
402 private static Option<String> OUTGOING_REPO = options.newStringOption("hg-repo", null, "override outgoing repository"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
403 private static Option<Boolean> EXHAUSTIVE = options.newBooleanOption("hg-exhaustive", false, "check all hg managed files"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
404 private static Option<Boolean> FIX = options.newBooleanOption("fix", false, "fix all copyright errors"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
405 private static Option<String> FILE_PATTERN = options.newStringOption("file-pattern", null, "append additional file patterns for copyright checks"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
406 private static Option<Boolean> REPORT_ERRORS = options.newBooleanOption("report-errors", false, "report non-fatal errors"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
407 private static Option<Boolean> HALT_ON_ERROR = options.newBooleanOption("halt-on-error", false, "continue after normally fatal error"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
408 private static Option<String> HG_PATH = options.newStringOption("hg-path", "hg", "path to hg executable"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
409 private static Option<Boolean> VERBOSE = options.newBooleanOption("verbose", false, "verbose output"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
410 private static Option<Boolean> VERY_VERBOSE = options.newBooleanOption("very-verbose", false, "very verbose output"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
411 private static Option<String> CUSTOM_COPYRIGHT_DIR = options.newStringOption("custom-copyright-dir", null, "file containing filenames with custom copyrights"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
412 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
413 private static String CANNOT_FOLLOW_FILE = "abort: cannot follow"; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
414 private static String hgPath; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
415 private static boolean error; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
416 // private static File workSpaceDirectory; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
417 private static boolean verbose; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
418 private static boolean veryVerbose; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
419 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
420 public static void main(String[] args) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
421 // parse the arguments |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
422 options.parseArguments(args); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
423 if (help.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
424 options.printHelp(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
425 return; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
426 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
427 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
428 verbose = VERBOSE.getValue(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
429 veryVerbose = VERY_VERBOSE.getValue(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
430 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
431 hgPath = HG_PATH.getValue(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
432 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
433 if (FILE_PATTERN.getValue() != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
434 CopyrightHandler.addCopyrightFilesPattern(FILE_PATTERN.getValue()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
435 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
436 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
437 try { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
438 initCopyrightKinds(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
439 List<String> filesToCheck = null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
440 if (HG_ALL.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
441 filesToCheck = getAllFiles(true); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
442 } else if (HG_OUTGOING.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
443 filesToCheck = getOutgoingFiles(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
444 } else if (HG_MODIFIED.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
445 filesToCheck = getAllFiles(false); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
446 } else if (Integer.parseInt(HG_LOG.getValue()) > 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
447 filesToCheck = getLastNFiles(Integer.parseInt(HG_LOG.getValue())); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
448 } else if (FILE_LIST.getValue() != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
449 filesToCheck = readFileList(FILE_LIST.getValue()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
450 } else if (DIR_WALK.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
451 filesToCheck = getDirWalkFiles(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
452 } else if (FILES_TO_CHECK.getValue() != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
453 filesToCheck = FILES_TO_CHECK.getValue(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
454 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
455 // no option set, default to HG_ALL |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
456 filesToCheck = getAllFiles(true); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
457 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
458 if (filesToCheck != null && filesToCheck.size() > 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
459 processFiles(filesToCheck); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
460 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
461 System.out.println("nothing to check"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
462 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
463 System.exit(error ? 1 : 0); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
464 } catch (Exception ex) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
465 System.err.println("processing failed: " + ex); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
466 ex.printStackTrace(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
467 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
468 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
469 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
470 private static void processFiles(List<String> fileNames) throws Exception { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
471 final List<String> projects = PROJECT.getValue(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
472 Calendar cal = Calendar.getInstance(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
473 for (String fileName : fileNames) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
474 if (projects == null || isInProjects(fileName, projects)) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
475 File file = new File(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
476 if (file.isDirectory()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
477 continue; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
478 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
479 if (verbose) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
480 System.out.println("checking " + fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
481 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
482 try { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
483 Info info = null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
484 if (DIR_WALK.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
485 info = getFromLastModified(cal, fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
486 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
487 final List<String> logInfo = hglog(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
488 if (logInfo.size() == 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
489 // an added file, so go with last modified |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
490 info = getFromLastModified(cal, fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
491 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
492 info = getInfo(fileName, true, logInfo); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
493 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
494 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
495 checkFile(fileName, info); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
496 } catch (Exception e) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
497 System.err.format("COPYRIGHT CHECK WARNING: error while processing %s: %s%n", fileName, e.getMessage()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
498 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
499 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
500 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
501 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
502 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
503 private static Info getFromLastModified(Calendar cal, String fileName) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
504 File file = new File(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
505 cal.setTimeInMillis(file.lastModified()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
506 int year = cal.get(Calendar.YEAR); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
507 return new Info(fileName, year, year); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
508 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
509 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
510 private static boolean isInProjects(String fileName, List<String> projects) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
511 final int ix = fileName.indexOf(File.separatorChar); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
512 if (ix < 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
513 return false; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
514 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
515 final String fileProject = fileName.substring(0, ix); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
516 for (String project : projects) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
517 if (fileProject.equals(project)) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
518 return true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
519 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
520 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
521 return false; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
522 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
523 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
524 private static List<String> readFileList(String fileListName) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
525 final List<String> result = new ArrayList<String>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
526 BufferedReader b = null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
527 try { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
528 b = new BufferedReader(new FileReader(fileListName)); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
529 while (true) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
530 final String fileName = b.readLine(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
531 if (fileName == null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
532 break; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
533 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
534 if (fileName.length() == 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
535 continue; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
536 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
537 result.add(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
538 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
539 } finally { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
540 if (b != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
541 b.close(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
542 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
543 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
544 return result; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
545 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
546 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
547 private static Info getInfo(String fileName, boolean lastOnly, List<String> logInfo) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
548 // process sequence of changesets |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
549 int lastYear = 0; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
550 int firstYear = 0; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
551 int ix = 0; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
552 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
553 while (ix < logInfo.size()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
554 Map<String, String> tagMap = new HashMap<>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
555 ix = getChangeset(logInfo, ix, tagMap); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
556 String date = tagMap.get("date"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
557 assert date != null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
558 final int csYear = getYear(date); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
559 if (lastYear == 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
560 lastYear = csYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
561 firstYear = lastYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
562 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
563 firstYear = csYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
564 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
565 // if we only want the last modified year, quit now |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
566 if (lastOnly) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
567 break; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
568 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
569 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
570 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
571 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
572 if (HG_MODIFIED.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
573 // We are only looking at modified and, therefore, uncommitted files. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
574 // This means that the lastYear value will be the current year once the |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
575 // file is committed, so that is what we want to check against. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
576 lastYear = currentYear; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
577 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
578 return new Info(fileName, firstYear, lastYear); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
579 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
580 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
581 /** |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
582 * Process all the changeset data, storing in {@outMap}. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
583 * Return updated value of {@code ix}. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
584 */ |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
585 private static int getChangeset(List<String> logInfo, int ixx, Map<String, String> outMap) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
586 int ix = ixx; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
587 String s = logInfo.get(ix++); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
588 while (s.length() > 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
589 int cx = s.indexOf(':'); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
590 String tag = s.substring(0, cx); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
591 String value = s.substring(cx + 1); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
592 outMap.put(tag, value); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
593 s = logInfo.get(ix++); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
594 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
595 return ix; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
596 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
597 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
598 private static int getYear(String dateLine) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
599 final String[] parts = dateLine.split(" "); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
600 assert parts[parts.length - 2].startsWith("20"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
601 return Integer.parseInt(parts[parts.length - 2]); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
602 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
603 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
604 private static void checkFile(String c, Info info) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
605 String fileName = info.fileName; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
606 File file = new File(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
607 if (!file.exists()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
608 System.err.println("COPYRIGHT CHECK WARNING: file " + file + " doesn't exist"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
609 return; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
610 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
611 int fileLength = (int) file.length(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
612 byte[] fileContentBytes = new byte[fileLength]; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
613 FileInputStream is = new FileInputStream(file); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
614 is.read(fileContentBytes); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
615 is.close(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
616 final String fileContent = new String(fileContentBytes); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
617 CopyrightHandler copyrightHandler = CopyrightHandler.getCopyrightHandler(fileName); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
618 if (file.getName().equals("Makefile")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
619 System.console(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
620 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
621 if (copyrightHandler != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
622 Matcher copyrightMatcher = copyrightHandler.getMatcher(fileName, fileContent); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
623 if (copyrightMatcher.matches()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
624 error = error | !copyrightHandler.checkYearInfo(fileName, fileContent, copyrightMatcher, info); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
625 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
626 // If copyright is missing, insert it, otherwise user has to manually fix existing copyright. |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
627 if (!fileContent.contains("Copyright")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
628 System.out.print("file " + fileName + " has missing copyright"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
629 if (FIX.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
630 final FileOutputStream os = new FileOutputStream(file); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
631 os.write(CopyrightHandler.getCopyrightText(fileName) |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
632 .getBytes()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
633 os.write(fileContentBytes); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
634 os.close(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
635 System.out.println("...fixed"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
636 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
637 System.out.println(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
638 error = true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
639 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
640 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
641 System.out.println("file " + fileName + " has malformed copyright" + (FIX.getValue() ? " not fixing" : "")); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
642 error = true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
643 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
644 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
645 } else if (EXHAUSTIVE.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
646 System.out.println("ERROR: file " + fileName + " has no copyright"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
647 error = true; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
648 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
649 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
650 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
651 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
652 private static List<String> hglog(String fileName) throws Exception { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
653 final String[] cmd = new String[] {hgPath, "log", "-f", fileName}; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
654 return exec(null, cmd, true); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
655 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
656 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
657 private static List<String> getLastNFiles(int n) throws Exception { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
658 final String[] cmd = new String[] {hgPath, "log", "-v", "-l", Integer.toString(n)}; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
659 return getFilesFiles(exec(null, cmd, false)); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
660 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
661 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
662 private static List<String> getAllFiles(boolean all) throws Exception { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
663 final String[] cmd; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
664 if (HG_MODIFIED.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
665 cmd = new String[] {hgPath, "status"}; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
666 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
667 cmd = new String[] {hgPath, "status", "--all"}; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
668 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
669 List<String> output = exec(null, cmd, true); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
670 final List<String> result = new ArrayList<String>(output.size()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
671 for (String s : output) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
672 final char ch = s.charAt(0); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
673 if (!(ch == 'R' || ch == 'I' || ch == '?' || ch == '!')) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
674 result.add(s.substring(2)); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
675 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
676 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
677 return result; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
678 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
679 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
680 private static List<String> getOutgoingFiles() throws Exception { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
681 final String[] cmd; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
682 if (OUTGOING_REPO.getValue() == null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
683 cmd = new String[] {hgPath, "-v", "outgoing"}; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
684 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
685 cmd = new String[] {hgPath, "-v", "outgoing", OUTGOING_REPO.getValue()}; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
686 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
687 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
688 final List<String> output = exec(null, cmd, false); // no outgoing exits with result 1 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
689 return getFilesFiles(output); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
690 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
691 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
692 private static List<String> getFilesFiles(List<String> output) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
693 // there may be multiple changesets so merge the "files:" |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
694 final Map<String, String> outSet = new TreeMap<String, String>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
695 for (String s : output) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
696 if (s.startsWith("files:")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
697 int ix = s.indexOf(' '); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
698 while (ix < s.length() && s.charAt(ix) == ' ') { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
699 ix++; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
700 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
701 final String[] files = s.substring(ix).split(" "); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
702 for (String file : files) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
703 outSet.put(file, file); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
704 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
705 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
706 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
707 return new ArrayList<String>(outSet.values()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
708 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
709 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
710 private static List<String> getDirWalkFiles() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
711 File cwd = new File(System.getProperty("user.dir")); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
712 ArrayList<String> result = new ArrayList<String>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
713 getDirWalkFiles(cwd, result); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
714 // remove "user.dir" prefix to make files relative as per hg |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
715 String cwdPath = cwd.getAbsolutePath() + '/'; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
716 for (int i = 0; i < result.size(); i++) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
717 String path = result.get(i); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
718 result.set(i, path.replace(cwdPath, "")); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
719 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
720 return result; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
721 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
722 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
723 private static void getDirWalkFiles(File dir, ArrayList<String> list) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
724 File[] files = dir.listFiles(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
725 for (File file : files) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
726 if (ignoreFile(file.getName())) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
727 continue; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
728 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
729 if (file.isDirectory()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
730 getDirWalkFiles(file, list); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
731 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
732 list.add(file.getAbsolutePath()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
733 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
734 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
735 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
736 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
737 private static final String IGNORE_LIST = "\\.hg|.*\\.class|bin|src_gen"; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
738 private static final Pattern ignorePattern = Pattern.compile(IGNORE_LIST); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
739 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
740 private static boolean ignoreFile(String name) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
741 return ignorePattern.matcher(name).matches(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
742 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
743 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
744 private static List<String> exec(File workingDir, String[] command, boolean failOnError) throws IOException, InterruptedException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
745 List<String> result = new ArrayList<String>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
746 if (veryVerbose) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
747 System.out.println("Executing process in directory: " + workingDir); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
748 for (String c : command) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
749 System.out.println(" " + c); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
750 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
751 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
752 final Process process = Runtime.getRuntime().exec(command, null, workingDir); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
753 try { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
754 result = readOutput(process.getInputStream()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
755 final int exitValue = process.waitFor(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
756 if (exitValue != 0) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
757 final List<String> errorResult = readOutput(process.getErrorStream()); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
758 if (REPORT_ERRORS.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
759 System.err.print("execution of command: "); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
760 for (String c : command) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
761 System.err.print(c); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
762 System.err.print(' '); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
763 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
764 System.err.println("failed with result " + exitValue); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
765 for (String e : errorResult) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
766 System.err.println(e); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
767 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
768 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
769 if (failOnError && HALT_ON_ERROR.getValue()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
770 if (!cannotFollowNonExistentFile(errorResult)) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
771 throw new Error("terminating"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
772 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
773 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
774 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
775 } finally { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
776 process.destroy(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
777 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
778 return result; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
779 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
780 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
781 private static boolean cannotFollowNonExistentFile(List<String> errorResult) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
782 return errorResult.size() == 1 && errorResult.get(0).startsWith(CANNOT_FOLLOW_FILE); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
783 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
784 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
785 private static List<String> readOutput(InputStream is) throws IOException { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
786 final List<String> result = new ArrayList<String>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
787 BufferedReader bs = null; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
788 try { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
789 bs = new BufferedReader(new InputStreamReader(is)); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
790 while (true) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
791 final String line = bs.readLine(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
792 if (line == null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
793 break; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
794 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
795 result.add(line); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
796 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
797 } finally { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
798 if (bs != null) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
799 bs.close(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
800 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
801 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
802 return result; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
803 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
804 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
805 private static class Options { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
806 private static Map<String, Option<?>> optionMap = new TreeMap<>(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
807 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
808 private Option<Boolean> newBooleanOption(String name, boolean defaultValue, String help) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
809 Option<Boolean> option = new Option<Boolean>(name, help, defaultValue, false, false); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
810 optionMap.put(key(name), option); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
811 return option; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
812 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
813 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
814 private Option<String> newStringOption(String name, String defaultValue, String help) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
815 Option<String> option = new Option<String>(name, help, defaultValue); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
816 optionMap.put(key(name), option); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
817 return option; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
818 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
819 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
820 private Option<List<String>> newStringListOption(String name, List<String> defaultValue, String help) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
821 Option<List<String>> option = new Option<List<String>>(name, help, defaultValue, true, true); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
822 optionMap.put(key(name), option); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
823 return option; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
824 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
825 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
826 private static String key(String name) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
827 return "--" + name; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
828 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
829 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
830 void parseArguments(String[] args) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
831 for (int i = 0; i < args.length; i++) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
832 final String arg = args[i]; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
833 if (arg.startsWith("--")) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
834 Option<?> option = optionMap.get(arg); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
835 if (option == null || (option.consumesNext() && i == args.length - 1)) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
836 System.out.println("usage:"); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
837 printHelp(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
838 System.exit(1); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
839 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
840 if (option.consumesNext()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
841 i++; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
842 option.setValue(args[i]); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
843 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
844 option.setValue(true); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
845 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
846 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
847 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
848 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
849 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
850 void printHelp() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
851 int maxKeyLen = 0; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
852 for (Map.Entry<String, Option<?>> entrySet : optionMap.entrySet()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
853 int l = entrySet.getKey().length(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
854 if (l > maxKeyLen) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
855 maxKeyLen = l; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
856 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
857 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
858 for (Map.Entry<String, Option<?>> entrySet : optionMap.entrySet()) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
859 String key = entrySet.getKey(); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
860 System.out.printf(" %s", key); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
861 for (int i = 0; i < maxKeyLen - key.length(); i++) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
862 System.out.print(' '); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
863 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
864 System.out.printf(" %s%n", entrySet.getValue().help); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
865 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
866 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
867 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
868 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
869 private static class Option<T> { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
870 private final String name; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
871 private final String help; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
872 private final boolean consumesNext; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
873 private final boolean isList; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
874 private T value; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
875 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
876 Option(String name, String help, T defaultValue, boolean consumesNext, boolean isList) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
877 this.name = name; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
878 this.help = help; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
879 this.value = defaultValue; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
880 this.consumesNext = consumesNext; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
881 this.isList = isList; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
882 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
883 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
884 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
885 Option(String name, String help, T defaultValue) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
886 this(name, help, defaultValue, true, false); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
887 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
888 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
889 T getValue() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
890 return value; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
891 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
892 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
893 boolean consumesNext() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
894 return consumesNext; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
895 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
896 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
897 @SuppressWarnings("unchecked") |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
898 void setValue(boolean value) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
899 this.value = (T) new Boolean(value); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
900 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
901 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
902 @SuppressWarnings("unchecked") |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
903 void setValue(String value) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
904 if (isList) { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
905 String[] parts = value.split(","); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
906 this.value = (T) Arrays.asList(parts); |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
907 } else { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
908 this.value = (T) value; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
909 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
910 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
911 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
912 @SuppressWarnings("unused") |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
913 String getName() { |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
914 return name; |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
915 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
916 } |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
917 |
c190ed6b84bf
added checkcopyrights command (from mxtool2)
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
918 } |