'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)
|
||||
|
||||
Reference in New Issue
Block a user