Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package fr.emse.gitlab.saref.jobs.library;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.emse.gitlab.saref.jobs.AbstractJobRunner;
public class RepositoryStructureChecker extends AbstractJobRunner {
static Logger logger = Logger.getLogger("MaintenanceReport");
private final Set<String> gitignoreLines = new HashSet<>(
Arrays.asList("target", "*~", ".DS_Store", "catalog-v001.xml"));
private final String NAME = "Check the structure of the repository";
public RepositoryStructureChecker(File dir) {
super(dir);
}
public void doJob0() {
try {
if (!directory.isDirectory()) {
error("The SAREF pipeline must be run inside a directory");
}
} catch (SecurityException ex) {
error("Security error while running the SAREF pipeline", ex);
}
checkThereExistsDirectory("diagrams");
checkThereExistsDirectory("ontology");
String repoName = checkRepoName();
if (repoName == null) {
return;
}
String ontologyFile = repoName.equals("saref-core") ? "saref.ttl" : repoName + ".ttl";
checkOntoloyFolder(ontologyFile);
checkGitIgnoreFolder();
checkLICENSE();
checkExamplesFolder();
}
private void checkThereExistsDirectory(String dirName) {
final File dir = new File(directory, dirName);
try {
if (dir.isDirectory()) {
success(String.format("There exists a folder named `%s`", dirName));
} else {
failure(String.format("There should exist a folder named `%s`", dirName));
}
} catch (SecurityException ex) {
error(String.format("Error while checking if there exists a folder named `%s`", dirName), ex);
}
}
private String checkRepoName() {
String REGEX = "^(?<ext>saref-core|saref4[a-z][a-z][a-z][a-z])";
String name = directory.getName();
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(name);
if (!matcher.find()) {
error(String.format(
"The SAREF pipeline must be run inside a directory whose name begins with `saref-core`, or `saref4abcd` (where abcd can be any sequence of four letters). Got: %s",
name));
return null;
}
return matcher.group("ext");
}
private void checkOntoloyFolder(String ontoName) {
final File ontoDir = new File(directory, "ontology");
final File ontoFile = new File(ontoDir, ontoName);
if (!ontoFile.isFile()) {
error(String.format("The ontology folder must contain an ontology file named `%s`", ontoName));
return;
}
}
private void checkGitIgnoreFolder() {
final File gitignore = new File(directory, ".gitignore");
if (!gitignore.exists()) {
failure("The directory must contain a file named `.gitignore`");
}
try (BufferedReader br = Files.newBufferedReader(gitignore.toPath())) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
gitignoreLines.remove(line);
}
if (!gitignoreLines.isEmpty()) {
failure(String.format("The file `.gitignore` lacks one line for each of the following strings: %s",
gitignoreLines.toString()));
}
} catch (IOException ex) {
error("Error while checking if the file `.gitignore` has the appropriate lines", ex);
}
}
private void checkLICENSE() {
final File licenseFile = new File(directory, "LICENSE");
if (!licenseFile.exists()) {
failure("The directory must contain a file named `LICENSE`");
return;
}
File licenseModelFile = new File(
RepositoryStructureChecker.class.getClassLoader().getResource("LICENSE_MODEL").getFile());
try (BufferedReader licenseModelFileReader = new BufferedReader(new FileReader(licenseModelFile));
BufferedReader licenseFileReader = new BufferedReader(new FileReader(licenseFile));) {
// first line
String modelLine = licenseModelFileReader.readLine();
String licenseLine = licenseFileReader.readLine();
String regex = modelLine.replaceFirst("YYYY", "(?<year>\\\\d{4})");
Pattern pattern = Pattern.compile(String.format("^%s$", regex));
Matcher regexMatcher = pattern.matcher(licenseLine);
if (!regexMatcher.find()) {
failure("First line of the `LICENSE` file must match the following string (see details below)",
"First line of the `LICENSE` file must match the following string, where YYYY is a year. Got: "
+ licenseLine,
modelLine);
return;
}
int year = Integer.parseInt(regexMatcher.group("year"));
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
if (year < 2015 || year > currentYear) {
failure(String.format("The year in the `LICENSE` should be between %s and %s", 2015, currentYear));
return;
}
int i = 1;
modelLine = licenseModelFileReader.readLine();
licenseLine = licenseFileReader.readLine();
while (modelLine != null && licenseLine != null) {
i++;
if (!modelLine.equals(licenseLine)) {
failure(String.format("Line %s in the `LICENSE` should be `%s`", i, modelLine));
return;
}
modelLine = licenseModelFileReader.readLine();
licenseLine = licenseFileReader.readLine();
}
if(modelLine == null && licenseLine != null) {
failure(String.format("The license contains too many lines. Need %s", i));
return;
}
if(modelLine != null && licenseLine == null) {
failure(String.format("The license is incomplete.", i));
return;
}
} catch (Exception ex) {
error("Exception while creating the path for the LICENSE model", ex);
return;
}
}
private void checkExamplesFolder() {
final File dir = new File(directory, "examples");
try {
if (!dir.isDirectory()) {
failure("There should exist a folder named `examples`");
return;
}
} catch (SecurityException ex) {
error("Error while checking if there exists a folder named `examples`", ex);
return;
}
try {
boolean containsExample = Files.walk(dir.toPath()).anyMatch(p -> {
return p.endsWith(".ttl");
});
if (!containsExample) {
failure("The `examples` folder should contain at least one example file (some `.ttl` document");
}
} catch (IOException ex) {
error("Exception while browsing the `examples` folder", ex);
}
}
}