package Visual_DVM_2021.Passes.All; import Common.Utils.CommonUtils; import Common_old.Constants; import Common_old.Current; import Common_old.Utils.Files.VDirectoryChooser; import Common_old.Utils.Utils; import GlobalData.Settings.SettingName; import ProjectData.Files.ProjectFile; import ProjectData.LanguageName; import Repository.Component.Sapfor.Sapfor; import TestingSystem.Common.Group.Group; import TestingSystem.Common.Test.Test; 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.util.Arrays; import java.util.Vector; public class CreateTestFromDirectory extends Pass_2021 { @Override public String getIconPath() { return "/icons/OpenProject.png"; } @Override public String getButtonText() { return ""; } @Override protected boolean needsAnimation() { return true; } Group group = null; File dir = null; boolean from_files_chooser = false; //-- void saveDirectory() { Pass_2021.passes.get(PassCode_2021.UpdateSetting).Do(SettingName.ProjectsSearchDirectory, (dir.getParentFile() == null) ? dir.getAbsolutePath() : dir.getParent() ); } Vector files = null; protected boolean selectFiles() { VDirectoryChooser directoryChooser = new VDirectoryChooser("Выбор домашней папки теста"); Utils.RestoreSelectedDirectory(directoryChooser); dir = directoryChooser.ShowDialog(); if (dir == null) { Log.Writeln_("Папка не выбрана."); return false; } else { files = new Vector<>(Arrays.asList(dir.listFiles())); saveDirectory(); } return true; } //-- Vector project_files = new Vector<>(); protected boolean initTarget() throws Exception { target = new Test(); target.sender_address = Current.getAccount().email; target.sender_name = Current.getAccount().name; target.group_id = group.id; target.description = dir.getName(); return true; } @Override protected boolean canStart(Object... args) throws Exception { files = null; if (args.length == 0) { //-- from_files_chooser = true; if (!Current.Check(Log, Current.Group)) return false; group = Current.getGroup(); if (!selectFiles()) return false; //- } else { from_files_chooser = false; dir = (File) args[0]; group = (Group) args[1]; files = new Vector<>(Arrays.asList(dir.listFiles())); } //--- int subdirs = 0; int bad = 0; int active_programs = 0; int headers = 0; int other_project_files = 0; //--- if (dir.getName().equalsIgnoreCase(Constants.data)) { Log.Writeln_("Папка " + CommonUtils.Brackets(dir) + " является служебной папкой визуализатора."); return false; } //-- if (CommonUtils.ContainsCyrillic(dir.getName()) || CommonUtils.ContainsForbiddenName(dir.getName())) { Log.Writeln_("Имя папки " + CommonUtils.Brackets(dir.getName()) + " содержит запрещённые символы " + CommonUtils.printAllForbiddenCharacters() + ", или кириллицу."); return false; } //-- if (files == null) { Log.Writeln_("Не удалось получить список файлов для папки " + CommonUtils.Brackets(dir) + "."); return false; } //--- project_files = new Vector<>(); //-- for (File file : files) { //----- if (file.isDirectory()) { //если это подпапка нам все равно на каком она языке. не версия и не служебная. ее наличие уже не допустимо. if (!file.getName().equalsIgnoreCase(Constants.data) && !Utils.isVersion(file)) { subdirs++; } } else if (file.isFile() && !CommonUtils.ContainsCyrillic(file.getName()) && !CommonUtils.ContainsForbiddenName(file.getName())) { //если файл. все недопустимые файлы просто игнорируются. ProjectFile projectFile = new ProjectFile(file); if (isNotExcluded(projectFile)) { switch (projectFile.fileType) { case program: if (projectFile.languageName.equals(group.language)) { active_programs++; project_files.add(projectFile); } else other_project_files++; break; case header: headers++; project_files.add(projectFile); break; case none: project_files.add(projectFile); other_project_files++; break; default: other_project_files++; break; } } } } //-- if (subdirs > 0) { Log.Writeln_("Папка " + CommonUtils.Brackets(dir) + " содержит вложенные подпапки,\n" + "не являющиеся версиями или данными визуализатора"); return false; } if (active_programs == 0) { Log.Writeln_("Папка не содержит ни одной программы на языке " + group.language.getDescription() + "."); return false; } if (project_files.isEmpty()) { Log.Writeln_("В папке не найдено файлов с допустимыми расширениями для языка " + group.language.getDescription() + "\n" + group.language.PrintExtensions() ); } //---- if (!initTarget()) return false; //---- Vector filesNames = new Vector<>(); for (ProjectFile projectFile : project_files) filesNames.add(projectFile.file.getName()); target.files = String.join("\n", filesNames); return true; } public boolean isNotExcluded(ProjectFile projectFile) { return true; } //- public File packTestCode() throws Exception { target.temp_project_name = CommonUtils.getDateName("test"); //- File tempProject = target.getTempProject(); File tempArchive = target.getTempArchive(); //- создать бд. FileUtils.forceMkdir(tempProject); //-- for (ProjectFile projectFile : project_files) { File dst = new File(tempProject, projectFile.file.getName()); FileUtils.copyFile(projectFile.file, dst); } //--- Utils.ClearProjectData(tempProject); //-- ZipFolderPass zip = new ZipFolderPass(); if (zip.Do(tempProject.getAbsolutePath(), tempArchive.getAbsolutePath())) { target.project_archive_bytes = CommonUtils.fileToBytes(tempArchive); } else throw new PassException("Не удалось создать архив папки с кодом."); return tempProject; } //- @Override protected void body() throws Exception { ShowMessage1(dir.getName()); //-- File tempProject = packTestCode(); //создание копии папки, и архивация. //-- получить размерность консольным сапфором. папка уже отправлена и чистить ее не нужно!! ShowMessage2("Синтаксический анализ и определение размерности"); if (group.language == LanguageName.fortran) {//если не определит, будут нули. Sapfor.getMinMaxDim(Sapfor.getTempCopy(Current.getSapfor().getFile()), tempProject, target); } } @Override protected boolean validate() { return Log.isEmpty(); } @Override protected void performDone() throws Exception { super.performDone(); if (from_files_chooser) passes.get(PassCode_2021.PublishTest).Do(target); } }