aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--src/ipm/utils/loader.py42
1 files changed, 11 insertions, 31 deletions
diff --git a/src/ipm/utils/loader.py b/src/ipm/utils/loader.py
index 3a0d707..dc018dc 100644
--- a/src/ipm/utils/loader.py
+++ b/src/ipm/utils/loader.py
@@ -1,7 +1,6 @@
from pathlib import Path
from ipm.utils.freeze import extract_ipk
from ipm.const import STORAGE
-from ipm.logging import info, success
from ipm.models.ipk import InfiniFrozenPackage
import requests
@@ -9,50 +8,29 @@ import tempfile
import shutil
-def load_from_remote(
- name: str, baseurl: str = "", filename: str = "", echo: bool = False
-) -> InfiniFrozenPackage:
- ipk_uri = baseurl.rstrip("/") + "/" + filename
- hash_uri = baseurl.rstrip("/") + "/" + filename + ".hash"
- info(f"开始下载[.ipk]文件[{ipk_uri}]...", echo)
- ipk_bytes = requests.get(ipk_uri).content
- info(f"开始下载哈希验证文件[{hash_uri}]...", echo)
- hash_bytes = requests.get(hash_uri).content
+def load_from_remote(name: str, url, hash: str) -> InfiniFrozenPackage:
+ ipk_bytes = requests.get(url).content
temp_dir = tempfile.TemporaryDirectory()
temp_path = Path(temp_dir.name).resolve()
- info(f"创建临时目录[{temp_dir}], 开始释放文件...", echo)
ipk_path = temp_path / f"{name}.ipk"
ipk_file = ipk_path.open("w+b")
ipk_file.write(ipk_bytes)
ipk_file.close()
- hash_path = temp_path / f"{name}.ipk.hash"
- hash_file = hash_path.open("w+b")
- hash_file.write(hash_bytes)
- hash_file.close()
-
- info("解压中...", echo)
- temp_ipk = extract_ipk(ipk_path, temp_path)
+ temp_ipk = extract_ipk(ipk_path, temp_path, hash=hash)
if not temp_ipk:
raise RuntimeError("解压时出现异常.")
- success(f"包[{temp_ipk.name}]解压完成.")
- move_to = STORAGE / temp_ipk.name
- move_to.mkdir(parents=True, exist_ok=True)
- info("转存缓存文件中...", echo)
- shutil.copy2(ipk_path, move_to / temp_ipk.default_name)
- shutil.copy2(hash_path, move_to / (temp_ipk.default_name + ".hash"))
- info(f"缓存文件转存至[{move_to}]完毕.", echo)
+ STORAGE.mkdir(parents=True, exist_ok=True)
+ move_to = STORAGE.joinpath(temp_ipk.default_name + ".ipk")
- ifp = InfiniFrozenPackage(
- move_to / temp_ipk.default_name, name=temp_ipk.name, version=temp_ipk.version
- )
+ shutil.copy2(ipk_path, move_to)
+
+ ifp = InfiniFrozenPackage(move_to, name=temp_ipk.name, version=temp_ipk.version)
- info("任务完成, 开始清理下载临时文件...")
temp_dir.cleanup()
- info("下载临时文件清理完毕.")
return ifp
@@ -70,7 +48,9 @@ def load_from_local(source_path: Path) -> InfiniFrozenPackage:
shutil.copy2(source_path.parent / (source_path.name + ".hash"), move_to)
ifp = InfiniFrozenPackage(
- move_to.joinpath(temp_ipk.default_name + ".ipk"), name=temp_ipk.name, version=temp_ipk.version
+ move_to.joinpath(temp_ipk.default_name + ".ipk"),
+ name=temp_ipk.name,
+ version=temp_ipk.version,
)
temp_dir.cleanup()