centralize getting the hash because duh

This commit is contained in:
brent s. 2021-01-21 11:59:18 -05:00
parent 0e8d460565
commit e8a75a42df
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
4 changed files with 35 additions and 37 deletions

View File

@ -90,17 +90,16 @@ class BaseUpdater(object):
return(None)
if not self.iso_url:
raise RuntimeError('iso_url attribute must be set first')
req = requests.get(self.iso_url, stream = True, headers = {'User-Agent': 'curl/7.74.0'})
if not req.ok:
raise RuntimeError('Received non-200/30x {0} for {1}'.format(req.status_code, self.iso_url))
req_chk = requests.head(self.iso_url, headers = {'User-Agent': 'curl/7.74.0'})
if not req_chk.ok:
raise RuntimeError('Received non-200/30x {0} for {1}'.format(req_chk.status_code, self.iso_url))
os.makedirs(os.path.dirname(self.dest_iso), exist_ok = True)
with req as uri:
with requests.get(self.iso_url, stream = True, headers = {'User-Agent': 'curl/7.74.0'}) as req:
req.raise_for_status()
with open(self.dest_iso, 'wb') as fh:
shutil.copyfileobj(uri.raw, fh)
hasher = hashlib.new(self.hash_type)
with open(self.dest_iso, 'rb') as fh:
hasher.update(fh.read())
realhash = hasher.hexdigest().lower()
for chunk in req.iter_content(chunk_size = 8192):
fh.write(chunk)
realhash = self.getISOHash()
if realhash != self.new_hash:
raise RuntimeError('Hash mismatch: {0} (LOCAL), {1} (REMOTE)'.format(realhash, self.new_hash))
self.updateVer()
@ -110,6 +109,17 @@ class BaseUpdater(object):
raise RuntimeError('BaseUpdater should be subclassed and its updateVer, getCurVer, and getNewVer methods '
'should be replaced.')

def getISOHash(self):
hasher = hashlib.new(self.hash_type)
# TODO: later on when python 3.8 is more prevalent, https://stackoverflow.com/a/1131238/733214
with open(self.dest_iso, 'rb') as fh:
while True:
chunk = fh.read(8192)
if not chunk:
break
hasher.update(chunk)
return(hasher.hexdigest().lower())

def getNewVer(self):
raise RuntimeError('BaseUpdater should be subclassed and its updateVer, getCurVer, and getNewVer methods '
'should be replaced.')

14
arch.py
View File

@ -13,7 +13,6 @@

import datetime
import json
import hashlib
import os
import re
##
@ -133,17 +132,14 @@ class Updater(_base.BaseUpdater):
# if ver_info.get('arch') != self.arch:
# self.do_update = True
# self.force_update = True
try:
hasher = hashlib.new(self.hash_type)
with open(self.dest_iso, 'rb') as fh:
hasher.update(fh.read())
if self.old_hash != hasher.hexdigest().lower():
self.do_update = True
self.force_update = True
except FileNotFoundError:
if not os.path.isfile(self.dest_iso):
self.do_update = True
self.force_update = True
return(None)
realhash = self.getISOHash()
if self.old_hash != realhash:
self.do_update = True
self.force_update = True
return(None)

def getNet(self):

14
grml.py
View File

@ -10,7 +10,6 @@

import datetime
import json
import hashlib
import os
import re
##
@ -94,17 +93,14 @@ class Updater(_base.BaseUpdater):
if ver_info.get('arch') != self.arch:
self.do_update = True
self.force_update = True
try:
hasher = hashlib.new(self.hash_type)
with open(self.dest_iso, 'rb') as fh:
hasher.update(fh.read())
if self.old_hash != hasher.hexdigest().lower():
self.do_update = True
self.force_update = True
except FileNotFoundError:
if not os.path.isfile(self.dest_iso):
self.do_update = True
self.force_update = True
return(None)
realhash = self.getISOHash()
if self.old_hash != realhash:
self.do_update = True
self.force_update = True
return(None)

def getNewVer(self):

View File

@ -10,7 +10,6 @@

import datetime
import json
import hashlib
import os
import re
##
@ -78,17 +77,14 @@ class Updater(_base.BaseUpdater):
if ver_info.get('arch') != self.arch:
self.do_update = True
self.force_update = True
try:
hasher = hashlib.new(self.hash_type)
with open(self.dest_iso, 'rb') as fh:
hasher.update(fh.read())
if self.old_hash != hasher.hexdigest().lower():
self.do_update = True
self.force_update = True
except FileNotFoundError:
if not os.path.isfile(self.dest_iso):
self.do_update = True
self.force_update = True
return (None)
realhash = self.getISOHash()
if self.old_hash != realhash:
self.do_update = True
self.force_update = True
return(None)
return (None)

def getNewVer(self):