[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -1,29 +1,36 @@
"""Cache Management
"""
from __future__ import annotations
import hashlib
import json
import logging
import os
from typing import Any, Dict, List, Optional, Set
from pip._vendor.packaging.tags import Tag, interpreter_name, interpreter_version
from pip._vendor.packaging.utils import canonicalize_name
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Set
from pip._internal.exceptions import InvalidWheelFilename
from pip._internal.models.format_control import FormatControl
from pip._internal.models.link import Link
from pip._internal.models.wheel import Wheel
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
from pip._internal.utils.temp_dir import tempdir_kinds
from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.urls import path_to_url
from pip._vendor.packaging.tags import interpreter_name
from pip._vendor.packaging.tags import interpreter_version
from pip._vendor.packaging.tags import Tag
from pip._vendor.packaging.utils import canonicalize_name
logger = logging.getLogger(__name__)
def _hash_dict(d: Dict[str, str]) -> str:
def _hash_dict(d: dict[str, str]) -> str:
"""Return a stable sha224 of a dictionary."""
s = json.dumps(d, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
return hashlib.sha224(s.encode("ascii")).hexdigest()
s = json.dumps(d, sort_keys=True, separators=(',', ':'), ensure_ascii=True)
return hashlib.sha224(s.encode('ascii')).hexdigest()
class Cache:
@ -38,7 +45,7 @@ class Cache:
"""
def __init__(
self, cache_dir: str, format_control: FormatControl, allowed_formats: Set[str]
self, cache_dir: str, format_control: FormatControl, allowed_formats: set[str],
) -> None:
super().__init__()
assert not cache_dir or os.path.isabs(cache_dir)
@ -46,28 +53,28 @@ class Cache:
self.format_control = format_control
self.allowed_formats = allowed_formats
_valid_formats = {"source", "binary"}
_valid_formats = {'source', 'binary'}
assert self.allowed_formats.union(_valid_formats) == _valid_formats
def _get_cache_path_parts(self, link: Link) -> List[str]:
def _get_cache_path_parts(self, link: Link) -> list[str]:
"""Get parts of part that must be os.path.joined with cache_dir"""
# We want to generate an url to use as our cache key, we don't want to
# just re-use the URL because it might have other items in the fragment
# and we don't care about those.
key_parts = {"url": link.url_without_fragment}
key_parts = {'url': link.url_without_fragment}
if link.hash_name is not None and link.hash is not None:
key_parts[link.hash_name] = link.hash
if link.subdirectory_fragment:
key_parts["subdirectory"] = link.subdirectory_fragment
key_parts['subdirectory'] = link.subdirectory_fragment
# Include interpreter name, major and minor version in cache key
# to cope with ill-behaved sdists that build a different wheel
# depending on the python version their setup.py is being run on,
# and don't encode the difference in compatibility tags.
# https://github.com/pypa/pip/issues/7296
key_parts["interpreter_name"] = interpreter_name()
key_parts["interpreter_version"] = interpreter_version()
key_parts['interpreter_name'] = interpreter_name()
key_parts['interpreter_version'] = interpreter_version()
# Encode our key url with sha224, we'll use this because it has similar
# security properties to sha256, but with a shorter total output (and
@ -82,7 +89,7 @@ class Cache:
return parts
def _get_candidates(self, link: Link, canonical_package_name: str) -> List[Any]:
def _get_candidates(self, link: Link, canonical_package_name: str) -> list[Any]:
can_not_cache = not self.cache_dir or not canonical_package_name or not link
if can_not_cache:
return []
@ -105,8 +112,8 @@ class Cache:
def get(
self,
link: Link,
package_name: Optional[str],
supported_tags: List[Tag],
package_name: str | None,
supported_tags: list[Tag],
) -> Link:
"""Returns a link to a cached item if it exists, otherwise returns the
passed link.
@ -118,7 +125,7 @@ class SimpleWheelCache(Cache):
"""A cache of wheels for future installs."""
def __init__(self, cache_dir: str, format_control: FormatControl) -> None:
super().__init__(cache_dir, format_control, {"binary"})
super().__init__(cache_dir, format_control, {'binary'})
def get_path_for_link(self, link: Link) -> str:
"""Return a directory to store cached wheels for link
@ -138,13 +145,13 @@ class SimpleWheelCache(Cache):
parts = self._get_cache_path_parts(link)
assert self.cache_dir
# Store wheels within the root cache_dir
return os.path.join(self.cache_dir, "wheels", *parts)
return os.path.join(self.cache_dir, 'wheels', *parts)
def get(
self,
link: Link,
package_name: Optional[str],
supported_tags: List[Tag],
package_name: str | None,
supported_tags: list[Tag],
) -> Link:
candidates = []
@ -159,8 +166,8 @@ class SimpleWheelCache(Cache):
continue
if canonicalize_name(wheel.name) != canonical_package_name:
logger.debug(
"Ignoring cached wheel %s for %s as it "
"does not match the expected distribution name %s.",
'Ignoring cached wheel %s for %s as it '
'does not match the expected distribution name %s.',
wheel_name,
link,
package_name,
@ -174,7 +181,7 @@ class SimpleWheelCache(Cache):
wheel.support_index_min(supported_tags),
wheel_name,
wheel_dir,
)
),
)
if not candidates:
@ -214,7 +221,7 @@ class WheelCache(Cache):
"""
def __init__(self, cache_dir: str, format_control: FormatControl) -> None:
super().__init__(cache_dir, format_control, {"binary"})
super().__init__(cache_dir, format_control, {'binary'})
self._wheel_cache = SimpleWheelCache(cache_dir, format_control)
self._ephem_cache = EphemWheelCache(format_control)
@ -227,8 +234,8 @@ class WheelCache(Cache):
def get(
self,
link: Link,
package_name: Optional[str],
supported_tags: List[Tag],
package_name: str | None,
supported_tags: list[Tag],
) -> Link:
cache_entry = self.get_cache_entry(link, package_name, supported_tags)
if cache_entry is None:
@ -238,9 +245,9 @@ class WheelCache(Cache):
def get_cache_entry(
self,
link: Link,
package_name: Optional[str],
supported_tags: List[Tag],
) -> Optional[CacheEntry]:
package_name: str | None,
supported_tags: list[Tag],
) -> CacheEntry | None:
"""Returns a CacheEntry with a link to a cached item if it exists or
None. The cache entry indicates if the item was found in the persistent
or ephemeral cache.