summaryrefslogtreecommitdiffstats
path: root/tools/xmlGenerator/domainGenerator.py
blob: 3e840f0d71f2dd3c93fb10371aec27128c4da0b7 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#! /usr/bin/python
#
# Copyright (c) 2011-2014, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import PyPfw
import EddParser
from PfwBaseTranslator import PfwBaseTranslator, PfwException
import hostConfig

import argparse
import re
import sys
import tempfile
import os
import logging

def wrap_pfw_error_semantic(func):
    def wrapped(*args, **kwargs):
        ok, error = func(*args, **kwargs)
        if not ok:
            raise PfwException(error)

    return wrapped

class PfwTranslator(PfwBaseTranslator):
    """Generates calls to the Pfw's python bindings"""

    def __init__(self, pfw_instance, error_handler):
        super(PfwTranslator, self).__init__()
        self._pfw = pfw_instance
        self._error_handler = error_handler

    def _handleException(self, ex):
        if isinstance(ex, PfwException):
            # catch and handle translation errors...
            self._error_handler(ex, self._getContext())
        else:
            # ...but let any other error fall through
            raise ex

    @wrap_pfw_error_semantic
    def _doCreateDomain(self, name):
        return self._pfw.createDomain(name)

    @wrap_pfw_error_semantic
    def _doSetSequenceAware(self):
        return self._pfw.setSequenceAwareness(self._ctx_domain, True)

    @wrap_pfw_error_semantic
    def _doAddElement(self, path):
        return self._pfw.addConfigurableElementToDomain(self._ctx_domain, path)

    @wrap_pfw_error_semantic
    def _doCreateConfiguration(self, name):
        return self._pfw.createConfiguration(self._ctx_domain, name)

    @wrap_pfw_error_semantic
    def _doSetElementSequence(self, paths):
        return self._pfw.setElementSequence(self._ctx_domain, self._ctx_configuration, paths)

    @wrap_pfw_error_semantic
    def _doSetRule(self, rule):
        return self._pfw.setApplicationRule(self._ctx_domain, self._ctx_configuration, rule)

    @wrap_pfw_error_semantic
    def _doSetParameter(self, path, value):
        ok, _, error = self._pfw.accessConfigurationValue(
                self._ctx_domain, self._ctx_configuration, path, value, True)

        return ok, error


class PfwTranslationErrorHandler:
    def __init__(self):
        self._errors = []
        self._hasFailed = False

    def __call__(self, error, context):
        sys.stderr.write("Error in context {}:\n\t{}\n".format(context, error))
        self._hasFailed = True

    def hasFailed(self):
        return self._hasFailed

class PfwLogger(PyPfw.ILogger):
    def __init__(self):
        super(PfwLogger, self).__init__()
        self.__logger = logging.root.getChild("parameter-framework")

    def log(self, is_warning, message):
        log_func = self.__logger.warning if is_warning else self.__logger.info
        log_func(message)

# If this file is directly executed
if __name__ == "__main__":
    logging.root.setLevel(logging.INFO)

    argparser = argparse.ArgumentParser(description="Parameter-Framework XML \
        Settings file generator")
    argparser.add_argument('--toplevel-config',
            help="Top-level parameter-framework configuration file. Mandatory.",
            metavar="TOPLEVEL_CONFIG_FILE",
            required=True)
    argparser.add_argument('--criteria',
            help="Criteria file, in '<type> <name> : <value> <value...>' \
        format. Mandatory.",
            metavar="CRITERIA_FILE",
            type=argparse.FileType('r'),
            required=True)
    argparser.add_argument('--initial-settings',
            help="Initial XML settings file (containing a \
        <ConfigurableDomains>  tag",
            nargs='?',
            metavar="XML_SETTINGS_FILE")
    argparser.add_argument('--add-domains',
            help="List of single domain files (each containing a single \
        <ConfigurableDomain> tag",
            metavar="XML_DOMAIN_FILE",
            nargs='*',
            dest='xml_domain_files',
            default=[])
    argparser.add_argument('--add-edds',
            help="List of files in EDD syntax (aka \".pfw\" files)",
            metavar="EDD_FILE",
            type=argparse.FileType('r'),
            nargs='*',
            default=[],
            dest='edd_files')
    argparser.add_argument('--schemas-dir',
            help="Directory of parameter-framework XML Schemas for generation \
        validation",
            default=None)
    argparser.add_argument('--target-schemas-dir',
            help="Directory of parameter-framework XML Schemas on target \
        machine (may be different than generating machine). \
        Defaults to \"Schemas\"",
            default="Schemas")
    argparser.add_argument('--validate',
            help="Validate the settings against XML schemas",
            action='store_true')
    argparser.add_argument('--verbose',
            action='store_true')

    args = argparser.parse_args()

    #
    # Criteria file
    #
    # This file define one criteria per line; they should respect this format:
    #
    # <type> <name> : <values>
    #
    # Where <type> is 'InclusiveCriterion' or 'ExclusiveCriterion';
    #       <name> is any string w/o whitespace
    #       <values> is a list of whitespace-separated values, each of which is any
    #                string w/o a whitespace
    criteria_pattern = re.compile(
        r"^(?P<type>(?:Inclusive|Exclusive)Criterion)\s*" \
        r"(?P<name>\S+)\s*:\s*" \
        r"(?P<values>.*)$")
    criterion_inclusiveness_table = {
        'InclusiveCriterion' : True,
        'ExclusiveCriterion' : False}
    all_criteria = []

    # Parse the criteria file
    for line_number, line in enumerate(args.criteria, 1):
        match = criteria_pattern.match(line)
        if not match:
            raise ValueError("The following line is invalid: {}:{}\n{}".format(
                args.criteria.name, line_number, line))

        criterion_name = match.groupdict()['name']
        criterion_type = match.groupdict()['type']
        criterion_values = re.split("\s*", match.groupdict()['values'])

        criterion_inclusiveness = criterion_inclusiveness_table[criterion_type]

        all_criteria.append({
            "name" : criterion_name,
            "inclusive" : criterion_inclusiveness,
            "values" : criterion_values})

    #
    # EDD files (aka ".pfw" files)
    #
    parsed_edds = []
    for edd_file in args.edd_files:
        try:
            root = parser = EddParser.Parser().parse(edd_file, args.verbose)
        except EddParser.MySyntaxError as ex:
            logging.critical(str(ex))
            logging.info("EXIT ON FAILURE")
            exit(2)

        try:
            root.propagate()
        except EddParser.MyPropagationError, ex :
            logging.critical(str(ex))
            logging.info("EXIT ON FAILURE")
            exit(1)

        parsed_edds.append((edd_file.name, root))

    # We need to modify the toplevel configuration file to account for differences
    # between development setup and target (installation) setup, in particular, the
    # TuningMode must be enforced, regardless of what will be allowed on the target
    with tempfile.NamedTemporaryFile(mode='w') as fake_toplevel_config:
        install_path = os.path.dirname(os.path.realpath(args.toplevel_config))
        hostConfig.configure(
                infile=args.toplevel_config,
                outfile=fake_toplevel_config,
                structPath=install_path)
        fake_toplevel_config.flush()

        # Create a new Pfw instance
        pfw = PyPfw.ParameterFramework(fake_toplevel_config.name)

        # create and inject all the criteria
        logging.info("Creating all criteria")
        for criterion in all_criteria:
            criterion_type = pfw.createSelectionCriterionType(criterion['inclusive'])

            for numerical, literal in enumerate(criterion['values']):
                if criterion['inclusive']:
                    # inclusive criteria are "bitfields"
                    numerical = 1 << numerical

                ok = criterion_type.addValuePair(numerical, literal)
                if not ok:
                    logging.critical("valuepair {}/{} rejected for {}".format(
                        numerical, literal, criterion['name']))
                    exit(1)

            # we don't need the reference to the created criterion type; ignore the
            # return value
            pfw.createSelectionCriterion(criterion['name'], criterion_type)

        # Set failure conditions
        pfw.setFailureOnMissingSubsystem(False)
        pfw.setFailureOnFailedSettingsLoad(False)
        if args.validate:
            pfw.setValidateSchemasOnStart(True)
            if args.schemas_dir is not None:
                schemas_dir = args.schemas_dir
            else:
                schemas_dir = os.path.join(install_path, "Schemas")
            pfw.setSchemaFolderLocation(schemas_dir)

        logger = PfwLogger()
        pfw.setLogger(logger)

        # Disable the remote interface because we don't need it and it might
        # get in the way (e.g. the port is already in use)
        pfw.setForceNoRemoteInterface(True)

        # Finally, start the Pfw
        ok, error = pfw.start()
        if not ok:
            logging.critical("Error while starting the pfw: {}".format(error))
            exit(1)

    ok, error = pfw.setTuningMode(True)
    if not ok:
        logging.critical(error)
        exit(1)

    # Import initial settings file
    if args.initial_settings:
        initial_settings = os.path.realpath(args.initial_settings)
        logging.info(
            "Importing initial settings file {}".format(initial_settings))
        ok, error = pfw.importDomainsXml(initial_settings, True, True)
        if not ok:
            logging.critical(error)
            exit(1)

    # Import each standalone domain files
    for domain_file in args.xml_domain_files:
        logging.info("Importing single domain file {}".format(domain_file))
        ok, error = pfw.importSingleDomainXml(os.path.realpath(domain_file), False)
        if not ok:
            logging.critical(error)
            exit(1)

    # Parse and inject each EDD file
    error_handler = PfwTranslationErrorHandler()
    translator = PfwTranslator(pfw, error_handler)

    for filename, parsed_edd in parsed_edds:
        logging.info("Translating and injecting EDD file {}".format(filename))
        parsed_edd.translate(translator)
        if error_handler.hasFailed():
            logging.error("Error while importing parsed EDD files.\n")
            exit(1)

    # dirty hack: we change the schema location (right before exporting the
    # domains) to their location on the target (which may be different than on
    # the machine that is generating the domains)
    pfw.setSchemaFolderLocation(args.target_schemas_dir)

    # Export the resulting settings to the standard output
    ok, domains, error = pfw.exportDomainsXml("", True, False)
    sys.stdout.write(domains)