]>
Commit | Line | Data |
---|---|---|
42b1161e | 1 | #!/usr/bin/python |
f7a9f785 | 2 | # Copyright (C) 2014-2016 Free Software Foundation, Inc. |
42b1161e SP |
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 | # <http://www.gnu.org/licenses/>. | |
18 | """Benchmark output validator | |
19 | ||
20 | Given a benchmark output file in json format and a benchmark schema file, | |
21 | validate the output against the schema. | |
22 | """ | |
23 | ||
24 | from __future__ import print_function | |
25 | import json | |
26 | import sys | |
27 | import os | |
28 | ||
29 | try: | |
0994b9b6 | 30 | import import_bench as bench |
42b1161e | 31 | except ImportError: |
0994b9b6 | 32 | print('Import Error: Output will not be validated.') |
42b1161e SP |
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 | ||
0994b9b6 SP |
38 | def print_and_exit(message, exitcode): |
39 | """Prints message to stderr and returns the exit code. | |
42b1161e SP |
40 | |
41 | Args: | |
0994b9b6 SP |
42 | message: The message to print |
43 | exitcode: The exit code to return | |
42b1161e | 44 | |
0994b9b6 SP |
45 | Returns: |
46 | The passed exit code | |
42b1161e | 47 | """ |
0994b9b6 SP |
48 | print(message, file=sys.stderr) |
49 | return exitcode | |
42b1161e SP |
50 | |
51 | ||
52 | def 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: | |
0994b9b6 SP |
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) | |
42b1161e | 80 | |
0994b9b6 | 81 | print("Benchmark output in %s is valid." % args[0]) |
42b1161e SP |
82 | return os.EX_OK |
83 | ||
84 | ||
85 | if __name__ == '__main__': | |
86 | sys.exit(main(sys.argv[1:])) |