#NEWLINE# def unarchive( archive_filename , dest_dir , format = None , check = True ) : #NEWLINE# #INDENT# def check_path( path ) : #NEWLINE# #INDENT# if ( not isinstance ( path , text_type ) ) : #NEWLINE# #INDENT# path = path . decode ( 'utf-8' ) #NEWLINE# #UNINDENT# p = os . path . abspath ( os . path . join ( dest_dir , path ) ) #NEWLINE# if ( ( not p . startswith ( dest_dir ) ) or ( p [ plen ] != os . sep ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'path outside destination: %r' % p ) ) #NEWLINE# #UNINDENT# #UNINDENT# dest_dir = os . path . abspath ( dest_dir ) #NEWLINE# plen = len ( dest_dir ) #NEWLINE# archive = None #NEWLINE# if ( format is None ) : #NEWLINE# #INDENT# if archive_filename . endswith ( ( '.zip' , '.whl' ) ) : #NEWLINE# #INDENT# format = 'zip' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if archive_filename . endswith ( ( '.tar.gz' , '.tgz' ) ) : #NEWLINE# #INDENT# format = 'tgz' #NEWLINE# mode = 'r:gz' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if archive_filename . endswith ( ( '.tar.bz2' , '.tbz' ) ) : #NEWLINE# #INDENT# format = 'tbz' #NEWLINE# mode = 'r:bz2' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if archive_filename . endswith ( '.tar' ) : #NEWLINE# #INDENT# format = 'tar' #NEWLINE# mode = 'r' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'Unknown format for %r' % archive_filename ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# if ( format == 'zip' ) : #NEWLINE# #INDENT# archive = ZipFile ( archive_filename , 'r' ) #NEWLINE# if check : #NEWLINE# #INDENT# names = archive . namelist ( ) #NEWLINE# for name in names : #NEWLINE# #INDENT# check_path ( name ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# archive = tarfile . open ( archive_filename , mode ) #NEWLINE# if check : #NEWLINE# #INDENT# names = archive . getnames ( ) #NEWLINE# for name in names : #NEWLINE# #INDENT# check_path ( name ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( ( format != 'zip' ) and ( sys . version_info [ 0 ] < 3 ) ) : #NEWLINE# #INDENT# for tarinfo in archive . getmembers ( ) : #NEWLINE# #INDENT# if ( not isinstance ( tarinfo . name , text_type ) ) : #NEWLINE# #INDENT# tarinfo . name = tarinfo . name . decode ( 'utf-8' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# archive . extractall ( dest_dir ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if archive : #NEWLINE# #INDENT# archive . close ( )