summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-01-17 22:09:29 +0800
committer苏向夜 <fu050409@163.com>2024-01-17 22:09:29 +0800
commitc13003abc9019917d3b21cce4a035349095cdd12 (patch)
tree00a67255406a9f69247291538bc654ce0c4a734f
parent1cae21ddb53726f17249cfab4df5ff65fced2f2c (diff)
downloadipm-c13003abc9019917d3b21cce4a035349095cdd12.tar.gz
ipm-c13003abc9019917d3b21cce4a035349095cdd12.zip
:sparkles: feat(cli): improve command line output
-rw-r--r--src/ipm/__main__.py19
-rw-r--r--src/ipm/api.py41
-rw-r--r--src/ipm/models/ipk.py11
-rw-r--r--src/ipm/utils/freeze.py16
4 files changed, 62 insertions, 25 deletions
diff --git a/src/ipm/__main__.py b/src/ipm/__main__.py
index c40e86e..e760939 100644
--- a/src/ipm/__main__.py
+++ b/src/ipm/__main__.py
@@ -1,7 +1,11 @@
from .api import install, extract, build
+from .exceptions import IpmException
+from .logging import logger
+
import argparse
import sys
+
def main():
parser = argparse.ArgumentParser(
prog="ipm", description="Infini 包管理器", exit_on_error=False
@@ -31,11 +35,20 @@ def main():
args = parser.parse_args(sys.argv[1:] or ["-h"])
if args.command == "install":
- install(args.uri, args.index, echo=True)
+ try:
+ install(args.uri, args.index, echo=True)
+ except IpmException as error:
+ return logger.error(error)
elif args.command == "extract":
- extract(args.package, args.dist)
+ try:
+ extract(args.package, args.dist, echo=True)
+ except IpmException as error:
+ return logger.error(error)
elif args.command == "build":
- build(args.package)
+ try:
+ build(args.package, echo=True)
+ except IpmException as error:
+ return logger.error(error)
if __name__ == "__main__":
diff --git a/src/ipm/api.py b/src/ipm/api.py
index 30f7040..0a83d6e 100644
--- a/src/ipm/api.py
+++ b/src/ipm/api.py
@@ -2,15 +2,24 @@ from pathlib import Path
from .typing import StrPath
from .utils import freeze, urlparser, loader
from .models.ipk import InfiniPackage
-from .exceptions import FileTypeMismatch
+from .exceptions import FileTypeMismatch, TomlLoadFailed, FileNotFoundError
from .const import INDEX, HOME
from .logging import info, success
-import os
+
+def new(dist_path: StrPath, echo: bool = False) -> None:
+ ...
def build(source_path: StrPath, echo: bool = False) -> None:
- freeze.build_ipk(InfiniPackage(source_path))
+ info("检查构建环境...", echo)
+ try:
+ ipk = InfiniPackage(source_path)
+ info(f"包[{ipk.name}]构建环境载入完毕.", echo)
+ except TomlLoadFailed as error:
+ error(f"环境存在异常: {error}", echo)
+ return
+ freeze.build_ipk(ipk)
def extract(
@@ -28,13 +37,8 @@ def install(uri: str, index: str = "", echo: bool = False) -> None:
HOME.mkdir(parents=True, exist_ok=True)
index = index if index else INDEX
- if os.path.isabs(uri):
- info(f"检定给定的 URI 地址[{uri}]为本地路径.", echo)
- if uri.endswith(".ipk"):
- info("安装中...", echo)
- ipk = extract(Path(uri).resolve(), HOME, echo)
- else:
- raise FileTypeMismatch("文件类型与预期[.ipk]不匹配.")
+ if uri.isalpha():
+ ...
elif urlparser.is_valid_url(uri):
filename = uri.rstrip("/").split("/")[-1]
ipk = loader.load(
@@ -42,9 +46,20 @@ def install(uri: str, index: str = "", echo: bool = False) -> None:
uri.rstrip("/").rsplit("/")[0],
filename,
)
- elif uri.isalpha():
- ...
else:
- raise FileTypeMismatch("URI指向未知的位置.")
+ info(f"检定给定的 URI 地址[{uri}]为本地路径.", echo)
+ path = Path(uri).resolve()
+ if not path.exists():
+ raise FileNotFoundError("给定的 URI 路径不存在!")
+
+ if uri.endswith(".ipk"):
+ info("安装中...", echo)
+ ipk = extract(Path(uri).resolve(), HOME, echo)
+ else:
+ raise FileTypeMismatch("文件类型与预期[.ipk]不匹配.")
success(f"包[{ipk.name}]成功安装在[{ipk.source_path}].", echo)
+
+
+def uninstall(ipk: str | InfiniPackage):
+ ...
diff --git a/src/ipm/models/ipk.py b/src/ipm/models/ipk.py
index a50ebde..27a3d68 100644
--- a/src/ipm/models/ipk.py
+++ b/src/ipm/models/ipk.py
@@ -1,6 +1,6 @@
from pathlib import Path
from ..typing import List, Dict, Literal
-from ..exceptions import SyntaxError
+from ..exceptions import SyntaxError, TomlLoadFailed
import toml
@@ -39,7 +39,11 @@ class InfiniPackage:
self.source_path = Path(path).resolve()
toml_path = self.source_path / "infini.toml"
- data_load = toml.load(toml_path.open("r", encoding="utf-8"))
+ try:
+ data_load = toml.load(toml_path.open("r", encoding="utf-8"))
+ except Exception as error:
+ raise TomlLoadFailed(f"项目文件[infini.toml]导入失败: {error}") from error
+
if "infini" not in data_load.keys():
raise SyntaxError("配置文件中缺少[infini]项.")
@@ -58,9 +62,6 @@ class InfiniPackage:
def hash_name(self) -> str:
return f"{self.name}-{self.version}.ipk.hash"
- # @property
- # def home_p
-
class InfiniFrozenPackage:
source_path: Path
diff --git a/src/ipm/utils/freeze.py b/src/ipm/utils/freeze.py
index 74f4b40..ef3deb2 100644
--- a/src/ipm/utils/freeze.py
+++ b/src/ipm/utils/freeze.py
@@ -10,8 +10,9 @@ import tempfile
import shutil
-def build_ipk(ipk: InfiniPackage) -> InfiniFrozenPackage:
- build_dir = ipk.source_path / ".build"
+def build_ipk(ipk: InfiniPackage, echo: bool = False) -> InfiniFrozenPackage:
+ info("正在初始化开发环境...")
+ build_dir = ipk.source_path / "build"
src_path = ipk.source_path / "src"
dist_path = ipk.source_path / "dist"
ifp_path = dist_path / ipk.default_name
@@ -24,21 +25,28 @@ def build_ipk(ipk: InfiniPackage) -> InfiniFrozenPackage:
dist_path.mkdir(parents=True, exist_ok=True)
build_dir.mkdir(parents=True, exist_ok=True)
+ info("开发环境构建完成, 开始复制工程文件...")
shutil.copytree(src_path, build_dir / "src")
shutil.copy2(ipk.source_path / "infini.toml", build_dir / "infini.toml")
+ info("工程文件复制完毕, 开始打包[ipk]文件...")
_freeze.create_tar_gz(
str(build_dir),
str(ifp_path),
)
- (dist_path / ipk.hash_name).write_bytes(ifp_hash(ifp_path))
+ success(f"打包文件已存至[{ifp_path}].")
+ info("开始创建SHA256验证文件...")
+ hash_bytes = ifp_hash(ifp_path)
+ info(f"文件SHA256值为[{hash_bytes.hex()}].")
+ (dist_path / ipk.hash_name).write_bytes(hash_bytes)
+ success(f"包[{ipk.name}]构建成功.")
return InfiniFrozenPackage(source_path=ifp_path, **{"name": ipk.name})
def extract_ipk(
- source_path: StrPath, dist_path: str | Path, echo: bool = False
+ source_path: StrPath, dist_path: StrPath, echo: bool = False
) -> InfiniPackage:
ifp_path = Path(source_path).resolve()
dist_path = Path(dist_path).resolve()