Files
VisualSapfor/src/SapforTestingSystem/SapforTest/SapforTest.java

168 lines
6.7 KiB
Java
Raw Normal View History

2023-09-21 21:17:02 +03:00
package SapforTestingSystem.SapforTest;
2023-09-30 00:18:44 +03:00
import Common.Constants.Constants;
2023-09-30 18:22:14 +03:00
import Common.Global;
2023-09-30 00:18:44 +03:00
import Common.Utils.Utils;
import GlobalData.Tasks.TaskState;
2023-09-30 00:18:44 +03:00
import ProjectData.Project.db_project_info;
import SapforTestingSystem.Json.SapforVersion_json;
import SapforTestingSystem.SapforTask.SapforTask;
2023-09-30 00:18:44 +03:00
import Visual_DVM_2021.Passes.PassCode_2021;
import Visual_DVM_2021.Passes.PassException;
import org.apache.commons.io.FileUtils;
2023-09-22 00:16:46 +03:00
import java.io.File;
2023-09-30 00:18:44 +03:00
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;
2023-09-21 21:17:02 +03:00
public class SapforTest {
2023-09-30 00:18:44 +03:00
File root;
File sapfor_drv;
String flags;
Vector<PassCode_2021> codes;
//-----
File parentTask;
File task;
//-----
Process process = null;
int exit_code = Constants.Nan;
//----
File outputFile = null;
File errorsFile = null;
2023-09-22 00:16:46 +03:00
//--
2023-09-30 00:18:44 +03:00
Vector<String> outputLines;
Vector<String> errorsLines;
//---
public SapforTask taskResult = null;
2023-09-30 00:18:44 +03:00
//---
public SapforTest(File sapfor_drv_in, File root_in, String flags_in, List<PassCode_2021> codes_in) {
sapfor_drv = sapfor_drv_in;
root = root_in;
flags = flags_in;
codes = new Vector<>(codes_in);
}
protected static boolean checkLines(Vector<String> lines) {
for (String line : lines) {
if (line.toLowerCase().contains("internal error")) {
return false;
}
if (line.toLowerCase().contains("exception")) {
return false;
}
if (line.contains("[ERROR]")) {
return false;
}
}
return true;
}
protected boolean performSapforScript(String name, File workspace, String command, String outName, String errName) throws Exception {
process = null;
exit_code = Constants.Nan;
//---
File data_workspace = new File(workspace, db_project_info.data);
Utils.CheckDirectory(data_workspace);
outputFile = new File(data_workspace, outName);
errorsFile = new File(data_workspace, errName);
Utils.delete_with_check(outputFile);
Utils.delete_with_check(errorsFile);
//---
File file = new File(data_workspace, name + (Global.isWindows ? ".bat" : ".sh"));
2023-09-30 00:18:44 +03:00
FileUtils.write(file,
Utils.DQuotes(sapfor_drv.getAbsolutePath())
+ (flags.isEmpty() ? "" : (" " + flags))
+ " -noLogo"
+ " " + command +
" 1>" +
Utils.DQuotes(outputFile.getAbsolutePath()) +
" 2>" +
Utils.DQuotes(errorsFile.getAbsolutePath()),
Charset.defaultCharset());
if (!file.setExecutable(true))
2023-09-30 18:22:14 +03:00
throw new Exception("Не удалось сделать файл скрипта " + name + " исполняемым!");
2023-09-30 00:18:44 +03:00
//--
ProcessBuilder procBuilder = new ProcessBuilder(file.getAbsolutePath());
procBuilder.directory(workspace);
process = procBuilder.start();
exit_code = process.waitFor();
if (exit_code != 0)
throw new PassException("Процесс завершился с кодом " + exit_code);
process = null;
//---
outputLines = new Vector<>(FileUtils.readLines(outputFile));
errorsLines = new Vector<>(FileUtils.readLines(errorsFile));
return checkLines(outputLines) && checkLines(errorsLines);
}
protected boolean parse() throws Exception {
if (performSapforScript("parse", parentTask,
"-parse *.f *.for *.fdv *.f90 *.f77",
"parse_out.txt", "parse_err.txt")
&& (new File(parentTask, "dvm.proj")).exists()) {
return true;
} else {
taskResult.state = TaskState.DoneWithErrors;
2023-09-30 00:18:44 +03:00
return false;
}
}
protected boolean transformation(PassCode_2021 code) throws Exception {
task = new File(parentTask, "v1");
Utils.CheckAndCleanDirectory(task); //папка для преобразования.
if (performSapforScript("transformation", parentTask,
code.getTestingCommand() + " -F " + Utils.DQuotes(task.getAbsolutePath()),
"out.txt",
"err.txt"
)) {
taskResult.state = TaskState.Done;
taskResult.versions.add(new SapforVersion_json(task.getAbsolutePath(), code.getDescription()));
2023-09-30 00:18:44 +03:00
parentTask = task;
return true;
}
Utils.delete_with_check(task);
taskResult.state = TaskState.DoneWithErrors;
2023-09-30 00:18:44 +03:00
return false;
}
protected boolean variants() throws Exception {
if (performSapforScript("create_variants", parentTask, " -t 13 -allVars",
"out.txt",
"err.txt"
)) {
//найти папки с вариантами.
File[] files_ = parentTask.listFiles((dir, name) -> dir.isDirectory() && Utils.isParallelVersionName(name));
if ((files_ != null) && (files_.length > 0)) {
Vector<File> files = new Vector<>(Arrays.asList(files_));
files.sort(Comparator.comparingInt(o -> Integer.parseInt(o.getName().substring(1))));
taskResult.state = TaskState.Done;
for (File file : files)
taskResult.variants.add(new SapforVersion_json(file.getAbsolutePath(), PassCode_2021.SPF_CreateParallelVariant.getDescription()));
return true;
}
}
taskResult.state = TaskState.DoneWithErrors;
2023-09-30 00:18:44 +03:00
return false;
}
public void Do() throws Exception {
parentTask = root;
//--
taskResult = new SapforTask();
taskResult.test_description = root.getName();
taskResult.sapfortaskspackage_2023_id = Integer.parseInt(root.getParentFile().getName());
taskResult.versions.add(new SapforVersion_json(parentTask.getAbsolutePath(), "исходная"));
2023-09-30 00:18:44 +03:00
//--
2023-09-30 00:39:13 +03:00
System.out.println(root.getName() + " " + Utils.Brackets(flags));
2023-09-30 00:18:44 +03:00
for (PassCode_2021 code : codes) {
2023-09-30 00:39:13 +03:00
System.out.println(code);
2023-09-30 00:18:44 +03:00
if (parse()) {
if (code.equals(PassCode_2021.CreateParallelVariants))
variants();
else if (!transformation(code))
break;
} else
break;
}
//Запись результатов.
File data_workspace = new File(root, db_project_info.data);
Utils.CheckDirectory(data_workspace);
Utils.jsonToFile(taskResult, new File(data_workspace, "result.txt"));
2023-09-22 00:16:46 +03:00
}
2023-09-21 21:17:02 +03:00
}