blob: 82e4c19bfc129a74e1a8b61ad9bfb039e58ece04 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001# Copyright (C) 2016 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4import os
5import sys
6import glob
7import re
8
Brad Bishopf86d0552018-12-04 14:18:15 -08009from oeqa.core.context import OETestContext, OETestContextExecutor
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010
Brad Bishopf86d0552018-12-04 14:18:15 -080011class OESDKTestContext(OETestContext):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050012 sdk_files_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "files")
13
14 def __init__(self, td=None, logger=None, sdk_dir=None, sdk_env=None,
15 target_pkg_manifest=None, host_pkg_manifest=None):
16 super(OESDKTestContext, self).__init__(td, logger)
17
18 self.sdk_dir = sdk_dir
19 self.sdk_env = sdk_env
20 self.target_pkg_manifest = target_pkg_manifest
21 self.host_pkg_manifest = host_pkg_manifest
22
23 def _hasPackage(self, manifest, pkg):
24 for host_pkg in manifest.keys():
25 if re.search(pkg, host_pkg):
26 return True
27 return False
28
29 def hasHostPackage(self, pkg):
30 return self._hasPackage(self.host_pkg_manifest, pkg)
31
32 def hasTargetPackage(self, pkg):
33 return self._hasPackage(self.target_pkg_manifest, pkg)
34
35class OESDKTestContextExecutor(OETestContextExecutor):
36 _context_class = OESDKTestContext
37
38 name = 'sdk'
39 help = 'sdk test component'
40 description = 'executes sdk tests'
41
42 default_cases = [os.path.join(os.path.abspath(os.path.dirname(__file__)),
43 'cases')]
44 default_test_data = None
45
46 def register_commands(self, logger, subparsers):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 super(OESDKTestContextExecutor, self).register_commands(logger, subparsers)
48
49 sdk_group = self.parser.add_argument_group('sdk options')
50 sdk_group.add_argument('--sdk-env', action='store',
51 help='sdk environment')
52 sdk_group.add_argument('--target-manifest', action='store',
53 help='sdk target manifest')
54 sdk_group.add_argument('--host-manifest', action='store',
55 help='sdk host manifest')
56
57 sdk_dgroup = self.parser.add_argument_group('sdk display options')
58 sdk_dgroup.add_argument('--list-sdk-env', action='store_true',
59 default=False, help='sdk list available environment')
60
61 # XXX this option is required but argparse_oe has a bug handling
62 # required options, seems that don't keep track of already parsed
63 # options
64 sdk_rgroup = self.parser.add_argument_group('sdk required options')
65 sdk_rgroup.add_argument('--sdk-dir', required=False, action='store',
66 help='sdk installed directory')
67
68 @staticmethod
69 def _load_manifest(manifest):
70 pkg_manifest = {}
71 if manifest:
72 with open(manifest) as f:
73 for line in f:
74 (pkg, arch, version) = line.strip().split()
75 pkg_manifest[pkg] = (version, arch)
76
77 return pkg_manifest
78
79 def _process_args(self, logger, args):
80 super(OESDKTestContextExecutor, self)._process_args(logger, args)
81
82 self.tc_kwargs['init']['sdk_dir'] = args.sdk_dir
83 self.tc_kwargs['init']['sdk_env'] = self.sdk_env
84 self.tc_kwargs['init']['target_pkg_manifest'] = \
85 OESDKTestContextExecutor._load_manifest(args.target_manifest)
86 self.tc_kwargs['init']['host_pkg_manifest'] = \
87 OESDKTestContextExecutor._load_manifest(args.host_manifest)
88
89 @staticmethod
90 def _get_sdk_environs(sdk_dir):
91 sdk_env = {}
92
93 environ_pattern = sdk_dir + '/environment-setup-*'
94 full_sdk_env = glob.glob(sdk_dir + '/environment-setup-*')
95 for env in full_sdk_env:
96 m = re.search('environment-setup-(.*)', env)
97 if m:
98 sdk_env[m.group(1)] = env
99
100 return sdk_env
101
102 def _display_sdk_envs(self, log, args, sdk_envs):
103 log("Available SDK environments at directory %s:" \
104 % args.sdk_dir)
105 log("")
106 for env in sdk_envs:
107 log(env)
108
109 def run(self, logger, args):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110 import argparse_oe
111
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 if not args.sdk_dir:
113 raise argparse_oe.ArgumentUsageError("No SDK directory "\
114 "specified please do, --sdk-dir SDK_DIR", self.name)
115
116 sdk_envs = OESDKTestContextExecutor._get_sdk_environs(args.sdk_dir)
117 if not sdk_envs:
118 raise argparse_oe.ArgumentUsageError("No available SDK "\
119 "enviroments found at %s" % args.sdk_dir, self.name)
120
121 if args.list_sdk_env:
122 self._display_sdk_envs(logger.info, args, sdk_envs)
123 sys.exit(0)
124
125 if not args.sdk_env in sdk_envs:
126 self._display_sdk_envs(logger.error, args, sdk_envs)
127 raise argparse_oe.ArgumentUsageError("No valid SDK "\
128 "environment (%s) specified" % args.sdk_env, self.name)
129
130 self.sdk_env = sdk_envs[args.sdk_env]
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131 return super(OESDKTestContextExecutor, self).run(logger, args)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132
133_executor_class = OESDKTestContextExecutor