1#!/usr/bin/env python
2# Author: Pierre Schnizer <schnizer@users.sourceforge.net> 2016, 2017
3# $Id$
4"""Generate python testcases from GSL src
5
6Reads all test_* files from GSL source directory.
7Extracts the test_sf macros one by one.
8The arguments of the macros are converted into sf_test_types cls instances
9These can be used later on to convert automatically tests from its
10"""
11
12from __future__ import print_function
13import sys
14import os
15import glob
16import os.path
17import process_tests
18import generate_sf_tests
19
20_gsl_dir_env_var = "GSL_SRC"
21
22# _base_dir = os.path.abspath(os.path.dirname(__file__))
23# _t_dir = os.path.join(_base_dir, "extract_tests")
24# sys.path.append(_t_dir)
25
26
27def find_gsl_src_dir_from_env():
28    """Find the top directory of the GSL SRC tree
29    """
30
31    # Installation of the GSL_SRC
32
33    gsl_dir = None
34    try:
35        gsl_dir = os.environ[_gsl_dir_env_var]
36    except KeyError as des:
37        pass
38
39    if gsl_dir == None:
40        msg = """You must specify where the GSL source directory is to be found.
41Either use the command line switch '-g' or define the environment variable '%s',
42which specifies where the GSL source directory is found. This module will then
43parse the tests in the directory 'specfunc' and translate them to
44unittest.TestCase tests.
45"""
46        raise ValueError(msg %(_gsl_dir_env_var,))
47
48    return gsl_dir
49
50def help_text():
51    msg = """
52Usage '%s' -g gsl_src --gsl-src-directory gsl_src
53
54Parses test_*.c files in the gsl_src/sepcfunc directory and generates
55unittest.TestCases out of it.
56"""
57    print(msg % (sys.argv[0], ))
58
59def find_gsl_src_dir():
60    import getopt
61
62    progname = "%s %s" % (sys.executable, sys.argv[0])
63    try:
64        res = getopt.getopt(sys.argv[1:], 'g:h', ('gsl-src-directory='))
65    except getopt.GetoptError as err:
66        err.msg += ". Run %s -h for further information" %(progname,)
67        raise(err)
68
69    found_options = res[0]
70
71
72    for opt in found_options:
73        if '-h' in opt:
74            help_text()
75            return
76
77    gsl_src = None
78    opt_arg = None
79    how_specified = None
80    for opt in found_options:
81        if '-g' or '--gsl-src-directory' in opt:
82            gsl_src = opt[1]
83            how_specified = "command line option %s %s" % opt
84
85    if gsl_src == None:
86        # No option found ... try environement
87        gsl_src = find_gsl_src_dir_from_env()
88        how_specified = "Environment variable  os.environ[%s]=%s"
89        how_specified = how_specified %(_gsl_dir_env_var, gsl_src)
90
91    # Check if gsl_src is a directory
92    if not os.path.isdir(gsl_src):
93        msg = """
94    The path '%s' specified via '%s' is not a directory!
95"""
96        print(msg %(gsl_src, how_specified))
97        return
98
99    t_dir =  "specfunc",
100    spec_func_dir = os.path.join(*((gsl_src,) + t_dir))
101
102    # Check if gsl_spec_func_dir is a directory
103    if not os.path.isdir(spec_func_dir):
104        msg = """
105    The path '%s' specified via '%s' is not a directory!
106"""
107        print(msg %(spec_func_dir, how_specified))
108        return
109
110    return spec_func_dir
111
112def run():
113    "main function"
114    gsl_src_dir= find_gsl_src_dir()
115    if gsl_src_dir == None:
116        return
117
118    print("Using GSL source dir '%s'" %(gsl_src_dir,))
119    test_files = os.path.join(gsl_src_dir, "*test*.c")
120    #test_files = os.path.join(gsl_src_dir, "*test_bessel*.c")
121    all_tests = glob.glob(test_files)
122
123    for a_test in all_tests:
124        #if "airy" in a_test:
125        #    continue
126        first_comment, tests = process_tests.handle_one_test_file(a_test)
127
128        base = os.path.basename(a_test)
129        outfile_name = base[:-2] + '_from_gsl_src.py'
130        print("Writing tests to '%s'" % (outfile_name,))
131        all_stores = map(lambda x: x.GetStore(), tests)
132        generate_sf_tests.write_tests(outfile_name, first_comment, all_stores)
133
134if __name__ == '__main__':
135    run()
136