From 7b3e8250471103572fd1e4e6c8e6b2bed99baaba Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Thu, 28 Mar 2024 22:25:08 +0800 Subject: feat(hash): sync changes with hash util --- src/ipm/api.py | 2 +- src/ipm/models/ipk.py | 16 +++++++--------- src/ipm/utils/freeze.py | 2 +- src/ipm/utils/hash.py | 2 +- 4 files changed, 10 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/ipm/api.py b/src/ipm/api.py index 1214f6c..dca7975 100644 --- a/src/ipm/api.py +++ b/src/ipm/api.py @@ -246,7 +246,7 @@ def extract( dist_path = ( Path(dist_path).resolve() if dist_path else Path(source_path).resolve().parent ) - return freeze.extract_ipk(source_path, dist_path, echo) + return freeze.extract_ipk(source_path, dist_path, None, echo) def yggdrasil_add( diff --git a/src/ipm/models/ipk.py b/src/ipm/models/ipk.py index 325c7a9..25b1bbc 100644 --- a/src/ipm/models/ipk.py +++ b/src/ipm/models/ipk.py @@ -3,6 +3,7 @@ from typing import Any, Optional, Union from tomlkit.toml_document import TOMLDocument from ipm.const import INDEX +from ipm.utils.hash import ifp_hash from ipm.models.index import Yggdrasil from ipm.models.lock import PackageLock from ipm.typing import List, Dict, Literal, StrPath @@ -11,6 +12,7 @@ from ipm.exceptions import ProjectError, TomlLoadFailed import tomlkit import abc + global_lock = PackageLock() @@ -247,19 +249,15 @@ class InfiniProject(InfiniPackage): class InfiniFrozenPackage(InfiniPackage): def __init__(self, source_path: Union[str, Path], name: str, version: str) -> None: self._source_path = Path(source_path).resolve() - - self.hash = ( - (self._source_path.parent.joinpath(self._source_path.name + ".hash")) - .read_bytes() - .hex() - ) - self._name = name self._version = version + def __hash__(self) -> str: + return ifp_hash(self._source_path) + @property - def hash_name(self) -> str: - return f"{self._source_path.name}.hash" + def hash(self) -> str: + return self.hash @property def name(self) -> str: diff --git a/src/ipm/utils/freeze.py b/src/ipm/utils/freeze.py index 2d30a0f..063d74e 100644 --- a/src/ipm/utils/freeze.py +++ b/src/ipm/utils/freeze.py @@ -68,7 +68,7 @@ def extract_ipk( dist_path = Path(dist_path).resolve() update("文件校验...", echo) - if not ifp_verify(ifp_path, hash): + if hash and not ifp_verify(ifp_path, hash): raise VerifyFailed("文件完整性验证失败!") success("文件校验成功.", echo) diff --git a/src/ipm/utils/hash.py b/src/ipm/utils/hash.py index c5cc990..6cf4eaf 100644 --- a/src/ipm/utils/hash.py +++ b/src/ipm/utils/hash.py @@ -11,6 +11,6 @@ def ifp_hash(lfp_path: StrPath, block_size=65536) -> str: return sha256.digest().hex() -def ifp_verify(lfp_path, expected_hash) -> bool: +def ifp_verify(lfp_path: StrPath, expected_hash: str) -> bool: actual_hash = ifp_hash(lfp_path) return actual_hash == expected_hash -- cgit v1.2.3-70-g09d2