summaryrefslogtreecommitdiffstats
path: root/module/plugins/hooks/Checksum.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/plugins/hooks/Checksum.py')
-rw-r--r--module/plugins/hooks/Checksum.py57
1 files changed, 34 insertions, 23 deletions
diff --git a/module/plugins/hooks/Checksum.py b/module/plugins/hooks/Checksum.py
index 0fe2ae88c..eeda2d849 100644
--- a/module/plugins/hooks/Checksum.py
+++ b/module/plugins/hooks/Checksum.py
@@ -18,7 +18,7 @@ def computeChecksum(local_file, algorithm):
h = getattr(hashlib, algorithm)()
with open(local_file, 'rb') as f:
- for chunk in iter(lambda: f.read(128 * h.block_size), b''):
+ for chunk in iter(lambda: f.read(128 * h.block_size), ''):
h.update(chunk)
return h.hexdigest()
@@ -28,7 +28,7 @@ def computeChecksum(local_file, algorithm):
last = 0
with open(local_file, 'rb') as f:
- for chunk in iter(lambda: f.read(8192), b''):
+ for chunk in iter(lambda: f.read(8192), ''):
last = hf(chunk, last)
return "%x" % last
@@ -38,19 +38,22 @@ def computeChecksum(local_file, algorithm):
class Checksum(Hook):
- __name__ = "Checksum"
- __type__ = "hook"
- __version__ = "0.12"
+ __name__ = "Checksum"
+ __type__ = "hook"
+ __version__ = "0.14"
- __config__ = [("activated", "bool", "Activated", False),
+ __config__ = [("check_checksum", "bool", "Check checksum? (If False only size will be verified)", True),
("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)]
__description__ = """Verify downloaded file size and checksum"""
- __author_name__ = ("zoidberg", "Walter Purcaro")
- __author_mail__ = ("zoidberg@mujmail.cz", "vuolter@gmail.com")
+ __license__ = "GPLv3"
+ __authors__ = [("zoidberg", "zoidberg@mujmail.cz"),
+ ("Walter Purcaro", "vuolter@gmail.com"),
+ ("stickell", "l.stickell@yahoo.it")]
+
methods = {'sfv': 'crc32', 'crc': 'crc32', 'hash': 'md5'}
regexps = {'sfv': r'^(?P<name>[^;].+)\s+(?P<hash>[0-9A-Fa-f]{8})$',
@@ -60,8 +63,9 @@ class Checksum(Hook):
def coreReady(self):
- if not self.config['general']['checksum']:
- self.logInfo("Checksum validation is disabled in general configuration")
+ if not self.getConfig("check_checksum"):
+ self.logInfo(_("Checksum validation is disabled in plugin configuration"))
+
def setup(self):
self.algorithms = sorted(
@@ -69,6 +73,7 @@ class Checksum(Hook):
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.
@@ -76,10 +81,15 @@ class Checksum(Hook):
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")
"""
- if hasattr(pyfile.plugin, "check_data") and (isinstance(pyfile.plugin.check_data, dict)):
+ if hasattr(pyfile.plugin, "check_data") and isinstance(pyfile.plugin.check_data, dict):
data = pyfile.plugin.check_data.copy()
- elif hasattr(pyfile.plugin, "api_data") and (isinstance(pyfile.plugin.api_data, dict)):
+
+ elif hasattr(pyfile.plugin, "api_data") and isinstance(pyfile.plugin.api_data, dict):
data = pyfile.plugin.api_data.copy()
+
+ # elif hasattr(pyfile.plugin, "info") and isinstance(pyfile.plugin.info, dict):
+ # data = pyfile.plugin.info.copy()
+
else:
return
@@ -100,12 +110,12 @@ class Checksum(Hook):
api_size = int(data['size'])
file_size = getsize(local_file)
if api_size != file_size:
- self.logWarning("File %s has incorrect size: %d B (%d expected)" % (pyfile.name, file_size, api_size))
+ self.logWarning(_("File %s has incorrect size: %d B (%d expected)") % (pyfile.name, file_size, api_size))
self.checkFailed(pyfile, local_file, "Incorrect file size")
del data['size']
# validate checksum
- if data and self.config['general']['checksum']:
+ if data and self.getConfig("check_checksum"):
if "checksum" in data:
data['md5'] = data['checksum']
@@ -114,17 +124,18 @@ 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).' %
+ 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)" %
+ 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())
+ self.logWarning(_("Unsupported hashing algorithm"), key.upper())
else:
- self.logWarning("Unable to validate checksum for file %s" % pyfile.name)
+ self.logWarning(_("Unable to validate checksum for file: ") + pyfile.name)
+
def checkFailed(self, pyfile, local_file, msg):
check_action = self.getConfig("check_action")
@@ -134,26 +145,26 @@ class Checksum(Hook):
if pyfile.plugin.retries < max_tries:
if local_file:
remove(local_file)
- pyfile.plugin.retry(max_tries=max_tries, wait_time=self.getConfig("wait_time"), reason=msg)
+ pyfile.plugin.retry(max_tries, self.getConfig("wait_time"), msg)
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, "")
for link in pypack.getChildren().itervalues():
file_type = splitext(link['name'])[1][1:].lower()
- #self.logDebug(link, file_type)
if file_type not in self.formats:
continue
hash_file = fs_encode(save_join(download_folder, link['name']))
if not isfile(hash_file):
- self.logWarning("File not found: %s" % link['name'])
+ self.logWarning(_("File not found"), link['name'])
continue
with open(hash_file) as f:
@@ -167,8 +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).' %
+ 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)" %
+ self.logWarning(_("%s checksum for file %s does not match (%s != %s)") %
(algorithm, data['name'], checksum, data['hash']))