diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-10-07 12:25:17 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-10-07 12:25:17 +0200 |
commit | 7b8bccc5f221a7f4af5e41a21b44ed6e6002601c (patch) | |
tree | 8d94f373011b93ebf45d4d077df2d2fc815f79d8 | |
parent | pycurl cleanup (diff) | |
download | pyload-7b8bccc5f221a7f4af5e41a21b44ed6e6002601c.tar.xz |
unrar ram check
-rw-r--r-- | module/plugins/hooks/UnRar.py | 24 | ||||
-rw-r--r-- | module/pyunrar.py | 26 |
2 files changed, 44 insertions, 6 deletions
diff --git a/module/plugins/hooks/UnRar.py b/module/plugins/hooks/UnRar.py index 00dbe36d3..48e69ead9 100644 --- a/module/plugins/hooks/UnRar.py +++ b/module/plugins/hooks/UnRar.py @@ -21,7 +21,7 @@ from __future__ import with_statement import sys from module.plugins.Hook import Hook -from module.pyunrar import Unrar, WrongPasswordError, CommandError, UnknownError +from module.pyunrar import Unrar, WrongPasswordError, CommandError, UnknownError, LowRamError from traceback import print_exc from os.path import exists, join @@ -36,7 +36,8 @@ class UnRar(Hook): ("fullpath", "bool", "extract full path", True), ("overwrite", "bool", "overwrite files", True), ("passwordfile", "str", "unrar passoword file", "unrar_passwords.txt"), - ("deletearchive", "bool", "delete archives when done", False) ] + ("deletearchive", "bool", "delete archives when done", False), + ("ramwarning", "bool", "warn about low ram", True)] __threaded__ = ["packageFinished"] __author_name__ = ("mkaay") __author_mail__ = ("mkaay@mkaay.de") @@ -54,6 +55,19 @@ class UnRar(Hook): with open(self.getConfig("passwordfile"), "w") as f: f.writelines(self.comments) self.re_splitfile = re.compile("(.*)\.part(\d+)\.rar$") + + self.ram = 0 #ram in kb for unix osses + try: + f = open("/proc/meminfo") + line = True + while line: + line = f.readline() + if line.startswith("MemTotal:"): + self.ram = int(re.search(r"([0-9]+)", line).group(1)) + except: + self.ram = 0 + + self.ram /= 1024 def addPassword(self, pws): if not type(pws) == list: pws = [pws] @@ -92,6 +106,7 @@ class UnRar(Hook): if pack.password and pack.password.strip() and pack.password.strip() != "None": self.addPassword(pack.password.splitlines()) files = [] + for fid, data in pack.getChildren().iteritems(): m = self.re_splitfile.search(data["name"]) if m and int(m.group(2)) == 1: @@ -112,7 +127,7 @@ class UnRar(Hook): else: folder = download_folder - u = Unrar(join(folder, fname), tmpdir=join(folder, "tmp")) + u = Unrar(join(folder, fname), tmpdir=join(folder, "tmp"), ramSize=(self.ram if self.getConfig("ramwarning") else 0)) try: success = u.crackPassword(passwords=self.passwords, statusFunction=s, overwrite=True, destination=folder, fullPath=self.getConfig("fullpath")) except WrongPasswordError: @@ -133,6 +148,9 @@ class UnRar(Hook): print_exc() self.core.log.info(_("Unrar of %s failed") % fname) continue + except LowRamError: + self.log.warning(_("Your ram amount of %s MB seems not sufficient to unrar this file. You can deactivate this warning and risk instability") % self.ram) + continue except UnknownError: if self.core.debug: print_exc() diff --git a/module/pyunrar.py b/module/pyunrar.py index a2810b66e..11b19330b 100644 --- a/module/pyunrar.py +++ b/module/pyunrar.py @@ -51,6 +51,9 @@ class NoFilesError(Exception): class WrongPasswordError(Exception): pass +class LowRamError(Exception): + pass + class CommandError(Exception): def __init__(self, ret=None, stdout=None, stderr=None): self.ret = ret @@ -73,7 +76,7 @@ class CommandError(Exception): return EXITMAP[self.ret] class Unrar(): - def __init__(self, archive, tmpdir=None): + def __init__(self, archive, tmpdir=None, ramSize=0): """ archive should be be first or only part """ @@ -96,7 +99,10 @@ class Unrar(): self.tmpdir = mkdtemp() else: self.tmpdir = tmpdir +"_" + basename(archive).replace(".rar", "").replace(".","") - + + self.ram = ramSize + + def listContent(self, password=None): """ returns a list with all infos to the files in the archive @@ -173,12 +179,16 @@ class Unrar(): """ files = self.listContent(password=password) smallest = (-1, -1) + biggest = (-1, -1) for i, f in enumerate(files): if f["size"] < smallest[1] or smallest[1] == -1: smallest = (i, f["size"]) - if smallest[0] == -1: + if f["size"] > biggest[1] or biggest[1] == -1: + biggest = (i, f["size"]) + if smallest[0] == -1 or biggest[0] == -1: raise UnknownError() self.smallestFiles = files[smallest[0]] + self.biggestFiles = files[biggest[0]] return files[smallest[0]] def needPassword(self): @@ -267,6 +277,16 @@ class Unrar(): sf.append(self.getSmallestFile(password)["name"]) except WrongPasswordError: continue + + if self.ram: + size = self.biggestFiles["size"] / 1024 ** 2 + if self.ram < 127 and size > 50: + raise LowRamError + elif self.ram < 256 and size > 500: + raise LowRamError + elif self.ram < 512 and size > 2000: + raise LowRamError + tdir = self.tmpdir if not exists(tdir): makedirs(tdir) |