<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import tarfile <EOL> import zipfile <EOL> from django . utils import six <EOL> class ArchiveException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class UnrecognizedArchiveFormat ( ArchiveException ) : <EOL> """<STR_LIT>""" <EOL> def extract ( path , to_path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> with Archive ( path ) as archive : <EOL> archive . extract ( to_path ) <EOL> class Archive ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file ) : <EOL> self . _archive = self . _archive_cls ( file ) ( file ) <EOL> @ staticmethod <EOL> def _archive_cls ( file ) : <EOL> cls = None <EOL> if isinstance ( file , six . string_types ) : <EOL> filename = file <EOL> else : <EOL> try : <EOL> filename = file . name <EOL> except AttributeError : <EOL> raise UnrecognizedArchiveFormat ( <EOL> "<STR_LIT>" ) <EOL> base , tail_ext = os . path . splitext ( filename . lower ( ) ) <EOL> cls = extension_map . get ( tail_ext ) <EOL> if not cls : <EOL> base , ext = os . path . splitext ( base ) <EOL> cls = extension_map . get ( ext ) <EOL> if not cls : <EOL> raise UnrecognizedArchiveFormat ( <EOL> "<STR_LIT>" % filename ) <EOL> return cls <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . close ( ) <EOL> def extract ( self , to_path = '<STR_LIT>' ) : <EOL> self . _archive . extract ( to_path ) <EOL> def list ( self ) : <EOL> self . _archive . list ( ) <EOL> def close ( self ) : <EOL> self . _archive . close ( ) <EOL> class BaseArchive ( object ) : <EOL> """<STR_LIT>""" <EOL> def split_leading_dir ( self , path ) : <EOL> path = str ( path ) <EOL> path = path . lstrip ( '<STR_LIT:/>' ) . lstrip ( '<STR_LIT:\\>' ) <EOL> if '<STR_LIT:/>' in path and ( ( '<STR_LIT:\\>' in path and path . find ( '<STR_LIT:/>' ) < path . find ( '<STR_LIT:\\>' ) ) or '<STR_LIT:\\>' not in path ) : <EOL> return path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> elif '<STR_LIT:\\>' in path : <EOL> return path . split ( '<STR_LIT:\\>' , <NUM_LIT:1> ) <EOL> else : <EOL> return path , '<STR_LIT>' <EOL> def has_leading_dir ( self , paths ) : <EOL> """<STR_LIT>""" <EOL> common_prefix = None <EOL> for path in paths : <EOL> prefix , rest = self . split_leading_dir ( path ) <EOL> if not prefix : <EOL> return False <EOL> elif common_prefix is None : <EOL> common_prefix = prefix <EOL> elif prefix != common_prefix : <EOL> return False <EOL> return True <EOL> def extract ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def list ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class TarArchive ( BaseArchive ) : <EOL> def __init__ ( self , file ) : <EOL> self . _archive = tarfile . open ( file ) <EOL> def list ( self , * args , ** kwargs ) : <EOL> self . _archive . list ( * args , ** kwargs ) <EOL> def extract ( self , to_path ) : <EOL> members = [ member for member in self . _archive . getmembers ( ) <EOL> if member . name != '<STR_LIT>' ] <EOL> leading = self . has_leading_dir ( x . name for x in members ) <EOL> for member in members : <EOL> name = member . name <EOL> if leading : <EOL> name = self . split_leading_dir ( name ) [ <NUM_LIT:1> ] <EOL> filename = os . path . join ( to_path , name ) <EOL> if member . isdir ( ) : <EOL> if filename and not os . path . exists ( filename ) : <EOL> os . makedirs ( filename ) <EOL> else : <EOL> try : <EOL> extracted = self . _archive . extractfile ( member ) <EOL> except ( KeyError , AttributeError ) as exc : <EOL> print ( "<STR_LIT>" % <EOL> ( name , member . name , exc ) ) <EOL> else : <EOL> dirname = os . path . dirname ( filename ) <EOL> if dirname and not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> with open ( filename , '<STR_LIT:wb>' ) as outfile : <EOL> shutil . copyfileobj ( extracted , outfile ) <EOL> finally : <EOL> if extracted : <EOL> extracted . close ( ) <EOL> def close ( self ) : <EOL> self . _archive . close ( ) <EOL> class ZipArchive ( BaseArchive ) : <EOL> def __init__ ( self , file ) : <EOL> self . _archive = zipfile . ZipFile ( file ) <EOL> def list ( self , * args , ** kwargs ) : <EOL> self . _archive . printdir ( * args , ** kwargs ) <EOL> def extract ( self , to_path ) : <EOL> namelist = self . _archive . namelist ( ) <EOL> leading = self . has_leading_dir ( namelist ) <EOL> for name in namelist : <EOL> data = self . _archive . read ( name ) <EOL> if leading : <EOL> name = self . split_leading_dir ( name ) [ <NUM_LIT:1> ] <EOL> filename = os . path . join ( to_path , name ) <EOL> dirname = os . path . dirname ( filename ) <EOL> if dirname and not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> if filename . endswith ( ( '<STR_LIT:/>' , '<STR_LIT:\\>' ) ) : <EOL> if not os . path . exists ( filename ) : <EOL> os . makedirs ( filename ) <EOL> else : <EOL> with open ( filename , '<STR_LIT:wb>' ) as outfile : <EOL> outfile . write ( data ) <EOL> def close ( self ) : <EOL> self . _archive . close ( ) <EOL> extension_map = { <EOL> '<STR_LIT>' : TarArchive , <EOL> '<STR_LIT>' : TarArchive , <EOL> '<STR_LIT>' : TarArchive , <EOL> '<STR_LIT>' : TarArchive , <EOL> '<STR_LIT>' : TarArchive , <EOL> '<STR_LIT>' : ZipArchive , <EOL> } </s>
