60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
|
|
import os
|
|
import zipfile
|
|
|
|
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):
|
|
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)
|
|
print(self.extract_to)
|
|
with zipfile.ZipFile(self.dl_file_name) as z:
|
|
z.extractall(self.extract_to)
|
|
def copy(self):
|
|
pass
|
|
def install(self):
|
|
# pass
|
|
self.download()
|
|
self.extract()
|
|
self.copy()
|