From 6152ddbd5f196da848578fbc3bfb0ab91e9f12cd Mon Sep 17 00:00:00 2001 From: mano3m Date: Sat, 29 Mar 2014 21:31:59 +0100 Subject: [PATCH] Unrar cleanup --- libs/unrar2/unix.py | 70 ++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/libs/unrar2/unix.py b/libs/unrar2/unix.py index bd9ee859..47129f84 100644 --- a/libs/unrar2/unix.py +++ b/libs/unrar2/unix.py @@ -41,36 +41,36 @@ def call_unrar(params): if rar_executable_cached is None: for command in ('unrar', 'rar'): try: - subprocess.Popen([command], stdout=subprocess.PIPE) + subprocess.Popen([command], stdout = subprocess.PIPE) rar_executable_cached = command break except OSError: pass if rar_executable_cached is None: raise UnpackerNotInstalled("No suitable RAR unpacker installed") - + assert type(params) == list, "params must be list" args = [rar_executable_cached] + params try: gc.disable() # See http://bugs.python.org/issue1336 - return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + return subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE) finally: gc.enable() class RarFileImplementation(object): - def init(self, password=None): + def init(self, password = None): global rar_executable_version self.password = password - - + + stdoutdata, stderrdata = self.call('v', []).communicate() - + for line in stderrdata.splitlines(): if line.strip().startswith("Cannot open"): raise FileOpenError - if line.find("CRC failed")>=0: - raise IncorrectRARPassword + if line.find("CRC failed") >= 0: + raise IncorrectRARPassword accum = [] source = iter(stdoutdata.splitlines()) line = '' @@ -107,28 +107,28 @@ class RarFileImplementation(object): else: self.comment = None else: - raise UnpackerNotInstalled("Unsupported RAR version, expected 4.x or 5.x, found: " + raise UnpackerNotInstalled("Unsupported RAR version, expected 4.x or 5.x, found: " + signature.split(" ")[1]) - - + + def escaped_password(self): return '-' if self.password == None else self.password - - - def call(self, cmd, options=[], files=[]): - options2 = options + ['p'+self.escaped_password()] - soptions = ['-'+x for x in options2] - return call_unrar([cmd]+soptions+['--',self.archiveName]+files) + + + def call(self, cmd, options = [], files = []): + options2 = options + ['p' + self.escaped_password()] + soptions = ['-' + x for x in options2] + return call_unrar([cmd] + soptions + ['--', self.archiveName] + files) def infoiter(self): - + command = "v" if rar_executable_version == 4 else "l" stdoutdata, stderrdata = self.call(command, ['c-']).communicate() - + for line in stderrdata.splitlines(): if line.strip().startswith("Cannot open"): raise FileOpenError - + accum = [] source = iter(stdoutdata.splitlines()) line = '' @@ -136,7 +136,7 @@ class RarFileImplementation(object): if line.strip().endswith('is not RAR archive'): raise InvalidRARArchive if line.startswith("CRC failed") or line.startswith("Checksum error"): - raise IncorrectRARPassword + raise IncorrectRARPassword line = source.next() line = source.next() i = 0 @@ -144,7 +144,7 @@ class RarFileImplementation(object): if rar_executable_version == 4: while not line.startswith('-----------'): accum.append(line) - if len(accum)==2: + if len(accum) == 2: data = {} data['index'] = i # asterisks mark password-encrypted files @@ -153,7 +153,7 @@ class RarFileImplementation(object): data['size'] = int(fields[0]) attr = fields[5] data['isdir'] = 'd' in attr.lower() - data['datetime'] = time.strptime(fields[3]+" "+fields[4], '%d-%m-%y %H:%M') + data['datetime'] = time.strptime(fields[3] + " " + fields[4], '%d-%m-%y %H:%M') data['comment'] = None yield data accum = [] @@ -168,23 +168,23 @@ class RarFileImplementation(object): data['size'] = int(fields[1]) attr = fields[0] data['isdir'] = 'd' in attr.lower() - data['datetime'] = time.strptime(fields[2]+" "+fields[3], '%d-%m-%y %H:%M') + data['datetime'] = time.strptime(fields[2] + " " + fields[3], '%d-%m-%y %H:%M') data['comment'] = None yield data i += 1 line = source.next() - + def read_files(self, checker): res = [] for info in self.infoiter(): checkres = checker(info) - if checkres==True and not info.isdir: + if checkres == True and not info.isdir: pipe = self.call('p', ['inul'], [info.filename]).stdout res.append((info, pipe.read())) - return res + return res + - def extract(self, checker, path, withSubpath, overwrite): res = [] command = 'x' @@ -202,17 +202,17 @@ class RarFileImplementation(object): checkres = checker(info) if type(checkres) in [str, unicode]: raise NotImplementedError("Condition callbacks returning strings are deprecated and only supported in Windows") - if checkres==True and not info.isdir: + if checkres == True and not info.isdir: names.append(info.filename) res.append(info) names.append(path) proc = self.call(command, options, names) stdoutdata, stderrdata = proc.communicate() - if stderrdata.find("CRC failed")>=0 or stderrdata.find("Checksum error")>=0: - raise IncorrectRARPassword - return res - + if stderrdata.find("CRC failed") >= 0 or stderrdata.find("Checksum error") >= 0: + raise IncorrectRARPassword + return res + def destruct(self): pass - +