1#!/usr/bin/env python
2
3"""
4Implements the "make check" target
5
6(C) 2020 Jack Lloyd, Rene Meusel
7
8Botan is released under the Simplified BSD License (see license.txt)
9"""
10
11import json
12import logging
13import optparse # pylint: disable=deprecated-module
14import os
15import subprocess
16import sys
17
18def run_and_check(cmd_line, env=None, cwd=None):
19
20    logging.info("Starting %s", ' '.join(cmd_line))
21
22    try:
23        proc = subprocess.Popen(cmd_line, cwd=cwd, env=env)
24        proc.communicate()
25    except OSError as e:
26        logging.error("Executing %s failed (%s)", ' '.join(cmd_line), e)
27
28    if proc.returncode != 0:
29        logging.error("Error running %s", ' '.join(cmd_line))
30        sys.exit(1)
31
32
33def make_environment(build_shared_lib):
34    if not build_shared_lib:
35        return None
36
37    env = os.environ.copy()
38
39    def extend_colon_list(k, n):
40        env[k] = n if k not in env else ":".join([env[k], n])
41
42    extend_colon_list("DYLD_LIBRARY_PATH", os.path.abspath("."))
43    extend_colon_list("LD_LIBRARY_PATH", os.path.abspath("."))
44
45    return env
46
47
48def parse_options(args):
49    parser = optparse.OptionParser()
50    parser.add_option('--build-dir', default='build', metavar='DIR',
51                      help='specify the botan build directory (default %default)')
52
53    (options, args) = parser.parse_args(args)
54
55    if len(args) > 1:
56        raise Exception("Unknown arguments")
57
58    return options
59
60
61def read_config(config):
62    try:
63        with open(config) as f:
64            return json.load(f)
65    except OSError:
66        raise Exception('Failed to load build config %s - is build dir correct?' % (config))
67
68
69def main(args=None):
70    if args is None:
71        args = sys.argv
72
73    options = parse_options(args)
74
75    cfg = read_config(os.path.join(options.build_dir, 'build_config.json'))
76
77    test_exe = cfg.get('test_exe')
78    build_shared_lib = cfg.get('build_shared_lib')
79
80    if not os.path.isfile(test_exe) or not os.access(test_exe, os.X_OK):
81        raise Exception("Test binary not built")
82
83    run_and_check([test_exe, "--data-dir=%s" % cfg.get('test_data_dir')],
84                  make_environment(build_shared_lib))
85
86    return 0
87
88if __name__ == '__main__':
89    sys.exit(main())
90