1 package com.proalpha.pds.gitutils.checks;
3 import java.io.BufferedReader;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
9 import java.lang.ProcessBuilder.Redirect;
10 import java.nio.file.Files;
11 import java.text.MessageFormat;
12 import java.util.ArrayList;
13 import java.util.List;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.SubMonitor;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jgit.lib.Constants;
24 import org.eclipse.jgit.lib.ObjectId;
25 import org.eclipse.swt.widgets.Display;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.proalpha.pds.gitutils.Activator;
30 import com.proalpha.pds.gitutils.PreferencesConstants;
31 import com.proalpha.pds.paconnector.PaProject;
34 private static final String LAST_PROCEDURE_ENV_NAME =
"LASTPROCEDURE";
35 private static final String COMPPROC_ENV_NAME =
"COMPPROC";
36 private Integer maxFileCount = 0;
37 private Logger logger;
41 private File compilerInputFile =
null;
45 this.repositoryTools = repositoryTools;
46 this.options = options;
48 this.maxFileCount = getMaxCompileCount();
49 this.logger = LoggerFactory.getLogger(
PaProject.class);
52 private boolean checkPreconditions(
PaProject paProject) {
53 if (paProject ==
null) {
54 Display.getDefault().syncExec(
new Runnable() {
56 MessageDialog.openError(Display.getDefault().getActiveShell(),
"Compile Job",
57 "There is no active proALPHA project associated with this git repository.");
63 }
else if (!paProject.isAvailable()) {
64 Display.getDefault().syncExec(
new Runnable() {
66 MessageDialog.openError(Display.getDefault().getActiveShell(),
"Compile Job",
67 "proALPHA not available! \n\nIs the OE project opened and proALPHA running?");
77 protected IStatus run(IProgressMonitor monitor) {
78 if (!this.checkPreconditions(repositoryTools.getPaProject()))
79 return Status.CANCEL_STATUS;
81 String paTmpDir = repositoryTools.getPaProject().getBridge().getpAStartupParam(
"temp",
"");
82 compilerInputFile =
new Path(paTmpDir).append(options.compilerInputFileName).toFile();
84 SubMonitor subMonitor = SubMonitor.convert(monitor,
"Compile Job", 3);
90 this.deleteCompilerFile(this.compilerInputFile);
91 if (this.createCompInputFile()) {
92 List<String> filesToCompile = this.readCompilerInputFile();
95 this.runCompiler(filesToCompile);
98 if (this.options.type == CompileType.DELTA) {
100 ObjectId head = repositoryTools.getRepository().resolve(Constants.HEAD);
104 catch (IOException e) {
105 logger.error(
"Error while writing objectcodeinfo file: {}\n Stack:\n{}",
106 e.getMessage(), e.getStackTrace());
111 }
catch (IOException e) {
113 return Status.CANCEL_STATUS;
118 return Status.OK_STATUS;
121 private boolean createCompInputFile() {
128 Integer callExitValue = -1;
129 ProcessBuilder builder =
new ProcessBuilder();
131 if (options.type == CompileType.BRANCH)
133 builder.command(
"python",
"-m",
"pa_common.compiler",
"-r",
134 repositoryTools.getRepository().getWorkTree().getAbsolutePath(),
"--branch",
"--no-compile",
135 "--input-file",
this.compilerInputFile.getAbsolutePath());
137 if (options.type == CompileType.DIFF)
139 builder.command(
"python",
"-m",
"pa_common.compiler",
"-r",
140 repositoryTools.getRepository().getWorkTree().getAbsolutePath(),
"--from", options.commit1,
"--to",
141 options.commit2,
"--no-compile",
"--input-file",
this.compilerInputFile.getAbsolutePath());
143 if (options.type == CompileType.DELTA)
145 builder.command(
"python",
"-m",
"pa_common.compiler",
"-r",
146 repositoryTools.getRepository().getWorkTree().getAbsolutePath(),
"--delta",
147 "--no-compile",
"--input-file",
this.compilerInputFile.getAbsolutePath());
149 builder.redirectErrorStream(
true);
150 builder.redirectInput(Redirect.PIPE);
151 builder.redirectOutput(Redirect.PIPE);
154 final Process p = builder.start();
156 logger.info(
"Start of compiler file generation: Running '{}'", builder.command());
157 try (Reader rOut =
new InputStreamReader(p.getInputStream());
158 BufferedReader inOut =
new BufferedReader(rOut)) {
162 line = inOut.readLine();
166 }
while (line !=
null);
170 callExitValue = p.waitFor();
172 if (callExitValue > 0) {
173 Reader rErr =
new InputStreamReader(p.getErrorStream());
174 try (BufferedReader inErr =
new BufferedReader(rErr)) {
177 line = inErr.readLine();
181 }
while (line !=
null);
184 if (callExitValue > 1) {
185 final String errorMsg = MessageFormat.format(
186 "Error while generating compiler input file {0} for git repo {1}\n\n"
187 +
"See console output for more information.",
188 this.compilerInputFile.getAbsolutePath(),
189 repositoryTools.getRepository().getWorkTree().getAbsolutePath());
191 Display.getDefault().syncExec(
new Runnable() {
193 MessageDialog.openError(Display.getDefault().getActiveShell(),
"Compile Job", errorMsg);
199 if (callExitValue == 1) {
200 final String errorMsg =
"No files to compile";
202 Display.getDefault().syncExec(
new Runnable() {
204 MessageDialog.openInformation(Display.getDefault().getActiveShell(),
"Compile Job",
212 }
catch (IOException e) {
213 final String errorMsg = MessageFormat.format(
214 "Exception while running python package pa_common.compiler with exitcode {0} and {1}",
215 callExitValue, e.getMessage());
216 logger.error(errorMsg);
218 }
catch (InterruptedException e) {
219 final String errorMsg = MessageFormat.format(
"Error {0} occured", e.getMessage());
220 logger.error(errorMsg);
221 Thread.currentThread().interrupt();
229 private Integer getMaxCompileCount() {
233 return Integer.parseInt(maxCountPref);
234 }
catch (NumberFormatException e) {
235 maxCountPref =
"100";
240 private String envNameLastprocedure(Map<String, String> envparam) {
245 for (Map.Entry<String, String> e : envparam.entrySet()) {
247 if (e.getKey().toUpperCase().equals(LAST_PROCEDURE_ENV_NAME))
252 return LAST_PROCEDURE_ENV_NAME;
255 private List<String> readCompilerInputFile() {
256 List<String> resultList =
new ArrayList<String>();
257 if (!this.compilerInputFile.exists())
260 try (BufferedReader reader =
new BufferedReader(
new FileReader(this.compilerInputFile))) {
261 String line = reader.readLine();
262 while (line !=
null) {
263 resultList.add(line);
264 line = reader.readLine();
266 }
catch (IOException e) {
273 private boolean deleteCompilerFile(File compFile) {
275 if (compFile.isFile()) {
277 Files.delete(compFile.toPath());
278 }
catch (IOException e) {
286 private void runCompiler(List<String> filesToCompile)
throws IOException {
288 logger.info(
"Programms to compile = {}", filesToCompile.toString());
289 if (filesToCompile.size() >=
this.maxFileCount
290 || (filesToCompile.size() == 1 && filesToCompile.get(0).equals(
"*"))) {
291 List<String> commands =
new ArrayList<>();
292 StringBuilder paCcommand =
new StringBuilder();
294 paCcommand.append(repositoryTools.getPaProject().getBridge().getpAStartupParam(
"PROALPHADIR",
"")
295 +
"\\common\\startup\\server\\pa-adm.bat");
296 paCcommand.append(
" dev ");
297 paCcommand.append(
" multi-comp ");
298 paCcommand.append(
"start ");
299 paCcommand.append(
"-p \"UserID=system\" ");
300 paCcommand.append(
"-p \"Password=compakt\" ");
301 paCcommand.append(
"-n ");
302 paCcommand.append(
"-pool active ");
307 commands.add(paCcommand.toString());
309 ProcessBuilder pb =
new ProcessBuilder(commands);
312 String env_name = envNameLastprocedure(pb.environment());
314 pb.environment().put(env_name, this.compilerInputFile.getAbsolutePath());
316 pb.environment().put(COMPPROC_ENV_NAME,
"pastartd.p");
317 logger.debug(pb.environment().toString());
318 logger.debug(pb.command().toString());
320 Process p = pb.start();
322 BufferedReader bir =
new BufferedReader(
new InputStreamReader(p.getInputStream()));
324 while ((line = bir.readLine()) !=
null) {
330 repositoryTools.getPaProject().getBridge().startCompilerOpt(this.compilerInputFile.getAbsolutePath(),
true);
static void writeObjectCodeInfoFile(Repository repository, String commitId)
static Activator getDefault()