Create a numbered dir and register its cleanup. Similar to make_numbered_dir, but also maintains a lock file indicating that the directory is currently in use, and registers the cleanup of the lock and of stale numbered directories. :param keep: The number of sessions to re
(
*,
root: Path,
prefix: str,
mode: int,
keep: int,
lock_timeout: float,
register: Any,
)
| 372 | |
| 373 | |
| 374 | def make_numbered_dir_with_cleanup( |
| 375 | *, |
| 376 | root: Path, |
| 377 | prefix: str, |
| 378 | mode: int, |
| 379 | keep: int, |
| 380 | lock_timeout: float, |
| 381 | register: Any, |
| 382 | ) -> Path: |
| 383 | class="st">"""Create a numbered dir and register its cleanup. |
| 384 | |
| 385 | Similar to make_numbered_dir, but also maintains a lock file indicating that |
| 386 | the directory is currently in use, and registers the cleanup of the lock and |
| 387 | of stale numbered directories. |
| 388 | |
| 389 | :param keep: |
| 390 | The number of sessions to retain the directory. |
| 391 | :param lock_timeout: |
| 392 | In case of a crash, the lock remains class="st">"stuck". The timeout is a time |
| 393 | limit after which the lock is considered stale and can be removed. |
| 394 | :param register: |
| 395 | Called as register(cleanup_func, params...). Should schedule to call |
| 396 | passed cleanup functions on session finish. |
| 397 | class="st">""" |
| 398 | e = None |
| 399 | for i in range(10): |
| 400 | try: |
| 401 | p = make_numbered_dir(root, prefix, mode) |
| 402 | class="cm"># Only lock the current dir when keep is not 0 |
| 403 | if keep != 0: |
| 404 | lock_path = create_cleanup_lock(p) |
| 405 | register_cleanup_lock_removal(lock_path, register) |
| 406 | except Exception as exc: |
| 407 | e = exc |
| 408 | else: |
| 409 | consider_lock_dead_if_created_before = p.stat().st_mtime - lock_timeout |
| 410 | class="cm"># Register a cleanup for program exit |
| 411 | register( |
| 412 | cleanup_numbered_dir, |
| 413 | root, |
| 414 | prefix, |
| 415 | keep, |
| 416 | consider_lock_dead_if_created_before, |
| 417 | ) |
| 418 | return p |
| 419 | assert e is not None |
| 420 | raise e |
| 421 | |
| 422 | |
| 423 | def resolve_from_str(input: str, rootpath: Path) -> Path: |
no test coverage detected