<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import re <EOL> import sys <EOL> import stat <EOL> import errno <EOL> import fnmatch <EOL> from shutil import copy2 , copystat , Error <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> FULL_PERMS = <NUM_LIT> <EOL> RW_PERMS = <NUM_LIT> <EOL> _SINGLE_FULL_PERM = <NUM_LIT:7> <EOL> try : <EOL> basestring <EOL> except NameError : <EOL> unicode = str <EOL> basestring = ( str , bytes ) <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> return <EOL> raise <EOL> return <EOL> class FilePerms ( object ) : <EOL> """<STR_LIT>""" <EOL> class _FilePermProperty ( object ) : <EOL> _perm_chars = '<STR_LIT>' <EOL> _perm_set = frozenset ( '<STR_LIT>' ) <EOL> _perm_val = { '<STR_LIT:r>' : <NUM_LIT:4> , '<STR_LIT:w>' : <NUM_LIT:2> , '<STR_LIT:x>' : <NUM_LIT:1> } <EOL> def __init__ ( self , attribute , offset ) : <EOL> self . attribute = attribute <EOL> self . offset = offset <EOL> def __get__ ( self , fp_obj , type_ = None ) : <EOL> if fp_obj is None : <EOL> return self <EOL> return getattr ( fp_obj , self . attribute ) <EOL> def __set__ ( self , fp_obj , value ) : <EOL> cur = getattr ( fp_obj , self . attribute ) <EOL> if cur == value : <EOL> return <EOL> try : <EOL> invalid_chars = set ( str ( value ) ) - self . _perm_set <EOL> except TypeError : <EOL> raise TypeError ( '<STR_LIT>' % value ) <EOL> if invalid_chars : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( invalid_chars , value , self . _perm_chars ) ) <EOL> sort_key = lambda c : self . _perm_val [ c ] <EOL> new_value = '<STR_LIT>' . join ( sorted ( set ( value ) , <EOL> key = sort_key , reverse = True ) ) <EOL> setattr ( fp_obj , self . attribute , new_value ) <EOL> self . _update_integer ( fp_obj , new_value ) <EOL> def _update_integer ( self , fp_obj , value ) : <EOL> mode = <NUM_LIT:0> <EOL> key = '<STR_LIT>' <EOL> for symbol in value : <EOL> bit = <NUM_LIT:2> ** key . index ( symbol ) <EOL> mode |= ( bit << ( self . offset * <NUM_LIT:3> ) ) <EOL> fp_obj . _integer |= mode <EOL> def __init__ ( self , user = '<STR_LIT>' , group = '<STR_LIT>' , other = '<STR_LIT>' ) : <EOL> self . _user , self . _group , self . _other = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> self . _integer = <NUM_LIT:0> <EOL> self . user = user <EOL> self . group = group <EOL> self . other = other <EOL> @ classmethod <EOL> def from_int ( cls , i ) : <EOL> """<STR_LIT>""" <EOL> i &= FULL_PERMS <EOL> key = ( '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT:w>' , '<STR_LIT>' , '<STR_LIT:r>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> parts = [ ] <EOL> while i : <EOL> parts . append ( key [ i & _SINGLE_FULL_PERM ] ) <EOL> i >>= <NUM_LIT:3> <EOL> parts . reverse ( ) <EOL> return cls ( * parts ) <EOL> @ classmethod <EOL> def from_path ( cls , path ) : <EOL> """<STR_LIT>""" <EOL> stat_res = os . stat ( path ) <EOL> return cls . from_int ( stat . S_IMODE ( stat_res . st_mode ) ) <EOL> def __int__ ( self ) : <EOL> return self . _integer <EOL> user = _FilePermProperty ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> "<STR_LIT>" <EOL> group = _FilePermProperty ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> "<STR_LIT>" <EOL> other = _FilePermProperty ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> cn = self . __class__ . __name__ <EOL> return ( '<STR_LIT>' <EOL> % ( cn , self . user , self . group , self . other ) ) <EOL> _TEXT_OPENFLAGS = os . O_RDWR | os . O_CREAT | os . O_EXCL <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> _TEXT_OPENFLAGS |= os . O_NOINHERIT <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> _TEXT_OPENFLAGS |= os . O_NOFOLLOW <EOL> _BIN_OPENFLAGS = _TEXT_OPENFLAGS <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> _BIN_OPENFLAGS |= os . O_BINARY <EOL> try : <EOL> import fcntl as fcntl <EOL> except ImportError : <EOL> def set_cloexec ( fd ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> else : <EOL> def set_cloexec ( fd ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> flags = fcntl . fcntl ( fd , fcntl . F_GETFD , <NUM_LIT:0> ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> flags |= fcntl . FD_CLOEXEC <EOL> fcntl . fcntl ( fd , fcntl . F_SETFD , flags ) <EOL> return <EOL> def atomic_save ( dest_path , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return AtomicSaver ( dest_path , ** kwargs ) <EOL> def path_to_unicode ( path ) : <EOL> if isinstance ( path , unicode ) : <EOL> return path <EOL> encoding = sys . getfilesystemencoding ( ) or sys . getdefaultencoding ( ) <EOL> return path . decode ( encoding ) <EOL> if os . name == '<STR_LIT>' : <EOL> import ctypes <EOL> from ctypes import c_wchar_p <EOL> from ctypes . wintypes import DWORD , LPVOID <EOL> _ReplaceFile = ctypes . windll . kernel32 . ReplaceFile <EOL> _ReplaceFile . argtypes = [ c_wchar_p , c_wchar_p , c_wchar_p , <EOL> DWORD , LPVOID , LPVOID ] <EOL> def replace ( src , dst ) : <EOL> try : <EOL> os . rename ( src , dst ) <EOL> return <EOL> except WindowsError as we : <EOL> if we . errno == errno . EEXIST : <EOL> pass <EOL> else : <EOL> raise <EOL> src = path_to_unicode ( src ) <EOL> dst = path_to_unicode ( dst ) <EOL> bak = dst + u'<STR_LIT>' <EOL> res = _ReplaceFile ( c_wchar_p ( dst ) , c_wchar_p ( src ) , c_wchar_p ( bak ) , <EOL> <NUM_LIT:0> , None , None ) <EOL> if not res : <EOL> raise OSError ( '<STR_LIT>' % ( dst , src ) ) <EOL> return <EOL> def atomic_rename ( src , dst , overwrite = False ) : <EOL> "<STR_LIT>" <EOL> if overwrite : <EOL> replace ( src , dst ) <EOL> else : <EOL> os . rename ( src , dst ) <EOL> return <EOL> else : <EOL> def replace ( src , dst ) : <EOL> return os . rename ( src , dst ) <EOL> def atomic_rename ( src , dst , overwrite = False ) : <EOL> "<STR_LIT>" <EOL> if overwrite : <EOL> os . rename ( src , dst ) <EOL> else : <EOL> os . link ( src , dst ) <EOL> os . unlink ( dst ) <EOL> return <EOL> _atomic_rename = atomic_rename <EOL> replace . __doc__ = """<STR_LIT>""" <EOL> class AtomicSaver ( object ) : <EOL> """<STR_LIT>""" <EOL> _default_file_perms = RW_PERMS <EOL> def __init__ ( self , dest_path , ** kwargs ) : <EOL> self . dest_path = dest_path <EOL> self . overwrite = kwargs . pop ( '<STR_LIT>' , True ) <EOL> self . file_perms = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . overwrite_part = kwargs . pop ( '<STR_LIT>' , False ) <EOL> self . part_filename = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . rm_part_on_exc = kwargs . pop ( '<STR_LIT>' , True ) <EOL> self . text_mode = kwargs . pop ( '<STR_LIT>' , False ) <EOL> self . buffering = kwargs . pop ( '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> if kwargs : <EOL> raise TypeError ( '<STR_LIT>' % ( kwargs . keys ( ) , ) ) <EOL> self . dest_path = os . path . abspath ( self . dest_path ) <EOL> self . dest_dir = os . path . dirname ( self . dest_path ) <EOL> if not self . part_filename : <EOL> self . part_path = dest_path + '<STR_LIT>' <EOL> else : <EOL> self . part_path = os . path . join ( self . dest_dir , self . part_filename ) <EOL> self . mode = '<STR_LIT>' if self . text_mode else '<STR_LIT>' <EOL> self . open_flags = _TEXT_OPENFLAGS if self . text_mode else _BIN_OPENFLAGS <EOL> self . part_file = None <EOL> def _open_part_file ( self ) : <EOL> do_chmod = True <EOL> file_perms = self . file_perms <EOL> if file_perms is None : <EOL> try : <EOL> stat_res = os . stat ( self . dest_path ) <EOL> file_perms = stat . S_IMODE ( stat_res . st_mode ) <EOL> except ( OSError , IOError ) : <EOL> file_perms = self . _default_file_perms <EOL> do_chmod = False <EOL> fd = os . open ( self . part_path , self . open_flags , file_perms ) <EOL> set_cloexec ( fd ) <EOL> self . part_file = os . fdopen ( fd , self . mode , self . buffering ) <EOL> if do_chmod : <EOL> try : <EOL> os . chmod ( self . part_path , file_perms ) <EOL> except ( OSError , IOError ) : <EOL> self . part_file . close ( ) <EOL> raise <EOL> return <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . lexists ( self . dest_path ) : <EOL> if not self . overwrite : <EOL> raise OSError ( errno . EEXIST , <EOL> '<STR_LIT>' , <EOL> self . dest_path ) <EOL> if self . overwrite_part and os . path . lexists ( self . part_path ) : <EOL> os . unlink ( self . part_path ) <EOL> self . _open_part_file ( ) <EOL> return <EOL> def __enter__ ( self ) : <EOL> self . setup ( ) <EOL> return self . part_file <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> self . part_file . close ( ) <EOL> if exc_type : <EOL> if self . rm_part_on_exc : <EOL> try : <EOL> os . unlink ( self . part_path ) <EOL> except Exception : <EOL> pass <EOL> return <EOL> try : <EOL> atomic_rename ( self . part_path , self . dest_path , <EOL> overwrite = self . overwrite ) <EOL> except OSError : <EOL> if self . rm_part_on_exc : <EOL> try : <EOL> os . unlink ( self . part_path ) <EOL> except Exception : <EOL> pass <EOL> raise <EOL> return <EOL> _CUR_DIR = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> def iter_find_files ( directory , patterns , ignored = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( patterns , basestring ) : <EOL> patterns = [ patterns ] <EOL> pats_re = re . compile ( '<STR_LIT:|>' . join ( [ fnmatch . translate ( p ) for p in patterns ] ) ) <EOL> if not ignored : <EOL> ignored = [ ] <EOL> elif isinstance ( ignored , basestring ) : <EOL> ignored = [ ignored ] <EOL> ign_re = re . compile ( '<STR_LIT:|>' . join ( [ fnmatch . translate ( p ) for p in ignored ] ) ) <EOL> for root , dirs , files in os . walk ( directory ) : <EOL> for basename in files : <EOL> if pats_re . match ( basename ) : <EOL> if ignored and ign_re . match ( basename ) : <EOL> continue <EOL> filename = os . path . join ( root , basename ) <EOL> yield filename <EOL> return <EOL> def copy_tree ( src , dst , symlinks = False , ignore = None ) : <EOL> """<STR_LIT>""" <EOL> names = os . listdir ( src ) <EOL> if ignore is not None : <EOL> ignored_names = ignore ( src , names ) <EOL> else : <EOL> ignored_names = set ( ) <EOL> mkdir_p ( dst ) <EOL> errors = [ ] <EOL> for name in names : <EOL> if name in ignored_names : <EOL> continue <EOL> srcname = os . path . join ( src , name ) <EOL> dstname = os . path . join ( dst , name ) <EOL> try : <EOL> if symlinks and os . path . islink ( srcname ) : <EOL> linkto = os . readlink ( srcname ) <EOL> os . symlink ( linkto , dstname ) <EOL> elif os . path . isdir ( srcname ) : <EOL> copytree ( srcname , dstname , symlinks , ignore ) <EOL> else : <EOL> copy2 ( srcname , dstname ) <EOL> except Error as e : <EOL> errors . extend ( e . args [ <NUM_LIT:0> ] ) <EOL> except EnvironmentError as why : <EOL> errors . append ( ( srcname , dstname , str ( why ) ) ) <EOL> try : <EOL> copystat ( src , dst ) <EOL> except OSError as why : <EOL> if WindowsError is not None and isinstance ( why , WindowsError ) : <EOL> pass <EOL> else : <EOL> errors . append ( ( src , dst , str ( why ) ) ) <EOL> if errors : <EOL> raise Error ( errors ) <EOL> copytree = copy_tree <EOL> try : <EOL> file <EOL> except NameError : <EOL> file = object <EOL> class DummyFile ( file ) : <EOL> def __init__ ( self , path , mode = '<STR_LIT:r>' , buffering = None ) : <EOL> self . name = path <EOL> self . mode = mode <EOL> self . closed = False <EOL> self . errors = None <EOL> self . isatty = False <EOL> self . encoding = None <EOL> self . newlines = None <EOL> self . softspace = <NUM_LIT:0> <EOL> def close ( self ) : <EOL> self . closed = True <EOL> def fileno ( self ) : <EOL> return - <NUM_LIT:1> <EOL> def flush ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return <EOL> def next ( self ) : <EOL> raise StopIteration ( ) <EOL> def read ( self , size = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def readline ( self , size = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def readlines ( self , size = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return [ ] <EOL> def seek ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return <EOL> def tell ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return <NUM_LIT:0> <EOL> def truncate ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return <EOL> def write ( self , string ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return <EOL> def writelines ( self , list_of_strings ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return <EOL> def __next__ ( self ) : <EOL> raise StopIteration ( ) <EOL> def __enter__ ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> return <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> with atomic_save ( '<STR_LIT>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT:\n>' ) </s>
