Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 调整获取 logo 失败时的日志级别 #1440

Merged
merged 8 commits into from
Jul 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
from paasng.platform.declarative.handlers import get_desc_handler
from paasng.platform.smart_app.services.path import PathProtocol
from paasng.platform.sourcectl.models import SPStat
from paasng.platform.sourcectl.package.client import BinaryTarClient, InvalidPackageFileFormatError, ZipClient
from paasng.platform.sourcectl.package.client import (
BinaryTarClient,
FileDoesNotExistError,
InvalidPackageFileFormatError,
ZipClient,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -184,6 +189,8 @@ def _load_logo(archive: Union[ZipClient, BinaryTarClient], relative_path: str) -
# Q: 为什么需要进行 base64 编码?
# A: 因为 meta_info 会被归档存储进数据库, bytes 类型无法序列化成 json
logo_b64data = "base64," + base64.b64encode(archive.read_file(relative_path + "logo.png")).decode()
except FileDoesNotExistError:
logger.info("The logo.png does not exist, using default logo.")
except (RuntimeError, KeyError):
logger.exception("Can't read logo.png.")
return logo_b64data
Expand Down
24 changes: 21 additions & 3 deletions apiserver/paasng/paasng/platform/sourcectl/package/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class InvalidPackageFileFormatError(Exception):
"""The package file is not in a valid format, it might be corrupt."""


class FileDoesNotExistError(KeyError, RuntimeError):
SheepSheepChen marked this conversation as resolved.
Show resolved Hide resolved
"""The file does not exist.

This exception is used to maintain compatibility with existing code that handles missing files.
TODO: Consider unifying exception handling for read_file.
"""


class BasePackageClient(metaclass=abc.ABCMeta):
@abc.abstractmethod
def read_file(self, file_path: str) -> bytes:
Expand Down Expand Up @@ -99,7 +107,7 @@ def read_file(self, file_path: str) -> bytes:
key = os.path.join(self.relative_path, key)
file = self.tar.extractfile(key)
if not file:
raise KeyError(f"filename: {file_path} Don't exists.")
raise FileDoesNotExistError(f"filename: {file_path} Don't exists.")
return file.read()

def export(self, local_path: str):
Expand Down Expand Up @@ -143,7 +151,10 @@ def read_file(self, filename) -> bytes:
if p.returncode != 0:
if self._is_invalid_file_format_error(stderr):
raise InvalidPackageFileFormatError()
raise RuntimeError(f"Failed to extractfile from the tarball, error: {stderr!r}")
if self._is_not_found_error(stderr):
raise FileDoesNotExistError(f"Failed to extractfile from the tarball, error: {stderr!r}")
else:
raise RuntimeError(f"Failed to extractfile from the tarball, error: {stderr!r}")
return (temp_dir / filename).read_bytes()

def export(self, local_path: str):
Expand Down Expand Up @@ -185,6 +196,13 @@ def _is_invalid_file_format_error(message: str) -> bool:
return True
return False

@staticmethod
def _is_not_found_error(message: str) -> bool:
"""Check if the stderr message indicates a file not found."""
if re.search(r"tar:.*: Not found in archive", message):
return True
return False


class ZipClient(BasePackageClient):
"""基于本地 zip 包初始化的 client"""
Expand Down Expand Up @@ -220,7 +238,7 @@ def read_file(self, file_path: str) -> bytes:
try:
info = self.zip_.getinfo(key)
except KeyError as e:
raise KeyError(f"filename: {file_path} Don't exists.") from e
raise FileDoesNotExistError(f"filename: {file_path} Don't exists.") from e

return self.zip_.read(info)

Expand Down