Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pulp-glue/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

9 changes: 4 additions & 5 deletions pulp-glue/src/pulp_glue/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions pulp-glue/src/pulp_glue/common/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions pulp-glue/src/pulp_glue/core/context.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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""):
Expand Down Expand Up @@ -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
Expand Down
Loading