summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/sdk/context.py
blob: 0189ed851e201bf0068ead833070c3b1d3c3b365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (C) 2016 Intel Corporation
# Released under the MIT license (see COPYING.MIT)

import os
import sys
import glob
import re

from oeqa.core.context import OETestContext, OETestContextExecutor

class OESDKTestContext(OETestContext):
    sdk_files_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "files")

    def __init__(self, td=None, logger=None, sdk_dir=None, sdk_env=None,
            target_pkg_manifest=None, host_pkg_manifest=None):
        super(OESDKTestContext, self).__init__(td, logger)

        self.sdk_dir = sdk_dir
        self.sdk_env = sdk_env
        self.target_pkg_manifest = target_pkg_manifest
        self.host_pkg_manifest = host_pkg_manifest

    def _hasPackage(self, manifest, pkg):
        for host_pkg in manifest.keys():
            if re.search(pkg, host_pkg):
                return True
        return False

    def hasHostPackage(self, pkg):
        return self._hasPackage(self.host_pkg_manifest, pkg)

    def hasTargetPackage(self, pkg):
        return self._hasPackage(self.target_pkg_manifest, pkg)

class OESDKTestContextExecutor(OETestContextExecutor):
    _context_class = OESDKTestContext

    name = 'sdk'
    help = 'sdk test component'
    description = 'executes sdk tests'

    default_cases = [os.path.join(os.path.abspath(os.path.dirname(__file__)),
            'cases')]
    default_test_data = None

    def register_commands(self, logger, subparsers):
        import argparse_oe

        super(OESDKTestContextExecutor, self).register_commands(logger, subparsers)

        sdk_group = self.parser.add_argument_group('sdk options')
        sdk_group.add_argument('--sdk-env', action='store',
            help='sdk environment')
        sdk_group.add_argument('--target-manifest', action='store',
            help='sdk target manifest')
        sdk_group.add_argument('--host-manifest', action='store',
            help='sdk host manifest')

        sdk_dgroup = self.parser.add_argument_group('sdk display options')
        sdk_dgroup.add_argument('--list-sdk-env', action='store_true',
            default=False, help='sdk list available environment')

        # XXX this option is required but argparse_oe has a bug handling
        # required options, seems that don't keep track of already parsed
        # options
        sdk_rgroup = self.parser.add_argument_group('sdk required options')
        sdk_rgroup.add_argument('--sdk-dir', required=False, action='store', 
            help='sdk installed directory')

    @staticmethod
    def _load_manifest(manifest):
        pkg_manifest = {}
        if manifest:
            with open(manifest) as f:
                for line in f:
                    (pkg, arch, version) = line.strip().split()
                    pkg_manifest[pkg] = (version, arch)

        return pkg_manifest

    def _process_args(self, logger, args):
        super(OESDKTestContextExecutor, self)._process_args(logger, args)

        self.tc_kwargs['init']['sdk_dir'] = args.sdk_dir
        self.tc_kwargs['init']['sdk_env'] = self.sdk_env
        self.tc_kwargs['init']['target_pkg_manifest'] = \
                OESDKTestContextExecutor._load_manifest(args.target_manifest)
        self.tc_kwargs['init']['host_pkg_manifest'] = \
                OESDKTestContextExecutor._load_manifest(args.host_manifest)

    @staticmethod
    def _get_sdk_environs(sdk_dir):
        sdk_env = {}

        environ_pattern = sdk_dir + '/environment-setup-*'
        full_sdk_env = glob.glob(sdk_dir + '/environment-setup-*')
        for env in full_sdk_env:
            m = re.search('environment-setup-(.*)', env)
            if m:
                sdk_env[m.group(1)] = env

        return sdk_env

    def _display_sdk_envs(self, log, args, sdk_envs):
        log("Available SDK environments at directory %s:" \
                % args.sdk_dir)
        log("")
        for env in sdk_envs:
            log(env)

    def run(self, logger, args):
        if not args.sdk_dir:
            raise argparse_oe.ArgumentUsageError("No SDK directory "\
                   "specified please do, --sdk-dir SDK_DIR", self.name)

        sdk_envs = OESDKTestContextExecutor._get_sdk_environs(args.sdk_dir)
        if not sdk_envs:
            raise argparse_oe.ArgumentUsageError("No available SDK "\
                   "enviroments found at %s" % args.sdk_dir, self.name)

        if args.list_sdk_env:
            self._display_sdk_envs(logger.info, args, sdk_envs)
            sys.exit(0)

        if not args.sdk_env in sdk_envs:
            self._display_sdk_envs(logger.error, args, sdk_envs)
            raise argparse_oe.ArgumentUsageError("No valid SDK "\
                   "environment (%s) specified" % args.sdk_env, self.name)

        self.sdk_env = sdk_envs[args.sdk_env]
        super(OESDKTestContextExecutor, self).run(logger, args)

_executor_class = OESDKTestContextExecutor