Add checksum control od db download and switch to urllib3

This commit is contained in:
alessandro90
2018-11-03 20:38:05 +01:00
parent e28d3b6286
commit f224fcc0bc
3 changed files with 46 additions and 8 deletions

View File

@@ -2,13 +2,15 @@ from io import BytesIO
from os import mkdir
import os.path
from shutil import rmtree
import urllib
import urllib3
from zipfile import ZipFile
from PyQt5.QtCore import QThread, pyqtSignal
import utilities
class DownloadThread(QThread):
no_connection_error = pyqtSignal()
bad_db_download_error = pyqtSignal()
bad_file_error = pyqtSignal()
def __init__(self, db_location, path):
super().__init__()
@@ -22,12 +24,11 @@ class DownloadThread(QThread):
self.wait()
def run(self):
if os.path.exists(self.__path):
rmtree(self.__path)
try:
db = urllib.request.urlopen(self.__db_location)
db = urllib3.PoolManager().request('GET', self.__db_location)
# db = urllib.request.urlopen(self.__db_location)
# raise urllib.error.URLError('Test')
except urllib.error.URLError: # No internet connection.
except urllib3.exceptions.MaxRetryError: # No internet connection.
self.regular_execution = False
self.no_connection_error.emit()
return
@@ -36,8 +37,18 @@ class DownloadThread(QThread):
self.reason = db.reason
self.bad_db_download_error.emit()
return
if not utilities.checksum_ok(db.data, "folder"):
regular_execution = False
self.bad_db_download_error.emit()
return
if os.path.exists(self.__path):
rmtree(self.__path)
try:
with ZipFile(BytesIO(db.read())) as zipped:
# data_folder = db.read()
# data_folder = db.data
with ZipFile(BytesIO(db.data)) as zipped:
zipped.extractall()
except:
pass
self.regular_execution = False
self.bad_file_error.emit()
return

View File

@@ -18,14 +18,23 @@ class DownloadWindow(QWidget, Ui_Download_window):
self.everything_ok = True
self.no_internet_msg = QMessageBox(self)
self.no_internet_msg.setWindowTitle("No internet connection")
self.no_internet_msg.setText("Unable to establish an internet connection")
self.no_internet_msg.setText("Unable to establish an internet connection.")
# self.no_internet_msg.buttonClicked.connect(self.close)
self.no_internet_msg.finished.connect(self.close)
self.bad_db_download_msg = QMessageBox(self)
self.bad_db_download_msg.setWindowTitle("Something wrong")
self.bad_db_download_msg.setText("""Something went wrong with the downaload.
Check your internet connection and try again.""")
self.bad_db_download_msg.finished.connect(self.close)
self.bad_file_msg = QMessageBox(self)
self.bad_file_msg.setWindowTitle("Bad file detected")
self.bad_file_msg.setText("""The downloaded file seems to be corrupted.
The old database has not been deleted and
the downloaded file has been discarded.""")
self.bad_file_msg.finished.connect(self.close)
self.download_thread = DownloadThread(db_location, data_folder)
self.download_thread.finished.connect(self.wait_close)
self.download_thread.no_connection_error.connect(self.show_no_connection_warning)

18
utilities.py Normal file
View File

@@ -0,0 +1,18 @@
import hashlib
from pandas import read_csv
def checksum_ok(data, what):
code = hashlib.sha256()
code.update(data)
ref_loc = 'https://aresvalley.com/Storage/Artemis/Database/data.zip.log'
if what == "folder":
n = 0
elif what == "db":
n = 1
else:
raise ValueError("Wrong entry name.")
try:
reference = read_csv(ref_loc, delimiter = '*').iat[-1, n]
except HTTPError:
return False
return code.hexdigest() == reference