xref: /openbmc/openbmc/poky/meta/lib/oeqa/sdk/context.py (revision 82c905dc)
1#
2# Copyright (C) 2016 Intel Corporation
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import sys
9import glob
10import re
11
12from oeqa.core.context import OETestContext, OETestContextExecutor
13
14class OESDKTestContext(OETestContext):
15    sdk_files_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "files")
16
17    def __init__(self, td=None, logger=None, sdk_dir=None, sdk_env=None,
18            target_pkg_manifest=None, host_pkg_manifest=None):
19        super(OESDKTestContext, self).__init__(td, logger)
20
21        self.sdk_dir = sdk_dir
22        self.sdk_env = sdk_env
23        self.target_pkg_manifest = target_pkg_manifest
24        self.host_pkg_manifest = host_pkg_manifest
25
26    def _hasPackage(self, manifest, pkg, regex=False):
27        if regex:
28            # do regex match
29            pat = re.compile(pkg)
30            for p in manifest.keys():
31                if pat.search(p):
32                    return True
33        else:
34            # do exact match
35            if pkg in manifest.keys():
36                return True
37        return False
38
39    def hasHostPackage(self, pkg, regex=False):
40        return self._hasPackage(self.host_pkg_manifest, pkg, regex=regex)
41
42    def hasTargetPackage(self, pkg, multilib=False, regex=False):
43        if multilib:
44            # match multilib according to sdk_env
45            mls = self.td.get('MULTILIB_VARIANTS', '').split()
46            for ml in mls:
47                if ('ml'+ml) in self.sdk_env:
48                    pkg = ml + '-' + pkg
49        return self._hasPackage(self.target_pkg_manifest, pkg, regex=regex)
50
51class OESDKTestContextExecutor(OETestContextExecutor):
52    _context_class = OESDKTestContext
53
54    name = 'sdk'
55    help = 'sdk test component'
56    description = 'executes sdk tests'
57
58    default_cases = [os.path.join(os.path.abspath(os.path.dirname(__file__)),
59            'cases')]
60    default_test_data = None
61
62    def register_commands(self, logger, subparsers):
63        super(OESDKTestContextExecutor, self).register_commands(logger, subparsers)
64
65        sdk_group = self.parser.add_argument_group('sdk options')
66        sdk_group.add_argument('--sdk-env', action='store',
67            help='sdk environment')
68        sdk_group.add_argument('--target-manifest', action='store',
69            help='sdk target manifest')
70        sdk_group.add_argument('--host-manifest', action='store',
71            help='sdk host manifest')
72
73        sdk_dgroup = self.parser.add_argument_group('sdk display options')
74        sdk_dgroup.add_argument('--list-sdk-env', action='store_true',
75            default=False, help='sdk list available environment')
76
77        # XXX this option is required but argparse_oe has a bug handling
78        # required options, seems that don't keep track of already parsed
79        # options
80        sdk_rgroup = self.parser.add_argument_group('sdk required options')
81        sdk_rgroup.add_argument('--sdk-dir', required=False, action='store',
82            help='sdk installed directory')
83
84        self.parser.add_argument('-j', '--num-processes', dest='processes', action='store',
85                type=int, help="number of processes to execute in parallel with")
86
87    @staticmethod
88    def _load_manifest(manifest):
89        pkg_manifest = {}
90        if manifest:
91            with open(manifest) as f:
92                for line in f:
93                    (pkg, arch, version) = line.strip().split()
94                    pkg_manifest[pkg] = (version, arch)
95
96        return pkg_manifest
97
98    def _process_args(self, logger, args):
99        super(OESDKTestContextExecutor, self)._process_args(logger, args)
100
101        self.tc_kwargs['init']['sdk_dir'] = args.sdk_dir
102        self.tc_kwargs['init']['sdk_env'] = self.sdk_env
103        self.tc_kwargs['init']['target_pkg_manifest'] = \
104                OESDKTestContextExecutor._load_manifest(args.target_manifest)
105        self.tc_kwargs['init']['host_pkg_manifest'] = \
106                OESDKTestContextExecutor._load_manifest(args.host_manifest)
107        self.tc_kwargs['run']['processes'] = args.processes
108
109    @staticmethod
110    def _get_sdk_environs(sdk_dir):
111        sdk_env = {}
112
113        environ_pattern = sdk_dir + '/environment-setup-*'
114        full_sdk_env = glob.glob(sdk_dir + '/environment-setup-*')
115        for env in full_sdk_env:
116            m = re.search('environment-setup-(.*)', env)
117            if m:
118                sdk_env[m.group(1)] = env
119
120        return sdk_env
121
122    def _display_sdk_envs(self, log, args, sdk_envs):
123        log("Available SDK environments at directory %s:" \
124                % args.sdk_dir)
125        log("")
126        for env in sdk_envs:
127            log(env)
128
129    def run(self, logger, args):
130        import argparse_oe
131
132        if not args.sdk_dir:
133            raise argparse_oe.ArgumentUsageError("No SDK directory "\
134                   "specified please do, --sdk-dir SDK_DIR", self.name)
135
136        sdk_envs = OESDKTestContextExecutor._get_sdk_environs(args.sdk_dir)
137        if not sdk_envs:
138            raise argparse_oe.ArgumentUsageError("No available SDK "\
139                   "environments found at %s" % args.sdk_dir, self.name)
140
141        if args.list_sdk_env:
142            self._display_sdk_envs(logger.info, args, sdk_envs)
143            sys.exit(0)
144
145        if not args.sdk_env in sdk_envs:
146            self._display_sdk_envs(logger.error, args, sdk_envs)
147            raise argparse_oe.ArgumentUsageError("No valid SDK "\
148                   "environment (%s) specified" % args.sdk_env, self.name)
149
150        self.sdk_env = sdk_envs[args.sdk_env]
151        return super(OESDKTestContextExecutor, self).run(logger, args)
152
153_executor_class = OESDKTestContextExecutor
154