instant.config

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 -def get_swig_version():
      8      """ Return the current swig version in a 'str'"""
      9      # Check for swig installation
     10      result, output = get_status_output("swig -version")
     11      if result != 0:
     12          raise OSError("SWIG is not installed on the system.")
     13      pattern = "SWIG Version (.*)"
     14      r = re.search(pattern, output)
     15      return r.groups(0)[0]
     16
     17 -def check_swig_version(version, same=False):
     18      """ Check the swig version
     19
     20      Returns True if the version of the installed swig is equal or greater than the
     21      version passed to the function.
     22
     23      If same is True, the function returns True if and only if the two versions
     24      are the same.
     25
     26      Usage:
     27      if instant.check_swig_version('1.3.36'):
     28          print "Swig version is greater than or equal to 1.3.36"
     29      else:
     30          print "Swig version is lower than 1.3.36"
     31      """
     32      assert isinstance(version,str), "Provide the first version number as a 'str'"
     33      assert len(version.split("."))==3, "Provide the version number as three numbers seperated by '.'"
     34
     35      installed_version = map(int, get_swig_version().split('.'))
     36      handed_version    = map(int, version.split('.'))
     37
     38      # If same is True then just check that all numbers are equal
     39      if same:
     40          return all(i == h for i, h in zip(installed_version,handed_version))
     41
     42      swig_enough = True
     43      for i, v in enumerate([v for v in installed_version]):
     44          if handed_version[i] < v:
     45              break
     46          elif handed_version[i] == v:
     47              continue
     48          else:
     49              swig_enough = False
     50          break
     51
     52      return swig_enough
     53
     54 -def header_and_libs_from_pkgconfig(*packages, **kwargs):
     55      """This function returns list of include files, flags, libraries and library directories obtain from a pkgconfig file.
     56
     57      The usage is:
     58      (includes, flags, libraries, libdirs) = header_and_libs_from_pkgconfig(*list_of_packages)
     59      """
     60      returnLinkFlags = kwargs.get("returnLinkFlags", False)
     61      result, output = get_status_output("pkg-config --version ")
     62      if result != 0:
     63          raise OSError("The pkg-config package is not installed on the system.")
     64
     65      env = os.environ.copy()
     66      try:
     67          assert env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] == "0"
     68      except:
     69          env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "1"
     70
     71      includes = []
     72      flags = []
     73      libs = []
     74      libdirs = []
     75      linkflags = []
     76      for pack in packages:
     77          result, output = get_status_output("pkg-config --exists %s " % pack, env=env)
     78          if result == 0:
     79              tmp = get_status_output("pkg-config --cflags-only-I %s " % pack, env=env)[1].split()
     80              includes.extend(i[2:] for i in tmp)
     81
     82              tmp = get_status_output("pkg-config --cflags-only-other %s " % pack, env=env)[1].split()
     83              flags.extend(tmp)
     84
     85              tmp = get_status_output("pkg-config --libs-only-l  %s " % pack, env=env)[1].split()
     86              libs.extend(i[2:] for i in tmp)
     87
     88              tmp = get_status_output("pkg-config --libs-only-L  %s " % pack, env=env)[1].split()
     89              libdirs.extend(i[2:] for i in tmp)
     90
     91              tmp = get_status_output("pkg-config --libs-only-other  %s " % pack, env=env)[1].split()
     92              linkflags.extend(tmp)
     93
     94          else:
     95              raise OSError("The pkg-config file %s does not exist" % pack)
     96
     97      if returnLinkFlags: return (includes,flags,libs, libdirs, linkflags)
     98      return (includes,flags,libs, libdirs)
     99