<s> """<STR_LIT>""" <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ] <EOL> import os as _os <EOL> import errno as _errno <EOL> from random import Random as _Random <EOL> if _os . name == '<STR_LIT>' : <EOL> import Carbon . Folder as _Folder <EOL> import Carbon . Folders as _Folders <EOL> try : <EOL> import fcntl as _fcntl <EOL> except ImportError : <EOL> def _set_cloexec ( fd ) : <EOL> pass <EOL> else : <EOL> def _set_cloexec ( fd ) : <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> try : <EOL> import thread as _thread <EOL> except ImportError : <EOL> import dummy_thread as _thread <EOL> _allocate_lock = _thread . allocate_lock <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> if hasattr ( _os , '<STR_LIT>' ) : <EOL> TMP_MAX = _os . TMP_MAX <EOL> else : <EOL> TMP_MAX = <NUM_LIT> <EOL> template = "<STR_LIT>" <EOL> tempdir = None <EOL> _once_lock = _allocate_lock ( ) <EOL> if hasattr ( _os , "<STR_LIT>" ) : <EOL> _stat = _os . lstat <EOL> elif hasattr ( _os , "<STR_LIT>" ) : <EOL> _stat = _os . stat <EOL> else : <EOL> def _stat ( fn ) : <EOL> try : <EOL> f = open ( fn ) <EOL> except IOError : <EOL> raise _os . error <EOL> f . close ( ) <EOL> def _exists ( fn ) : <EOL> try : <EOL> _stat ( fn ) <EOL> except _os . error : <EOL> return False <EOL> else : <EOL> return True <EOL> class _RandomNameSequence : <EOL> """<STR_LIT>""" <EOL> characters = ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self ) : <EOL> self . mutex = _allocate_lock ( ) <EOL> self . rng = _Random ( ) <EOL> self . normcase = _os . path . normcase <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> m = self . mutex <EOL> c = self . characters <EOL> choose = self . rng . choice <EOL> m . acquire ( ) <EOL> try : <EOL> letters = [ choose ( c ) for dummy in "<STR_LIT>" ] <EOL> finally : <EOL> m . release ( ) <EOL> return self . normcase ( '<STR_LIT>' . join ( letters ) ) <EOL> def _candidate_tempdir_list ( ) : <EOL> """<STR_LIT>""" <EOL> dirlist = [ ] <EOL> for envname in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> dirname = _os . getenv ( envname ) <EOL> if dirname : dirlist . append ( dirname ) <EOL> if _os . name == '<STR_LIT>' : <EOL> try : <EOL> fsr = _Folder . FSFindFolder ( _Folders . kOnSystemDisk , <EOL> _Folders . kTemporaryFolderType , <NUM_LIT:1> ) <EOL> dirname = fsr . as_pathname ( ) <EOL> dirlist . append ( dirname ) <EOL> except _Folder . error : <EOL> pass <EOL> elif _os . name == '<STR_LIT>' : <EOL> dirname = _os . getenv ( '<STR_LIT>' ) <EOL> if dirname : dirlist . append ( dirname ) <EOL> elif _os . name == '<STR_LIT>' : <EOL> dirlist . extend ( [ r'<STR_LIT>' , r'<STR_LIT>' , r'<STR_LIT>' , r'<STR_LIT>' ] ) <EOL> else : <EOL> dirlist . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> dirlist . append ( _os . getcwd ( ) ) <EOL> except ( AttributeError , _os . error ) : <EOL> dirlist . append ( _os . curdir ) <EOL> return dirlist <EOL> def _get_default_tempdir ( ) : <EOL> """<STR_LIT>""" <EOL> namer = _RandomNameSequence ( ) <EOL> dirlist = _candidate_tempdir_list ( ) <EOL> flags = _text_openflags <EOL> for dir in dirlist : <EOL> if dir != _os . curdir : <EOL> dir = _os . path . normcase ( _os . path . abspath ( dir ) ) <EOL> for seq in xrange ( <NUM_LIT:100> ) : <EOL> name = namer . next ( ) <EOL> filename = _os . path . join ( dir , name ) <EOL> try : <EOL> fd = _os . open ( filename , flags , <NUM_LIT:0> <NUM_LIT> ) <EOL> fp = _os . fdopen ( fd , '<STR_LIT:w>' ) <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . close ( ) <EOL> _os . unlink ( filename ) <EOL> del fp , fd <EOL> return dir <EOL> except ( OSError , IOError ) , e : <EOL> if e [ <NUM_LIT:0> ] != _errno . EEXIST : <EOL> break <EOL> pass <EOL> raise IOError , ( _errno . ENOENT , <EOL> ( "<STR_LIT>" % dirlist ) ) <EOL> _name_sequence = None <EOL> def _get_candidate_names ( ) : <EOL> """<STR_LIT>""" <EOL> global _name_sequence <EOL> if _name_sequence is None : <EOL> _once_lock . acquire ( ) <EOL> try : <EOL> if _name_sequence is None : <EOL> _name_sequence = _RandomNameSequence ( ) <EOL> finally : <EOL> _once_lock . release ( ) <EOL> return _name_sequence <EOL> def _mkstemp_inner ( dir , pre , suf , flags ) : <EOL> """<STR_LIT>""" <EOL> names = _get_candidate_names ( ) <EOL> for seq in xrange ( TMP_MAX ) : <EOL> name = names . next ( ) <EOL> file = _os . path . join ( dir , pre + name + suf ) <EOL> try : <EOL> fd = _os . open ( file , flags , <NUM_LIT:0> <NUM_LIT> ) <EOL> _set_cloexec ( fd ) <EOL> return ( fd , _os . path . abspath ( file ) ) <EOL> except OSError , e : <EOL> if e . errno == _errno . EEXIST : <EOL> continue <EOL> raise <EOL> raise IOError , ( _errno . EEXIST , "<STR_LIT>" ) <EOL> def gettempprefix ( ) : <EOL> """<STR_LIT>""" <EOL> return template <EOL> tempdir = None <EOL> def gettempdir ( ) : <EOL> """<STR_LIT>""" <EOL> global tempdir <EOL> if tempdir is None : <EOL> _once_lock . acquire ( ) <EOL> try : <EOL> if tempdir is None : <EOL> tempdir = _get_default_tempdir ( ) <EOL> finally : <EOL> _once_lock . release ( ) <EOL> return tempdir <EOL> def mkstemp ( suffix = "<STR_LIT>" , prefix = template , dir = None , text = False ) : <EOL> """<STR_LIT>""" <EOL> if dir is None : <EOL> dir = gettempdir ( ) <EOL> if text : <EOL> flags = _text_openflags <EOL> else : <EOL> flags = _bin_openflags <EOL> return _mkstemp_inner ( dir , prefix , suffix , flags ) <EOL> def mkdtemp ( suffix = "<STR_LIT>" , prefix = template , dir = None ) : <EOL> """<STR_LIT>""" <EOL> if dir is None : <EOL> dir = gettempdir ( ) <EOL> names = _get_candidate_names ( ) <EOL> for seq in xrange ( TMP_MAX ) : <EOL> name = names . next ( ) <EOL> file = _os . path . join ( dir , prefix + name + suffix ) <EOL> try : <EOL> _os . mkdir ( file , <NUM_LIT:0> <NUM_LIT> ) <EOL> return file <EOL> except OSError , e : <EOL> if e . errno == _errno . EEXIST : <EOL> continue <EOL> raise <EOL> raise IOError , ( _errno . EEXIST , "<STR_LIT>" ) <EOL> def mktemp ( suffix = "<STR_LIT>" , prefix = template , dir = None ) : <EOL> """<STR_LIT>""" <EOL> if dir is None : <EOL> dir = gettempdir ( ) <EOL> names = _get_candidate_names ( ) <EOL> for seq in xrange ( TMP_MAX ) : <EOL> name = names . next ( ) <EOL> file = _os . path . join ( dir , prefix + name + suffix ) <EOL> if not _exists ( file ) : <EOL> return file <EOL> raise IOError , ( _errno . EEXIST , "<STR_LIT>" ) <EOL> class _TemporaryFileWrapper : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file , name ) : <EOL> self . file = file <EOL> self . name = name <EOL> self . close_called = False <EOL> def __getattr__ ( self , name ) : <EOL> file = self . __dict__ [ '<STR_LIT:file>' ] <EOL> a = getattr ( file , name ) <EOL> if type ( a ) != type ( <NUM_LIT:0> ) : <EOL> setattr ( self , name , a ) <EOL> return a <EOL> if _os . name != '<STR_LIT>' : <EOL> unlink = _os . unlink <EOL> def close ( self ) : <EOL> if not self . close_called : <EOL> self . close_called = True <EOL> self . file . close ( ) <EOL> self . unlink ( self . name ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def NamedTemporaryFile ( mode = '<STR_LIT>' , bufsize = - <NUM_LIT:1> , suffix = "<STR_LIT>" , <EOL> prefix = template , dir = None ) : <EOL> """<STR_LIT>""" <EOL> if dir is None : <EOL> dir = gettempdir ( ) <EOL> if '<STR_LIT:b>' in mode : <EOL> flags = _bin_openflags <EOL> else : <EOL> flags = _text_openflags <EOL> if _os . name == '<STR_LIT>' : <EOL> flags |= _os . O_TEMPORARY <EOL> ( fd , name ) = _mkstemp_inner ( dir , prefix , suffix , flags ) <EOL> file = _os . fdopen ( fd , mode , bufsize ) <EOL> return _TemporaryFileWrapper ( file , name ) <EOL> if _os . name != '<STR_LIT>' or _os . sys . platform == '<STR_LIT>' : <EOL> TemporaryFile = NamedTemporaryFile <EOL> else : <EOL> def TemporaryFile ( mode = '<STR_LIT>' , bufsize = - <NUM_LIT:1> , suffix = "<STR_LIT>" , <EOL> prefix = template , dir = None ) : <EOL> """<STR_LIT>""" <EOL> if dir is None : <EOL> dir = gettempdir ( ) <EOL> if '<STR_LIT:b>' in mode : <EOL> flags = _bin_openflags <EOL> else : <EOL> flags = _text_openflags <EOL> ( fd , name ) = _mkstemp_inner ( dir , prefix , suffix , flags ) <EOL> try : <EOL> _os . unlink ( name ) <EOL> return _os . fdopen ( fd , mode , bufsize ) <EOL> except : <EOL> _os . close ( fd ) <EOL> raise </s>
