blob: a1d282db3333eb73a8b97d37ced1e1dd6205eb56 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#!/usr/bin/env python3
2
3# OpenEmbedded test tool
4#
5# Copyright (C) 2016 Intel Corporation
6# Released under the MIT license (see COPYING.MIT)
7
8import os
9import sys
10import argparse
11import importlib
12import logging
13
14scripts_path = os.path.dirname(os.path.realpath(__file__))
15lib_path = scripts_path + '/lib'
16sys.path = sys.path + [lib_path]
17import argparse_oe
18import scriptutils
19
20# oe-test is used for testexport and it doesn't have oe lib
21# so we just skip adding these libraries (not used in testexport)
22try:
23 import scriptpath
24 scriptpath.add_oe_lib_path()
25except ImportError:
26 pass
27
28from oeqa.core.context import OETestContextExecutor
29
30logger = scriptutils.logger_create('oe-test')
31
32def _load_test_components(logger):
33 components = {}
34
35 for path in sys.path:
36 base_dir = os.path.join(path, 'oeqa')
37 if os.path.exists(base_dir) and os.path.isdir(base_dir):
38 for file in os.listdir(base_dir):
39 comp_name = file
40 comp_context = os.path.join(base_dir, file, 'context.py')
41 if os.path.exists(comp_context):
42 comp_plugin = importlib.import_module('oeqa.%s.%s' % \
43 (comp_name, 'context'))
44 try:
45 if not issubclass(comp_plugin._executor_class,
46 OETestContextExecutor):
47 raise TypeError("Component %s in %s, _executor_class "\
48 "isn't derived from OETestContextExecutor."\
49 % (comp_name, comp_context))
50
51 components[comp_name] = comp_plugin._executor_class()
52 except AttributeError:
53 raise AttributeError("Component %s in %s don't have "\
54 "_executor_class defined." % (comp_name, comp_context))
55
56 return components
57
58def main():
59 parser = argparse_oe.ArgumentParser(description="OpenEmbedded test tool",
60 add_help=False,
61 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
62 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
63 parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
64 global_args, unparsed_args = parser.parse_known_args()
65
66 # Help is added here rather than via add_help=True, as we don't want it to
67 # be handled by parse_known_args()
68 parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
69 help='show this help message and exit')
70
71 if global_args.debug:
72 logger.setLevel(logging.DEBUG)
73 elif global_args.quiet:
74 logger.setLevel(logging.ERROR)
75
76 components = _load_test_components(logger)
77
78 subparsers = parser.add_subparsers(dest="subparser_name", title='subcommands', metavar='<subcommand>')
79 subparsers.add_subparser_group('components', 'Test components')
80 subparsers.required = True
81 for comp_name in sorted(components.keys()):
82 comp = components[comp_name]
83 comp.register_commands(logger, subparsers)
84
85 try:
86 args = parser.parse_args(unparsed_args, namespace=global_args)
87 results = args.func(logger, args)
88 ret = 0 if results.wasSuccessful() else 1
89 except SystemExit as err:
90 if err.code != 0:
91 raise err
92 ret = err.code
93 except argparse_oe.ArgumentUsageError as ae:
94 parser.error_subcommand(ae.message, ae.subcommand)
95
96 return ret
97
98if __name__ == '__main__':
99 try:
100 ret = main()
101 except Exception:
102 ret = 1
103 import traceback
104 traceback.print_exc()
105 sys.exit(ret)