aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ipm/api.py55
-rw-r--r--src/ipm/const.py1
-rw-r--r--src/ipm/utils/urlparser.py6
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