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
/*
* Copyright 2020 ETSI
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package fr.emse.gitlab.saref.checkers;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.emse.gitlab.saref.SAREFPipeline.Mode;
import fr.emse.gitlab.saref.SAREFPipelineException;
import fr.emse.gitlab.saref.managers.RepositoryManager;
/**
* Checks TS 103 673 Clause 9.2: Structure of the SAREF project version directory
*
*/
public class Clause_9_2_Checker extends AbstractClauseChecker {
private static enum MESSAGE {
requirements, ontology, tests, examples, documentation, readme_missing, gitignore_missing, gitignore_incomplete, gitignore_ioexception,
license_missing, license_firstline, license_year, license_line, license_overcomplete,
license_incomplete, license_ioexception, securityexception;
}
private static final Set<String> gitignoreLines = new HashSet<>(
Arrays.asList("target", "*~", ".DS_Store", "catalog-v001.xml", "saref-pipeline.jar"));
public Clause_9_2_Checker(RepositoryManager repositoryManager) {
super(repositoryManager, Clause_9_2_Checker.class);
}
@Override
public void checkClause() throws SAREFPipelineException {
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
checkReadme();
checkGitignore();
checkLICENSE();
checkDirectory("requirements", MESSAGE.requirements);
checkDirectory("ontology", MESSAGE.ontology);
checkDirectory("tests", MESSAGE.tests);
checkDirectory("examples", MESSAGE.examples);
checkDirectory("documentation", MESSAGE.documentation);
}
private void checkReadme() {
final File file = new File(repository.getDirectory(), "README.md");
if (file.isFile()) {
return;
}
log(getMessage(MESSAGE.readme_missing), Mode.RELEASE, Mode.PORTAL);
}
private void checkGitignore() throws SAREFPipelineException {
final File file = new File(repository.getDirectory(), ".gitignore");
if (!file.isFile()) {
log(getMessage(MESSAGE.gitignore_missing), Mode.RELEASE, Mode.PORTAL);
return;
}
try (BufferedReader br = Files.newBufferedReader(file.toPath())) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
gitignoreLines.remove(line);
}
if (!gitignoreLines.isEmpty()) {
log(getMessage(MESSAGE.gitignore_incomplete, gitignoreLines.toString()), Mode.RELEASE, Mode.PORTAL);
}
} catch (IOException ex) {
log(getMessage(MESSAGE.gitignore_ioexception, gitignoreLines.toString()), Mode.RELEASE, Mode.PORTAL);
}
}
private void checkLICENSE() throws SAREFPipelineException {
final File licenseFile = new File(repository.getDirectory(), "LICENSE");
if (!licenseFile.exists()) {
log(getMessage(MESSAGE.license_missing), Mode.RELEASE, Mode.PORTAL);
return;
}
InputStream licenseModelFile = Clause_9_2_Checker.class.getClassLoader()
.getResourceAsStream("LICENSE_MODEL");
try (BufferedReader licenseModelFileReader = new BufferedReader(new InputStreamReader(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()) {
log(getMessage(MESSAGE.license_firstline, licenseLine, modelLine), Mode.RELEASE, Mode.PORTAL);
return;
}
int year = Integer.parseInt(regexMatcher.group("year"));
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
if (year < 2015 || year > currentYear) {
log(getMessage(MESSAGE.license_year, 2015, currentYear), Mode.RELEASE, Mode.PORTAL);
return;
}
int i = 1;
modelLine = licenseModelFileReader.readLine();
licenseLine = licenseFileReader.readLine();
while (modelLine != null && licenseLine != null) {
i++;
if (!modelLine.equals(licenseLine)) {
log(getMessage(MESSAGE.license_line, i, modelLine), Mode.RELEASE, Mode.PORTAL);
return;
}
modelLine = licenseModelFileReader.readLine();
licenseLine = licenseFileReader.readLine();
}
if (modelLine == null && licenseLine != null) {
log(getMessage(MESSAGE.license_overcomplete, i, modelLine), Mode.RELEASE, Mode.PORTAL);
return;
}
if (modelLine != null && licenseLine == null) {
log(getMessage(MESSAGE.license_incomplete, i), Mode.RELEASE, Mode.PORTAL);
return;
}
} catch (Exception ex) {
String msg = getMessage(MESSAGE.license_ioexception);
log(msg, ex, Mode.RELEASE, Mode.PORTAL);
throw new SAREFPipelineException(msg, ex);
}
}
private void checkDirectory(String dirName, MESSAGE message) throws SAREFPipelineException {
final File dir = new File(repository.getDirectory(), dirName);
try {
if (!dir.isDirectory()) {
String msg = getMessage(message, dirName);
log(msg, Mode.RELEASE, Mode.PORTAL);
}
} catch (SecurityException ex) {
String msg = getMessage(MESSAGE.securityexception, dirName);
log(msg, ex, Mode.RELEASE, Mode.PORTAL);
throw new SAREFPipelineException(msg, ex);
}
}
}