diff --git a/cookiecutter/ci/{{ cookiecutter.__project_name }}/pulp-glue{{ cookiecutter.__app_label_suffix }}/pyproject.toml.update b/cookiecutter/ci/{{ cookiecutter.__project_name }}/pulp-glue{{ cookiecutter.__app_label_suffix }}/pyproject.toml.update index 137b11066..87109948f 100644 --- a/cookiecutter/ci/{{ cookiecutter.__project_name }}/pulp-glue{{ cookiecutter.__app_label_suffix }}/pyproject.toml.update +++ b/cookiecutter/ci/{{ cookiecutter.__project_name }}/pulp-glue{{ cookiecutter.__app_label_suffix }}/pyproject.toml.update @@ -18,7 +18,7 @@ line-length = 100 [tool.ruff.lint] # This section is managed by the cookiecutter templates. -extend-select = ["I", "INT"] +extend-select = ["I", "INT", "PTH"] [tool.mypy] # This section is managed by the cookiecutter templates. diff --git a/cookiecutter/ci/{{ cookiecutter.__project_name }}/pyproject.toml.update b/cookiecutter/ci/{{ cookiecutter.__project_name }}/pyproject.toml.update index 926b773d9..b1bb2d812 100644 --- a/cookiecutter/ci/{{ cookiecutter.__project_name }}/pyproject.toml.update +++ b/cookiecutter/ci/{{ cookiecutter.__project_name }}/pyproject.toml.update @@ -22,7 +22,7 @@ lint = [ ] test = [ "pygments>=2.19.2", - "pytest>=7.0.0,<9.1", + "pytest>=7.0.0,<9.2", "pytest-xdist>=3.8.0,<3.9", "python-gnupg>=0.5.0,<0.6", "secretstorage>=3.5.0", diff --git a/pulp-glue/pyproject.toml b/pulp-glue/pyproject.toml index 10c2493d2..135c31800 100644 --- a/pulp-glue/pyproject.toml +++ b/pulp-glue/pyproject.toml @@ -75,5 +75,5 @@ line-length = 100 [tool.ruff.lint] # This section is managed by the cookiecutter templates. -extend-select = ["I", "INT"] +extend-select = ["I", "INT", "PTH"] diff --git a/pulp-glue/src/pulp_glue/common/context.py b/pulp-glue/src/pulp_glue/common/context.py index be6f74e9c..fff3ea885 100644 --- a/pulp-glue/src/pulp_glue/common/context.py +++ b/pulp-glue/src/pulp_glue/common/context.py @@ -386,8 +386,7 @@ def from_config_files( configs: dict[str, dict[str, t.Any]] = {} for location in config_locations: try: - with open(location, "rb") as fp: - new_configs = tomllib.load(fp) + new_configs = tomllib.loads(Path(location).read_text()) except (FileNotFoundError, PermissionError): pass else: @@ -1750,7 +1749,7 @@ def _prepare_upload( chunk_size: int | None, ) -> None: _chunk_size: int | None = chunk_size or self.pulp_ctx.chunk_size - size = os.path.getsize(file.name) + size = Path(file.name).stat().st_size if not self.pulp_ctx.fake_mode: # Skip the uploading part in fake_mode if _chunk_size is None or _chunk_size > size: body["file"] = file @@ -1778,7 +1777,7 @@ def create( chunk_size: int | None = body.pop("chunk_size", None) if file: if isinstance(file, str | Path): - file = open(file, "rb") + file = Path(file).open("rb") cleanup.enter_context(file) self._prepare_upload(body, file, chunk_size) if self.repository_ctx is not None: @@ -1802,7 +1801,7 @@ def upload( This function is deprecated. The create call can handle the upload logic transparently. Parameters: - file: A file like object that supports `os.path.getsize`. + file: A file like object. chunk_size: Size of the chunks to upload independently. `None` to disable chunking. repository: Repository context to add the newly created content to. kwargs: Extra args specific to the content type, passed to the create call. diff --git a/pulp-glue/src/pulp_glue/common/openapi.py b/pulp-glue/src/pulp_glue/common/openapi.py index 5f5681826..9b0e0b6b4 100644 --- a/pulp-glue/src/pulp_glue/common/openapi.py +++ b/pulp-glue/src/pulp_glue/common/openapi.py @@ -10,6 +10,7 @@ from datetime import datetime, timedelta from functools import cached_property from io import BufferedReader +from pathlib import Path from urllib.parse import urlencode, urljoin import requests @@ -220,30 +221,29 @@ def ssl_context(self) -> ssl.SSLContext | bool: def load_api(self, refresh_cache: bool = False) -> None: # TODO: Find a way to invalidate caches on upstream change - xdg_cache_home: str = os.environ.get("XDG_CACHE_HOME") or "~/.cache" - apidoc_cache: str = os.path.join( - os.path.expanduser(xdg_cache_home), - "squeezer", - (self._base_url + "_" + self._doc_path) + xdg_cache_home = Path(os.environ.get("XDG_CACHE_HOME") or "~/.cache").expanduser() + apidoc_cache = ( + xdg_cache_home + / "squeezer" + / (self._base_url + "_" + self._doc_path) .replace(":", "_") .replace("/", "_") - .replace("?", "_"), + .replace("?", "_") ) + try: if refresh_cache: # Fake that we did not find the cache. raise OSError() - with open(apidoc_cache, "rb") as f: - data: bytes = f.read() + data = apidoc_cache.read_bytes() self._parse_api(data) except Exception: # Try again with a freshly downloaded version data = self._download_api() self._parse_api(data) # Write to cache as it seems to be valid - os.makedirs(os.path.dirname(apidoc_cache), exist_ok=True) - with open(apidoc_cache, "bw") as f: - f.write(data) + apidoc_cache.parent.mkdir(parents=True, exist_ok=True) + apidoc_cache.write_bytes(data) def _parse_api(self, data: bytes) -> None: raw_spec = self._patch_api_hook(json.loads(data)) diff --git a/pulp-glue/src/pulp_glue/core/context.py b/pulp-glue/src/pulp_glue/core/context.py index 4dc9f57f3..cd70a6187 100644 --- a/pulp-glue/src/pulp_glue/core/context.py +++ b/pulp-glue/src/pulp_glue/core/context.py @@ -1,8 +1,8 @@ import datetime import hashlib -import os import sys import typing as t +from pathlib import Path from pulp_glue.common.context import ( EntityDefinition, @@ -53,7 +53,7 @@ def upload( chunk_size: int | None = None, sha256: str | None = None, ) -> t.Any: - size = os.path.getsize(file.name) + size = Path(file.name).stat().st_size sha256_hasher = hashlib.sha256() for chunk in iter(lambda: file.read(10_000_000), b""): @@ -549,7 +549,7 @@ def commit(self, sha256: str) -> t.Any: def upload_file(self, file: t.IO[bytes], chunk_size: int = 1000000) -> t.Any: """Upload a file and return the uncommitted upload_href.""" start = 0 - size = os.path.getsize(file.name) + size = Path(file.name).stat().st_size upload_href = self.create(body={"size": size})["pulp_href"] try: self.pulp_href = upload_href