<s> import os , sys , __builtin__ , tempfile , operator , pkg_resources <EOL> _os = sys . modules [ os . name ] <EOL> _open = open <EOL> _file = file <EOL> from distutils . errors import DistutilsError <EOL> from pkg_resources import working_set <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> ] <EOL> def run_setup ( setup_script , args ) : <EOL> """<STR_LIT>""" <EOL> old_dir = os . getcwd ( ) <EOL> save_argv = sys . argv [ : ] <EOL> save_path = sys . path [ : ] <EOL> setup_dir = os . path . abspath ( os . path . dirname ( setup_script ) ) <EOL> temp_dir = os . path . join ( setup_dir , '<STR_LIT>' ) <EOL> if not os . path . isdir ( temp_dir ) : os . makedirs ( temp_dir ) <EOL> save_tmp = tempfile . tempdir <EOL> save_modules = sys . modules . copy ( ) <EOL> pr_state = pkg_resources . __getstate__ ( ) <EOL> try : <EOL> tempfile . tempdir = temp_dir ; os . chdir ( setup_dir ) <EOL> try : <EOL> sys . argv [ : ] = [ setup_script ] + list ( args ) <EOL> sys . path . insert ( <NUM_LIT:0> , setup_dir ) <EOL> working_set . __init__ ( ) <EOL> working_set . callbacks . append ( lambda dist : dist . activate ( ) ) <EOL> DirectorySandbox ( setup_dir ) . run ( <EOL> lambda : execfile ( <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT>' : setup_script , '<STR_LIT>' : '<STR_LIT:__main__>' } <EOL> ) <EOL> ) <EOL> except SystemExit , v : <EOL> if v . args and v . args [ <NUM_LIT:0> ] : <EOL> raise <EOL> finally : <EOL> pkg_resources . __setstate__ ( pr_state ) <EOL> sys . modules . update ( save_modules ) <EOL> for key in list ( sys . modules ) : <EOL> if key not in save_modules : del sys . modules [ key ] <EOL> os . chdir ( old_dir ) <EOL> sys . path [ : ] = save_path <EOL> sys . argv [ : ] = save_argv <EOL> tempfile . tempdir = save_tmp <EOL> class AbstractSandbox : <EOL> """<STR_LIT>""" <EOL> _active = False <EOL> def __init__ ( self ) : <EOL> self . _attrs = [ <EOL> name for name in dir ( _os ) <EOL> if not name . startswith ( '<STR_LIT:_>' ) and hasattr ( self , name ) <EOL> ] <EOL> def _copy ( self , source ) : <EOL> for name in self . _attrs : <EOL> setattr ( os , name , getattr ( source , name ) ) <EOL> def run ( self , func ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _copy ( self ) <EOL> __builtin__ . file = self . _file <EOL> __builtin__ . open = self . _open <EOL> self . _active = True <EOL> return func ( ) <EOL> finally : <EOL> self . _active = False <EOL> __builtin__ . open = _file <EOL> __builtin__ . file = _open <EOL> self . _copy ( _os ) <EOL> def _mk_dual_path_wrapper ( name ) : <EOL> original = getattr ( _os , name ) <EOL> def wrap ( self , src , dst , * args , ** kw ) : <EOL> if self . _active : <EOL> src , dst = self . _remap_pair ( name , src , dst , * args , ** kw ) <EOL> return original ( src , dst , * args , ** kw ) <EOL> return wrap <EOL> for name in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if hasattr ( _os , name ) : locals ( ) [ name ] = _mk_dual_path_wrapper ( name ) <EOL> def _mk_single_path_wrapper ( name , original = None ) : <EOL> original = original or getattr ( _os , name ) <EOL> def wrap ( self , path , * args , ** kw ) : <EOL> if self . _active : <EOL> path = self . _remap_input ( name , path , * args , ** kw ) <EOL> return original ( path , * args , ** kw ) <EOL> return wrap <EOL> _open = _mk_single_path_wrapper ( '<STR_LIT>' , _open ) <EOL> _file = _mk_single_path_wrapper ( '<STR_LIT:file>' , _file ) <EOL> for name in [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ] : <EOL> if hasattr ( _os , name ) : locals ( ) [ name ] = _mk_single_path_wrapper ( name ) <EOL> def _mk_single_with_return ( name ) : <EOL> original = getattr ( _os , name ) <EOL> def wrap ( self , path , * args , ** kw ) : <EOL> if self . _active : <EOL> path = self . _remap_input ( name , path , * args , ** kw ) <EOL> return self . _remap_output ( name , original ( path , * args , ** kw ) ) <EOL> return original ( path , * args , ** kw ) <EOL> return wrap <EOL> for name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if hasattr ( _os , name ) : locals ( ) [ name ] = _mk_single_with_return ( name ) <EOL> def _mk_query ( name ) : <EOL> original = getattr ( _os , name ) <EOL> def wrap ( self , * args , ** kw ) : <EOL> retval = original ( * args , ** kw ) <EOL> if self . _active : <EOL> return self . _remap_output ( name , retval ) <EOL> return retval <EOL> return wrap <EOL> for name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if hasattr ( _os , name ) : locals ( ) [ name ] = _mk_query ( name ) <EOL> def _validate_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return path <EOL> def _remap_input ( self , operation , path , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . _validate_path ( path ) <EOL> def _remap_output ( self , operation , path ) : <EOL> """<STR_LIT>""" <EOL> return self . _validate_path ( path ) <EOL> def _remap_pair ( self , operation , src , dst , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> self . _remap_input ( operation + '<STR_LIT>' , src , * args , ** kw ) , <EOL> self . _remap_input ( operation + '<STR_LIT>' , dst , * args , ** kw ) <EOL> ) <EOL> class DirectorySandbox ( AbstractSandbox ) : <EOL> """<STR_LIT>""" <EOL> write_ops = dict . fromkeys ( [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> ] ) <EOL> def __init__ ( self , sandbox ) : <EOL> self . _sandbox = os . path . normcase ( os . path . realpath ( sandbox ) ) <EOL> self . _prefix = os . path . join ( self . _sandbox , '<STR_LIT>' ) <EOL> AbstractSandbox . __init__ ( self ) <EOL> def _violation ( self , operation , * args , ** kw ) : <EOL> raise SandboxViolation ( operation , args , kw ) <EOL> def _open ( self , path , mode = '<STR_LIT:r>' , * args , ** kw ) : <EOL> if mode not in ( '<STR_LIT:r>' , '<STR_LIT>' , '<STR_LIT:rb>' , '<STR_LIT>' , '<STR_LIT>' ) and not self . _ok ( path ) : <EOL> self . _violation ( "<STR_LIT>" , path , mode , * args , ** kw ) <EOL> return _open ( path , mode , * args , ** kw ) <EOL> def tmpnam ( self ) : <EOL> self . _violation ( "<STR_LIT>" ) <EOL> def _ok ( self , path ) : <EOL> active = self . _active <EOL> try : <EOL> self . _active = False <EOL> realpath = os . path . normcase ( os . path . realpath ( path ) ) <EOL> if realpath == self . _sandbox or realpath . startswith ( self . _prefix ) : <EOL> return True <EOL> finally : <EOL> self . _active = active <EOL> def _remap_input ( self , operation , path , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if operation in self . write_ops and not self . _ok ( path ) : <EOL> self . _violation ( operation , os . path . realpath ( path ) , * args , ** kw ) <EOL> return path <EOL> def _remap_pair ( self , operation , src , dst , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if not self . _ok ( src ) or not self . _ok ( dst ) : <EOL> self . _violation ( operation , src , dst , * args , ** kw ) <EOL> return ( src , dst ) <EOL> def _file ( self , path , mode = '<STR_LIT:r>' , * args , ** kw ) : <EOL> if mode not in ( '<STR_LIT:r>' , '<STR_LIT>' , '<STR_LIT:rb>' , '<STR_LIT>' , '<STR_LIT>' ) and not self . _ok ( path ) : <EOL> self . _violation ( "<STR_LIT:file>" , path , mode , * args , ** kw ) <EOL> return _file ( path , mode , * args , ** kw ) <EOL> def open ( self , file , flags , mode = <NUM_LIT:0> <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if flags & WRITE_FLAGS and not self . _ok ( file ) : <EOL> self . _violation ( "<STR_LIT>" , file , flags , mode ) <EOL> return _os . open ( file , flags , mode ) <EOL> WRITE_FLAGS = reduce ( <EOL> operator . or_ , [ getattr ( _os , a , <NUM_LIT:0> ) for a in <EOL> "<STR_LIT>" . split ( ) ] <EOL> ) <EOL> class SandboxViolation ( DistutilsError ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return """<STR_LIT>""" % self . args </s>
