1#!/usr/bin/python3
2# Copyright (C) 2014-2022 Free Software Foundation, Inc.
3# This file is part of the GNU C Library.
4#
5# The GNU C Library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# The GNU C Library is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with the GNU C Library; if not, see
17# <https://www.gnu.org/licenses/>.
18"""Benchmark output validator
19
20Given a benchmark output file in json format and a benchmark schema file,
21validate the output against the schema.
22"""
23
24from __future__ import print_function
25import json
26import sys
27import os
28
29try:
30    import import_bench as bench
31except ImportError:
32    print('Import Error: Output will not be validated.')
33    # Return success because we don't want the bench target to fail just
34    # because the jsonschema module was not found.
35    sys.exit(os.EX_OK)
36
37
38def print_and_exit(message, exitcode):
39    """Prints message to stderr and returns the exit code.
40
41    Args:
42        message: The message to print
43        exitcode: The exit code to return
44
45    Returns:
46        The passed exit code
47    """
48    print(message, file=sys.stderr)
49    return exitcode
50
51
52def main(args):
53    """Main entry point
54
55    Args:
56        args: The command line arguments to the program
57
58    Returns:
59        0 on success or a non-zero failure code
60
61    Exceptions:
62        Exceptions thrown by validate_bench
63    """
64    if len(args) != 2:
65        return print_and_exit("Usage: %s <bench.out file> <bench.out schema>"
66                % sys.argv[0], os.EX_USAGE)
67
68    try:
69        bench.parse_bench(args[0], args[1])
70    except IOError as e:
71        return print_and_exit("IOError(%d): %s" % (e.errno, e.strerror),
72                os.EX_OSFILE)
73
74    except bench.validator.ValidationError as e:
75        return print_and_exit("Invalid benchmark output: %s" % e.message,
76                os.EX_DATAERR)
77
78    except bench.validator.SchemaError as e:
79        return print_and_exit("Invalid schema: %s" % e.message, os.EX_DATAERR)
80
81    except json.decoder.JSONDecodeError as e:
82        return print_and_exit("Benchmark output in %s is not JSON." % args[0],
83                os.EX_DATAERR)
84
85    print("Benchmark output in %s is valid." % args[0])
86    return os.EX_OK
87
88
89if __name__ == '__main__':
90    sys.exit(main(sys.argv[1:]))
91