Package instant :: Module config
[hide private]
[frames] | no frames]

Source Code for Module instant.config

  1  """This module contains helper functions for configuration using pkg-config.""" 
  2   
  3  import os 
  4  from .output import get_status_output 
  5  import re 
  6   
  7  # Global cache variables 
  8  _swig_binary_cache = None 
  9  _swig_version_cache = None 
 10  _pkg_config_installed = None 
 11  _header_and_library_cache = {} 
 12   
13 -def check_and_set_swig_binary(binary="swig", path=""):
14 """ Check if a particular swig binary is available""" 15 global _swig_binary_cache 16 if not isinstance(binary, str): 17 raise TypeError("expected a 'str' as first argument") 18 if not isinstance(path, str): 19 raise TypeError("expected a 'str' as second argument") 20 swig_binary = os.path.join(path, binary) 21 if swig_binary == _swig_binary_cache: 22 return True 23 24 result, output = get_status_output("%s -version"%swig_binary) 25 if result != 0: 26 return False 27 28 # Set binary cache 29 _swig_binary_cache = swig_binary 30 31 # Reset SWIG version cache 32 _swig_version_cache = None 33 34 return True
35
36 -def get_swig_binary():
37 "Return any cached swig binary" 38 return _swig_binary_cache if _swig_binary_cache else "swig"
39
40 -def get_swig_version():
41 """ Return the current swig version in a 'str'""" 42 global _swig_version_cache 43 if _swig_version_cache is None: 44 # Check for swig installation 45 result, output = get_status_output("%s -version"%get_swig_binary()) 46 if result != 0: 47 raise OSError("SWIG is not installed on the system.") 48 pattern = "SWIG Version (.*)" 49 r = re.search(pattern, output) 50 _swig_version_cache = r.groups(0)[0] 51 return _swig_version_cache
52
53 -def check_swig_version(version, same=False):
54 """ Check the swig version 55 56 Returns True if the version of the installed swig is equal or greater than the 57 version passed to the function. 58 59 If same is True, the function returns True if and only if the two versions 60 are the same. 61 62 Usage: 63 if instant.check_swig_version('1.3.36'): 64 print "Swig version is greater than or equal to 1.3.36" 65 else: 66 print "Swig version is lower than 1.3.36" 67 """ 68 assert isinstance(version,str), "Provide the first version number as a 'str'" 69 assert len(version.split("."))==3, "Provide the version number as three numbers seperated by '.'" 70 71 installed_version = list(map(int, get_swig_version().split('.'))) 72 handed_version = list(map(int, version.split('.'))) 73 74 # If same is True then just check that all numbers are equal 75 if same: 76 return all(i == h for i, h in zip(installed_version,handed_version)) 77 78 swig_enough = True 79 for i, v in enumerate([v for v in installed_version]): 80 if handed_version[i] < v: 81 break 82 elif handed_version[i] == v: 83 continue 84 else: 85 swig_enough = False 86 break 87 88 return swig_enough
89
90 -def header_and_libs_from_pkgconfig(*packages, **kwargs):
91 """This function returns list of include files, flags, 92 libraries and library directories obtain from a pkgconfig file. 93 94 The usage is: 95 (includes, flags, libraries, libdirs) = \ 96 header_and_libs_from_pkgconfig(*list_of_packages) 97 or: 98 (includes, flags, libraries, libdirs, linkflags) = \ 99 header_and_libs_from_pkgconfig(*list_of_packages, \ 100 returnLinkFlags=True) 101 """ 102 global _pkg_config_installed, _header_and_library_cache 103 returnLinkFlags = kwargs.get("returnLinkFlags", False) 104 if _pkg_config_installed is None: 105 result, output = get_status_output("pkg-config --version ") 106 _pkg_config_installed = (result == 0) 107 if not _pkg_config_installed: 108 raise OSError("The pkg-config package is not installed on the system.") 109 110 env = os.environ.copy() 111 try: 112 assert env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] == "0" 113 except: 114 env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "1" 115 116 includes = [] 117 flags = [] 118 libs = [] 119 libdirs = [] 120 linkflags = [] 121 for pack in packages: 122 if not pack in _header_and_library_cache: 123 result, output = get_status_output(\ 124 "pkg-config --exists %s " % pack, env=env) 125 if result == 0: 126 tmp = get_status_output(\ 127 "pkg-config --cflags-only-I %s " % pack, env=env)[1].split() 128 _includes = [i[2:] for i in tmp] 129 130 _flags = get_status_output(\ 131 "pkg-config --cflags-only-other %s " % pack, env=env)[1].split() 132 133 tmp = get_status_output(\ 134 "pkg-config --libs-only-l %s " % pack, env=env)[1].split() 135 _libs = [i[2:] for i in tmp] 136 137 tmp = get_status_output(\ 138 "pkg-config --libs-only-L %s " % pack, env=env)[1].split() 139 _libdirs = [i[2:] for i in tmp] 140 141 _linkflags = get_status_output(\ 142 "pkg-config --libs-only-other %s " % pack, env=env)[1].split() 143 144 _header_and_library_cache[pack] = (_includes, _flags, _libs, \ 145 _libdirs, _linkflags) 146 else: 147 _header_and_library_cache[pack] = None 148 149 result = _header_and_library_cache[pack] 150 if not result: 151 raise OSError("The pkg-config file %s does not exist" % pack) 152 153 _includes, _flags, _libs, _libdirs, _linkflags = result 154 includes.extend(_includes) 155 flags.extend(_flags) 156 libs.extend(_libs) 157 libdirs.extend(_libdirs) 158 linkflags.extend(_linkflags) 159 160 if returnLinkFlags: 161 return (includes, flags, libs, libdirs, linkflags) 162 return (includes, flags, libs, libdirs)
163