Skip to content

Commit

Permalink
update associated folders
Browse files Browse the repository at this point in the history
  • Loading branch information
dipinknair committed Oct 22, 2024
1 parent 82d64ca commit eb91ef4
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import atexit
import os
import shutil
import tempfile
import typing
import warnings

Expand Down Expand Up @@ -212,30 +211,28 @@ def save_as(self, path: str, overwrite: bool = False):
"""
if not os.path.exists(path):
self.DataModel.Project.SaveAs(path)
return
else:
if not overwrite:
raise Exception(
f"File already exists in {path}, Use ``overwrite`` flag to "
"replace the existing file."
)

temp_dir = tempfile.mkdtemp()
temp_file_path = os.path.join(temp_dir, os.path.basename(path))
file_name = os.path.basename(path)
file_dir = os.path.dirname(path)
associated_dir = os.path.join(file_dir, file_name.replace(".mechdb", "_Mech_Files"))

try:
# Move existing file to temp location
shutil.move(path, temp_file_path)
# Save as new file
# Remove existing files and associated folder
if os.path.exists(path):
os.remove(path)
if os.path.exists(associated_dir):
shutil.rmtree(associated_dir)
# Save the new file
self.DataModel.Project.SaveAs(path)
# Remove file from temp location
os.remove(temp_file_path)
except Exception as e:
# Restore original file from temp location
shutil.move(temp_file_path, path)
raise e
finally:
# Cleanup temp directory
shutil.rmtree(temp_dir)

def launch_gui(self, delete_tmp_on_close: bool = True, dry_run: bool = False):
"""Launch the GUI."""
Expand Down

0 comments on commit eb91ef4

Please sign in to comment.