Create a directory with an increased number as suffix for the given prefix.
(root: Path, prefix: str, mode: int = 0o700)
| 222 | |
| 223 | |
| 224 | def make_numbered_dir(root: Path, prefix: str, mode: int = 0o700) -> Path: |
| 225 | """Create a directory with an increased number as suffix for the given prefix.""" |
| 226 | for i in range(10): |
| 227 | # try up to 10 times to create the directory |
| 228 | max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1) |
| 229 | new_number = max_existing + 1 |
| 230 | new_path = root.joinpath(f"{prefix}{new_number}") |
| 231 | try: |
| 232 | new_path.mkdir(mode=mode) |
| 233 | except Exception: |
| 234 | pass |
| 235 | else: |
| 236 | _force_symlink(root, prefix + "current", new_path) |
| 237 | return new_path |
| 238 | else: |
| 239 | raise OSError( |
| 240 | "could not create numbered dir with prefix " |
| 241 | f"{prefix} in {root} after 10 tries" |
| 242 | ) |
| 243 | |
| 244 | |
| 245 | def create_cleanup_lock(p: Path) -> Path: |