1import sys
2import os
3import distutils.cmd
4import distutils.log
5import setuptools
6import subprocess
7from distutils.file_util import copy_file
8
9from gsl_Extension import gsl_Location
10class gsl_CodeGenerator(distutils.cmd.Command):
11  """Create code using GSL code generator
12  """
13
14  description = 'Code generator for GSL'
15  user_options = [
16      # The format is (long option, short option, description).
17      #('pylint-rcfile=', None, 'path to Pylint config file'),
18  ]
19
20  def initialize_options(self):
21    """Set default values for options."""
22    # Each user option must be listed here with their default value.
23    #self.pylint_rcfile = ''
24
25  def finalize_options(self):
26    """Post-process options."""
27    #if self.pylint_rcfile:
28    #  assert os.path.exists(self.pylint_rcfile), (
29    #      'Pylint config file %s does not exist.' % self.pylint_rcfile)
30
31  def _get_gsl_src_dir(self):
32    this_dir = os.path.dirname(__file__)
33    tmp = os.path.join(this_dir, os.path.pardir)
34    result = os.path.normpath(tmp)
35    return result
36
37  def _get_ufunc_src_dir(self):
38    "Where is the ufunc code expected"
39    tmp = os.path.join(self._get_gsl_src_dir(), "testing", "src", "sf")
40    result = os.path.normpath(tmp)
41    return result
42
43  def _swig_create_xml_file(self, t_file):
44    """Swig parser used for generating a swig file
45
46    SWIG allows exporting the read code as an xml file.
47    This is used by a self made code to build ufunc wrappers
48    See :func:`_create_ufunc_wrapper`
49    """
50    inc = []
51    for a_inc in gsl_Location.get_gsl_include_dirs():
52      inc.append('-I' + a_inc)
53
54    command = [gsl_Location.get_swig()] + inc + ["-xml", t_file]
55
56
57    self.announce(
58        'Exporting parser tree using swig: %s' % str(command),
59        level=distutils.log.INFO)
60    subprocess.check_call(command)
61
62  def _create_ufunc_wrapper_code(self):
63    """Create the code from the xml file
64    """
65    sf_src_dir = self._get_ufunc_src_dir()
66
67    tools_dir = os.path.join(self._get_gsl_src_dir(), "testing", "tools", "generate_interface")
68    api_doc_dir = os.path.join(self._get_gsl_src_dir(), "doc", "api", "sf")
69
70    tool = os.path.join(tools_dir, "extract_ufunc_swig.py")
71    swig_xml_file = os.path.join(sf_src_dir, "sf_wrap.xml")
72
73    prefix = "sf_"
74    command = [sys.executable, tool,  "--input", swig_xml_file, "--output-dir", sf_src_dir, "--prefix", "sf_", "--doc-dir", api_doc_dir]
75    self.announce(
76      'Creating ufunc wrappers: %s' % str(command),
77      level=distutils.log.INFO)
78    subprocess.check_call(command)
79
80    #self.announce(
81    #  'Copying generated doc (.rst) file to api doc directory')
82
83    #src = os.path.join(sf_src_dir, "sf__doc.rst")
84    #copy_file(src, api_doc_dir)
85
86  def _create_ufunc_wrapper(self):
87    """
88    """
89    sf_src_dir = self._get_ufunc_src_dir()
90    t_file = os.path.join(sf_src_dir,  "sf.i")
91    self._swig_create_xml_file(t_file)
92
93    self._create_ufunc_wrapper_code()
94
95
96  def run(self):
97    """Run command
98
99    Creates the code by:
100        * running swig for creating an xml file containing the headers
101        * running a dedicated tool creating the wrapper code
102    """
103    #if self.pylint_rcfile:
104    #  command.append('--rcfile=%s' % self.pylint_rcfile)
105    self._create_ufunc_wrapper()
106
107