diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-07-19 19:37:09 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-07-19 19:37:09 +0200 |
commit | 7c00440dfb7fd9772a5674f8e04f66aea2e73a10 (patch) | |
tree | e1dbb1eb2ad2d453f8b1ce1932ced58e1e8183fc /module/plugins | |
parent | cleanup (diff) | |
download | pyload-7c00440dfb7fd9772a5674f8e04f66aea2e73a10.tar.xz |
fix in unrar plugin
Diffstat (limited to 'module/plugins')
-rw-r--r-- | module/plugins/hooks/UnRar.py | 131 |
1 files changed, 72 insertions, 59 deletions
diff --git a/module/plugins/hooks/UnRar.py b/module/plugins/hooks/UnRar.py index 6dfe2c195..c636664ee 100644 --- a/module/plugins/hooks/UnRar.py +++ b/module/plugins/hooks/UnRar.py @@ -24,27 +24,31 @@ from module.plugins.Hook import Hook from module.lib.pyunrar import Unrar, WrongPasswordError, CommandError, UnknownError, LowRamError from traceback import print_exc +import os from os.path import exists, join, isabs, isdir from os import remove, makedirs, rmdir, listdir, chown, chmod -from pwd import getpwnam + +if os.name != "nt": + from pwd import getpwnam + import re class UnRar(Hook): __name__ = "UnRar" __version__ = "0.11" __description__ = """unrar""" - __config__ = [ ("activated", "bool", "Activated", False), - ("fullpath", "bool", "extract full path", True), - ("overwrite", "bool", "overwrite files", True), - ("passwordfile", "str", "unrar password file", "unrar_passwords.txt"), - ("deletearchive", "bool", "delete archives when done", False), - ("ramwarning", "bool", "warn about low ram", True), - ("renice", "int", "Cpu Priority", 10), - ("unrar_destination", "str", "Unpack files to", "")] + __config__ = [("activated", "bool", "Activated", False), + ("fullpath", "bool", "extract full path", True), + ("overwrite", "bool", "overwrite files", True), + ("passwordfile", "str", "unrar password file", "unrar_passwords.txt"), + ("deletearchive", "bool", "delete archives when done", False), + ("ramwarning", "bool", "warn about low ram", True), + ("renice", "int", "Cpu Priority", 10), + ("unrar_destination", "str", "Unpack files to", "")] __threaded__ = ["packageFinished"] __author_name__ = ("mkaay") __author_mail__ = ("mkaay@mkaay.de") - + def setup(self): self.comments = ["# one password each line"] self.passwords = [] @@ -71,31 +75,31 @@ class UnRar(Hook): self.ram = 0 self.ram /= 1024 - - def setOwner(self,d,uid,gid,mode): + + def setOwner(self, d, uid, gid, mode): if not exists(d): - self.core.log.debug(_("Directory %s does not exist!") % d) - return - fileList=listdir(d) - for fileEntry in fileList: - fullEntryName=join(d,fileEntry) - if isdir(fullEntryName): - self.setOwner(fullEntryName,uid,gid,mode) - try: - chown(fullEntryName,uid,gid) - chmod(fullEntryName,mode) - except: - self.core.log.debug(_("Chown/Chmod for %s failed") % fullEntryName) - self.core.log.debug(_("Exception: %s") % sys.exc_info()[0]) - continue + self.core.log.debug(_("Directory %s does not exist!") % d) + return + + for fileEntry in listdir(d): + fullEntryName = join(d, fileEntry) + if isdir(fullEntryName): + self.setOwner(fullEntryName, uid, gid, mode) + try: + chown(fullEntryName, uid, gid) + chmod(fullEntryName, mode) + except: + self.core.log.debug(_("Chown/Chmod for %s failed") % fullEntryName) + self.core.log.debug(_("Exception: %s") % sys.exc_info()[0]) + continue try: - chown(d,uid,gid) - chmod(d,mode) + chown(d, uid, gid) + chmod(d, mode) except: - self.core.log.debug(_("Chown/Chmod for %s failed") % d) - self.core.log.debug(_("Exception: %s") % sys.exc_info()[0]) - return - + self.core.log.debug(_("Chown/Chmod for %s failed") % d) + self.core.log.debug(_("Exception: %s") % sys.exc_info()[0]) + return + def addPassword(self, pws): if not type(pws) == list: pws = [pws] pws.reverse() @@ -105,14 +109,14 @@ class UnRar(Hook): self.passwords.insert(0, pw) with open(self.getConfig("passwordfile"), "w") as f: - f.writelines([c+"\n" for c in self.comments]) - f.writelines([p+"\n" for p in self.passwords]) - + f.writelines([c + "\n" for c in self.comments]) + f.writelines([p + "\n" for p in self.passwords]) + def removeFiles(self, pack, fname): if not self.getConfig("deletearchive"): return m = self.re_splitfile.search(fname) - + download_folder = self.core.config['general']['download_folder'] if self.core.config['general']['folder_per_package']: folder = join(download_folder, pack.folder.decode(sys.getfilesystemencoding())) @@ -124,11 +128,11 @@ class UnRar(Hook): if nre.match(data["name"]): remove(join(folder, data["name"])) elif not m and fname.endswith(".rar"): - nre = re.compile("^%s\.r..$" % fname.replace(".rar","")) + nre = re.compile("^%s\.r..$" % fname.replace(".rar", "")) for fid, data in pack.getChildren().iteritems(): if nre.match(data["name"]): remove(join(folder, data["name"])) - + def packageFinished(self, pack): if pack.password and pack.password.strip() and pack.password.strip() != "None": self.addPassword(pack.password.splitlines()) @@ -137,15 +141,16 @@ class UnRar(Hook): for fid, data in pack.getChildren().iteritems(): m = self.re_splitfile.search(data["name"]) if m and int(m.group(2)) == 1: - files.append((fid,m.group(0))) + files.append((fid, m.group(0))) elif not m and data["name"].endswith(".rar"): - files.append((fid,data["name"])) - + files.append((fid, data["name"])) + for fid, fname in files: self.core.log.info(_("starting Unrar of %s") % fname) pyfile = self.core.files.getFile(fid) pyfile.setStatus("processing") pyfile.progress.setRange(0, 100) + def s(p): pyfile.progress.setValue(p) @@ -170,12 +175,14 @@ class UnRar(Hook): self.core.log.debug(_("Destination folder %s") % destination) if not exists(destination): - self.core.log.info(_("Creating destination folder %s") % destination) - makedirs(destination) + self.core.log.info(_("Creating destination folder %s") % destination) + makedirs(destination) - u = Unrar(join(folder, fname), tmpdir=join(folder, "tmp"), ramSize=(self.ram if self.getConfig("ramwarning") else 0), cpu=self.getConfig("renice")) + u = Unrar(join(folder, fname), tmpdir=join(folder, "tmp"), + ramSize=(self.ram if self.getConfig("ramwarning") else 0), cpu=self.getConfig("renice")) try: - success = u.crackPassword(passwords=self.passwords, statusFunction=s, overwrite=True, destination=destination, fullPath=self.getConfig("fullpath")) + success = u.crackPassword(passwords=self.passwords, statusFunction=s, overwrite=True, + destination=destination, fullPath=self.getConfig("fullpath")) except WrongPasswordError: self.core.log.info(_("Unrar of %s failed (wrong password)") % fname) continue @@ -195,7 +202,8 @@ class UnRar(Hook): 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) + 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: @@ -207,19 +215,24 @@ class UnRar(Hook): self.core.log.info(_("Unrar of %s ok") % fname) self.removeFiles(pack, fname) if self.core.config['general']['folder_per_package']: - if self.getConfig("deletearchive"): - self.core.log.debug(_("Deleting package directory %s...") % folder) - rmdir(folder) - self.core.log.debug(_("Package directory %s has been deleted.") % folder) - ownerUser=self.core.config['permission']['user'] - uinfo=getpwnam(ownerUser) - fileMode=int(self.core.config['permission']['file'],8) - self.core.log.debug(_("Setting destination file/directory owner to %s.") % ownerUser) - self.core.log.debug(_("Setting destination file/directory mode to %s.") % fileMode) - self.core.log.debug(_("Uid is %s.") % uinfo.pw_uid) - self.core.log.debug(_("Gid is %s.") % uinfo.pw_gid) - self.setOwner(destination,uinfo.pw_uid,uinfo.pw_gid,fileMode) - self.core.log.debug(_("The owner/rights have been successfully changed.")) + if self.getConfig("deletearchive"): + self.core.log.debug(_("Deleting package directory %s...") % folder) + rmdir(folder) + self.core.log.debug(_("Package directory %s has been deleted.") % folder) + + if os.name != "nt" and self.core.config['permission']['change_dl'] and\ + self.core.config['permission']['change_file']: + ownerUser = self.core.config['permission']['user'] + fileMode = int(self.core.config['permission']['file'], 8) + + self.core.log.debug("Setting destination file/directory owner / mode to %s / %s" + % (ownerUser, fileMode)) + + uinfo = getpwnam(ownerUser) + self.core.log.debug("Uid/Gid is %s/%s." % (uinfo.pw_uid, uinfo.pw_gid)) + self.setOwner(destination, uinfo.pw_uid, uinfo.pw_gid, fileMode) + self.core.log.debug("The owner/rights have been successfully changed.") + self.core.hookManager.unrarFinished(folder, fname) else: self.core.log.info(_("Unrar of %s failed (wrong password or bad parts)") % fname) |