<s> import os <EOL> import stat <EOL> import sys <EOL> from os . path import join , normcase , normpath , abspath , isabs , sep , dirname <EOL> from django . utils . encoding import force_text <EOL> from django . utils import six <EOL> try : <EOL> WindowsError = WindowsError <EOL> except NameError : <EOL> class WindowsError ( Exception ) : <EOL> pass <EOL> if not six . PY3 : <EOL> fs_encoding = sys . getfilesystemencoding ( ) or sys . getdefaultencoding ( ) <EOL> if six . PY3 or os . name == '<STR_LIT>' : <EOL> abspathu = abspath <EOL> else : <EOL> def abspathu ( path ) : <EOL> """<STR_LIT>""" <EOL> if not isabs ( path ) : <EOL> path = join ( os . getcwdu ( ) , path ) <EOL> return normpath ( path ) <EOL> def upath ( path ) : <EOL> """<STR_LIT>""" <EOL> if not six . PY3 : <EOL> return path . decode ( fs_encoding ) <EOL> return path <EOL> def npath ( path ) : <EOL> """<STR_LIT>""" <EOL> if not six . PY3 and not isinstance ( path , bytes ) : <EOL> return path . encode ( fs_encoding ) <EOL> return path <EOL> def safe_join ( base , * paths ) : <EOL> """<STR_LIT>""" <EOL> base = force_text ( base ) <EOL> paths = [ force_text ( p ) for p in paths ] <EOL> final_path = abspathu ( join ( base , * paths ) ) <EOL> base_path = abspathu ( base ) <EOL> if ( not normcase ( final_path ) . startswith ( normcase ( base_path + sep ) ) and <EOL> normcase ( final_path ) != normcase ( base_path ) and <EOL> dirname ( normcase ( base_path ) ) != normcase ( base_path ) ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( final_path , base_path ) ) <EOL> return final_path <EOL> def rmtree_errorhandler ( func , path , exc_info ) : <EOL> """<STR_LIT>""" <EOL> exctype , value = exc_info [ : <NUM_LIT:2> ] <EOL> if exctype is not WindowsError or '<STR_LIT>' not in str ( value ) : <EOL> raise <EOL> if ( ( os . stat ( path ) . st_mode & stat . S_IREAD ) != stat . S_IREAD ) : <EOL> raise <EOL> os . chmod ( path , stat . S_IWRITE ) <EOL> func ( path ) </s>
