aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts/check.py
blob: b27c92672520a2d0ef4f10da2de81c473fca2728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python

"""
Implements the "make check" target

(C) 2020 Jack Lloyd, Rene Meusel

Botan is released under the Simplified BSD License (see license.txt)
"""

import json
import logging
import optparse # pylint: disable=deprecated-module
import os
import platform
import subprocess
import sys

def is_macos():
    return platform.system() == "Darwin"

def is_linux():
    return platform.system() == "Linux"


def run_and_check(cmd_line, env=None, cwd=None):

    logging.info("Starting %s", ' '.join(cmd_line))

    try:
        proc = subprocess.Popen(cmd_line, cwd=cwd, env=env)
        proc.communicate()
    except OSError as e:
        logging.error("Executing %s failed (%s)", ' '.join(cmd_line), e)

    if proc.returncode != 0:
        logging.error("Error running %s", ' '.join(cmd_line))
        sys.exit(1)


def make_environment(build_shared_lib):
    if not build_shared_lib:
        return None

    env = os.environ.copy()

    if is_macos():
        env["DYLD_LIBRARY_PATH"] = os.path.abspath(".")
    elif is_linux():
        env["LD_LIBRARY_PATH"] = os.path.abspath(".")

    return env


def parse_options(args):
    parser = optparse.OptionParser()
    parser.add_option('--build-dir', default='build', metavar='DIR',
                      help='specify the botan build directory (default %default)')

    (options, args) = parser.parse_args(args)

    if len(args) > 1:
        raise Exception("Unknown arguments")

    return options


def read_config(config):
    try:
        with open(config) as f:
            return json.load(f)
    except OSError:
        raise Exception('Failed to load build config %s - is build dir correct?' % (config))


def main(args=None):
    if args is None:
        args = sys.argv

    options = parse_options(args)

    cfg = read_config(os.path.join(options.build_dir, 'build_config.json'))

    test_exe         = cfg.get('test_exe')
    build_shared_lib = cfg.get('build_shared_lib')

    if not os.path.isfile(test_exe) or not os.access(test_exe, os.X_OK):
        raise Exception("Test binary not built")

    run_and_check([ test_exe ], make_environment(build_shared_lib))

    return 0

if __name__ == '__main__':
    sys.exit(main())