Modified download hashing

This commit is contained in:
Phill Ridout 2015-02-02 19:25:34 +00:00
parent 4c36bba911
commit b8838f496a
1 changed files with 24 additions and 15 deletions

View File

@ -51,7 +51,7 @@ class ThemeScreenshotWorker(QtCore.QObject):
screenshot_downloaded = QtCore.pyqtSignal(str, str) screenshot_downloaded = QtCore.pyqtSignal(str, str)
finished = QtCore.pyqtSignal() finished = QtCore.pyqtSignal()
def __init__(self, themes_url, title, filename, screenshot): def __init__(self, themes_url, title, filename, sha256, screenshot):
""" """
Set up the worker object Set up the worker object
""" """
@ -59,6 +59,7 @@ class ThemeScreenshotWorker(QtCore.QObject):
self.themes_url = themes_url self.themes_url = themes_url
self.title = title self.title = title
self.filename = filename self.filename = filename
self.sha256 = sha256
self.screenshot = screenshot self.screenshot = screenshot
super(ThemeScreenshotWorker, self).__init__() super(ThemeScreenshotWorker, self).__init__()
@ -72,7 +73,7 @@ class ThemeScreenshotWorker(QtCore.QObject):
urllib.request.urlretrieve('%s%s' % (self.themes_url, self.screenshot), urllib.request.urlretrieve('%s%s' % (self.themes_url, self.screenshot),
os.path.join(gettempdir(), 'openlp', self.screenshot)) os.path.join(gettempdir(), 'openlp', self.screenshot))
# Signal that the screenshot has been downloaded # Signal that the screenshot has been downloaded
self.screenshot_downloaded.emit(self.title, self.filename) self.screenshot_downloaded.emit(self.title, self.filename, self.sha256)
except: except:
log.exception('Unable to download screenshot') log.exception('Unable to download screenshot')
finally: finally:
@ -189,9 +190,9 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
ans = msg.exec_() ans = msg.exec_()
web_config = False web_config = False
if web_config: if web_config:
files = web_config.read() files = open('/home/phill/Downloads/download.cfg').read()#web_config.read()
try: try:
self.config.read_string(files.decode()) self.config.read_string(files)#.decode())
self.web = self.config.get('general', 'base url') self.web = self.config.get('general', 'base url')
self.songs_url = self.web + self.config.get('songs', 'directory') + '/' self.songs_url = self.web + self.config.get('songs', 'directory') + '/'
self.bibles_url = self.web + self.config.get('bibles', 'directory') + '/' self.bibles_url = self.web + self.config.get('bibles', 'directory') + '/'
@ -239,8 +240,9 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
self.application.process_events() self.application.process_events()
title = self.config.get('bible_%s' % bible, 'title') title = self.config.get('bible_%s' % bible, 'title')
filename = self.config.get('bible_%s' % bible, 'filename') filename = self.config.get('bible_%s' % bible, 'filename')
sha256 = self.config.get('bible_%s' % song, 'sha256', fallback=None)
item = QtGui.QTreeWidgetItem(lang_item, [title]) item = QtGui.QTreeWidgetItem(lang_item, [title])
item.setData(0, QtCore.Qt.UserRole, filename) item.setData(0, QtCore.Qt.UserRole, (filename, sha256))
item.setCheckState(0, QtCore.Qt.Unchecked) item.setCheckState(0, QtCore.Qt.Unchecked)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable) item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
self.bibles_tree_widget.expandAll() self.bibles_tree_widget.expandAll()
@ -251,8 +253,9 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
self.application.process_events() self.application.process_events()
title = self.config.get('theme_%s' % theme, 'title') title = self.config.get('theme_%s' % theme, 'title')
filename = self.config.get('theme_%s' % theme, 'filename') filename = self.config.get('theme_%s' % theme, 'filename')
sha256 = self.config.get('theme_%s' % song, 'sha256', fallback=None)
screenshot = self.config.get('theme_%s' % theme, 'screenshot') screenshot = self.config.get('theme_%s' % theme, 'screenshot')
worker = ThemeScreenshotWorker(self.themes_url, title, filename, screenshot) worker = ThemeScreenshotWorker(self.themes_url, title, filename, sha256, screenshot)
self.theme_screenshot_workers.append(worker) self.theme_screenshot_workers.append(worker)
worker.screenshot_downloaded.connect(self.on_screenshot_downloaded) worker.screenshot_downloaded.connect(self.on_screenshot_downloaded)
thread = QtCore.QThread(self) thread = QtCore.QThread(self)
@ -352,7 +355,7 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
time.sleep(0.1) time.sleep(0.1)
self.application.set_normal_cursor() self.application.set_normal_cursor()
def on_screenshot_downloaded(self, title, filename): def on_screenshot_downloaded(self, title, filename, sha256):
""" """
Add an item to the list when a theme has been downloaded Add an item to the list when a theme has been downloaded
@ -360,7 +363,7 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
:param filename: The filename of the theme :param filename: The filename of the theme
""" """
item = QtGui.QListWidgetItem(title, self.themes_list_widget) item = QtGui.QListWidgetItem(title, self.themes_list_widget)
item.setData(QtCore.Qt.UserRole, filename) item.setData(QtCore.Qt.UserRole, (filename, sha256))
item.setCheckState(QtCore.Qt.Unchecked) item.setCheckState(QtCore.Qt.Unchecked)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable) item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
@ -389,16 +392,20 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
try: try:
url_file = urllib.request.urlopen(url, timeout=CONNECTION_TIMEOUT) url_file = urllib.request.urlopen(url, timeout=CONNECTION_TIMEOUT)
filename = open(f_path, "wb") filename = open(f_path, "wb")
if sha256:
hasher = hashlib.sha256()
# Download until finished or canceled. # Download until finished or canceled.
while not self.was_cancelled: while not self.was_cancelled:
data = url_file.read(block_size) data = url_file.read(block_size)
if not data: if not data:
break break
filename.write(data) filename.write(data)
if sha256:
hasher.update(data)
block_count += 1 block_count += 1
self._download_progress(block_count, block_size) self._download_progress(block_count, block_size)
filename.close() filename.close()
if sha256 and hashlib.sha256(open(f_path, 'rb').read()).hexdigest() != sha256: if sha256 and hasher.hexdigest() != sha256:
log.error('sha256 sums did not match for file: {}'.format(f_path)) log.error('sha256 sums did not match for file: {}'.format(f_path))
os.remove(f_path) os.remove(f_path)
return False return False
@ -493,7 +500,7 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
self.application.process_events() self.application.process_events()
item = iterator.value() item = iterator.value()
if item.parent() and item.checkState(0) == QtCore.Qt.Checked: if item.parent() and item.checkState(0) == QtCore.Qt.Checked:
filename = item.data(0, QtCore.Qt.UserRole) filename, _ = item.data(0, QtCore.Qt.UserRole)
size = self._get_file_size('%s%s' % (self.bibles_url, filename)) size = self._get_file_size('%s%s' % (self.bibles_url, filename))
self.max_progress += size self.max_progress += size
iterator += 1 iterator += 1
@ -502,7 +509,7 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
self.application.process_events() self.application.process_events()
item = self.themes_list_widget.item(i) item = self.themes_list_widget.item(i)
if item.checkState() == QtCore.Qt.Checked: if item.checkState() == QtCore.Qt.Checked:
filename = item.data(QtCore.Qt.UserRole) filename, _ = item.data(QtCore.Qt.UserRole)
size = self._get_file_size('%s%s' % (self.themes_url, filename)) size = self._get_file_size('%s%s' % (self.themes_url, filename))
self.max_progress += size self.max_progress += size
except urllib.error.URLError: except urllib.error.URLError:
@ -612,20 +619,22 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
while bibles_iterator.value(): while bibles_iterator.value():
item = bibles_iterator.value() item = bibles_iterator.value()
if item.parent() and item.checkState(0) == QtCore.Qt.Checked: if item.parent() and item.checkState(0) == QtCore.Qt.Checked:
bible = item.data(0, QtCore.Qt.UserRole) bible, sha256 = item.data(0, QtCore.Qt.UserRole)
self._increment_progress_bar(self.downloading % bible, 0) self._increment_progress_bar(self.downloading % bible, 0)
self.previous_size = 0 self.previous_size = 0
if not self.url_get_file('%s%s' % (self.bibles_url, bible), os.path.join(bibles_destination, bible)): if not self.url_get_file('%s%s' % (self.bibles_url, bible), os.path.join(bibles_destination, bible),
sha256):
return False return False
bibles_iterator += 1 bibles_iterator += 1
# Download themes # Download themes
for i in range(self.themes_list_widget.count()): for i in range(self.themes_list_widget.count()):
item = self.themes_list_widget.item(i) item = self.themes_list_widget.item(i)
if item.checkState() == QtCore.Qt.Checked: if item.checkState() == QtCore.Qt.Checked:
theme = item.data(QtCore.Qt.UserRole) theme, sha256 = item.data(QtCore.Qt.UserRole)
self._increment_progress_bar(self.downloading % theme, 0) self._increment_progress_bar(self.downloading % theme, 0)
self.previous_size = 0 self.previous_size = 0
if not self.url_get_file('%s%s' % (self.themes_url, theme), os.path.join(themes_destination, theme)): if not self.url_get_file('%s%s' % (self.themes_url, theme), os.path.join(themes_destination, theme),
sha256):
return False return False
return True return True