Remove a numbered directory if its lock can be obtained and it does not seem to be in use.
(path: Path)
| 277 | |
| 278 | |
| 279 | def maybe_delete_a_numbered_dir(path: Path) -> None: |
| 280 | class="st">"""Remove a numbered directory if its lock can be obtained and it does |
| 281 | not seem to be in use.class="st">""" |
| 282 | path = ensure_extended_length_path(path) |
| 283 | lock_path = None |
| 284 | try: |
| 285 | lock_path = create_cleanup_lock(path) |
| 286 | parent = path.parent |
| 287 | |
| 288 | garbage = parent.joinpath(fclass="st">"garbage-{uuid.uuid4()}") |
| 289 | path.rename(garbage) |
| 290 | rm_rf(garbage) |
| 291 | except OSError: |
| 292 | class="cm"># known races: |
| 293 | class="cm"># * other process did a cleanup at the same time |
| 294 | class="cm"># * deletable directory was found |
| 295 | class="cm"># * process cwd (Windows) |
| 296 | return |
| 297 | finally: |
| 298 | class="cm"># If we created the lock, ensure we remove it even if we failed |
| 299 | class="cm"># to properly remove the numbered dir. |
| 300 | if lock_path is not None: |
| 301 | try: |
| 302 | lock_path.unlink() |
| 303 | except OSError: |
| 304 | pass |
| 305 | |
| 306 | |
| 307 | def ensure_deletable(path: Path, consider_lock_dead_if_created_before: float) -> bool: |