diff options
author | Walter Purcaro <vuolter@gmail.com> | 2013-12-13 13:43:58 +0100 |
---|---|---|
committer | Stefano <l.stickell@yahoo.it> | 2013-12-16 17:06:10 +0100 |
commit | 1c476ab1884695a8d2632a4c2fdd67d8493053b3 (patch) | |
tree | cf7d84971c87dc3d09edb1c046ad49ae77694cfb | |
parent | Keep2share: fixed #405 (diff) | |
download | pyload-1c476ab1884695a8d2632a4c2fdd67d8493053b3.tar.xz |
Checksum: merged #388
(cherry picked from commit 01a7c4ec37996e87067876eab40b0b8403f0100f)
-rw-r--r-- | pyload/plugins/addons/Checksum.py | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/pyload/plugins/addons/Checksum.py b/pyload/plugins/addons/Checksum.py index 081e8ac3b..e1c827167 100644 --- a/pyload/plugins/addons/Checksum.py +++ b/pyload/plugins/addons/Checksum.py @@ -16,6 +16,7 @@ @author: zoidberg """ + from __future__ import with_statement import hashlib import zlib @@ -30,10 +31,9 @@ from module.plugins.Hook import Hook def computeChecksum(local_file, algorithm): if algorithm in getattr(hashlib, "algorithms", ("md5", "sha1", "sha224", "sha256", "sha384", "sha512")): h = getattr(hashlib, algorithm)() - chunk_size = 128 * h.block_size with open(local_file, 'rb') as f: - for chunk in iter(lambda: f.read(chunk_size), ''): + for chunk in iter(lambda: f.read(128 * h.block_size), b''): h.update(chunk) return h.hexdigest() @@ -43,7 +43,7 @@ def computeChecksum(local_file, algorithm): last = 0 with open(local_file, 'rb') as f: - for chunk in iter(lambda: f.read(8192), ''): + for chunk in iter(lambda: f.read(8192), b''): last = hf(chunk, last) return "%x" % last @@ -54,13 +54,15 @@ def computeChecksum(local_file, algorithm): class Checksum(Hook): __name__ = "Checksum" - __version__ = "0.10" - __description__ = "Verify downloaded file size and checksum (enable in general preferences)" + __version__ = "0.11" + __description__ = "Verify downloaded file size and checksum" __config__ = [("activated", "bool", "Activated", True), - ("action", "fail;retry;nothing", "What to do if check fails?", "retry"), - ("max_tries", "int", "Number of retries", 2)] - __author_name__ = ("zoidberg") - __author_mail__ = ("zoidberg@mujmail.cz") + ("check_action", "fail;retry;nothing", "What to do if check fails?", "retry"), + ("max_tries", "int", "Number of retries", 2), + ("retry_action", "fail;nothing", "What to do if all retries fail?", "fail"), + ("wait_time", "int", "Time to wait before each retry (seconds)", 1)] + __author_name__ = ("zoidberg", "Walter Purcaro") + __author_mail__ = ("zoidberg@mujmail.cz", "vuolter@gmail.com") methods = {'sfv': 'crc32', 'crc': 'crc32', 'hash': 'md5'} regexps = {'sfv': r'^(?P<name>[^;].+)\s+(?P<hash>[0-9A-Fa-f]{8})$', @@ -68,21 +70,22 @@ class Checksum(Hook): 'crc': r'filename=(?P<name>.+)\nsize=(?P<size>\d+)\ncrc32=(?P<hash>[0-9A-Fa-f]{8})$', 'default': r'^(?P<hash>[0-9A-Fa-f]+)\s+\*?(?P<name>.+)$'} - def setup(self): + def coreReady(self): if not self.config['general']['checksum']: self.logInfo("Checksum validation is disabled in general configuration") + def setup(self): self.algorithms = sorted( getattr(hashlib, "algorithms", ("md5", "sha1", "sha224", "sha256", "sha384", "sha512")), reverse=True) self.algorithms.extend(["crc32", "adler32"]) self.formats = self.algorithms + ['sfv', 'crc', 'hash'] def downloadFinished(self, pyfile): - """ + """ Compute checksum for the downloaded file and compare it with the hash provided by the hoster. pyfile.plugin.check_data should be a dictionary which can contain: a) if known, the exact filesize in bytes (e.g. "size": 123456789) - b) hexadecimal hash string with algorithm name as key (e.g. "md5": "d76505d0869f9f928a17d42d66326307") + b) hexadecimal hash string with algorithm name as key (e.g. "md5": "d76505d0869f9f928a17d42d66326307") """ if hasattr(pyfile.plugin, "check_data") and (isinstance(pyfile.plugin.check_data, dict)): data = pyfile.plugin.check_data.copy() @@ -122,15 +125,12 @@ class Checksum(Hook): checksum = computeChecksum(local_file, key.replace("-", "").lower()) if checksum: if checksum == data[key].lower(): - self.logInfo('File integrity of "%s" verified by %s checksum (%s).' % (pyfile.name, - key.upper(), - checksum)) - return + self.logInfo('File integrity of "%s" verified by %s checksum (%s).' % + (pyfile.name, key.upper(), checksum)) + break else: - self.logWarning("%s checksum for file %s does not match (%s != %s)" % (key.upper(), - pyfile.name, - checksum, - data[key])) + self.logWarning("%s checksum for file %s does not match (%s != %s)" % + (key.upper(), pyfile.name, checksum, data[key])) self.checkFailed(pyfile, local_file, "Checksums do not match") else: self.logWarning("Unsupported hashing algorithm: %s" % key.upper()) @@ -138,13 +138,19 @@ class Checksum(Hook): self.logWarning("Unable to validate checksum for file %s" % pyfile.name) def checkFailed(self, pyfile, local_file, msg): - action = self.getConfig("action") - if action == "fail": - pyfile.plugin.fail(reason=msg) - elif action == "retry": - if local_file: - remove(local_file) - pyfile.plugin.retry(reason=msg, max_tries=self.getConfig("max_tries")) + check_action = self.getConfig("check_action") + if check_action == "retry": + max_tries = self.getConfig("max_tries") + retry_action = self.getConfig("retry_action") + if pyfile.plugin.retries < max_tries: + if local_file: + remove(local_file) + pyfile.plugin.retry(reason=msg, max_tries=max_tries, wait_time=self.getConfig("wait_time")) + elif retry_action == "nothing": + return + elif check_action == "nothing": + return + pyfile.plugin.fail(reason=msg) def packageFinished(self, pypack): download_folder = save_join(self.config['general']['download_folder'], pypack.folder, "") @@ -172,11 +178,8 @@ class Checksum(Hook): algorithm = self.methods.get(file_type, file_type) checksum = computeChecksum(local_file, algorithm) if checksum == data["hash"]: - self.logInfo('File integrity of "%s" verified by %s checksum (%s).' % (data["name"], - algorithm, - checksum)) + self.logInfo('File integrity of "%s" verified by %s checksum (%s).' % + (data["name"], algorithm, checksum)) else: - self.logWarning("%s checksum for file %s does not match (%s != %s)" % (algorithm, - data["name"], - checksum, - data["hash"])) + self.logWarning("%s checksum for file %s does not match (%s != %s)" % + (algorithm, data["name"], checksum, data["hash"])) |