Ensure directory exists, create if necessary. Args: path: Directory path mode: Directory permissions (default: 0o700 - user only) Returns: Path to the directory Raises: FileSystemError: If directory cannot be created
(path: Path, mode: int = 0o700)
| 11 | |
| 12 | |
| 13 | def ensure_directory(path: Path, mode: int = 0o700) -> Path: |
| 14 | """ |
| 15 | Ensure directory exists, create if necessary. |
| 16 | |
| 17 | Args: |
| 18 | path: Directory path |
| 19 | mode: Directory permissions (default: 0o700 - user only) |
| 20 | |
| 21 | Returns: |
| 22 | Path to the directory |
| 23 | |
| 24 | Raises: |
| 25 | FileSystemError: If directory cannot be created |
| 26 | """ |
| 27 | try: |
| 28 | path = Path(path).expanduser().resolve() |
| 29 | path.mkdir(parents=True, exist_ok=True, mode=mode) |
| 30 | return path |
| 31 | except PermissionError: |
| 32 | raise FileSystemError( |
| 33 | f"Permission denied: Cannot create directory {path}\nTry: chmod u+w {path.parent}" |
| 34 | ) |
| 35 | except OSError as e: |
| 36 | raise FileSystemError(f"Cannot create directory {path}: {e}") |
| 37 | |
| 38 | |
| 39 | def check_writable(path: Path) -> bool: |
| 40 | """ |
no test coverage detected