[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,5 +1,6 @@
"""Utilities related archives.
"""
from __future__ import annotations
import logging
import os
@ -7,16 +8,16 @@ import shutil
import stat
import tarfile
import zipfile
from typing import Iterable, List, Optional
from typing import Iterable
from typing import List
from typing import Optional
from zipfile import ZipInfo
from pip._internal.exceptions import InstallationError
from pip._internal.utils.filetypes import (
BZ2_EXTENSIONS,
TAR_EXTENSIONS,
XZ_EXTENSIONS,
ZIP_EXTENSIONS,
)
from pip._internal.utils.filetypes import BZ2_EXTENSIONS
from pip._internal.utils.filetypes import TAR_EXTENSIONS
from pip._internal.utils.filetypes import XZ_EXTENSIONS
from pip._internal.utils.filetypes import ZIP_EXTENSIONS
from pip._internal.utils.misc import ensure_dir
logger = logging.getLogger(__name__)
@ -29,7 +30,7 @@ try:
SUPPORTED_EXTENSIONS += BZ2_EXTENSIONS
except ImportError:
logger.debug("bz2 module is not available")
logger.debug('bz2 module is not available')
try:
# Only for Python 3.3+
@ -37,7 +38,7 @@ try:
SUPPORTED_EXTENSIONS += XZ_EXTENSIONS
except ImportError:
logger.debug("lzma module is not available")
logger.debug('lzma module is not available')
def current_umask() -> int:
@ -47,16 +48,16 @@ def current_umask() -> int:
return mask
def split_leading_dir(path: str) -> List[str]:
path = path.lstrip("/").lstrip("\\")
if "/" in path and (
("\\" in path and path.find("/") < path.find("\\")) or "\\" not in path
def split_leading_dir(path: str) -> list[str]:
path = path.lstrip('/').lstrip('\\')
if '/' in path and (
('\\' in path and path.find('/') < path.find('\\')) or '\\' not in path
):
return path.split("/", 1)
elif "\\" in path:
return path.split("\\", 1)
return path.split('/', 1)
elif '\\' in path:
return path.split('\\', 1)
else:
return [path, ""]
return [path, '']
def has_leading_dir(paths: Iterable[str]) -> bool:
@ -110,7 +111,7 @@ def unzip_file(filename: str, location: str, flatten: bool = True) -> None:
no-ops per the python docs.
"""
ensure_dir(location)
zipfp = open(filename, "rb")
zipfp = open(filename, 'rb')
try:
zip = zipfile.ZipFile(zipfp, allowZip64=True)
leading = has_leading_dir(zip.namelist()) and flatten
@ -123,11 +124,11 @@ def unzip_file(filename: str, location: str, flatten: bool = True) -> None:
dir = os.path.dirname(fn)
if not is_within_directory(location, fn):
message = (
"The zip file ({}) has a file ({}) trying to install "
"outside target directory ({})"
'The zip file ({}) has a file ({}) trying to install '
'outside target directory ({})'
)
raise InstallationError(message.format(filename, fn, location))
if fn.endswith("/") or fn.endswith("\\"):
if fn.endswith('/') or fn.endswith('\\'):
# A directory
ensure_dir(fn)
else:
@ -136,7 +137,7 @@ def unzip_file(filename: str, location: str, flatten: bool = True) -> None:
# chunk of memory for the file's content
fp = zip.open(name)
try:
with open(fn, "wb") as destfp:
with open(fn, 'wb') as destfp:
shutil.copyfileobj(fp, destfp)
finally:
fp.close()
@ -156,21 +157,21 @@ def untar_file(filename: str, location: str) -> None:
no-ops per the python docs.
"""
ensure_dir(location)
if filename.lower().endswith(".gz") or filename.lower().endswith(".tgz"):
mode = "r:gz"
if filename.lower().endswith('.gz') or filename.lower().endswith('.tgz'):
mode = 'r:gz'
elif filename.lower().endswith(BZ2_EXTENSIONS):
mode = "r:bz2"
mode = 'r:bz2'
elif filename.lower().endswith(XZ_EXTENSIONS):
mode = "r:xz"
elif filename.lower().endswith(".tar"):
mode = "r"
mode = 'r:xz'
elif filename.lower().endswith('.tar'):
mode = 'r'
else:
logger.warning(
"Cannot determine compression type for file %s",
'Cannot determine compression type for file %s',
filename,
)
mode = "r:*"
tar = tarfile.open(filename, mode, encoding="utf-8")
mode = 'r:*'
tar = tarfile.open(filename, mode, encoding='utf-8')
try:
leading = has_leading_dir([member.name for member in tar.getmembers()])
for member in tar.getmembers():
@ -180,8 +181,8 @@ def untar_file(filename: str, location: str) -> None:
path = os.path.join(location, fn)
if not is_within_directory(location, path):
message = (
"The tar file ({}) has a file ({}) trying to install "
"outside target directory ({})"
'The tar file ({}) has a file ({}) trying to install '
'outside target directory ({})'
)
raise InstallationError(message.format(filename, path, location))
if member.isdir():
@ -194,7 +195,7 @@ def untar_file(filename: str, location: str) -> None:
# Some corrupt tar files seem to produce this
# (specifically bad symlinks)
logger.warning(
"In the tar file %s the member %s is invalid: %s",
'In the tar file %s the member %s is invalid: %s',
filename,
member.name,
exc,
@ -207,7 +208,7 @@ def untar_file(filename: str, location: str) -> None:
# Some corrupt tar files seem to produce this
# (specifically bad symlinks)
logger.warning(
"In the tar file %s the member %s is invalid: %s",
'In the tar file %s the member %s is invalid: %s',
filename,
member.name,
exc,
@ -215,7 +216,7 @@ def untar_file(filename: str, location: str) -> None:
continue
ensure_dir(os.path.dirname(path))
assert fp is not None
with open(path, "wb") as destfp:
with open(path, 'wb') as destfp:
shutil.copyfileobj(fp, destfp)
fp.close()
# Update the timestamp (useful for cython compiled files)
@ -230,29 +231,29 @@ def untar_file(filename: str, location: str) -> None:
def unpack_file(
filename: str,
location: str,
content_type: Optional[str] = None,
content_type: str | None = None,
) -> None:
filename = os.path.realpath(filename)
if (
content_type == "application/zip"
or filename.lower().endswith(ZIP_EXTENSIONS)
or zipfile.is_zipfile(filename)
content_type == 'application/zip' or
filename.lower().endswith(ZIP_EXTENSIONS) or
zipfile.is_zipfile(filename)
):
unzip_file(filename, location, flatten=not filename.endswith(".whl"))
unzip_file(filename, location, flatten=not filename.endswith('.whl'))
elif (
content_type == "application/x-gzip"
or tarfile.is_tarfile(filename)
or filename.lower().endswith(TAR_EXTENSIONS + BZ2_EXTENSIONS + XZ_EXTENSIONS)
content_type == 'application/x-gzip' or
tarfile.is_tarfile(filename) or
filename.lower().endswith(TAR_EXTENSIONS + BZ2_EXTENSIONS + XZ_EXTENSIONS)
):
untar_file(filename, location)
else:
# FIXME: handle?
# FIXME: magic signatures?
logger.critical(
"Cannot unpack file %s (downloaded from %s, content-type: %s); "
"cannot detect archive format",
'Cannot unpack file %s (downloaded from %s, content-type: %s); '
'cannot detect archive format',
filename,
location,
content_type,
)
raise InstallationError(f"Cannot determine archive format of {location}")
raise InstallationError(f'Cannot determine archive format of {location}')