package SapforTestingSystem; import Common.Constants.Constants; import Common.Global; import Common.Utils.Utils; import GlobalData.Tasks.TaskState; import ProjectData.Project.db_project_info; import SapforTestingSystem.Json.SapforPackage_json; import SapforTestingSystem.Json.SapforVersion_json; import SapforTestingSystem.SapforTask.SapforTask; import Visual_DVM_2021.Passes.PassCode_2021; import Visual_DVM_2021.Passes.PassException; import Visual_DVM_2021.Passes.Pass_2021; import org.apache.commons.io.FileUtils; import java.io.File; import java.nio.charset.Charset; import java.nio.file.Paths; import java.util.Arrays; import java.util.Comparator; import java.util.Vector; public class PerformSapforTask extends Pass_2021 { @Override public String getDescription() { return "Запуск задачи SAPFOR"; } @Override protected boolean needsAnimation() { return false; } //-- File sapfor_drv; SapforPackage_json sapforPackage_json; //----- File parentTask; File task; //----- Process process = null; int exit_code = Constants.Nan; //---- File outputFile = null; File errorsFile = null; //-- Vector outputLines; Vector errorsLines; //--- @Override protected boolean canStart(Object... args) throws Exception { sapfor_drv = (File) args[0]; sapforPackage_json = (SapforPackage_json) args[1]; String testDescription = (String) args[2]; target = (SapforTask) args[3]; //--->> parentTask = Paths.get(Global.Home, sapforPackage_json.id, testDescription).toFile(); task = null; //--->> target.sapfortaskspackage_2023_id = Integer.parseInt(sapforPackage_json.id); target.test_description = testDescription; target.root = parentTask.getAbsolutePath(); return true; } protected static boolean checkLines(Vector 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")); FileUtils.write(file, Utils.DQuotes(sapfor_drv.getAbsolutePath()) + (sapforPackage_json.flags.isEmpty() ? "" : (" " + sapforPackage_json.flags)) + " -noLogo" + " " + command + " 1>" + Utils.DQuotes(outputFile.getAbsolutePath()) + " 2>" + Utils.DQuotes(errorsFile.getAbsolutePath()), Charset.defaultCharset()); if (!file.setExecutable(true)) throw new Exception("Не удалось сделать файл скрипта " + name + " исполняемым!"); //-- 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 { target.state = TaskState.DoneWithErrors; 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" )) { target.state = TaskState.Done; target.versions.add(new SapforVersion_json(task.getAbsolutePath(), code.getDescription())); parentTask = task; return true; } Utils.delete_with_check(task); target.state = TaskState.DoneWithErrors; 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 files = new Vector<>(Arrays.asList(files_)); files.sort(Comparator.comparingInt(o -> Integer.parseInt(o.getName().substring(1)))); target.state = TaskState.Done; for (File file : files) target.variants.add(new SapforVersion_json(file.getAbsolutePath(), PassCode_2021.SPF_CreateParallelVariant.getDescription())); return true; } } target.state = TaskState.DoneWithErrors; return false; } @Override protected void body() throws Exception { System.out.println(target.root + " " + Utils.Brackets(sapforPackage_json.flags));//!! for (PassCode_2021 code : sapforPackage_json.codes) { System.out.println(code); //!! if (parse()) { if (code.equals(PassCode_2021.CreateParallelVariants)) variants(); else if (!transformation(code)) break; } else break; } /* //Запись результатов. todo сделать единый файл со списком итогов. уже после выполнения нитей. File data_workspace = new File(target.root, db_project_info.data); Utils.CheckDirectory(data_workspace); Utils.jsonToFile(target, new File(data_workspace, "result.txt")); */ } }