diff options
| author | 2024-01-16 21:30:19 +0800 | |
|---|---|---|
| committer | 2024-01-16 21:30:19 +0800 | |
| commit | 4aeac0408f5ea839ad6b3c149ea462eacde5c7b2 (patch) | |
| tree | 1001f08ed8f998805e35fb1548071e4c4cb443d9 | |
| parent | 476697c15fc60eb9bc492437dfcf9bbd83d3426f (diff) | |
| download | ipm-4aeac0408f5ea839ad6b3c149ea462eacde5c7b2.tar.gz ipm-4aeac0408f5ea839ad6b3c149ea462eacde5c7b2.zip | |
:sparkles: feat(api): add index check
| -rw-r--r-- | src/ipm/api.py | 55 | ||||
| -rw-r--r-- | src/ipm/const.py | 1 | ||||
| -rw-r--r-- | src/ipm/utils/urlparser.py | 6 |
3 files changed, 36 insertions, 26 deletions
diff --git a/src/ipm/api.py b/src/ipm/api.py index af91dd5..323ce5b 100644 --- a/src/ipm/api.py +++ b/src/ipm/api.py @@ -1,9 +1,9 @@ from pathlib import Path -from urllib.parse import urlparse from .typing import StrPath -from .utils import freeze +from .utils import freeze, urlparser from .models.ipk import InfiniPackage from .exceptions import FileTypeMismatch +from .const import INDEX import os import requests @@ -21,31 +21,34 @@ def extract(source_path: StrPath, dist_path: StrPath | None = None): freeze.extract_ipk(source_path, dist_path) -def install(uri: str | None = "", index: str | None = ""): +def install(uri: str, index: str | None = ""): home = Path.home() / ".ipm" / "src" home.mkdir(parents=True, exist_ok=True) + index = index if index else INDEX - if uri: - if os.path.isabs(uri): - if uri.endswith(".ipk"): - extract(Path(uri).resolve(), home) - else: - raise FileTypeMismatch("文件类型与预期[.ipk]不匹配.") - elif urlparse(uri).scheme and urlparse(uri).netloc: - ipk_bytes = requests.get(uri).content - hash_bytes = requests.get(uri.rstrip("/") + ".hash").content - - temp_dir = tempfile.TemporaryDirectory() - temp_path = Path(temp_dir.name).resolve() - - ipk_file = (temp_path / "temp.ipk").open("w+b") - ipk_file.write(ipk_bytes) - ipk_file.close() - - hash_file = (temp_path / "temp.ipk.hash").open("w+b") - hash_file.write(hash_bytes) - hash_file.close() - - extract(ipk_file, home) + if os.path.isabs(uri): + if uri.endswith(".ipk"): + extract(Path(uri).resolve(), home) else: - raise FileTypeMismatch("URI指向未知的位置.") + raise FileTypeMismatch("文件类型与预期[.ipk]不匹配.") + elif urlparser.is_valid_url(uri): + ipk_bytes = requests.get(uri).content + hash_bytes = requests.get(uri.rstrip("/") + ".hash").content + + temp_dir = tempfile.TemporaryDirectory() + temp_path = Path(temp_dir.name).resolve() + + ipk_file = (temp_path / "temp.ipk").open("w+b") + ipk_file.write(ipk_bytes) + ipk_file.close() + + hash_file = (temp_path / "temp.ipk.hash").open("w+b") + hash_file.write(hash_bytes) + hash_file.close() + + extract(ipk_file, home) + temp_dir.cleanup() + elif uri.isalpha(): + ... + else: + raise FileTypeMismatch("URI指向未知的位置.") diff --git a/src/ipm/const.py b/src/ipm/const.py index 51c64dd..c9dac26 100644 --- a/src/ipm/const.py +++ b/src/ipm/const.py @@ -1 +1,2 @@ DEBUG = True +INDEX = "https://ipm.hydroroll.team/index/" diff --git a/src/ipm/utils/urlparser.py b/src/ipm/utils/urlparser.py new file mode 100644 index 0000000..89bc064 --- /dev/null +++ b/src/ipm/utils/urlparser.py @@ -0,0 +1,6 @@ +from urllib.parse import urlparse + + +def is_valid_url(uri: str): + parsed_uri = urlparse(uri) + return parsed_uri.scheme and parsed_uri.netloc |
