's'
This commit is contained in:
@@ -1,24 +1,49 @@
|
||||
|
||||
|
||||
import os
|
||||
import zipfile
|
||||
import hashlib
|
||||
|
||||
from tools.helper import bcolors, download_file, print_color
|
||||
from tools.helper import bcolors, download_file, file_md5, print_color
|
||||
|
||||
class General:
|
||||
def _local_md5(self):
|
||||
if not os.path.isfile(self.dl_file_name):
|
||||
return ""
|
||||
return file_md5(self.dl_file_name)
|
||||
|
||||
def _has_valid_local_file(self):
|
||||
return os.path.isfile(self.dl_file_name) and self._local_md5() == self.act_md5
|
||||
|
||||
def download(self):
|
||||
loc_md5 = ""
|
||||
if os.path.isfile(self.dl_file_name):
|
||||
with open(self.dl_file_name,"rb") as f:
|
||||
bytes = f.read()
|
||||
loc_md5 = hashlib.md5(bytes).hexdigest()
|
||||
while not os.path.isfile(self.dl_file_name) or loc_md5 != self.act_md5:
|
||||
if os.path.isfile(self.dl_file_name):
|
||||
os.remove(self.dl_file_name)
|
||||
print_color("md5 mismatches, redownloading now ....",bcolors.YELLOW)
|
||||
loc_md5 = download_file(self.dl_link, self.dl_file_name)
|
||||
|
||||
temp_path = self.dl_file_name + ".tmp"
|
||||
local_valid = self._has_valid_local_file()
|
||||
|
||||
try:
|
||||
if os.path.isfile(temp_path):
|
||||
os.remove(temp_path)
|
||||
print_color("Trying online download first ...", bcolors.GREEN)
|
||||
remote_md5 = download_file(self.dl_link, temp_path)
|
||||
if remote_md5 != self.act_md5:
|
||||
raise ValueError(
|
||||
"Downloaded file md5 mismatch: expected {}, got {}".format(
|
||||
self.act_md5,
|
||||
remote_md5,
|
||||
)
|
||||
)
|
||||
os.replace(temp_path, self.dl_file_name)
|
||||
except Exception as exc:
|
||||
if os.path.isfile(temp_path):
|
||||
os.remove(temp_path)
|
||||
if local_valid:
|
||||
print_color(
|
||||
"Online download failed, falling back to local {} ({})".format(
|
||||
self.dl_file_name,
|
||||
exc,
|
||||
),
|
||||
bcolors.YELLOW,
|
||||
)
|
||||
return
|
||||
raise
|
||||
|
||||
def extract(self):
|
||||
print_color("Extracting archive...", bcolors.GREEN)
|
||||
print(self.dl_file_name)
|
||||
|
||||
@@ -137,13 +137,41 @@ on property:init.svc.zygote=stopped
|
||||
with open(self.release_meta_file, "w", encoding="utf-8") as meta_file:
|
||||
meta_file.write(self.release_version)
|
||||
|
||||
def _validate_archive(self):
|
||||
if not zipfile.is_zipfile(self.dl_file_name):
|
||||
def _validate_archive(self, path=None):
|
||||
archive_path = path or self.dl_file_name
|
||||
if not zipfile.is_zipfile(archive_path):
|
||||
raise ValueError(
|
||||
"Downloaded Magisk/Kitsune archive is not a valid APK/ZIP. "
|
||||
"Set REDROID_MAGISK_APK to a local APK or REDROID_MAGISK_URL to a working direct APK URL."
|
||||
)
|
||||
|
||||
def _has_valid_local_archive(self):
|
||||
if not os.path.isfile(self.dl_file_name):
|
||||
return False
|
||||
try:
|
||||
self._validate_archive()
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
def _download_remote_archive(self):
|
||||
temp_path = self.dl_file_name + ".tmp"
|
||||
try:
|
||||
if os.path.isfile(temp_path):
|
||||
os.remove(temp_path)
|
||||
print_color(
|
||||
"Trying online download for Kitsune Mask {} ...".format(self.release_version),
|
||||
bcolors.GREEN,
|
||||
)
|
||||
download_file(self.dl_link, temp_path)
|
||||
self._validate_archive(temp_path)
|
||||
os.replace(temp_path, self.dl_file_name)
|
||||
self._write_version()
|
||||
except Exception:
|
||||
if os.path.isfile(temp_path):
|
||||
os.remove(temp_path)
|
||||
raise
|
||||
|
||||
def resolve_release(self):
|
||||
print_color("Resolving Kitsune Mask stable release ...", bcolors.GREEN)
|
||||
response = requests.get(self.stable_json, timeout=30)
|
||||
@@ -174,41 +202,42 @@ on property:init.svc.zygote=stopped
|
||||
self._validate_archive()
|
||||
return
|
||||
|
||||
if not self.custom_url and os.path.isfile(self.dl_file_name):
|
||||
self.release_version = self._cached_version() or "bundled"
|
||||
print_color(
|
||||
"Using bundled Kitsune Mask {} ...".format(self.release_version),
|
||||
bcolors.GREEN,
|
||||
)
|
||||
self._validate_archive()
|
||||
return
|
||||
local_valid = self._has_valid_local_archive()
|
||||
local_version = self._cached_version() or "bundled"
|
||||
|
||||
if self.custom_url:
|
||||
self.dl_link = self.custom_url
|
||||
self.release_version = "url:" + os.path.basename(self.custom_url)
|
||||
else:
|
||||
self.resolve_release()
|
||||
try:
|
||||
if self.custom_url:
|
||||
self.dl_link = self.custom_url
|
||||
self.release_version = "url:" + os.path.basename(self.custom_url)
|
||||
else:
|
||||
self.resolve_release()
|
||||
except Exception as exc:
|
||||
if local_valid:
|
||||
self.release_version = local_version
|
||||
print_color(
|
||||
"Resolving Kitsune Mask online failed, falling back to local {} ({})".format(
|
||||
self.release_version,
|
||||
exc,
|
||||
),
|
||||
bcolors.YELLOW,
|
||||
)
|
||||
return
|
||||
raise
|
||||
|
||||
if (
|
||||
os.path.isfile(self.dl_file_name)
|
||||
and self._cached_version() == self.release_version
|
||||
):
|
||||
print_color(
|
||||
"Using cached Kitsune Mask {} ...".format(self.release_version),
|
||||
bcolors.GREEN,
|
||||
)
|
||||
self._validate_archive()
|
||||
return
|
||||
|
||||
print_color(
|
||||
"Downloading Kitsune Mask {} now .....".format(self.release_version),
|
||||
bcolors.GREEN,
|
||||
)
|
||||
if os.path.isfile(self.dl_file_name):
|
||||
os.remove(self.dl_file_name)
|
||||
download_file(self.dl_link, self.dl_file_name)
|
||||
self._validate_archive()
|
||||
self._write_version()
|
||||
try:
|
||||
self._download_remote_archive()
|
||||
except Exception as exc:
|
||||
if local_valid:
|
||||
self.release_version = local_version
|
||||
print_color(
|
||||
"Downloading Kitsune Mask online failed, falling back to local {} ({})".format(
|
||||
self.release_version,
|
||||
exc,
|
||||
),
|
||||
bcolors.YELLOW,
|
||||
)
|
||||
return
|
||||
raise
|
||||
|
||||
def extract(self):
|
||||
print_color("Extracting archive...", bcolors.GREEN)
|
||||
|
||||
Reference in New Issue
Block a user