<s> import os <EOL> import shutil <EOL> from errno import ENOENT , EEXIST <EOL> import hashlib <EOL> import sys <EOL> from os . path import abspath , realpath , join as joinpath <EOL> import platform <EOL> import re <EOL> import six <EOL> from conans . util . log import logger <EOL> import tarfile <EOL> def decode_text ( text ) : <EOL> decoders = [ "<STR_LIT:utf-8>" , "<STR_LIT>" ] <EOL> for decoder in decoders : <EOL> try : <EOL> return text . decode ( decoder ) <EOL> except UnicodeDecodeError : <EOL> continue <EOL> logger . warn ( "<STR_LIT>" % str ( text ) ) <EOL> return text . decode ( "<STR_LIT:utf-8>" , "<STR_LIT:ignore>" ) <EOL> def touch ( fname , times = None ) : <EOL> with open ( fname , '<STR_LIT:a>' ) : <EOL> os . utime ( fname , times ) <EOL> def delete_empty_dirs ( folder ) : <EOL> for root , _ , _ in os . walk ( folder , topdown = False ) : <EOL> try : <EOL> os . rmdir ( root ) <EOL> except OSError : <EOL> pass <EOL> def normalize ( text ) : <EOL> if platform . system ( ) == "<STR_LIT>" : <EOL> return re . sub ( "<STR_LIT>" , "<STR_LIT:\r\n>" , text ) <EOL> else : <EOL> return text <EOL> def md5 ( content ) : <EOL> md5alg = hashlib . md5 ( ) <EOL> if isinstance ( content , bytes ) : <EOL> tmp = content <EOL> else : <EOL> tmp = content . encode ( ) <EOL> md5alg . update ( tmp ) <EOL> return md5alg . hexdigest ( ) <EOL> def md5sum ( file_path ) : <EOL> return _generic_algorithm_sum ( file_path , "<STR_LIT>" ) <EOL> def _generic_algorithm_sum ( file_path , algorithm_name ) : <EOL> with open ( file_path , '<STR_LIT:rb>' ) as fh : <EOL> m = hashlib . new ( algorithm_name ) <EOL> while True : <EOL> data = fh . read ( <NUM_LIT> ) <EOL> if not data : <EOL> break <EOL> m . update ( data ) <EOL> return m . hexdigest ( ) <EOL> def save ( path , content ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> os . makedirs ( os . path . dirname ( path ) ) <EOL> except : <EOL> pass <EOL> if six . PY3 : <EOL> if not isinstance ( content , bytes ) : <EOL> content = bytes ( content , "<STR_LIT:utf-8>" ) <EOL> with open ( path , '<STR_LIT:wb>' ) as handle : <EOL> handle . write ( content ) <EOL> def save_files ( path , files ) : <EOL> for name , content in list ( files . items ( ) ) : <EOL> save ( os . path . join ( path , name ) , content ) <EOL> def load ( path , binary = False ) : <EOL> '''<STR_LIT>''' <EOL> with open ( path , '<STR_LIT:rb>' ) as handle : <EOL> tmp = handle . read ( ) <EOL> return tmp if binary else decode_text ( tmp ) <EOL> def build_files_set ( basedir , rel_files ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> for filename in rel_files : <EOL> abs_path = os . path . join ( basedir , filename ) <EOL> ret [ filename ] = { <EOL> "<STR_LIT>" : load ( abs_path , binary = True ) , <EOL> "<STR_LIT>" : os . stat ( abs_path ) . st_mode <EOL> } <EOL> return ret <EOL> def relative_dirs ( path ) : <EOL> '''<STR_LIT>''' <EOL> ret = [ ] <EOL> for dirpath , _ , fnames in os . walk ( path ) : <EOL> for filename in fnames : <EOL> tmp = os . path . join ( dirpath , filename ) <EOL> tmp = tmp [ len ( path ) + <NUM_LIT:1> : ] <EOL> ret . append ( tmp ) <EOL> return ret <EOL> def _change_permissions ( func , path , exc_info ) : <EOL> import stat <EOL> if not os . access ( path , os . W_OK ) : <EOL> os . chmod ( path , stat . S_IWUSR ) <EOL> func ( path ) <EOL> else : <EOL> raise <EOL> def rmdir ( path , raise_if_not_exist = False ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> shutil . rmtree ( path , onerror = _change_permissions ) <EOL> except OSError as err : <EOL> if err . errno == ENOENT and not raise_if_not_exist : <EOL> return <EOL> raise <EOL> def mkdir ( path , raise_if_already_exists = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . makedirs ( path ) <EOL> except OSError as err : <EOL> if err . errno == EEXIST and not raise_if_already_exists : <EOL> return <EOL> raise <EOL> def path_exists ( path , basedir = None ) : <EOL> """<STR_LIT>""" <EOL> exists = os . path . exists ( path ) <EOL> if not exists or sys . platform == "<STR_LIT>" : <EOL> return exists <EOL> path = os . path . normpath ( path ) <EOL> if basedir : <EOL> path = os . path . relpath ( path , basedir ) <EOL> chunks = path . split ( os . sep ) <EOL> tmp = basedir <EOL> else : <EOL> chunks = path . split ( os . sep ) <EOL> tmp = chunks [ <NUM_LIT:0> ] <EOL> chunks = chunks [ <NUM_LIT:1> : ] <EOL> for chunk in chunks [ <NUM_LIT:0> : ] : <EOL> tmp = tmp + os . sep <EOL> if chunk and chunk not in os . listdir ( tmp ) : <EOL> return False <EOL> tmp += chunk <EOL> return True <EOL> def gzopen_without_timestamps ( name , mode = "<STR_LIT:r>" , fileobj = None , compresslevel = <NUM_LIT:9> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from tarfile import CompressionError , ReadError <EOL> if mode not in ( "<STR_LIT:r>" , "<STR_LIT:w>" ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> import gzip <EOL> gzip . GzipFile <EOL> except ( ImportError , AttributeError ) : <EOL> raise CompressionError ( "<STR_LIT>" ) <EOL> try : <EOL> fileobj = gzip . GzipFile ( name , mode , compresslevel , fileobj , mtime = <NUM_LIT:0> ) <EOL> except OSError : <EOL> if fileobj is not None and mode == '<STR_LIT:r>' : <EOL> raise ReadError ( "<STR_LIT>" ) <EOL> raise <EOL> try : <EOL> t = tarfile . TarFile . taropen ( name , mode , fileobj , ** kwargs ) <EOL> except IOError : <EOL> fileobj . close ( ) <EOL> if mode == '<STR_LIT:r>' : <EOL> raise ReadError ( "<STR_LIT>" ) <EOL> raise <EOL> except : <EOL> fileobj . close ( ) <EOL> raise <EOL> t . _extfileobj = False <EOL> return t <EOL> def tar_extract ( fileobj , destination_dir ) : <EOL> '''<STR_LIT>''' <EOL> def badpath ( path , base ) : <EOL> return not realpath ( abspath ( joinpath ( base , path ) ) ) . startswith ( base ) <EOL> def safemembers ( members ) : <EOL> base = realpath ( abspath ( "<STR_LIT:.>" ) ) <EOL> for finfo in members : <EOL> if badpath ( finfo . name , base ) or finfo . issym ( ) or finfo . islnk ( ) : <EOL> continue <EOL> else : <EOL> finfo . name = finfo . name . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) <EOL> yield finfo <EOL> the_tar = tarfile . open ( fileobj = fileobj ) <EOL> the_tar . extractall ( path = destination_dir , members = safemembers ( the_tar ) ) <EOL> the_tar . close ( ) </s>
