buildframework/helium/sf/python/pythoncore/lib/ats3/matti2.py
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     1
# -*- encoding: latin-1 -*-
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     2
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     3
#============================================================================ 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     4
#Name        : matti.py 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     5
#Part of     : Helium 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     6
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     7
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     8
#All rights reserved.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
     9
#This component and the accompanying materials are made available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    10
#under the terms of the License "Eclipse Public License v1.0"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    11
#which accompanies this distribution, and is available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    12
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    13
#
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    14
#Initial Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    15
#Nokia Corporation - initial contribution.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    16
#
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    17
#Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    18
#
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    19
#Description:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    20
#===============================================================================
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    21
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    22
"""MATTI test drop generation."""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    23
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    24
# pylint: disable=R0201,R0903,R0902,W0142
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    25
#W0142 => * and ** were used
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    26
#R* removed during refactoring
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    27
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    28
from optparse import OptionParser
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    29
from xml.etree import ElementTree as et
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    30
import logging
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    31
import os
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    32
import re
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    33
import tempfile
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    34
import zipfile
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    35
import pkg_resources # pylint: disable=F0401
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    36
from path import path # pylint: disable=F0401
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    37
import amara
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    38
import ntpath as atspath
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    39
import jinja2 # pylint: disable=F0401
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    40
import ats3.parsers as parser
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    41
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    42
_logger = logging.getLogger('matti')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    43
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    44
# Shortcuts
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    45
E = et.Element
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    46
SE = et.SubElement
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    47
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    48
class Configuration(object):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    49
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    50
    MATTI drop generation configuration.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    51
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    52
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    53
    def __init__(self, opts):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    54
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    55
        Initialize from optparse configuration options.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    56
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    57
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    58
        self._opts = opts
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    59
        # Customize some attributes from how optparse leaves them.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    60
        self.build_drive = path(self._opts.build_drive)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    61
        self.file_store = path(self._opts.file_store)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    62
        self.flash_images = self.split_paths(self._opts.flash_images)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    63
        self.matti_sis_files = self.split_paths(self._opts.matti_sis_files)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    64
        self.test_assets = self.split_paths(self._opts.testasset_location)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    65
        self.template_loc = path(self._opts.template_loc)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    66
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    67
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    68
    def split_paths(self, arg, delim=","):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    69
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    70
        Split the string by delim, removing extra whitespace.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    71
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    72
        return [path(part.strip()) 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    73
                for part in arg.split(delim) if part.strip()]
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    74
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    75
    def __getattr__(self, attr):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    76
        return getattr(self._opts, attr)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    77
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    78
    def __str__(self):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    79
        dump = "Configuration:\n"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    80
        seen = set()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    81
        for key, value in vars(self).items():
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    82
            if not key.startswith("_"):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    83
                dump += "\t%s = %s\n" % (key, value)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    84
                seen.add(key)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    85
        for key, value in vars(self._opts).items():
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    86
            if key not in seen:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    87
                dump += "\t%s = %s\n" % (key, value)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    88
                seen.add(key)                
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    89
        return dump
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    90
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    91
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    92
class MattiTestPlan(object):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    93
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    94
    Tells MATTI server what to test and how.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    95
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    96
    The MATTI test plan from which the test.xml file can be written. The test
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    97
    plan requires TestAsset(s) to perform the tests
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    98
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
    99
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   100
    def __init__(self, config):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   101
        self.pkg_parser = parser.PkgFileParser()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   102
        self.file_store = config.file_store
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   103
        self.matti_timeout = config.matti_timeout
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   104
        self.test_profiles = config.test_profiles
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   105
        self.sierra_enabled = to_bool(config.sierra_enabled)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   106
        self.sierra_parameters = config.sierra_parameters
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   107
        self.test_profiles = config.test_profiles.strip().split(",")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   108
        self.build_drive = config.build_drive
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   109
        self.matti_sis_files = ""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   110
        self.install_files = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   111
        self.matti_task_files = None
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   112
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   113
    def insert_execution_block(self, block_count=1, image_files=None, matti_sis_files=None, test_asset_path=None, matti_parameters=None):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   114
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   115
        Insert Matti tasks and test data files into execution block
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   116
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   117
        self.matti_sis_files = matti_sis_files
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   118
        temp_sis_files = [] 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   119
        if self.matti_sis_files != None:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   120
            for sis_file in self.matti_sis_files:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   121
                temp_sis_files.append(sis_file.split("#"))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   122
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   123
        test_asset_path = test_asset_path
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   124
        if image_files is None:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   125
            image_files = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   126
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   127
        exe_dict = dict(name="exe%d" % block_count, asset_path=test_asset_path, image_files=image_files, matti_sis_files=temp_sis_files)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   128
        exe_dict = dict(exe_dict, test_timeout=self.matti_timeout)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   129
        exe_dict = dict(exe_dict, matti_parameters=matti_parameters)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   130
        exe_dict = dict(exe_dict, sierra_enabled=self.sierra_enabled.lower())
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   131
        exe_dict = dict(exe_dict, sierra_parameters=self.sierra_parameters)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   132
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   133
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   134
        self.matti_task_files = self.create_matti_task_files_list(self.sierra_enabled, test_asset_path)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   135
        exe_dict = dict(exe_dict, matti_task_files=self.matti_task_files)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   136
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   137
        self.install_files = self.create_install_files_list(test_asset_path)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   138
        exe_dict = dict(exe_dict, install_files=self.install_files)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   139
        return exe_dict        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   140
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   141
    def create_matti_task_files_list(self, enabler=None, asset_path=None):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   142
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   143
        Creates list of files needed to include in MATTI execution tasks
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   144
        if sierra.enabled then 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   145
            profiles (.sip files) are included
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   146
        else
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   147
            all ruby (.rb) files are included
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   148
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   149
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   150
        profiles = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   151
        rb_files = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   152
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   153
        #If sierra engine is enabled (set to True)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   154
        if self.sierra_enabled.lower() == "true":
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   155
            profile_path = path(os.path.join(asset_path, "profile"))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   156
            if os.path.exists(profile_path):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   157
                for profile_name in self.test_profiles: 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   158
                    item = list(profile_path.walkfiles("%s.sip"%profile_name.lower().strip()))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   159
                    if len(item) > 0:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   160
                        #profiles.append(os.path.join(profile_path, item[0]))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   161
                        profiles.append(asset_path.rsplit(os.sep, 1)[1] + "/" + "profile" + "/" + item[0].rsplit(os.sep, 1)[1])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   162
                return profiles
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   163
        else: #If sierra engine is not enabled (set to False)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   164
            if os.path.exists(asset_path):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   165
                #returns list(asset_path.walkfiles("*.rb")):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   166
                for item in list(asset_path.walkfiles("*.rb")):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   167
                    rb_files.append(asset_path.rsplit(os.sep, 1)[1] + "/" + item.rsplit(os.sep, 1)[1])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   168
                # Sorting the result, so we ensure they are always in similar order.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   169
                rb_files.sort()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   170
                return rb_files                      
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   171
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   172
    def create_install_files_list(self, asset_path=None):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   173
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   174
        Collects all the .pkg files and extract data
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   175
        Creates  a list of src, dst files.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   176
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   177
        pkg_files = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   178
        if os.path.exists(asset_path):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   179
            pkg_files =  list(asset_path.walkfiles("*.pkg"))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   180
            return self.pkg_parser.get_data_files(pkg_files, self.build_drive)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   181
        else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   182
            return None
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   183
         
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   184
    def __getitem__(self, key):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   185
        return self.__dict__[key]
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   186
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   187
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   188
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   189
class MattiTestDropGenerator(object):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   190
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   191
    Generate test drop zip file for Matti.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   192
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   193
    Generates drop zip files file from Test Assets. The main
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   194
    responsibility of this class is to create testdrop and test.xml
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   195
    file and build a zip file for the MATTI drop.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   196
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   197
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   198
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   199
    def __init__(self):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   200
        self.drop_path_root = path("MATTIDrop")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   201
        self.drop_path = None
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   202
        self.defaults = {}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   203
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   204
    def generate(self, xml_dict, output_file, template_loc=None):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   205
        """Generate a test drop file."""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   206
        xml = self.generate_xml(xml_dict, template_loc)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   207
        return self.generate_drop(xml_dict, xml, output_file)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   208
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   209
    def generate_drop(self, xml_dict, xml, output_file):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   210
        """Generate test drop zip file."""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   211
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   212
        zfile = zipfile.ZipFile(output_file, "w", zipfile.ZIP_DEFLATED)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   213
        try:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   214
            for drop_file, src_file in self.drop_files(xml_dict):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   215
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   216
                _logger.info("   + Adding: %s" % src_file.strip())
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   217
                try:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   218
                    zfile.write(src_file.strip(), drop_file.encode('utf-8'))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   219
                except OSError, expr:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   220
                    _logger.error(expr)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   221
            doc = amara.parse(et.tostring(xml.getroot()))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   222
            _logger.debug("XML output: %s" % doc.xml(indent=u"yes", encoding="ISO-8859-1"))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   223
            zfile.writestr("test.xml", doc.xml(indent="yes", encoding="ISO-8859-1"))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   224
        finally:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   225
            _logger.info("Matti testdrop created successfully!")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   226
            zfile.close()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   227
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   228
    def generate_xml(self, xml_dict, template_loc):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   229
        """ generate an XML file"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   230
        template_loc = path(template_loc).normpath()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   231
        loader = jinja2.ChoiceLoader([jinja2.PackageLoader(__name__, 'templates')])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   232
        env = jinja2.Environment(loader=loader)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   233
        if template_loc is None or not ".xml" in template_loc.lower():
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   234
            template = env.from_string(pkg_resources.resource_string(__name__, 'matti_template.xml'))# pylint: disable=E1101
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   235
        else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   236
            template = env.from_string(open(template_loc).read())# pylint: disable=E1101
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   237
            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   238
        xmltext = template.render(xml_dict=xml_dict, os=os, atspath=atspath, atsself=self).encode('ISO-8859-1')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   239
        #print xmltext
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   240
        return et.ElementTree(et.XML(xmltext))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   241
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   242
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   243
    def generate_testasset_zip(self, xml_dict, output_file=None):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   244
        """Generate TestAsset.zip for the MATTI server"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   245
        filename = xml_dict["temp_directory"].joinpath(r"TestAsset.zip")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   246
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   247
        if output_file != None:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   248
            filename = output_file
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   249
            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   250
        for exe_block in xml_dict["execution_blocks"]:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   251
            testasset_location = path(exe_block["asset_path"])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   252
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   253
            zfile = zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   254
            try:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   255
                for file_ in list(testasset_location.walkfiles()):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   256
                    file_mod = file_.replace(testasset_location, "")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   257
                    zfile.write(file_, file_mod.encode('utf-8'))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   258
            finally:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   259
                zfile.close()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   260
        return filename
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   261
            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   262
    def drop_files(self, xml_dict):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   263
        """Yield a list of drop files."""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   264
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   265
        drop_set = set()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   266
        drop_files = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   267
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   268
        #Adding test asset, there's an execution block for every test asset
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   269
        for execution_block in xml_dict["execution_blocks"]:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   270
            testasset_location = path(execution_block["asset_path"])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   271
            asset_files = list(testasset_location.walkfiles())
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   272
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   273
            drop_path = path(execution_block["name"])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   274
            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   275
            drop_files = ((drop_path.parent, "images", execution_block["image_files"]),
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   276
                          (drop_path.parent,  "sisfiles", execution_block["matti_sis_files"]),
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   277
                          (drop_path.parent,  "mattiparameters", execution_block["matti_parameters"]),
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   278
                          (drop_path.parent,  execution_block["name"], asset_files))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   279
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   280
            for drop_dir, sub_dir, files in drop_files:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   281
                for file_path in files:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   282
                    if file_path != None:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   283
                        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   284
                        #Adding image files to the top level,                         
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   285
                        #Also adding mattiparameters.xml file
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   286
                        if  sub_dir.lower() == "images" or sub_dir.lower() == "mattiparameters":
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   287
                            drop_file = drop_dir.joinpath(sub_dir, file_path.name)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   288
                        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   289
                        #Adding sisfiles, installation of matti sisfiles is a bit different
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   290
                        #than normal sisfiles
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   291
                        elif sub_dir.lower() == "sisfiles":
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   292
                            drop_file = drop_dir.joinpath(sub_dir, path(file_path[0]).name)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   293
                            file_path = path(file_path[0])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   294
                                                    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   295
                        #Adding test asset files                        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   296
                        else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   297
                            temp_file = file_path.rsplit(os.sep, 1)[0]
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   298
                            replace_string = testasset_location.rsplit(os.sep, 1)[0]
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   299
                            drop_file = drop_dir.joinpath(sub_dir + "\\" + temp_file.replace(replace_string, ""), file_path.name)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   300
                            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   301
                        drop_file = drop_file.normpath()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   302
                        if drop_file not in drop_set:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   303
                            drop_set.add(drop_file)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   304
                            yield (drop_file, file_path.normpath())        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   305
       
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   306
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   307
class MattiComponentParser(object):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   308
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   309
    Add information to the XML dictionary
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   310
    """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   311
    def __init__(self, config):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   312
        self.flash_images = [path(p) for p in config.flash_images]
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   313
        self.matti_parameters = [path(config.matti_parameters).normpath()]
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   314
        self.matti_sis_files = config.matti_sis_files
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   315
        self.build_drive = config.build_drive
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   316
        self.test_timeout = config.matti_timeout
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   317
        self.diamonds_build_url = config.diamonds_build_url
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   318
        self.testrun_name = config.testrun_name
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   319
        self.alias_name = config.alias_name
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   320
        self.device_type = config.device_type
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   321
        self.report_email = config.report_email
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   322
        self.email_format = config.email_format
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   323
        self.email_subject = config.email_subject
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   324
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   325
        self.xml_dict = {}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   326
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   327
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   328
    def insert_pre_data(self):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   329
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   330
        Creates a dictionary for the data before
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   331
        the <execution> block starts.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   332
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   333
        self.xml_dict = dict(self.xml_dict, temp_directory=path(tempfile.mkdtemp()))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   334
        self.xml_dict = dict(self.xml_dict, diamonds_build_url=self.diamonds_build_url)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   335
        self.xml_dict = dict(self.xml_dict, testrun_name=self.testrun_name)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   336
        self.xml_dict = dict(self.xml_dict, alias_name=self.alias_name)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   337
        self.xml_dict = dict(self.xml_dict, device_type=self.device_type)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   338
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   339
    def create_execution_block(self, config):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   340
        """Parse flash images and creates execution block for matti"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   341
        execution_block_list = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   342
        block_count = 0
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   343
        for test_asset in config.test_assets:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   344
            if os.path.exists(test_asset):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   345
                test_plan = MattiTestPlan(config)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   346
                block_count += 1
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   347
                execution_block_list.append(test_plan.insert_execution_block(block_count, self.flash_images, self.matti_sis_files, test_asset, self.matti_parameters))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   348
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   349
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   350
        self.xml_dict = dict(self.xml_dict,  execution_blocks=execution_block_list)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   351
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   352
    def insert_post_data(self):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   353
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   354
        Creates a dictionary for the data after
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   355
        the <execution> block ends. Or, Postaction data
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   356
        """
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   357
        self.xml_dict = dict(self.xml_dict, report_email=self.report_email)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   358
        self.xml_dict = dict(self.xml_dict, email_format=self.email_format)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   359
        self.xml_dict = dict(self.xml_dict, email_subject=self.email_subject)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   360
            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   361
        return self.xml_dict
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   362
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   363
def create_drop(config):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   364
    """Create a test drop."""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   365
    xml_dict = {}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   366
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   367
    _logger.debug("initialize Matti dictionary")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   368
    drop_parser = MattiComponentParser(config)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   369
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   370
    #Inserting data for test run and global through out the dictionary
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   371
    drop_parser.insert_pre_data()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   372
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   373
    #for every asset path there should be a
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   374
    #separate execution block
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   375
    drop_parser.create_execution_block(config) 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   376
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   377
    #Inserting reporting and email data (post actions)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   378
    xml_dict = drop_parser.insert_post_data()    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   379
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   380
    generator = MattiTestDropGenerator()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   381
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   382
    _logger.info("generating drop file: %s" % config.drop_file)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   383
    generator.generate(xml_dict, output_file=config.drop_file, template_loc=config.template_loc)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   384
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   385
def to_bool(param):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   386
    """setting a true or false based on a param value"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   387
    param = str(param).lower()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   388
    if "true" == param or "t" == param or "1" == param:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   389
        return "True"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   390
    else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   391
        return "False"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   392
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   393
def main():
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   394
    """Main entry point."""    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   395
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   396
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   397
    cli = OptionParser(usage="%prog [options] PATH1 [PATH2 [PATH3 ...]]")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   398
    cli.add_option("--ats4-enabled", help="ATS4 enabled", default="True")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   399
    cli.add_option("--build-drive", help="Build area root drive")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   400
    cli.add_option("--drop-file", help="Name for the final drop zip file", default="MATTIDrop.zip")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   401
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   402
    cli.add_option("--minimum-flash-images", help="Minimum amount of flash images", default=2)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   403
    cli.add_option("--flash-images", help="Paths to the flash image files", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   404
    cli.add_option("--matti-sis-files", help="Sis files location", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   405
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   406
    cli.add_option("--testasset-location", help="MATTI test assets location", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   407
    cli.add_option("--template-loc", help="Custom template location", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   408
    cli.add_option("--sierra-enabled", help="Enabled or disabled Sierra", default=True)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   409
    cli.add_option("--test-profiles", help="Test profiles e.g. bat, fute", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   410
    cli.add_option("--matti-parameters", help="Location of xml file contains additional parameters for Matti", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   411
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   412
    cli.add_option("--matti-timeout", help="Test execution timeout value (default: %default)", default="60")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   413
    cli.add_option("--sierra-parameters", help="Additional sierra parameters for matti task", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   414
    cli.add_option("--file-store", help="Destination path for reports.", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   415
    cli.add_option("--report-email", help="Email notification receivers",  default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   416
    cli.add_option("--testrun-name", help="Name of the test run", default="run")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   417
    cli.add_option("--alias-name", help="Name of the alias", default="sut_s60")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   418
    cli.add_option("--device-type", help="Device type (e.g. 'PRODUCT')", default="unknown")    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   419
    cli.add_option("--diamonds-build-url", help="Diamonds build url")         
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   420
    cli.add_option("--email-format", help="Format of an email", default="")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   421
    cli.add_option("--email-subject", help="Subject of an email", default="Matti Testing")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   422
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   423
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   424
    cli.add_option("--verbose", help="Increase output verbosity", action="store_true", default=False)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   425
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   426
    opts, _ = cli.parse_args()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   427
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   428
    ats4_enabled = to_bool(opts.ats4_enabled)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   429
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   430
    if ats4_enabled == "False":
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   431
        cli.error("MATTI tests execute on ATS4. Set property 'ats4.enabled'")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   432
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   433
    if not opts.flash_images:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   434
        cli.error("no flash image files given")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   435
    if len(opts.flash_images.split(",")) < int(opts.minimum_flash_images):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   436
        cli.error("Not enough flash files: %i defined, %i needed" % (len(opts.flash_images.split(",")), int(opts.minimum_flash_images) ))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   437
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   438
    if opts.verbose:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   439
        _logger.setLevel(logging.DEBUG)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   440
        logging.basicConfig(level=logging.DEBUG)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   441
    _ = tempfile.mkdtemp()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   442
    config = Configuration(opts)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   443
    create_drop(config)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   444
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   445
if __name__ == "__main__":
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents:
diff changeset
   446
    main()