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