From 328834eb03172ba50ade21802bc5d3fecf0ba900 Mon Sep 17 00:00:00 2001 From: monosans Date: Thu, 1 Feb 2024 14:52:29 +0300 Subject: [PATCH] Improve permissions check --- proxy_scraper_checker/settings.py | 7 ++++--- proxy_scraper_checker/utils.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/proxy_scraper_checker/settings.py b/proxy_scraper_checker/settings.py index caa98ae..fe03b7f 100644 --- a/proxy_scraper_checker/settings.py +++ b/proxy_scraper_checker/settings.py @@ -5,6 +5,7 @@ import enum import json import logging import math +import os import sys from pathlib import Path from typing import ( @@ -30,7 +31,7 @@ from .http import get_response_text from .null_context import NullContext from .parsers import parse_ipv4 from .typing_compat import Any, Literal, Self -from .utils import IS_DOCKER, check_writable +from .utils import IS_DOCKER, check_access if TYPE_CHECKING: from .proxy import Proxy @@ -272,8 +273,8 @@ class Settings: ) _, _, (check_website_type, real_ip) = await asyncio.gather( - check_writable(output_path), - check_writable(cache.DIR), + check_access(output_path, mode=os.W_OK | os.X_OK), + check_access(cache.DIR, mode=os.R_OK | os.W_OK | os.X_OK), _get_check_website_type_and_real_ip( check_website=cfg["check_website"], session=session ), diff --git a/proxy_scraper_checker/utils.py b/proxy_scraper_checker/utils.py index 48906fc..2d7d454 100644 --- a/proxy_scraper_checker/utils.py +++ b/proxy_scraper_checker/utils.py @@ -20,7 +20,7 @@ def bytes_decode(value: bytes, /) -> str: return str(charset_normalizer.from_bytes(value)[0]) -async def check_writable(path: Union[Path, str], /) -> None: - if not await aiofiles.os.access(path, os.W_OK): - msg = f"{path} is not writable" +async def check_access(path: Union[Path, str], /, *, mode: int) -> None: + if not await aiofiles.os.access(path, mode): + msg = f"{path} is not accessible" raise ValueError(msg)