Files
proxy-scraper-checker/proxy_scraper_checker/fs.py
T

42 lines
1.1 KiB
Python
Raw Normal View History

2024-02-08 09:12:13 +03:00
from __future__ import annotations
import logging
2024-06-03 14:08:42 +05:00
from typing import TYPE_CHECKING
2024-02-08 09:12:13 +03:00
import platformdirs
2024-06-03 14:08:42 +05:00
if TYPE_CHECKING:
from pathlib import Path
2024-11-06 15:05:34 +03:00
_logger = logging.getLogger(__name__)
2024-02-08 09:12:13 +03:00
CACHE_PATH = platformdirs.user_cache_path("proxy_scraper_checker")
2024-02-08 09:42:30 +03:00
def add_permission(
path: Path, permission: int, /, *, missing_ok: bool = False
) -> None:
2024-02-08 09:12:13 +03:00
try:
2024-02-08 09:42:30 +03:00
current_permissions = path.stat().st_mode
new_permissions = current_permissions | permission
if current_permissions != new_permissions:
path.chmod(new_permissions)
2024-11-06 15:05:34 +03:00
_logger.info(
2024-02-08 09:42:30 +03:00
"Changed permissions of %s from %o to %o",
path,
current_permissions,
new_permissions,
)
2024-02-08 09:12:13 +03:00
except FileNotFoundError:
2024-02-08 09:42:30 +03:00
if not missing_ok:
raise
2024-02-08 09:12:13 +03:00
2024-02-08 12:01:37 +03:00
def create_or_fix_dir(path: Path, /, *, permission: int) -> None:
2024-02-08 09:12:13 +03:00
try:
path.mkdir(parents=True)
except FileExistsError:
if not path.is_dir():
msg = f"{path} is not a directory"
raise ValueError(msg) from None
2024-02-08 12:01:37 +03:00
add_permission(path, permission)