Safely remove a file with retry logic. Args: file_path: Path to the file to remove max_retries: Maximum number of retry attempts Returns: True if removal was successful, False otherwise
(file_path: Path, max_retries: int)
| 213 | |
| 214 | |
| 215 | def safe_file_removal(file_path: Path, max_retries: int) -> bool: |
| 216 | """ |
| 217 | Safely remove a file with retry logic. |
| 218 | |
| 219 | Args: |
| 220 | file_path: Path to the file to remove |
| 221 | max_retries: Maximum number of retry attempts |
| 222 | |
| 223 | Returns: |
| 224 | True if removal was successful, False otherwise |
| 225 | """ |
| 226 | if not file_path.exists(): |
| 227 | return True |
| 228 | |
| 229 | for attempt in range(max_retries): |
| 230 | try: |
| 231 | file_path.unlink() |
| 232 | locked_print(f"Successfully removed file {file_path}") |
| 233 | return True |
| 234 | except OSError as e: |
| 235 | if attempt == max_retries - 1: |
| 236 | locked_print( |
| 237 | f"Failed to remove file {file_path} after {max_retries} attempts: {e}" |
| 238 | ) |
| 239 | return False |
| 240 | |
| 241 | locked_print( |
| 242 | f"Failed to remove file {file_path} on attempt {attempt + 1}: {e}" |
| 243 | ) |
| 244 | |
| 245 | # Check if another process removed it |
| 246 | if not file_path.exists(): |
| 247 | locked_print(f"File {file_path} was removed by another process") |
| 248 | return True |
| 249 | |
| 250 | time.sleep(0.1 * (attempt + 1)) |
| 251 | |
| 252 | except KeyboardInterrupt as ki: |
| 253 | handle_keyboard_interrupt(ki) |
| 254 | raise |
| 255 | except Exception as e: |
| 256 | locked_print(f"Unexpected error removing file {file_path}: {e}") |
| 257 | return False |
| 258 | |
| 259 | return False |
| 260 | |
| 261 | |
| 262 | def create_build_dir( |
no test coverage detected