From e8a75a42df688e575d6b7ea5d983dc0b790c1c32 Mon Sep 17 00:00:00 2001 From: brent s Date: Thu, 21 Jan 2021 11:59:18 -0500 Subject: [PATCH] centralize getting the hash because duh --- _base.py | 28 +++++++++++++++++++--------- arch.py | 14 +++++--------- grml.py | 14 +++++--------- sysresccd.py | 16 ++++++---------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/_base.py b/_base.py index ccf4aa3..13f28e0 100644 --- a/_base.py +++ b/_base.py @@ -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.') diff --git a/arch.py b/arch.py index be844a9..7219c5b 100755 --- a/arch.py +++ b/arch.py @@ -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): diff --git a/grml.py b/grml.py index 5abf4ef..a932088 100755 --- a/grml.py +++ b/grml.py @@ -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): diff --git a/sysresccd.py b/sysresccd.py index e815d01..8002430 100755 --- a/sysresccd.py +++ b/sysresccd.py @@ -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):