Use pythons tempdir
Some checks failed
Python CI / test (push) Successful in 1m13s
Lint / lint (push) Successful in 1m16s
Tests / test (3.10) (push) Failing after 57s
Tests / test (3.11) (push) Failing after 53s
Tests / test (3.9) (push) Failing after 56s

This commit is contained in:
Duncan Tourolle 2025-11-11 17:00:45 +01:00
parent 099ccb4c6b
commit 7d8d2d42f8
2 changed files with 19 additions and 18 deletions

View File

@ -119,9 +119,9 @@ class Project:
# Embedded templates - templates that travel with the project # Embedded templates - templates that travel with the project
self.embedded_templates: Dict[str, Dict[str, Any]] = {} self.embedded_templates: Dict[str, Dict[str, Any]] = {}
# Track if this project was loaded from a temp directory and should be cleaned up # Temporary directory management (if loaded from .ppz)
self._is_temp_project = False # Using TemporaryDirectory instance that auto-cleans on deletion
self._temp_cleanup_path = None self._temp_dir = None
# Initialize asset manager # Initialize asset manager
self.asset_manager = AssetManager(self.folder_path) self.asset_manager = AssetManager(self.folder_path)
@ -370,14 +370,15 @@ class Project:
Cleanup project resources, including temporary directories. Cleanup project resources, including temporary directories.
Should be called when the project is closed or no longer needed. Should be called when the project is closed or no longer needed.
""" """
if self._is_temp_project and self._temp_cleanup_path: if self._temp_dir is not None:
try: try:
import shutil # Let TemporaryDirectory clean itself up
if os.path.exists(self._temp_cleanup_path): temp_path = self._temp_dir.name
shutil.rmtree(self._temp_cleanup_path) self._temp_dir.cleanup()
print(f"Cleaned up temporary project directory: {self._temp_cleanup_path}") self._temp_dir = None
print(f"Cleaned up temporary project directory: {temp_path}")
except Exception as e: except Exception as e:
print(f"Warning: Failed to cleanup temporary directory {self._temp_cleanup_path}: {e}") print(f"Warning: Failed to cleanup temporary directory: {e}")
def __del__(self): def __del__(self):
"""Destructor to ensure cleanup happens when project is deleted.""" """Destructor to ensure cleanup happens when project is deleted."""

View File

@ -135,15 +135,15 @@ def load_from_zip(zip_path: str, extract_to: Optional[str] = None) -> Tuple[Opti
return None, f"ZIP file not found: {zip_path}" return None, f"ZIP file not found: {zip_path}"
# Track if we created a temp directory # Track if we created a temp directory
is_temp_extraction = False temp_dir_obj = None
# Determine extraction directory # Determine extraction directory
if extract_to is None: if extract_to is None:
# Extract to a temporary directory # Create a temporary directory using TemporaryDirectory
# Use prefix to make it easier to identify temp project folders # This will be attached to the Project and auto-cleaned on deletion
zip_basename = os.path.splitext(os.path.basename(zip_path))[0] zip_basename = os.path.splitext(os.path.basename(zip_path))[0]
extract_to = tempfile.mkdtemp(prefix=f"pyPhotoAlbum_{zip_basename}_") temp_dir_obj = tempfile.TemporaryDirectory(prefix=f"pyPhotoAlbum_{zip_basename}_")
is_temp_extraction = True extract_to = temp_dir_obj.name
else: else:
# Create extraction directory if it doesn't exist # Create extraction directory if it doesn't exist
os.makedirs(extract_to, exist_ok=True) os.makedirs(extract_to, exist_ok=True)
@ -194,10 +194,10 @@ def load_from_zip(zip_path: str, extract_to: Optional[str] = None) -> Tuple[Opti
project.asset_manager.project_folder = extract_to project.asset_manager.project_folder = extract_to
project.asset_manager.assets_folder = os.path.join(extract_to, "assets") project.asset_manager.assets_folder = os.path.join(extract_to, "assets")
# Mark as temporary project if we extracted to a temp directory # Attach temporary directory to project (if we created one)
if is_temp_extraction: # The TemporaryDirectory will auto-cleanup when the project is deleted
project._is_temp_project = True if temp_dir_obj is not None:
project._temp_cleanup_path = extract_to project._temp_dir = temp_dir_obj
print(f"Project loaded to temporary directory: {extract_to}") print(f"Project loaded to temporary directory: {extract_to}")
# Normalize asset paths in all ImageData elements # Normalize asset paths in all ImageData elements