#NEWLINE# def untar_file( filename , location ) : #NEWLINE# #INDENT# 'Untar the file (tar file located at filename) to the destination location' #NEWLINE# if ( not os . path . exists ( location ) ) : #NEWLINE# #INDENT# os . makedirs ( location ) #NEWLINE# #UNINDENT# if ( filename . lower ( ) . endswith ( '.gz' ) or filename . lower ( ) . endswith ( '.tgz' ) ) : #NEWLINE# #INDENT# mode = 'r:gz' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( filename . lower ( ) . endswith ( '.bz2' ) or filename . lower ( ) . endswith ( '.tbz' ) ) : #NEWLINE# #INDENT# mode = 'r:bz2' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if filename . lower ( ) . endswith ( '.tar' ) : #NEWLINE# #INDENT# mode = 'r' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logger . warn ( ( 'Cannot determine compression type for file %s' % filename ) ) #NEWLINE# mode = 'r:*' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# tar = tarfile . open ( filename , mode ) #NEWLINE# try : #NEWLINE# #INDENT# leading = has_leading_dir ( [ member . name for member in tar . getmembers ( ) if ( member . name != 'pax_global_header' ) ] ) #NEWLINE# for member in tar . getmembers ( ) : #NEWLINE# #INDENT# fn = fp . name #NEWLINE# if ( fn == 'pax_global_header' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if leading : #NEWLINE# #INDENT# fn = split_leading_dir ( fn ) [ 1 ] #NEWLINE# #UNINDENT# path = os . path . join ( location , fn ) #NEWLINE# if member . isdir ( ) : #NEWLINE# #INDENT# if ( not os . path . exists ( path ) ) : #NEWLINE# #INDENT# os . makedirs ( path ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if member . issym ( ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# tar . _extract_member ( member , path ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# e = sys . exc_info ( ) [ 1 ] #NEWLINE# logger . warn ( ( 'In the tar file %s the member %s is invalid: %s' % ( filename , member . name , e ) ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# fp = tar . extractfile ( member ) #NEWLINE# #UNINDENT# except ( KeyError , AttributeError ) : #NEWLINE# #INDENT# e = sys . exc_info ( ) [ 1 ] #NEWLINE# logger . warn ( ( 'In the tar file %s the member %s is invalid: %s' % ( filename , member . name , e ) ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( not os . path . exists ( os . path . dirname ( path ) ) ) : #NEWLINE# #INDENT# os . makedirs ( os . path . dirname ( path ) ) #NEWLINE# #UNINDENT# destfp = open ( path , 'wb' ) #NEWLINE# try : #NEWLINE# #INDENT# shutil . copyfileobj ( fp , destfp ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# destfp . close ( ) #NEWLINE# #UNINDENT# fp . close ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# tar . close ( )