From e9e7e373e256b4091a09a4093e96c7e201b2a819 Mon Sep 17 00:00:00 2001 From: David Rojas Date: Fri, 22 Nov 2024 14:21:15 -0500 Subject: [PATCH] chore: changed working dir before deleting template dir and added more err handling --- template_content/post_init.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/template_content/post_init.py b/template_content/post_init.py index 56a4400..1d21c43 100644 --- a/template_content/post_init.py +++ b/template_content/post_init.py @@ -1,6 +1,7 @@ import shutil import sys from pathlib import Path +import os def main(): @@ -21,9 +22,28 @@ def main(): if project_dir.exists(): try: + # Change working directory to parent before deletion + os.chdir(project_dir.parent) shutil.rmtree(project_dir) - except: - pass + except PermissionError as e: + print( + f"Failed to clean up {project_dir}: Unable to remove directory due to permissions: {e}", + file=sys.stderr, + ) + print( + "Please ensure no files are open in the directory and try again.", + file=sys.stderr, + ) + sys.exit(1) + except OSError as e: + print(f"Failed to clean up {project_dir}: {str(e)}", file=sys.stderr) + print( + "Please ensure no files are open in the directory and try again.", + file=sys.stderr, + ) + sys.exit(1) + except Exception as e: + print(f"Failed to clean up {project_dir}: {str(e)}", file=sys.stderr) sys.exit(1)