1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 void main(String[] args) throws IOException{
26       Files.walkFileTree(Paths.get("./"), new SimpleFileVisitor<>() {
27          private static final Set<String> TARGET_EXTENSIONS = Set.of(".java", ".h", ".cpp", ".md");
28          private static final Set<String> SKIP_COPYRIGHT_EXTENSIONS = Set.of(".md");
29 
30          private static final Set<String> TARGET_FILES = Set.of("pom.xml"/* "CMakeLists.txt"*/);
31          private static final Pattern COPYRIGHT_PATTERN = Pattern.compile("^.*Copyright.*202[0-9].*(Intel|Oracle).*$", Pattern.MULTILINE);
32 
33          private final Set<String> IGNORED_DIRS = Set.of("target",  "build", "robertograham","hip");
34             @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
35                return IGNORED_DIRS.contains(dir.getFileName().toString())?FileVisitResult.SKIP_SUBTREE:FileVisitResult.CONTINUE;
36             }
37             @Override
38             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
39                var name = file.getFileName().toString();
40                if (TARGET_FILES.contains(name) || TARGET_EXTENSIONS.stream().anyMatch(name::endsWith)){
41                   try {
42                      var lines = Files.readAllLines(file, StandardCharsets.UTF_8);
43                      var tab = new ArrayList<Integer>();
44                      var eolWs = new ArrayList<Integer>();
45                      var copyright = SKIP_COPYRIGHT_EXTENSIONS.stream().anyMatch(name::endsWith);
46                      for (int i = 0; i < lines.size(); i++) {
47                         var line = lines.get(i);
48                         copyright = copyright || COPYRIGHT_PATTERN.matcher(line).find();
49                         if (line.contains("\t")) {
50                            tab.add(i + 1);
51                         }
52                         if (line.matches(".*\\s+$")) {
53                            eolWs.add(i + 1);
54                         }
55                      }
56                      if (!copyright){
57                         IO.println("[NO COPYRIGHT] " + file);
58                      }
59                      if (!tab.isEmpty()) {
60                         IO.println("[         Tab] " + file + " (" + tab + ")");
61                      }
62                      if (!eolWs.isEmpty()) {
63                         IO.println("[      EOL WS] " + file + " (" + eolWs + ")");
64                      }
65                   } catch (IOException e) {
66                      throw new RuntimeException("Could not read file: " + file);
67                   }
68                }
69                return  FileVisitResult.CONTINUE;
70             }
71       });
72 }