home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / sipdistutils.py < prev    next >
Encoding:
Python Source  |  2007-02-19  |  2.8 KB  |  83 lines

  1. # Subclasses disutils.command.build_ext,
  2. # replacing it with a SIP version that compiles .sip -> .cpp
  3. # before calling the original build_ext command.
  4. # Written by Giovanni Bajo <rasky at develer dot com>
  5. # Based on Pyrex.Distutils, written by Graham Fawcett and Darrel Gallion.
  6.  
  7. import distutils.command.build_ext
  8. from distutils.dep_util import newer
  9. import os
  10. import sys
  11.  
  12. def replace_suffix(path, new_suffix):
  13.     return os.path.splitext(path)[0] + new_suffix
  14.  
  15. class build_ext (distutils.command.build_ext.build_ext):
  16.  
  17.     description = "Compiler SIP descriptions, then build C/C++ extensions (compile/link to build directory)"
  18.  
  19.     def _get_sip_output_list(self, sbf):
  20.         """
  21.         Parse the sbf file specified to extract the name of the generated source
  22.         files. Make them absolute assuming they reside in the temp directory.
  23.         """
  24.         for L in file(sbf):
  25.             key, value = L.split("=", 1)
  26.             if key.strip() == "sources":
  27.                 out = []
  28.                 for o in value.split():
  29.                     out.append(os.path.join(self.build_temp, o))
  30.                 return out
  31.  
  32.         raise RuntimeError, "cannot parse SIP-generated '%s'" % sbf
  33.  
  34.     def _find_sip(self):
  35.         import sipconfig
  36.         cfg = sipconfig.Configuration()
  37.         return cfg.sip_bin
  38.  
  39.     def _sip_inc_dir(self):
  40.         import sipconfig
  41.         cfg = sipconfig.Configuration()
  42.         return cfg.sip_inc_dir
  43.  
  44.     def swig_sources (self, sources, extension=None):
  45.         if not self.extensions:
  46.             return
  47.  
  48.         # Add the SIP include directory to the include path
  49.         if extension is not None:
  50.             extension.include_dirs.append(self._sip_inc_dir())
  51.         else:
  52.             # pre-2.4 compatibility
  53.             self.include_dirs.append(self._sip_inc_dir())
  54.  
  55.         # Create the temporary directory if it does not exist already
  56.         if not os.path.isdir(self.build_temp):
  57.             os.makedirs(self.build_temp)
  58.  
  59.         # Collect the names of the source (.sip) files
  60.         sip_sources = []
  61.         sip_sources = [source for source in sources if source.endswith('.sip')]
  62.         other_sources = [source for source in sources if not source.endswith('.sip')]
  63.         generated_sources = []
  64.  
  65.         sip_bin = self._find_sip()
  66.  
  67.         for sip in sip_sources:
  68.             # Use the sbf file as dependency check
  69.             sipbasename = os.path.basename(sip)
  70.             sbf = os.path.join(self.build_temp, replace_suffix(sipbasename, "sbf"))
  71.             if newer(sip, sbf) or self.force:
  72.                 self._sip_compile(sip_bin, sip, sbf)
  73.             out = self._get_sip_output_list(sbf)
  74.             generated_sources.extend(out)
  75.  
  76.         return generated_sources + other_sources
  77.  
  78.     def _sip_compile(self, sip_bin, source, sbf):
  79.         self.spawn([sip_bin,
  80.                     "-c", self.build_temp,
  81.                     "-b", sbf,
  82.                     source])
  83.