<s> import subprocess <EOL> import gc <EOL> import os , os . path <EOL> import time , re <EOL> from rar_exceptions import * <EOL> class UnpackerNotInstalled ( Exception ) : pass <EOL> rar_executable_cached = None <EOL> rar_executable_version = None <EOL> def call_unrar ( params ) : <EOL> "<STR_LIT>" <EOL> global rar_executable_cached <EOL> if rar_executable_cached is None : <EOL> for command in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> subprocess . Popen ( [ command ] , stdout = subprocess . PIPE ) <EOL> rar_executable_cached = command <EOL> break <EOL> except OSError : <EOL> pass <EOL> if rar_executable_cached is None : <EOL> raise UnpackerNotInstalled ( "<STR_LIT>" ) <EOL> assert type ( params ) == list , "<STR_LIT>" <EOL> args = [ rar_executable_cached ] + params <EOL> try : <EOL> gc . disable ( ) <EOL> return subprocess . Popen ( args , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> finally : <EOL> gc . enable ( ) <EOL> class RarFileImplementation ( object ) : <EOL> def init ( self , password = None ) : <EOL> global rar_executable_version <EOL> self . password = password <EOL> stdoutdata , stderrdata = self . call ( '<STR_LIT:v>' , [ ] ) . communicate ( ) <EOL> for line in stderrdata . splitlines ( ) : <EOL> if line . strip ( ) . startswith ( "<STR_LIT>" ) : <EOL> raise FileOpenError <EOL> if line . find ( "<STR_LIT>" ) >= <NUM_LIT:0> : <EOL> raise IncorrectRARPassword <EOL> accum = [ ] <EOL> source = iter ( stdoutdata . splitlines ( ) ) <EOL> line = '<STR_LIT>' <EOL> while not ( line . startswith ( '<STR_LIT>' ) ) : <EOL> line = source . next ( ) <EOL> signature = line <EOL> if signature . startswith ( "<STR_LIT>" ) : <EOL> rar_executable_version = <NUM_LIT:4> <EOL> while not ( line . startswith ( '<STR_LIT>' ) or line . startswith ( '<STR_LIT>' ) ) : <EOL> if line . strip ( ) . endswith ( '<STR_LIT>' ) : <EOL> raise InvalidRARArchive <EOL> line = source . next ( ) <EOL> while not line . startswith ( '<STR_LIT>' ) : <EOL> accum . append ( line . rstrip ( '<STR_LIT:\n>' ) ) <EOL> line = source . next ( ) <EOL> if len ( accum ) : <EOL> accum [ <NUM_LIT:0> ] = accum [ <NUM_LIT:0> ] [ <NUM_LIT:9> : ] <EOL> self . comment = '<STR_LIT:\n>' . join ( accum [ : - <NUM_LIT:1> ] ) <EOL> else : <EOL> self . comment = None <EOL> elif signature . startswith ( "<STR_LIT>" ) : <EOL> rar_executable_version = <NUM_LIT:5> <EOL> line = source . next ( ) <EOL> while not line . startswith ( '<STR_LIT>' ) : <EOL> if line . strip ( ) . endswith ( '<STR_LIT>' ) : <EOL> raise InvalidRARArchive <EOL> accum . append ( line . rstrip ( '<STR_LIT:\n>' ) ) <EOL> line = source . next ( ) <EOL> if len ( accum ) : <EOL> self . comment = '<STR_LIT:\n>' . join ( accum [ : - <NUM_LIT:1> ] ) . strip ( ) <EOL> else : <EOL> self . comment = None <EOL> else : <EOL> raise UnpackerNotInstalled ( "<STR_LIT>" <EOL> + signature . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:1> ] ) <EOL> def escaped_password ( self ) : <EOL> return '<STR_LIT:->' if self . password == None else self . password <EOL> def call ( self , cmd , options = [ ] , files = [ ] ) : <EOL> options2 = options + [ '<STR_LIT:p>' + self . escaped_password ( ) ] <EOL> soptions = [ '<STR_LIT:->' + x for x in options2 ] <EOL> return call_unrar ( [ cmd ] + soptions + [ '<STR_LIT>' , self . archiveName ] + files ) <EOL> def infoiter ( self ) : <EOL> command = "<STR_LIT:v>" if rar_executable_version == <NUM_LIT:4> else "<STR_LIT:l>" <EOL> stdoutdata , stderrdata = self . call ( command , [ '<STR_LIT>' ] ) . communicate ( ) <EOL> for line in stderrdata . splitlines ( ) : <EOL> if line . strip ( ) . startswith ( "<STR_LIT>" ) : <EOL> raise FileOpenError <EOL> accum = [ ] <EOL> source = iter ( stdoutdata . splitlines ( ) ) <EOL> line = '<STR_LIT>' <EOL> while not line . startswith ( '<STR_LIT>' ) : <EOL> if line . strip ( ) . endswith ( '<STR_LIT>' ) : <EOL> raise InvalidRARArchive <EOL> if line . startswith ( "<STR_LIT>" ) or line . startswith ( "<STR_LIT>" ) : <EOL> raise IncorrectRARPassword <EOL> line = source . next ( ) <EOL> line = source . next ( ) <EOL> i = <NUM_LIT:0> <EOL> re_spaces = re . compile ( r"<STR_LIT>" ) <EOL> if rar_executable_version == <NUM_LIT:4> : <EOL> while not line . startswith ( '<STR_LIT>' ) : <EOL> accum . append ( line ) <EOL> if len ( accum ) == <NUM_LIT:2> : <EOL> data = { } <EOL> data [ '<STR_LIT:index>' ] = i <EOL> data [ '<STR_LIT:filename>' ] = accum [ <NUM_LIT:0> ] . strip ( ) . lstrip ( "<STR_LIT:*>" ) <EOL> fields = re_spaces . split ( accum [ <NUM_LIT:1> ] . strip ( ) ) <EOL> data [ '<STR_LIT:size>' ] = int ( fields [ <NUM_LIT:0> ] ) <EOL> attr = fields [ <NUM_LIT:5> ] <EOL> data [ '<STR_LIT>' ] = '<STR_LIT:d>' in attr . lower ( ) <EOL> data [ '<STR_LIT>' ] = time . strptime ( fields [ <NUM_LIT:3> ] + "<STR_LIT:U+0020>" + fields [ <NUM_LIT:4> ] , '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = None <EOL> yield data <EOL> accum = [ ] <EOL> i += <NUM_LIT:1> <EOL> line = source . next ( ) <EOL> elif rar_executable_version == <NUM_LIT:5> : <EOL> while not line . startswith ( '<STR_LIT>' ) : <EOL> fields = line . strip ( ) . lstrip ( "<STR_LIT:*>" ) . split ( ) <EOL> data = { } <EOL> data [ '<STR_LIT:index>' ] = i <EOL> data [ '<STR_LIT:filename>' ] = "<STR_LIT:U+0020>" . join ( fields [ <NUM_LIT:4> : ] ) <EOL> data [ '<STR_LIT:size>' ] = int ( fields [ <NUM_LIT:1> ] ) <EOL> attr = fields [ <NUM_LIT:0> ] <EOL> data [ '<STR_LIT>' ] = '<STR_LIT:d>' in attr . lower ( ) <EOL> data [ '<STR_LIT>' ] = time . strptime ( fields [ <NUM_LIT:2> ] + "<STR_LIT:U+0020>" + fields [ <NUM_LIT:3> ] , '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = None <EOL> yield data <EOL> i += <NUM_LIT:1> <EOL> line = source . next ( ) <EOL> def read_files ( self , checker ) : <EOL> res = [ ] <EOL> for info in self . infoiter ( ) : <EOL> checkres = checker ( info ) <EOL> if checkres == True and not info . isdir : <EOL> pipe = self . call ( '<STR_LIT:p>' , [ '<STR_LIT>' ] , [ info . filename ] ) . stdout <EOL> res . append ( ( info , pipe . read ( ) ) ) <EOL> return res <EOL> def extract ( self , checker , path , withSubpath , overwrite ) : <EOL> res = [ ] <EOL> command = '<STR_LIT:x>' <EOL> if not withSubpath : <EOL> command = '<STR_LIT:e>' <EOL> options = [ ] <EOL> if overwrite : <EOL> options . append ( '<STR_LIT>' ) <EOL> else : <EOL> options . append ( '<STR_LIT>' ) <EOL> if not path . endswith ( os . sep ) : <EOL> path += os . sep <EOL> names = [ ] <EOL> for info in self . infoiter ( ) : <EOL> checkres = checker ( info ) <EOL> if type ( checkres ) in [ str , unicode ] : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if checkres == True and not info . isdir : <EOL> names . append ( info . filename ) <EOL> res . append ( info ) <EOL> names . append ( path ) <EOL> proc = self . call ( command , options , names ) <EOL> stdoutdata , stderrdata = proc . communicate ( ) <EOL> if stderrdata . find ( "<STR_LIT>" ) >= <NUM_LIT:0> or stderrdata . find ( "<STR_LIT>" ) >= <NUM_LIT:0> : <EOL> raise IncorrectRARPassword <EOL> return res <EOL> def destruct ( self ) : <EOL> pass </s>
