<s> from __future__ import print_function , unicode_literals , absolute_import <EOL> import os <EOL> import os . path <EOL> import shutil <EOL> import errno <EOL> import logging <EOL> import io <EOL> import hashlib <EOL> import simiki <EOL> from simiki . compat import unicode <EOL> logger = logging . getLogger ( __name__ ) <EOL> COLOR_CODES = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> def color_msg ( color , msg ) : <EOL> return COLOR_CODES [ color ] + msg + COLOR_CODES [ "<STR_LIT>" ] <EOL> def check_extension ( filename ) : <EOL> """<STR_LIT>""" <EOL> exts = [ '<STR_LIT>' . format ( e ) for e in simiki . allowed_extensions ] <EOL> return os . path . splitext ( filename ) [ <NUM_LIT:1> ] in exts <EOL> def copytree ( src , dst , symlinks = False , ignore = None ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( dst ) : <EOL> os . makedirs ( dst ) <EOL> for item in os . listdir ( src ) : <EOL> s = os . path . join ( src , item ) <EOL> d = os . path . join ( dst , item ) <EOL> if os . path . isdir ( s ) : <EOL> copytree ( s , d , symlinks , ignore ) <EOL> else : <EOL> shutil . copy2 ( s , d ) <EOL> def emptytree ( directory , exclude_list = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( directory , unicode ) : <EOL> directory = unicode ( directory , '<STR_LIT:utf-8>' ) <EOL> if not exclude_list : <EOL> exclude_list = [ ] <EOL> for p in os . listdir ( directory ) : <EOL> if p in exclude_list : <EOL> continue <EOL> fp = os . path . join ( directory , p ) <EOL> if os . path . isdir ( fp ) : <EOL> try : <EOL> shutil . rmtree ( fp ) <EOL> logger . debug ( "<STR_LIT>" , fp ) <EOL> except OSError as e : <EOL> logger . error ( "<STR_LIT>" , <EOL> fp , unicode ( e ) ) <EOL> elif os . path . isfile ( fp ) : <EOL> try : <EOL> logging . debug ( "<STR_LIT>" , fp ) <EOL> os . remove ( fp ) <EOL> except OSError as e : <EOL> logger . error ( "<STR_LIT>" , fp , unicode ( e ) ) <EOL> else : <EOL> logger . error ( "<STR_LIT>" , fp ) <EOL> def mkdir_p ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . makedirs ( path ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . EEXIST and os . path . isdir ( path ) : <EOL> pass <EOL> else : <EOL> raise <EOL> def listdir_nohidden ( path ) : <EOL> """<STR_LIT>""" <EOL> for f in os . listdir ( path ) : <EOL> if isinstance ( f , str ) : <EOL> f = unicode ( f ) <EOL> if not f . startswith ( '<STR_LIT:.>' ) : <EOL> yield f <EOL> def write_file ( filename , content ) : <EOL> """<STR_LIT>""" <EOL> _dir , _ = os . path . split ( filename ) <EOL> if not os . path . exists ( _dir ) : <EOL> logging . debug ( "<STR_LIT>" , _dir ) <EOL> mkdir_p ( _dir ) <EOL> with io . open ( filename , "<STR_LIT>" , encoding = "<STR_LIT:utf-8>" ) as fd : <EOL> fd . write ( content ) <EOL> def get_md5 ( filename ) : <EOL> with open ( filename , '<STR_LIT:rb>' ) as fd : <EOL> md5_hash = hashlib . md5 ( fd . read ( ) ) . hexdigest ( ) <EOL> return md5_hash <EOL> def get_dir_md5 ( dirname ) : <EOL> '''<STR_LIT>''' <EOL> md5_hash = hashlib . md5 ( ) <EOL> for root , dirs , files in os . walk ( dirname ) : <EOL> dirs [ : ] = sorted ( dirs ) <EOL> for f in sorted ( files ) : <EOL> with open ( os . path . join ( root , f ) , '<STR_LIT:rb>' ) as fd : <EOL> md5_hash . update ( fd . read ( ) ) <EOL> md5_hash = md5_hash . hexdigest ( ) <EOL> return md5_hash <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> print ( color_msg ( "<STR_LIT>" , "<STR_LIT>" ) ) </s>
