<s> import os , sys , inspect , re , imp , py <EOL> from ctypes_configure import stdoutcapture <EOL> import distutils <EOL> debug = <NUM_LIT:0> <EOL> log = py . log . Producer ( "<STR_LIT>" ) <EOL> configdir = py . path . local . make_numbered_dir ( prefix = '<STR_LIT>' ) <EOL> class ExternalCompilationInfo ( object ) : <EOL> _ATTRIBUTES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> _AVOID_DUPLICATES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , <EOL> pre_include_lines = [ ] , <EOL> includes = [ ] , <EOL> include_dirs = [ ] , <EOL> post_include_lines = [ ] , <EOL> libraries = [ ] , <EOL> library_dirs = [ ] , <EOL> separate_module_sources = [ ] , <EOL> separate_module_files = [ ] ) : <EOL> """<STR_LIT>""" <EOL> for name in self . _ATTRIBUTES : <EOL> value = locals ( ) [ name ] <EOL> assert isinstance ( value , ( list , tuple ) ) <EOL> setattr ( self , name , tuple ( value ) ) <EOL> def _value ( self ) : <EOL> return tuple ( [ getattr ( self , x ) for x in self . _ATTRIBUTES ] ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . _value ( ) ) <EOL> def __eq__ ( self , other ) : <EOL> return self . __class__ is other . __class__ and self . _value ( ) == other . _value ( ) <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __repr__ ( self ) : <EOL> info = [ ] <EOL> for attr in self . _ATTRIBUTES : <EOL> val = getattr ( self , attr ) <EOL> info . append ( "<STR_LIT>" % ( attr , repr ( val ) ) ) <EOL> return "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( info ) <EOL> def merge ( self , * others ) : <EOL> others = list ( others ) <EOL> attrs = { } <EOL> for name in self . _ATTRIBUTES : <EOL> if name not in self . _AVOID_DUPLICATES : <EOL> s = [ ] <EOL> for i in [ self ] + others : <EOL> s += getattr ( i , name ) <EOL> attrs [ name ] = s <EOL> else : <EOL> s = set ( ) <EOL> attr = [ ] <EOL> for one in [ self ] + others : <EOL> for elem in getattr ( one , name ) : <EOL> if elem not in s : <EOL> s . add ( elem ) <EOL> attr . append ( elem ) <EOL> attrs [ name ] = attr <EOL> return ExternalCompilationInfo ( ** attrs ) <EOL> def write_c_header ( self , fileobj ) : <EOL> for line in self . pre_include_lines : <EOL> print >> fileobj , line <EOL> for path in self . includes : <EOL> print >> fileobj , '<STR_LIT>' % ( path , ) <EOL> for line in self . post_include_lines : <EOL> print >> fileobj , line <EOL> def _copy_attributes ( self ) : <EOL> d = { } <EOL> for attr in self . _ATTRIBUTES : <EOL> d [ attr ] = getattr ( self , attr ) <EOL> return d <EOL> def convert_sources_to_files ( self , cache_dir = None , being_main = False ) : <EOL> if not self . separate_module_sources : <EOL> return self <EOL> if cache_dir is None : <EOL> cache_dir = configdir . join ( '<STR_LIT>' ) . ensure ( dir = <NUM_LIT:1> ) <EOL> num = <NUM_LIT:0> <EOL> files = [ ] <EOL> for source in self . separate_module_sources : <EOL> while <NUM_LIT:1> : <EOL> filename = cache_dir . join ( '<STR_LIT>' % num ) <EOL> num += <NUM_LIT:1> <EOL> if not filename . check ( ) : <EOL> break <EOL> f = filename . open ( "<STR_LIT:w>" ) <EOL> if being_main : <EOL> f . write ( "<STR_LIT>" ) <EOL> self . write_c_header ( f ) <EOL> source = str ( source ) <EOL> f . write ( source ) <EOL> if not source . endswith ( '<STR_LIT:\n>' ) : <EOL> f . write ( '<STR_LIT:\n>' ) <EOL> f . close ( ) <EOL> files . append ( str ( filename ) ) <EOL> d = self . _copy_attributes ( ) <EOL> d [ '<STR_LIT>' ] = ( ) <EOL> d [ '<STR_LIT>' ] += tuple ( files ) <EOL> return ExternalCompilationInfo ( ** d ) <EOL> def compile_shared_lib ( self ) : <EOL> self = self . convert_sources_to_files ( ) <EOL> if not self . separate_module_files : <EOL> return self <EOL> lib = compile_c_module ( [ ] , '<STR_LIT>' , self ) <EOL> d = self . _copy_attributes ( ) <EOL> d [ '<STR_LIT>' ] += ( lib , ) <EOL> d [ '<STR_LIT>' ] = ( ) <EOL> d [ '<STR_LIT>' ] = ( ) <EOL> return ExternalCompilationInfo ( ** d ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> so_ext = '<STR_LIT>' <EOL> else : <EOL> so_ext = '<STR_LIT>' <EOL> def compiler_command ( ) : <EOL> return os . getenv ( '<STR_LIT>' ) <EOL> def enable_fast_compilation ( ) : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> dash = '<STR_LIT:/>' <EOL> else : <EOL> dash = '<STR_LIT:->' <EOL> from distutils import sysconfig <EOL> gcv = sysconfig . get_config_vars ( ) <EOL> opt = gcv . get ( '<STR_LIT>' ) <EOL> if opt : <EOL> opt = re . sub ( '<STR_LIT>' % dash , '<STR_LIT>' % dash , opt ) <EOL> else : <EOL> opt = '<STR_LIT>' % dash <EOL> gcv [ '<STR_LIT>' ] = opt <EOL> def ensure_correct_math ( ) : <EOL> if sys . platform != '<STR_LIT:win32>' : <EOL> return <EOL> from distutils import sysconfig <EOL> gcv = sysconfig . get_config_vars ( ) <EOL> opt = gcv . get ( '<STR_LIT>' ) <EOL> if opt and '<STR_LIT>' not in opt : <EOL> opt += '<STR_LIT>' <EOL> gcv [ '<STR_LIT>' ] = opt <EOL> def try_compile ( c_files , eci ) : <EOL> try : <EOL> build_executable ( c_files , eci ) <EOL> result = True <EOL> except ( distutils . errors . CompileError , <EOL> distutils . errors . LinkError ) : <EOL> result = False <EOL> return result <EOL> def compile_c_module ( cfiles , modbasename , eci , tmpdir = None ) : <EOL> cfiles = [ py . path . local ( f ) for f in cfiles ] <EOL> if tmpdir is None : <EOL> tmpdir = configdir . join ( "<STR_LIT>" ) . ensure ( dir = <NUM_LIT:1> ) <EOL> num = <NUM_LIT:0> <EOL> cfiles += eci . separate_module_files <EOL> include_dirs = list ( eci . include_dirs ) <EOL> library_dirs = list ( eci . library_dirs ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> for s in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if s + '<STR_LIT>' not in include_dirs and os . path . exists ( s + '<STR_LIT>' ) : <EOL> include_dirs . append ( s + '<STR_LIT>' ) <EOL> if s + '<STR_LIT>' not in library_dirs and os . path . exists ( s + '<STR_LIT>' ) : <EOL> library_dirs . append ( s + '<STR_LIT>' ) <EOL> num = <NUM_LIT:0> <EOL> modname = modbasename <EOL> while <NUM_LIT:1> : <EOL> if not tmpdir . join ( modname + so_ext ) . check ( ) : <EOL> break <EOL> num += <NUM_LIT:1> <EOL> modname = '<STR_LIT>' % ( modbasename , num ) <EOL> lastdir = tmpdir . chdir ( ) <EOL> libraries = eci . libraries <EOL> ensure_correct_math ( ) <EOL> try : <EOL> if debug : print "<STR_LIT>" , modname <EOL> c = stdoutcapture . Capture ( mixed_out_err = True ) <EOL> try : <EOL> try : <EOL> if compiler_command ( ) : <EOL> from distutils import sysconfig <EOL> gcv = sysconfig . get_config_vars ( ) <EOL> cmd = compiler_command ( ) . replace ( '<STR_LIT:%s>' , <EOL> str ( tmpdir . join ( modname ) ) ) <EOL> for dir in [ gcv [ '<STR_LIT>' ] ] + list ( include_dirs ) : <EOL> cmd += '<STR_LIT>' % dir <EOL> for dir in library_dirs : <EOL> cmd += '<STR_LIT>' % dir <EOL> os . system ( cmd ) <EOL> else : <EOL> from distutils . dist import Distribution <EOL> from distutils . extension import Extension <EOL> from distutils . ccompiler import get_default_compiler <EOL> saved_environ = os . environ . items ( ) <EOL> try : <EOL> extra_compile_args = [ ] <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> extra_compile_args . append ( '<STR_LIT>' ) <EOL> if get_default_compiler ( ) == '<STR_LIT>' : <EOL> old_version = False <EOL> try : <EOL> g = os . popen ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> verinfo = g . read ( ) <EOL> g . close ( ) <EOL> except ( OSError , IOError ) : <EOL> pass <EOL> else : <EOL> old_version = verinfo . startswith ( '<STR_LIT:2>' ) <EOL> if not old_version : <EOL> extra_compile_args . extend ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> attrs = { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : [ <EOL> Extension ( modname , [ str ( cfile ) for cfile in cfiles ] , <EOL> include_dirs = include_dirs , <EOL> library_dirs = library_dirs , <EOL> extra_compile_args = extra_compile_args , <EOL> libraries = list ( libraries ) , ) <EOL> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> dist = Distribution ( attrs ) <EOL> if not dist . parse_command_line ( ) : <EOL> raise ValueError , "<STR_LIT>" <EOL> dist . run_commands ( ) <EOL> finally : <EOL> for key , value in saved_environ : <EOL> if os . environ . get ( key ) != value : <EOL> os . environ [ key ] = value <EOL> finally : <EOL> foutput , foutput = c . done ( ) <EOL> data = foutput . read ( ) <EOL> if data : <EOL> fdump = open ( "<STR_LIT>" % modname , "<STR_LIT:w>" ) <EOL> fdump . write ( data ) <EOL> fdump . close ( ) <EOL> except : <EOL> print >> sys . stderr , data <EOL> raise <EOL> finally : <EOL> lastdir . chdir ( ) <EOL> return str ( tmpdir . join ( modname ) + so_ext ) <EOL> def make_module_from_c ( cfile , eci ) : <EOL> cfile = py . path . local ( cfile ) <EOL> modname = cfile . purebasename <EOL> compile_c_module ( [ cfile ] , modname , eci ) <EOL> return import_module_from_directory ( cfile . dirpath ( ) , modname ) <EOL> def import_module_from_directory ( dir , modname ) : <EOL> file , pathname , description = imp . find_module ( modname , [ str ( dir ) ] ) <EOL> try : <EOL> mod = imp . load_module ( modname , file , pathname , description ) <EOL> finally : <EOL> if file : <EOL> file . close ( ) <EOL> return mod <EOL> def log_spawned_cmd ( spawn ) : <EOL> def spawn_and_log ( cmd , * args , ** kwds ) : <EOL> if debug : <EOL> log . execute ( '<STR_LIT:U+0020>' . join ( cmd ) ) <EOL> return spawn ( cmd , * args , ** kwds ) <EOL> return spawn_and_log <EOL> class ProfOpt ( object ) : <EOL> name = "<STR_LIT>" <EOL> def __init__ ( self , compiler ) : <EOL> self . compiler = compiler <EOL> def first ( self ) : <EOL> self . build ( '<STR_LIT>' ) <EOL> def probe ( self , exe , args ) : <EOL> os . system ( "<STR_LIT>" % ( exe , args ) ) <EOL> def after ( self ) : <EOL> self . build ( '<STR_LIT>' ) <EOL> def build ( self , option ) : <EOL> compiler = self . compiler <EOL> compiler . compile_extra . append ( option ) <EOL> compiler . link_extra . append ( option ) <EOL> try : <EOL> compiler . _build ( ) <EOL> finally : <EOL> compiler . compile_extra . pop ( ) <EOL> compiler . link_extra . pop ( ) <EOL> class CCompiler : <EOL> def __init__ ( self , cfilenames , eci , outputfilename = None , <EOL> compiler_exe = None , profbased = None ) : <EOL> self . cfilenames = cfilenames <EOL> ext = '<STR_LIT>' <EOL> self . compile_extra = [ ] <EOL> self . link_extra = [ ] <EOL> self . libraries = list ( eci . libraries ) <EOL> self . include_dirs = list ( eci . include_dirs ) <EOL> self . library_dirs = list ( eci . library_dirs ) <EOL> self . compiler_exe = compiler_exe <EOL> self . profbased = profbased <EOL> if not sys . platform in ( '<STR_LIT:win32>' , '<STR_LIT>' ) : <EOL> if '<STR_LIT:m>' not in self . libraries : <EOL> self . libraries . append ( '<STR_LIT:m>' ) <EOL> if '<STR_LIT>' not in self . libraries : <EOL> self . libraries . append ( '<STR_LIT>' ) <EOL> self . compile_extra += [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . link_extra += [ '<STR_LIT>' ] <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> self . link_extra += [ '<STR_LIT>' ] <EOL> if sys . platform == '<STR_LIT>' : <EOL> for s in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if s + '<STR_LIT>' not in self . include_dirs and os . path . exists ( s + '<STR_LIT>' ) : <EOL> self . include_dirs . append ( s + '<STR_LIT>' ) <EOL> if s + '<STR_LIT>' not in self . library_dirs and os . path . exists ( s + '<STR_LIT>' ) : <EOL> self . library_dirs . append ( s + '<STR_LIT>' ) <EOL> self . compile_extra += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if outputfilename is None : <EOL> self . outputfilename = py . path . local ( cfilenames [ <NUM_LIT:0> ] ) . new ( ext = ext ) <EOL> else : <EOL> self . outputfilename = py . path . local ( outputfilename ) <EOL> self . eci = eci <EOL> def build ( self , noerr = False ) : <EOL> basename = self . outputfilename . new ( ext = '<STR_LIT>' ) <EOL> data = '<STR_LIT>' <EOL> try : <EOL> saved_environ = os . environ . copy ( ) <EOL> try : <EOL> c = stdoutcapture . Capture ( mixed_out_err = True ) <EOL> self . _build ( ) <EOL> finally : <EOL> for key , value in saved_environ . items ( ) : <EOL> if os . environ . get ( key ) != value : <EOL> os . environ [ key ] = value <EOL> foutput , foutput = c . done ( ) <EOL> data = foutput . read ( ) <EOL> if data : <EOL> fdump = basename . new ( ext = '<STR_LIT>' ) . open ( "<STR_LIT:w>" ) <EOL> fdump . write ( data ) <EOL> fdump . close ( ) <EOL> except : <EOL> if not noerr : <EOL> print >> sys . stderr , data <EOL> raise <EOL> def _build ( self ) : <EOL> from distutils . ccompiler import new_compiler <EOL> compiler = new_compiler ( force = <NUM_LIT:1> ) <EOL> if self . compiler_exe is not None : <EOL> for c in '''<STR_LIT>''' . split ( ) : <EOL> compiler . executables [ c ] [ <NUM_LIT:0> ] = self . compiler_exe <EOL> compiler . spawn = log_spawned_cmd ( compiler . spawn ) <EOL> objects = [ ] <EOL> for cfile in self . cfilenames : <EOL> cfile = py . path . local ( cfile ) <EOL> old = cfile . dirpath ( ) . chdir ( ) <EOL> try : <EOL> res = compiler . compile ( [ cfile . basename ] , <EOL> include_dirs = self . eci . include_dirs , <EOL> extra_preargs = self . compile_extra ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> cobjfile = py . path . local ( res [ <NUM_LIT:0> ] ) <EOL> assert cobjfile . check ( ) <EOL> objects . append ( str ( cobjfile ) ) <EOL> finally : <EOL> old . chdir ( ) <EOL> compiler . link_executable ( objects , str ( self . outputfilename ) , <EOL> libraries = self . eci . libraries , <EOL> extra_preargs = self . link_extra , <EOL> library_dirs = self . eci . library_dirs ) <EOL> def build_executable ( * args , ** kwds ) : <EOL> noerr = kwds . pop ( '<STR_LIT>' , False ) <EOL> compiler = CCompiler ( * args , ** kwds ) <EOL> compiler . build ( noerr = noerr ) <EOL> return str ( compiler . outputfilename ) </s>
