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