diff options
author | Walter Purcaro <vuolter@gmail.com> | 2015-01-25 03:05:00 +0100 |
---|---|---|
committer | Walter Purcaro <vuolter@gmail.com> | 2015-01-25 03:05:00 +0100 |
commit | 7fc3362307737cd7c565b710ec83c5bdc4d3e8a9 (patch) | |
tree | 68ed3207a9c8eb85d218afc3d5d882236f3e477a /module | |
parent | [DLC] Fix https://github.com/pyload/pyload/issues/1074 (diff) | |
download | pyload-7fc3362307737cd7c565b710ec83c5bdc4d3e8a9.tar.xz |
Revert Extractor to the old one (temp)
Diffstat (limited to 'module')
-rw-r--r-- | module/plugins/hooks/ExtractArchive.py | 219 | ||||
-rw-r--r-- | module/plugins/internal/Extractor.py | 79 | ||||
-rw-r--r-- | module/plugins/internal/UnRar.py | 216 | ||||
-rw-r--r-- | module/plugins/internal/UnZip.py | 69 |
4 files changed, 198 insertions, 385 deletions
diff --git a/module/plugins/hooks/ExtractArchive.py b/module/plugins/hooks/ExtractArchive.py index 38459e5f1..9e530ce8f 100644 --- a/module/plugins/hooks/ExtractArchive.py +++ b/module/plugins/hooks/ExtractArchive.py @@ -51,33 +51,32 @@ if os.name != "nt": from pwd import getpwnam from module.plugins.Hook import Hook, threaded, Expose -from module.plugins.internal.Extractor import ArchiveError, CRCError, PasswordError -from module.utils import save_join, uniqify +from module.plugins.internal.Extractor import ArchiveError, CRCError, WrongPassword +from module.utils import save_join, fs_encode class ExtractArchive(Hook): __name__ = "ExtractArchive" __type__ = "hook" - __version__ = "1.06" - - __config__ = [("activated" , "bool" , "Activated" , True ), - ("fullpath" , "bool" , "Extract full path" , True ), - ("overwrite" , "bool" , "Overwrite files" , False ), - ("keepbroken" , "bool" , "Extract broken archives" , False ), - ("repair" , "bool" , "Repair broken archives" , True ), - ("passwordfile" , "file" , "Store passwords in file" , "archive_password.txt" ), - ("delete" , "bool" , "Delete archive when successfully extracted", False ), - ("subfolder" , "bool" , "Create subfolder for each package" , False ), - ("destination" , "folder", "Extract files to" , "" ), - ("extensions" , "str" , "Extract the following extensions" , "7z,bz2,bzip2,gz,gzip,lha,lzh,lzma,rar,tar,taz,tbz,tbz2,tgz,xar,xz,z,zip"), - ("excludefiles" , "str" , "Don't extract the following files" , "*.nfo,*.DS_Store,index.dat,thumb.db" ), - ("recursive" , "bool" , "Extract archives in archives" , True ), - ("queue" , "bool" , "Wait for all downloads to be finished" , False ), - ("renice" , "int" , "CPU Priority" , 0 )] + __version__ = "1.07" + + __config__ = [("activated", "bool", "Activated", True), + ("fullpath", "bool", "Extract full path", True), + ("overwrite", "bool", "Overwrite files", True), + ("passwordfile", "file", "password file", "archive_password.txt"), + ("deletearchive", "bool", "Delete archives when done", False), + ("subfolder", "bool", "Create subfolder for each package", False), + ("destination", "folder", "Extract files to", ""), + ("excludefiles", "str", "Exclude files from unpacking (seperated by ;)", ""), + ("recursive", "bool", "Extract archives in archvies", True), + ("queue", "bool", "Wait for all downloads to be finished", True), + ("renice", "int", "CPU Priority", 0)] __description__ = """Extract different kind of archives""" __license__ = "GPLv3" - __authors__ = [("Walter Purcaro", "vuolter@gmail.com")] + __authors__ = [("RaNaN", "ranan@pyload.org"), + ("AndroKev", None), + ("Walter Purcaro", "vuolter@gmail.com")] event_list = ["allDownloadsProcessed"] @@ -88,16 +87,12 @@ class ExtractArchive(Hook): pass - def coreReady(self): - self.extracting = False - - def setup(self): self.plugins = [] self.passwords = [] names = [] - for p in ("UnRar", "SevenZip", "UnZip"): + for p in ("UnRar", "UnZip"): try: module = self.core.pluginManager.loadModule("internal", p) klass = getattr(module, p) @@ -127,69 +122,45 @@ class ExtractArchive(Hook): self.queue = [] - def periodical(self): - if not self.queue or self.extracting: - return - - local = copy(self.queue) - self.queue[:] = [] - - self.extractPackages(*local) - - @Expose def extractPackage(self, id): - """ Extract package wrapper""" - self.extractPackages(id) - - - @Expose - def extractPackages(self, *ids): - """ Extract packages with given id""" - self.manager.startThread(self.extract, ids) + """ Extract package with given id""" + self.manager.startThread(self.extract, [id]) def packageFinished(self, pypack): - if self.getConfig("queue") or self.extracting: + pid = pypack.id + if self.getConfig("queue"): self.logInfo(_("Package %s queued for later extracting") % pypack.name) - self.queue.append(pypack.id) + self.queue.append(pid) else: - self.extractPackage(pypack.id) + self.manager.startThread(self.extract, [pid]) @threaded - def allDownloadsProcessed(self): + def allDownloadsProcessed(self, thread): local = copy(self.queue) - self.queue[:] = [] - if self.extract(local): #: check only if all gone fine, no failed reporting for now + del self.queue[:] + + if self.extract(local, thread): #: check only if all gone fine, no failed reporting for now self.manager.dispatchEvent("all_archives_extracted") self.manager.dispatchEvent("all_archives_processed") - def extract(self, ids): - self.extracting = True - + def extract(self, ids, thread=None): processed = [] extracted = [] failed = [] - clearlist = lambda string: [x.lstrip('.') for x in string.replace(' ', '').replace(',', '|').replace(';', '|').split('|')] - destination = self.getConfig("destination") subfolder = self.getConfig("subfolder") fullpath = self.getConfig("fullpath") overwrite = self.getConfig("overwrite") - extensions = clearlist(self.getConfig("extensions")) - excludefiles = clearlist(self.getConfig("excludefiles")) + excludefiles = self.getConfig("excludefiles") renice = self.getConfig("renice") recursive = self.getConfig("recursive") - delete = self.getConfig("delete") - keepbroken = self.getConfig("keepbroken") - - if extensions: - self.logDebug("Extensions: %s" % "|.".join(extensions)) # reload from txt file self.reloadPasswords() @@ -200,7 +171,7 @@ class ExtractArchive(Hook): #iterate packages -> plugins -> targets for pid in ids: p = self.core.files.getPackage(pid) - self.logInfo(_("Check package: %s") % p.name) + self.logInfo(_("Check package %s") % p.name) if not p: continue @@ -208,25 +179,21 @@ class ExtractArchive(Hook): out = save_join(dl, p.folder, destination, "") #: force trailing slash if subfolder: - out = save_join(out, p.folder) + out = save_join(out, fs_encode(p.folder)) if not exists(out): makedirs(out) files_ids = [(save_join(dl, p.folder, x['name']), x['id']) for x in p.getChildren().itervalues()] - matched = False - success = True + matched = False + success = True # check as long there are unseen files while files_ids: new_files_ids = [] - if extensions: - files_ids = [(file, id) for file, id in files_ids if filter(lambda ext: file.endswith(ext), extensions)] - for plugin in self.plugins: targets = plugin.getTargets(files_ids) - if targets: self.logDebug("Targets for %s: %s" % (plugin.__name__, targets)) matched = True @@ -238,32 +205,19 @@ class ExtractArchive(Hook): processed.append(target) # prevent extracting same file twice - self.logInfo(basename(target), _("Extract to: %s") % out) + self.logInfo(basename(target), _("Extract to %s") % out) try: - klass = plugin(self, - target, - out, - p.password, - fullpath, - overwrite, - excludefiles, - renice, - delete, - keepbroken) + klass = plugin(self, target, out, fullpath, overwrite, excludefiles, renice) klass.init() - new_files = self._extract(klass, fid) + new_files = self._extract(klass, fid, [p.password.strip()], thread) except Exception, e: self.logError(basename(target), e) - new_files = None - - if new_files is None: - self.logWarning(basename(target), _("No files extracted")) success = False continue - self.logDebug("Extracted files: %s" % new_files) + self.logDebug("Extracted", new_files) self.setPermissions(new_files) for file in new_files: @@ -285,87 +239,46 @@ class ExtractArchive(Hook): else: self.logInfo(_("No files found to extract")) - if not matched or not success and subfolder: - try: - os.rmdir(out) - except OSError: - pass - - self.extracting = False return True if not failed else False - def _extract(self, plugin, fid): + def _extract(self, plugin, fid, passwords, thread): pyfile = self.core.files.getFile(fid) + deletearchive = self.getConfig("deletearchive") pyfile.setCustomStatus(_("extracting")) + thread.addActive(pyfile) # keep this file until everything is done try: - progress = lambda x: pyfile.setProgress(x) - encrypted = False - passwords = self.getPasswords() - - try: - self.logInfo(basename(plugin.file), "Verifying...") - - tmp_password = plugin.password - plugin.password = "" #: Force verifying without password - - plugin.verify() - - except PasswordError: - encrypted = True - - except CRCError: - self.logWarning(basename(plugin.file), _("Archive damaged")) - - if not self.getConfig("repair"): - raise CRCError - - elif plugin.repair(): - self.logInfo(basename(plugin.file), _("Successfully repaired")) - - elif not self.getConfig("keepbroken"): - raise ArchiveError(_("Broken archive")) - - else: - self.logInfo(basename(plugin.file), _("All OK")) - - plugin.password = tmp_password - - if not encrypted: - plugin.extract(progress) + progress = lambda x: pyfile.setProgress(x) + success = False + if not plugin.checkArchive(): + plugin.extract(progress, pw) + success = True else: self.logInfo(basename(plugin.file), _("Password protected")) + self.logDebug("Passwords: %s" % passwords if passwords else "No password provided") - if plugin.password: - passwords.insert(0, plugin.password) - passwords = uniqify(self.passwords) - self.logDebug("Password: %s" % plugin.password) - else: - self.logDebug("No package password provided") - - for pw in passwords: + for pw in set(passwords) | set(self.getPasswords()): try: self.logDebug("Try password: %s" % pw) - - if plugin.setPassword(pw): - plugin.extract(progress) + if plugin.checkPassword(pw): + plugin.extract(progress, pw) self.addPassword(pw) + success = True break - else: - raise PasswordError - except PasswordError: + except WrongPassword: self.logDebug("Password was wrong") - else: - raise PasswordError + + if not success: + raise Exception(_("Wrong password")) if self.core.debug: - self.logDebug("Would delete: %s" % ", ".join(plugin.getDeleteFiles())) + self.logDebug("Would delete", ", ".join(plugin.getDeleteFiles())) - if self.getConfig("delete"): + if deletearchive: files = plugin.getDeleteFiles() self.logInfo(_("Deleting %s files") % len(files)) for f in files: @@ -381,16 +294,12 @@ class ExtractArchive(Hook): return extracted_files - except PasswordError: - self.logError(basename(plugin.file), _("Wrong password" if passwords else "No password found")) - plugin.password = "" + except ArchiveError, e: + self.logError(basename(plugin.file), _("Archive Error"), e) except CRCError: self.logError(basename(plugin.file), _("CRC Mismatch")) - except ArchiveError, e: - self.logError(basename(plugin.file), _("Archive Error"), e) - except Exception, e: if self.core.debug: print_exc() @@ -398,7 +307,7 @@ class ExtractArchive(Hook): self.manager.dispatchEvent("archive_extract_failed", pyfile) - self.logError(basename(plugin.file), _("Extract failed")) + raise Exception(_("Extract failed")) @Expose @@ -428,13 +337,15 @@ class ExtractArchive(Hook): """ Adds a password to saved list""" passwordfile = self.getConfig("passwordfile") + if pw in self.passwords: + self.passwords.remove(pw) + self.passwords.insert(0, pw) - self.passwords = uniqify(self.passwords) try: with open(passwordfile, "wb") as f: for pw in self.passwords: - f.write(pw + '\n') + f.write(pw + "\n") except IOError, e: self.logError(e) diff --git a/module/plugins/internal/Extractor.py b/module/plugins/internal/Extractor.py index 0b2462dac..55d9b2e83 100644 --- a/module/plugins/internal/Extractor.py +++ b/module/plugins/internal/Extractor.py @@ -8,52 +8,37 @@ class CRCError(Exception): pass -class PasswordError(Exception): +class WrongPassword(Exception): pass class Extractor: __name__ = "Extractor" - __version__ = "0.13" + __version__ = "0.14" __description__ = """Base extractor plugin""" __license__ = "GPLv3" - __authors__ = [("RaNaN", "ranan@pyload.org"), - ("Walter Purcaro", "vuolter@gmail.com")] + __authors__ = [("pyLoad Team", "admin@pyload.org")] - EXTENSIONS = [] - - - @classmethod - def checkDeps(cls): + @staticmethod + def checkDeps(): """ Check if system statisfy dependencies :return: boolean """ return True - @classmethod - def isArchive(cls, file): - raise NotImplementedError - - - @classmethod - def getTargets(cls, files_ids): + @staticmethod + def getTargets(files_ids): """ Filter suited targets from list of filename id tuple list :param files_ids: List of filepathes :return: List of targets, id tuple list """ - targets = [] - - for file, id in files_ids: - if cls.isArchive(file): - targets.append((file, id)) - - return targets + raise NotImplementedError - def __init__(self, m, file, out, password, fullpath, overwrite, excludefiles, renice, delete, keepbroken): + def __init__(self, m, file, out, fullpath, overwrite, excludefiles, renice): """Initialize extractor for specific file :param m: ExtractArchive Hook plugin @@ -63,17 +48,14 @@ class Extractor: :param overwrite: Overwrite existing archives :param renice: Renice value """ - self.m = m - self.file = file - self.out = out - self.password = password - self.fullpath = fullpath - self.overwrite = overwrite + self.m = m + self.file = file + self.out = out + self.fullpath = fullpath + self.overwrite = overwrite self.excludefiles = excludefiles - self.renice = renice - self.delete = delete - self.keepbroken = keepbroken - self.files = [] #: Store extracted files here + self.renice = renice + self.files = [] #: Store extracted files here def init(self): @@ -81,45 +63,32 @@ class Extractor: pass - def verify(self): + def checkArchive(self): """Check if password if needed. Raise ArchiveError if integrity is questionable. + :return: boolean :raises ArchiveError """ - pass + return False - def isPassword(self, password): + def checkPassword(self, password): """ Check if the given password is/might be correct. If it can not be decided at this point return true. :param password: :return: boolean """ - if isinstance(password, basestring): - return True - else: - return False - - - def setPassword(self, password): - if self.isPassword(password): - self.password = password - return True - else: - return False - - - def repair(self): - return False + return True - def extract(self, progress=lambda x: None): + def extract(self, progress, password=None): """Extract the archive. Raise specific errors in case of failure. :param progress: Progress function, call this to update status - :raises PasswordError + :param password password to use + :raises WrongPassword :raises CRCError :raises ArchiveError :return: diff --git a/module/plugins/internal/UnRar.py b/module/plugins/internal/UnRar.py index 296405101..a1b438e47 100644 --- a/module/plugins/internal/UnRar.py +++ b/module/plugins/internal/UnRar.py @@ -4,11 +4,11 @@ import os import re from glob import glob -from os.path import basename, dirname, join +from os.path import basename, join from string import digits from subprocess import Popen, PIPE -from module.plugins.internal.Extractor import Extractor, ArchiveError, CRCError, PasswordError +from module.plugins.internal.Extractor import Extractor, WrongPassword, ArchiveError, CRCError from module.utils import save_join, decode @@ -22,142 +22,115 @@ def renice(pid, value): class UnRar(Extractor): __name__ = "UnRar" - __version__ = "1.01" + __version__ = "1.02" __description__ = """Rar extractor plugin""" __license__ = "GPLv3" - __authors__ = [("Walter Purcaro", "vuolter@gmail.com")] + __authors__ = [("RaNaN", "RaNaN@pyload.org")] CMD = "unrar" - EXTENSIONS = ["rar", "zip", "cab", "arj", "lzh", "tar", "gz", "bz2", "ace", "uue", "jar", "iso", "7z", "xz", "z"] + # there are some more uncovered rar formats + re_version = re.compile(r"(UNRAR 5[\d.]+(.*?)freeware)") + re_splitfile = re.compile(r"(.*)\.part(\d+)\.rar$", re.I) + re_partfiles = re.compile(r".*\.(rar|r\d+)", re.I) + re_filelist = re.compile(r"(.+)\s+(\d+)\s+(\d+)\s+") + re_filelist5 = re.compile(r"(.+)\s+(\d+)\s+\d\d-\d\d-\d\d\s+\d\d:\d\d\s+(.+)") + re_wrongpwd = re.compile("(Corrupt file or wrong password|password incorrect)", re.I) - #@NOTE: there are some more uncovered rar formats - re_rarpart = re.compile(r'(.*)\.part(\d+)\.rar$', re.I) - re_rarfile = re.compile(r'.*\.(rar|r\d+)$', re.I) - - re_filelist = re.compile(r'(.+)\s+(\d+)\s+(\d+)\s+|(.+)\s+(\d+)\s+\d\d-\d\d-\d\d\s+\d\d:\d\d\s+(.+)') - re_wrongpwd = re.compile(r'password', re.I) - re_wrongcrc = re.compile(r'encrypted|damaged|CRC failed|checksum error', re.I) - - - @classmethod - def checkDeps(cls): + @staticmethod + def checkDeps(): if os.name == "nt": - cls.CMD = join(pypath, "UnRAR.exe") - p = Popen([cls.CMD], stdout=PIPE, stderr=PIPE) + UnRar.CMD = join(pypath, "UnRAR.exe") + p = Popen([UnRar.CMD], stdout=PIPE, stderr=PIPE) p.communicate() else: try: - p = Popen([cls.CMD], stdout=PIPE, stderr=PIPE) + p = Popen([UnRar.CMD], stdout=PIPE, stderr=PIPE) p.communicate() - except OSError: + # fallback to rar - cls.CMD = "rar" - p = Popen([cls.CMD], stdout=PIPE, stderr=PIPE) + UnRar.CMD = "rar" + p = Popen([UnRar.CMD], stdout=PIPE, stderr=PIPE) p.communicate() return True - @classmethod - def isArchive(cls, file): - f = basename(file).lower() - return any(f.endswith('.%s' % ext) for ext in cls.EXTENSIONS) - - - @classmethod - def getTargets(cls, files_ids): - targets = [] + @staticmethod + def getTargets(files_ids): + result = [] for file, id in files_ids: - if not cls.isArchive(file): + if not file.endswith(".rar"): continue - m = cls.re_rarpart.findall(file) - if m: + match = UnRar.re_splitfile.findall(file) + if match: # only add first parts - if int(m[0][1]) == 1: - targets.append((file, id)) + if int(match[0][1]) == 1: + result.append((file, id)) else: - targets.append((file, id)) - - return targets + result.append((file, id)) + return result - def check(self, out="", err=""): - if not out or not err: - return - if err.strip(): - if self.re_wrongpwd.search(err): - raise PasswordError + def init(self): + self.passwordProtected = False + self.headerProtected = False #: list files will not work without password + self.smallestFile = None #: small file to test passwords + self.password = "" #: save the correct password - elif self.re_wrongcrc.search(err): - raise CRCError - else: #: raise error if anything is on stderr - raise ArchiveError(err.strip()) + def checkArchive(self): + p = self.call_unrar("l", "-v", self.file) + out, err = p.communicate() + if self.re_wrongpwd.search(err): + self.passwordProtected = True + self.headerProtected = True + return True # output only used to check if passworded files are present - for attr in self.re_filelist.findall(out): - if attr[0].startswith("*"): - raise PasswordError - - - def verify(self): - p = self.call_cmd("l", "-v", self.file, password=self.password) - - self.check(*p.communicate()) - - if p and p.returncode: - raise ArchiveError("Process terminated") - - if not self.list(): - raise ArchiveError("Empty archive") - - - def isPassword(self, password): - if isinstance(password, basestring): - p = self.call_cmd("l", "-v", self.file, password=password) - out, err = p.communicate() + if self.re_version.search(out): + for attr, size, name in self.re_filelist5.findall(out): + if attr.startswith("*"): + self.passwordProtected = True + return True + else: + for name, size, packed in self.re_filelist.findall(out): + if name.startswith("*"): + self.passwordProtected = True + return True - if not self.re_wrongpwd.search(err): - return True + self.listContent() + if not self.files: + raise ArchiveError("Empty Archive") return False - def repair(self): - p = self.call_cmd("rc", self.file) - out, err = p.communicate() - - if p.returncode or err.strip(): - p = self.call_cmd("r", self.file) + def checkPassword(self, password): + # at this point we can only verify header protected files + if self.headerProtected: + p = self.call_unrar("l", "-v", self.file, password=password) out, err = p.communicate() - - if p.returncode or err.strip(): + if self.re_wrongpwd.search(err): return False - else: - self.file = join(dirname(self.file), re.search(r'(fixed|rebuild)\.%s' % basename(self.file), out).group(0)) return True - def extract(self, progress=lambda x: None): - self.verify() - - progress(0) - + def extract(self, progress, password=None): command = "x" if self.fullpath else "e" - p = self.call_cmd(command, self.file, self.out, password=self.password) - + p = self.call_unrar(command, self.file, self.out, password=password) renice(p.pid, self.renice) + progress(0) progressstring = "" while True: c = p.stdout.read(1) @@ -165,7 +138,7 @@ class UnRar(Extractor): if not c: break # reading a percentage sign -> set progress and restart - if c is '%': + if c == '%': progress(int(progressstring)) progressstring = "" # not reading a digit -> therefore restart @@ -173,43 +146,44 @@ class UnRar(Extractor): progressstring = "" # add digit to progressstring else: - progressstring += c - + progressstring = progressstring + c progress(100) - self.files = self.list() - # retrieve stderr - self.check(err=p.stderr.read()) - + err = p.stderr.read() + + if "CRC failed" in err and not password and not self.passwordProtected: + raise CRCError + elif "CRC failed" in err: + raise WrongPassword + if err.strip(): #: raise error if anything is on stderr + raise ArchiveError(err.strip()) if p.returncode: raise ArchiveError("Process terminated") + if not self.files: + self.password = password + self.listContent() + def getDeleteFiles(self): if ".part" in basename(self.file): return glob(re.sub("(?<=\.part)([01]+)", "*", self.file, re.I)) - # get files which matches .r* and filter unsuited files out parts = glob(re.sub(r"(?<=\.r)ar$", "*", self.file, re.I)) + return filter(lambda x: self.re_partfiles.match(x), parts) - return filter(lambda x: self.re_rarfile.match(x), parts) - - def list(self): + def listContent(self): command = "vb" if self.fullpath else "lb" - - p = self.call_cmd(command, "-v", self.file, password=self.password) + p = self.call_unrar(command, "-v", self.file, password=self.password) out, err = p.communicate() - if err.strip(): - self.m.logError(err) - if "Cannot open" in err: - return list() + if "Cannot open" in err: + raise ArchiveError("Cannot open file") - if p.returncode: - self.m.logError("Process terminated") - return list() + if err.strip(): #: only log error at this point + self.m.logError(err.strip()) result = set() @@ -217,22 +191,17 @@ class UnRar(Extractor): f = f.strip() result.add(save_join(self.out, f)) - return list(result) + self.files = result - def call_cmd(self, command, *xargs, **kwargs): + def call_unrar(self, command, *xargs, **kwargs): args = [] - # overwrite flag - if self.overwrite: - args.append("-o+") - else: - args.append("-o-") - if self.delete: - args.append("-or") + args.append("-o+") if self.overwrite else args.append("-o-") - for word in self.excludefiles: - args.append("-x%s" % word.strip()) + if self.excludefiles: + for word in self.excludefiles.split(';'): + args.append("-x%s" % word) # assume yes on all queries args.append("-y") @@ -243,11 +212,10 @@ class UnRar(Extractor): else: args.append("-p-") - if self.keepbroken: - args.append("-kb") - # NOTE: return codes are not reliable, some kind of threading, cleanup whatever issue call = [self.CMD, command] + args + list(xargs) self.m.logDebug(" ".join(call)) - return Popen(call, stdout=PIPE, stderr=PIPE) + p = Popen(call, stdout=PIPE, stderr=PIPE) + + return p diff --git a/module/plugins/internal/UnZip.py b/module/plugins/internal/UnZip.py index 5ec56cbdf..888ae7ebe 100644 --- a/module/plugins/internal/UnZip.py +++ b/module/plugins/internal/UnZip.py @@ -1,86 +1,51 @@ # -*- coding: utf-8 -*- -from __future__ import with_statement - import sys import zipfile -from module.plugins.internal.Extractor import Extractor, ArchiveError, CRCError, PasswordError +from module.plugins.internal.Extractor import Extractor, WrongPassword, ArchiveError class UnZip(Extractor): __name__ = "UnZip" - __version__ = "1.01" + __version__ = "1.02" __description__ = """Zip extractor plugin""" __license__ = "GPLv3" - __authors__ = [("Walter Purcaro", "vuolter@gmail.com")] - - - EXTENSIONS = ["zip", "zip64"] + __authors__ = [("RaNaN", "RaNaN@pyload.org")] - @classmethod - def checkDeps(cls): + @staticmethod + def checkDeps(): return sys.version_info[:2] >= (2, 6) - @classmethod - def isArchive(cls, file): - return zipfile.is_zipfile(file) + @staticmethod + def getTargets(files_ids): + result = [] + for file, id in files_ids: + if file.endswith(".zip"): + result.append((file, id)) - def verify(self): - try: - with zipfile.ZipFile(self.file, 'r', allowZip64=True) as z: - z.setpassword(self.password) - badcrc = z.testzip() + return result - except (BadZipfile, LargeZipFile), e: - raise ArchiveError(e) - except RuntimeError, e: - if 'encrypted' in e: - raise PasswordError - else: - raise ArchiveError(e) - - else: - if badcrc: - raise CRCError - - if not self.list(): - raise ArchiveError("Empty archive") - - - def list(self): + def extract(self, progress, password=None): try: - with zipfile.ZipFile(self.file, 'r', allowZip64=True) as z: - z.setpassword(self.password) - return z.namelist() - except Exception: - return list() - - - def extract(self, progress=lambda x: None): - try: - with zipfile.ZipFile(self.file, 'r', allowZip64=True) as z: - progress(0) - z.extractall(self.out, pwd=self.password) - progress(100) + z = zipfile.ZipFile(self.file) + self.files = z.namelist() + z.extractall(self.out, pwd=password) except (BadZipfile, LargeZipFile), e: raise ArchiveError(e) except RuntimeError, e: if e is "Bad password for file": - raise PasswordError + raise WrongPassword else: raise ArchiveError(e) - finally: - self.files = self.list() - def getDeleteFiles(self): return [self.file] |