1# file: pygsl/gsl_dist/gsl_extension.py 2# $Id$ 3import re 4from sys import argv,version_info 5from distutils.errors import DistutilsModuleError, DistutilsExecError 6 7import os 8 9# steal --gsl-prefix from option list 10gsl_prefix_option=None 11gsl_prefix_option_pattern=re.compile("--gsl-prefix=(.+)") 12pos=0 13while pos<len(argv): 14 gsl_prefix_match=gsl_prefix_option_pattern.match(argv[pos]) 15 if gsl_prefix_match: 16 gsl_prefix_option=gsl_prefix_match.group(1) 17 gsl_prefix_option.strip() 18 argv[pos:pos+1]=[] 19 break 20 pos+=1 21 22# Extension class adapted for gsl 23class _gsl_Location: 24 """ 25 Wrapper for the location of the gsl library. 26 27 On unix one can run gsl-config to find the locations. On other systems 28 one has to revert to other ways to find the configuration. 29 """ 30 def __init__(self): 31 self.prefix = None 32 self.cflags = None 33 self.libs = None 34 self.version = None 35 self.swig = None 36 37 def get_gsl_prefix(self): 38 assert(self.prefix != None) 39 return self.prefix 40 41 def get_gsl_cflags(self): 42 assert(self.cflags != None) 43 return self.cflags 44 45 def get_gsl_libs(self): 46 #print self.libs 47 assert(self.libs != None) 48 return self.libs 49 50 51 def get_gsl_version(self): 52 assert(self.version != None) 53 return self.version 54 55 def get_gsl_config_version(self): 56 "Which version string gsl-config --version returned " 57 assert(self.config_version != None) 58 return self.config_version 59 60 def _split_version(self, version): 61 if version[-1] == '+': 62 version = version[:-1] 63 return re.split('\.',version) 64 65 def get_swig(self): 66 assert(self.swig) 67 return self.swig 68 69 # Originaly in GSL extension: 70 # Now used by gsl_Config and gsl_Extension so given here to be available to both 71 def get_gsl_include_dirs(self): 72 prefix = self.get_gsl_prefix() 73 t_prefix_include_dir = os.path.join(prefix, "include") 74 include_dirs =[t_prefix_include_dir] 75 return include_dirs 76 77 def get_gsl_library_dirs(self): 78 prefix = self.get_gsl_prefix() 79 t_prefix_lib_dir = os.path.join(prefix, "lib") 80 library_dirs =[t_prefix_lib_dir] 81 return library_dirs 82 83 def get_gsl_lib_list(self): 84 # prepare lib list 85 # split optionlist and strip blanks from each option 86 gsl_lib_list=map(lambda x: x.strip(),self.get_gsl_libs().split()) 87 88 # filter options with -l 89 not_lib_opt=lambda a:a[:2]=="-l" 90 gsl_lib_list=filter(not_lib_opt,gsl_lib_list) 91 # cut -l 92 only_lib_name=lambda a:a[2:] 93 gsl_lib_list=map(only_lib_name,gsl_lib_list) 94 95 return gsl_lib_list 96 97class _gsl_Location_gsl_config(_gsl_Location): 98 """ 99 Call gsl_config to find the location of gsl 100 """ 101 def __init__(self): 102 _gsl_Location.__init__(self) 103 gsl_prefix = None 104 if gsl_prefix is not None: 105 self.gsl_config_tool=os.path.join(gsl_prefix,"bin","gsl-config") 106 elif gsl_prefix_option is not None: 107 self.gsl_config_tool=os.path.join(gsl_prefix_option,"bin","gsl-config") 108 else: 109 self.gsl_config_tool="gsl-config" 110 111 self.prefix = self.get_gsl_info('--prefix').strip() 112 self.cflags = self.get_gsl_info('--cflags').strip() 113 self.libs = self.get_gsl_info('--libs').strip() 114 self.config_version = self.get_gsl_info('--version') 115 self.version = self._split_version(self.config_version.strip())[:2] 116 117 # I am running on swig. I suppose that swig is in the path 118 self.swig = "swig" 119 try: 120 self.swig = os.environ["SWIG"] 121 except KeyError: 122 pass 123 124 125 def get_gsl_info(self, arguments): 126 """ 127 executes gsl-config with given arguments 128 """ 129 gsl_command=os.popen(self.gsl_config_tool+' '+arguments) 130 gsl_output=gsl_command.readline() 131 gsl_command.close() 132 if not gsl_output: 133 raise DistutilsExecError("could not start %s"%self.gsl_config_tool) 134 return gsl_output 135 136 137class _gsl_Location_file(_gsl_Location): 138 def __init__(self): 139 _gsl_Location.__init__(self) 140 try: 141 import gsl_site 142 except ImportError as des: 143 msg = "I do not know how to run gsl-config \n"+\ 144 "on this system. Therefore you must provide me with the information\n" +\ 145 "where to find the GSL library. I could not import `gsl_site'.\n" +\ 146 "Reason: %s. Copy gsl_site_example.py to gsl_site.py.\n"+\ 147 "Edit the variables in that file to reflect your installation." 148 raise DistutilsExecError(msg % des) 149 150 self.prefix = gsl_site.prefix 151 self.cflags = gsl_site.cflags 152 self.libs = gsl_site.libs 153 self.swig = gsl_site.swig 154 self.config_version = gsl_site.version 155 self.version = self._split_version(gsl_site.version) 156 157if os.name == 'posix': 158 gsl_Location = _gsl_Location_gsl_config() 159else: 160 gsl_Location = _gsl_Location_file() 161