Remove a numbered directory if its lock can be obtained and it does not seem to be in use.
(path: Path)
| 262 | |
| 263 | |
| 264 | def maybe_delete_a_numbered_dir(path: Path) -> None: |
| 265 | """Remove a numbered directory if its lock can be obtained and it does |
| 266 | not seem to be in use.""" |
| 267 | path = ensure_extended_length_path(path) |
| 268 | lock_path = None |
| 269 | try: |
| 270 | lock_path = create_cleanup_lock(path) |
| 271 | parent = path.parent |
| 272 | |
| 273 | garbage = parent.joinpath(f"garbage-{uuid.uuid4()}") |
| 274 | path.rename(garbage) |
| 275 | rm_rf(garbage) |
| 276 | except OSError: |
| 277 | # known races: |
| 278 | # * other process did a cleanup at the same time |
| 279 | # * deletable folder was found |
| 280 | # * process cwd (Windows) |
| 281 | return |
| 282 | finally: |
| 283 | # If we created the lock, ensure we remove it even if we failed |
| 284 | # to properly remove the numbered dir. |
| 285 | if lock_path is not None: |
| 286 | try: |
| 287 | lock_path.unlink() |
| 288 | except OSError: |
| 289 | pass |
| 290 | |
| 291 | |
| 292 | def ensure_deletable(path: Path, consider_lock_dead_if_created_before: float) -> bool: |