diff --git a/AIASSE.py b/AIASSE.py index 4293e44710446b57f4c7e5a56c0fd0fab48f0349..993beea1ace721ef035a6b1673020ecd57b50c7f 100644 --- a/AIASSE.py +++ b/AIASSE.py @@ -1452,16 +1452,14 @@ class Ui_MainWindow(QtWidgets.QMainWindow): if not directory: return - # Convert the selected directory to an absolute path. directory = os.path.abspath(directory) - current_working_dir = os.path.abspath(os.getcwd()) - + current_working_dir = os.path.dirname(os.path.abspath(__file__)) + target_base = os.path.join(current_working_dir, "Large_box", "Large") target_base = os.path.abspath(target_base) if not os.path.exists(target_base): os.makedirs(target_base) - # Prevent selecting a folder that's already within target_base. if os.path.commonpath([directory, target_base]) == target_base: QMessageBox.information(self, "Info", "Selected folder is already in Large_box/Large.") return @@ -1500,7 +1498,6 @@ class Ui_MainWindow(QtWidgets.QMainWindow): QMessageBox.critical(self, "Error", f"Failed to delete existing work: {str(e)}") return - # If the selected folder does not have a "Large" subfolder, check if it needs adjustment. src_dir = directory if os.path.basename(directory) != "Large": subdir = os.path.join(directory, "Large") @@ -1522,147 +1519,102 @@ class Ui_MainWindow(QtWidgets.QMainWindow): QMessageBox.critical(self, "Error", f"Failed to transfer work: {str(e)}") return - # Copy additional required files - runscript_src = os.path.join(current_working_dir, "src", "runscript_LB.txt") - runscript_src = os.path.abspath(runscript_src) - if os.path.exists(runscript_src): - try: - shutil.copy2(runscript_src, target_base) - except Exception as e: - QMessageBox.warning(self, "Warning", f"Failed to copy runscript_LB.txt: {str(e)}") - - epsr25_src = os.path.join(current_working_dir, "src", "EPSR25", "EPSR", "epsr25") - epsr25_src = os.path.abspath(epsr25_src) - if os.path.exists(epsr25_src): - try: - shutil.copy2(epsr25_src, target_base) - except Exception as e: - QMessageBox.warning(self, "Warning", f"Failed to copy epsr25: {str(e)}") + # Copy additional files + def copy_optional_file(src, dest_dir): + if os.path.exists(src): + try: + shutil.copy2(src, dest_dir) + except Exception as e: + QMessageBox.warning(self, "Warning", f"Failed to copy {os.path.basename(src)}: {str(e)}") - epsr26_src = os.path.join(current_working_dir, "src", "EPSR26", "EPSR", "epsr26") - epsr26_src = os.path.abspath(epsr26_src) - if os.path.exists(epsr26_src): - try: - shutil.copy2(epsr26_src, target_base) - except Exception as e: - QMessageBox.warning(self, "Warning", f"Failed to copy epsr26: {str(e)}") + copy_optional_file(os.path.join(current_working_dir, "src", "runscript_LB.txt"), target_base) + copy_optional_file(os.path.join(current_working_dir, "src", "EPSR25", "EPSR", "epsr25"), target_base) + copy_optional_file(os.path.join(current_working_dir, "src", "EPSR26", "EPSR", "epsr26"), target_base) - # --- Renaming Files and Updating References --- - # Step A: Collect original EPSR file bases (from both .EPSR and .EPSR.inp) + # Rename EPSR files epsr_bases = set() for file in os.listdir(target_base): if file.endswith(".EPSR.inp"): - base = file[:-len(".EPSR.inp")] - epsr_bases.add(base) + epsr_bases.add(file[:-len(".EPSR.inp")]) elif file.endswith(".EPSR"): - base = file[:-len(".EPSR")] - epsr_bases.add(base) + epsr_bases.add(file[:-len(".EPSR")]) - # Step B: Rename EPSR files for file in list(os.listdir(target_base)): file_path = os.path.join(target_base, file) if os.path.isfile(file_path): if file.endswith(".EPSR.inp"): - new_name = "Largebox.EPSR.inp" - new_path = os.path.join(target_base, new_name) + new_path = os.path.join(target_base, "Largebox.EPSR.inp") try: os.rename(file_path, new_path) except Exception as e: - QMessageBox.critical(self, "Error", f"Failed to rename {file} to {new_name}: {str(e)}") + QMessageBox.critical(self, "Error", f"Failed to rename {file}: {str(e)}") return elif file.endswith(".EPSR"): - new_name = "Largebox.EPSR" - new_path = os.path.join(target_base, new_name) + new_path = os.path.join(target_base, "Largebox.EPSR") try: os.rename(file_path, new_path) except Exception as e: - QMessageBox.critical(self, "Error", f"Failed to rename {file} to {new_name}: {str(e)}") + QMessageBox.critical(self, "Error", f"Failed to rename {file}: {str(e)}") return - # Step C: Rename .ato files ato_files = [file for file in os.listdir(target_base) if file.endswith(".ato")] renamed = False for file in ato_files: for base in epsr_bases: if file == f"{base}.ato": - new_name = "Largebox.ato" - new_path = os.path.join(target_base, new_name) try: - os.rename(os.path.join(target_base, file), new_path) + os.rename(os.path.join(target_base, file), os.path.join(target_base, "Largebox.ato")) renamed = True + break except Exception as e: - QMessageBox.critical(self, "Error", f"Failed to rename {file} to {new_name}: {str(e)}") + QMessageBox.critical(self, "Error", f"Failed to rename {file}: {str(e)}") return - - # If no .ato file was renamed and there are .ato files present, prompt the user. if not renamed and ato_files: - selected, ok = QInputDialog.getItem( - self, - "Select Main .ato File", - "Multiple .ato files found. Please select the main file:", - ato_files, - 0, - False - ) + selected, ok = QInputDialog.getItem(self, "Select Main .ato File", "Multiple .ato files found:", ato_files, 0, False) if ok and selected: - new_name = "Largebox.ato" try: - os.rename(os.path.join(target_base, selected), os.path.join(target_base, new_name)) + os.rename(os.path.join(target_base, selected), os.path.join(target_base, "Largebox.ato")) except Exception as e: - QMessageBox.critical(self, "Error", f"Failed to rename {selected} to {new_name}: {str(e)}") + QMessageBox.critical(self, "Error", f"Failed to rename {selected}: {str(e)}") return - # Step D: Rename .pcof files for file in list(os.listdir(target_base)): - file_path = os.path.join(target_base, file) - if os.path.isfile(file_path) and file.endswith(".pcof"): - new_name = "Largebox.pcof" - new_path = os.path.join(target_base, new_name) + if file.endswith(".pcof"): try: - os.rename(file_path, new_path) + os.rename(os.path.join(target_base, file), os.path.join(target_base, "Largebox.pcof")) except Exception as e: - QMessageBox.critical(self, "Error", f"Failed to rename {file} to {new_name}: {str(e)}") + QMessageBox.critical(self, "Error", f"Failed to rename {file}: {str(e)}") return - # NEW STEP: Rename additional EPSR files with extra extensions epsr_pattern = re.compile(r".+\.EPSR\.(.+)") for file in list(os.listdir(target_base)): - file_path = os.path.join(target_base, file) - if os.path.isfile(file_path): - if file.endswith(".EPSR.inp") or file.endswith(".EPSR") or file.endswith(".ato") or file.endswith(".pcof"): - continue - match = epsr_pattern.fullmatch(file) - if match: - ext = match.group(1) - new_name = f"Largebox.EPSR.{ext}" - new_path = os.path.join(target_base, new_name) - try: - os.rename(file_path, new_path) - except Exception as e: - QMessageBox.critical(self, "Error", f"Failed to rename {file} to {new_name}: {str(e)}") - return + if file.endswith((".EPSR.inp", ".EPSR", ".ato", ".pcof")): + continue + match = epsr_pattern.fullmatch(file) + if match: + ext = match.group(1) + try: + os.rename(os.path.join(target_base, file), os.path.join(target_base, f"Largebox.EPSR.{ext}")) + except Exception as e: + QMessageBox.critical(self, "Error", f"Failed to rename {file}: {str(e)}") + return - # Step E: Update references inside any '.EPSR.inp' file for file in os.listdir(target_base): if file.endswith(".EPSR.inp"): inp_path = os.path.join(target_base, file) try: with open(inp_path, "r") as f: content = f.read() - # Update fnameato and fnamepcof entries using regex substitutions content = re.sub(r"(fnameato\s+)\S+", r"\1Largebox.ato", content) content = re.sub(r"(fnamepcof\s+)\S+", r"\1Largebox.pcof", content) - - # Also update other references based on original EPSR bases for base in epsr_bases: content = content.replace(f"{base}.EPSR", "Largebox.EPSR") content = content.replace(f"{base}.ato", "Largebox.ato") - content = content.replace(f"{base}Largebox.pcof", "Largebox.pcof") content = content.replace(f"{base}.pcof", "Largebox.pcof") with open(inp_path, "w") as f: f.write(content) except Exception as e: - QMessageBox.warning(self, "Warning", f"Failed to update references in {file}: {str(e)}") + QMessageBox.warning(self, "Warning", f"Failed to update {file}: {str(e)}") QMessageBox.information(self, "Success", "Work transferred and updated successfully.")