1
1
package org .codechecker .eclipse .plugin .runtime ;
2
2
3
3
import java .io .File ;
4
+ import java .util .HashMap ;
4
5
import java .util .Map ;
5
6
7
+ import org .codechecker .eclipse .plugin .Logger ;
6
8
import org .codechecker .eclipse .plugin .config .Config .ConfigTypes ;
7
9
import org .codechecker .eclipse .plugin .config .global .CcGlobalConfiguration ;
8
10
import org .codechecker .eclipse .plugin .config .project .CodeCheckerProject ;
9
11
import org .eclipse .core .runtime .IProgressMonitor ;
12
+ import org .eclipse .core .runtime .IStatus ;
10
13
11
14
import com .google .common .base .Optional ;
12
15
import com .google .common .collect .ImmutableMap ;
13
16
14
-
15
17
/**
16
18
* This class checks for Environments used by CodeChecker.
17
19
*
18
20
*/
19
21
public class CodeCheckEnvironmentChecker {
20
22
23
+ private static final String CC_BINARY = "CC_BINARY" ;
24
+
25
+ private static final String HELP_ARGUMENT = "-h" ;
26
+ private static final int WAIT_TIME_MULTIPLYER = 1000 ; // in milliseconds
27
+
21
28
public final Optional <String > pythonEnvironment ;
22
29
public final String checkerDir ; // root directory of CodeChecker
23
30
public final String codeCheckerCommand ; // CodecCheker executable path
31
+
32
+ public final ImmutableMap <String , String > environmentBefore ;
33
+
34
+ public Map <String , File > commandSubstitutionMap ;
35
+
24
36
private Map <ConfigTypes ,String > config ;
25
37
private CodeCheckerProject project ;
26
38
private String checkerList ;
27
39
28
- //with specific python. This
29
- // can be used to run CodeChecker
30
- public final ImmutableMap <String , String > environmentBefore ;
31
-
32
40
/**
33
41
*
34
42
* @param project
@@ -49,14 +57,23 @@ public CodeCheckEnvironmentChecker(CodeCheckerProject project) {
49
57
SLogger .log (LogI .INFO , "pythonenv is not set" );
50
58
}
51
59
else {
52
- SLogger .log (LogI .INFO , "pythonenv is set to:" + config .get (" PYTHON_PATH" ));
60
+ SLogger .log (LogI .INFO , "pythonenv is set to:" + config .get (ConfigTypes . PYTHON_PATH ));
53
61
pythonEnvironment =Optional .of (config .get (ConfigTypes .PYTHON_PATH ));
54
62
}
55
63
56
64
//checkerList=getConfigValue(ConfigTypes.CHECKER_LIST);
57
65
checkerDir =getConfigValue (ConfigTypes .CHECKER_PATH );
58
66
environmentBefore = getInitialEnvironment (pythonEnvironment );
59
67
codeCheckerCommand = checkerDir +"/bin/CodeChecker" ;
68
+
69
+
70
+ commandSubstitutionMap = new HashMap <String , File >() {{
71
+ put ("CC_BIN" , new File (codeCheckerCommand ));
72
+ }};
73
+ if (project != null )
74
+ commandSubstitutionMap .put ("RESULTS" ,
75
+ new File (project .getLogFileLocation ().getParent ().toString () + "/results/" ));
76
+
60
77
}
61
78
62
79
/**
@@ -83,24 +100,31 @@ private String getConfigValue(ConfigTypes key) {
83
100
* @param config The Configuration to be used,
84
101
* populated with {@link ConfigTypes}.
85
102
* @param codeCheckerBinaryPath Path to CodeChecker.
103
+ * TODO This method doesn't need codeCheckerBinaryPath in its arguments as it's a field in this class.
86
104
*/
87
105
public static void getCheckerEnvironment (
88
106
Map <ConfigTypes , String > config , String codeCheckerBinaryPath ) {
89
107
90
108
ShellExecutorHelper she = new ShellExecutorHelper (
91
109
getInitialEnvironment (Optional .of (config .get (ConfigTypes .PYTHON_PATH ))));
92
110
93
- String cmd =codeCheckerBinaryPath + " -h" ;
94
- SLogger .log (LogI .INFO , "Testing " + cmd );
95
- Optional <String > ccEnvOutput = she .quickReturnOutput (cmd );
111
+ String cmd = "'${CC_BINARY}' " + HELP_ARGUMENT ;
112
+ @ SuppressWarnings ("serial" )
113
+ Map <String , File > substitutinMap = new HashMap <String , File >() {{
114
+ put (CC_BINARY , new File (codeCheckerBinaryPath ));
115
+ }};
116
+
117
+ SLogger .log (LogI .INFO , "Testing " + substitutinMap .get (CC_BINARY ).getAbsolutePath () + " -h" );
118
+ Optional <String > ccEnvOutput = she .quickReturnOutput (cmd , substitutinMap );
96
119
double test = 0 ;
97
- // WTF
120
+ // TODO WTF -- check the twisted logic behind this, and simplify.
98
121
while (!ccEnvOutput .isPresent () && test <= 2 ){
99
- ccEnvOutput = she .quickReturnOutput (cmd , Math .pow ( 2.0 , test ) * 1000 );
122
+ ccEnvOutput = she .quickReturnOutput (cmd , substitutinMap , Math .pow ( 2.0 , test ) * WAIT_TIME_MULTIPLYER );
100
123
++test ;
101
- }
124
+ }
102
125
if (!ccEnvOutput .isPresent ()) {
103
- SLogger .log (LogI .ERROR , "Cannot run CodeChecker command:" +cmd );
126
+ SLogger .log (LogI .ERROR , "Cannot run CodeChecker command:" +
127
+ substitutinMap .get (CC_BINARY ).getAbsolutePath () + " " + HELP_ARGUMENT );
104
128
throw new IllegalArgumentException ("Couldn't run the specified CodeChecker for " +
105
129
"environment testing!" );
106
130
}
@@ -114,11 +138,13 @@ public static void getCheckerEnvironment(
114
138
private static ImmutableMap <String , String > getInitialEnvironment (Optional <String > pythonEnvironment ) {
115
139
if (pythonEnvironment .isPresent ()) {
116
140
ShellExecutorHelper she = new ShellExecutorHelper (System .getenv ());
117
-
118
- Optional <String > output = she .quickReturnOutput ("source " + pythonEnvironment .get () + "/bin/activate" +
119
- " ; env" );
141
+ File pyEnv = new File (pythonEnvironment .get () + "/bin/activate" );
142
+ String cmd = "source '${PY_ENV}' ; env" ;
143
+ @ SuppressWarnings ("serial" )
144
+ Map <String , File > substitutionMap = new HashMap <String , File >() {{ put ("PY_ENV" , pyEnv ); }};
145
+ Optional <String > output = she .quickReturnOutput (cmd , substitutionMap );
120
146
if (!output .isPresent ()) {
121
- SLogger .log (LogI . INFO , "SERVER_GUI_MSG >> Couldn't check the given python environment!" );
147
+ Logger .log (IStatus . ERROR , "Couldn't check the python environment!" );
122
148
throw new IllegalArgumentException ("Couldn't check the given python environment!" );
123
149
} else {
124
150
ImmutableMap <String , String > environment = (new EnvironmentParser ()).parse (output
@@ -151,40 +177,46 @@ public void setCheckerList(String list) {
151
177
* @param buildLog Path to the compile commands file, which the analyze command uses.
152
178
* @return The constructed analyze command.
153
179
*/
154
- public String createAnalyzeCommmand (String buildLog ){
155
- return codeCheckerCommand + " analyze " + getConfigValue (ConfigTypes .CHECKER_LIST ) +
156
- " -j " + getConfigValue (ConfigTypes .ANAL_THREADS ) + " -n javarunner" +
157
- " -o " + project .getLogFileLocation ().getParent ().toString () +"/results/ " + buildLog ;
180
+ public String createAnalyzeCommmand (String buildLog ) {
181
+ commandSubstitutionMap .put ("LOG" , new File (buildLog ));
182
+ return "'${CC_BIN}' analyze " + getConfigValue (ConfigTypes .CHECKER_LIST ) +
183
+ " -j " + getConfigValue (ConfigTypes .ANAL_THREADS ) + " -n javarunner" +
184
+ " -o " + "'${RESULTS}' " + "'${LOG}'" ;
158
185
}
159
186
160
187
/**
161
188
* Executes CodeChecker check command
162
189
* on the build log received in the fileName parameter.
163
- * @param fileName Build log in the http://clang.llvm.org/docs/JSONCompilationDatabase.html format.
190
+ * @param buildLog Build log in the http://clang.llvm.org/docs/JSONCompilationDatabase.html format.
164
191
* @param logToConsole Flag for indicating console logging
165
192
* @param monitor ProgressMonitor for to be able to increment progress bar.
166
193
* @param taskCount How many analyze step to be taken.
167
194
* @return CodeChecker check command output
168
195
*/
169
- public String processLog (String fileName , boolean logToConsole , IProgressMonitor monitor , int taskCount ) {
196
+ public String processLog (String buildLog , boolean logToConsole , IProgressMonitor monitor , int taskCount ) {
170
197
ShellExecutorHelper she = new ShellExecutorHelper (environmentBefore );
171
- String cmd = createAnalyzeCommmand (fileName );
172
-
198
+ String cmd = createAnalyzeCommmand (buildLog );
173
199
SLogger .log (LogI .INFO , "SERVER_SER_MSG >> processLog >> " + cmd );
174
- //Optional<String> ccOutput = she.waitReturnOutput(cmd,logToConsole);
175
- Optional <String > ccOutput = she .progressableWaitReturnOutput (cmd ,logToConsole , monitor , taskCount );
200
+ Optional <String > ccOutput = she .progressableWaitReturnOutput (cmd , commandSubstitutionMap , logToConsole , monitor , taskCount );
176
201
if (ccOutput .isPresent ()) {
177
202
// assume it succeeded, and delete the log file...
178
- File f = new File (fileName );
203
+ File f = new File (buildLog );
179
204
f .delete ();
180
205
}
181
206
return ccOutput .or ("" );
182
207
}
183
208
209
+ /**
210
+ * Returns the list of available checkers.
211
+ * Add the following for checker enability checking.
212
+ * CodeChecker checkers --details | cut -d " " -f "1-3"
213
+ * @return A String containing the checkers. One at a line.
214
+ */
184
215
public String getCheckerList () {
185
216
ShellExecutorHelper she = new ShellExecutorHelper (environmentBefore );
186
- String cmd = codeCheckerCommand + " checkers" ;
187
- Optional <String > ccOutput = she .waitReturnOutput (cmd ,false );
217
+ String cmd = "'${CC_BIN}' checkers" ;
218
+ Optional <String > ccOutput = she .waitReturnOutput (cmd , commandSubstitutionMap , false );
188
219
return ccOutput .or ("" );
189
220
}
221
+
190
222
}
0 commit comments