<s> from __future__ import print_function , division , absolute_import <EOL> import gzip <EOL> import tarfile <EOL> import zipfile <EOL> import re <EOL> import string <EOL> import io <EOL> class Archive ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , fileobj ) : <EOL> self . _filename = filename <EOL> self . _fileobj = fileobj <EOL> self . _file = None <EOL> self . _names = None <EOL> self . _read = None <EOL> @ property <EOL> def filename ( self ) : <EOL> return self . _filename <EOL> @ property <EOL> def names ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _file is None : <EOL> self . _prepare ( ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _namelist = self . _names ( ) <EOL> return self . _namelist <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _file is not None : <EOL> self . _file . close ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . _namelist <EOL> self . _filename = self . _fileobj = None <EOL> self . _file = self . _names = self . _read = None <EOL> def read ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if self . _file is None : <EOL> self . _prepare ( ) <EOL> return self . _read ( filename ) <EOL> def unpack ( self , to_path ) : <EOL> """<STR_LIT>""" <EOL> if self . _file is None : <EOL> self . _prepare ( ) <EOL> self . _safe_extractall ( to_path ) <EOL> def _prepare ( self ) : <EOL> if self . _filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> self . _prepare_tarball ( ) <EOL> elif self . _filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> self . _prepare_zip ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( self . _filename ) ) <EOL> def _safe_extractall ( self , to_path = '<STR_LIT:.>' ) : <EOL> unsafe = [ ] <EOL> for name in self . names : <EOL> if not self . is_safe ( name ) : <EOL> unsafe . append ( name ) <EOL> if unsafe : <EOL> raise ValueError ( "<STR_LIT>" . format ( unsafe ) ) <EOL> self . _file . extractall ( to_path ) <EOL> def _prepare_zip ( self ) : <EOL> self . _file = zipfile . ZipFile ( self . _fileobj ) <EOL> self . _names = self . _file . namelist <EOL> self . _read = self . _file . read <EOL> def _prepare_tarball ( self ) : <EOL> def _read ( filename ) : <EOL> f = self . _file . extractfile ( filename ) <EOL> return f . read ( ) <EOL> self . _file = tarfile . open ( mode = '<STR_LIT>' , fileobj = self . _fileobj ) <EOL> self . _names = self . _file . getnames <EOL> self . _read = _read <EOL> def is_safe ( self , filename ) : <EOL> return not ( filename . startswith ( ( "<STR_LIT:/>" , "<STR_LIT:\\>" ) ) or <EOL> ( len ( filename ) > <NUM_LIT:1> and filename [ <NUM_LIT:1> ] == "<STR_LIT::>" and <EOL> filename [ <NUM_LIT:0> ] in string . ascii_letter ) or <EOL> re . search ( r"<STR_LIT>" , filename ) ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . close ( ) <EOL> def top_level ( url , data ) : <EOL> """<STR_LIT>""" <EOL> sb = io . BytesIO ( data ) <EOL> txt = None <EOL> with Archive ( url , sb ) as archive : <EOL> file = None <EOL> for name in archive . names : <EOL> if name . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> file = name <EOL> break <EOL> if file : <EOL> txt = archive . read ( file ) . decode ( '<STR_LIT:utf-8>' ) <EOL> sb . close ( ) <EOL> return [ name . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) for name in txt . splitlines ( ) ] if txt else [ ] <EOL> def unpack_html ( data ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> sb = io . BytesIO ( data ) <EOL> gz = gzip . GzipFile ( fileobj = sb ) <EOL> data = gz . read ( ) <EOL> except Exception : <EOL> pass <EOL> finally : <EOL> gz . close ( ) <EOL> sb . close ( ) <EOL> return data . decode ( '<STR_LIT:utf-8>' ) </s>
