configurationengine/source/scripts/conesub_compare.py
author terytkon
Thu, 11 Mar 2010 17:04:37 +0200
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
permissions -rw-r--r--
Adding EPL version of configurationengine.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     1
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     2
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     3
# All rights reserved.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     4
# This component and the accompanying materials are made available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     5
# under the terms of "Eclipse Public License v1.0"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     6
# which accompanies this distribution, and is available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     8
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     9
# Initial Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    11
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    12
# Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    13
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    14
# Description: 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    15
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    16
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    17
import os
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    18
import sys
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    19
import logging
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    20
from optparse import OptionParser, OptionGroup
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    21
import codecs
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    22
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    23
import cone_common
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    24
import time
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    25
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    26
from cone.public import api, plugin, utils, exceptions
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    27
from time import gmtime, strftime
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    28
import report_util
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    29
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    30
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    31
VERSION = '1.0'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    32
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    33
log = logging.getLogger('cone')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    34
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    35
REPORT_SHORTCUTS = {
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    36
    'api': report_util.ReportShortcut(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    37
        os.path.join(ROOT_PATH, 'compare_api_report_template.html'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    38
        'api_comparison.html',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    39
        'Report changes in feature definitions'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    40
                       
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    41
    'data': report_util.ReportShortcut(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    42
        os.path.join(ROOT_PATH, 'compare_data_report_template.html'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    43
        'data_comparison.html',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    44
        'Report changes in data values'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    45
                       
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    46
    'crml_dc': report_util.ReportShortcut(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    47
        os.path.join(ROOT_PATH, 'crml_dc_report_template.html'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    48
        'crml_dc_report.html',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    49
        'Report CRML data compatibility issues'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    50
                       
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    51
    'crml_dc_csv': report_util.ReportShortcut(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    52
        os.path.join(ROOT_PATH, 'crml_dc_report_template.csv'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    53
        'crml_dc_report.csv',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    54
        'Report CRML data compatibility issues (CSV format)'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    55
}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    56
DEFAULT_SHORTCUT = 'data'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    57
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    58
def main():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    59
    shortcut_container = report_util.ReportShortcutContainer(REPORT_SHORTCUTS,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    60
                                                             DEFAULT_SHORTCUT)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    61
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    62
    gset = cone_common.get_settings([os.path.join(ROOT_PATH,'conesub_compare.cfg')])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    63
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    64
    parser = OptionParser(version="%%prog %s" % VERSION)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    65
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    66
    parser.add_options(cone_common.COMMON_OPTIONS)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    67
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    68
    parser.add_option("-p", "--project",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    69
                       dest="project",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    70
                       help="defines the location of current project. Default is the current working directory.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    71
                       default=".",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    72
                       metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    73
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    74
    group = OptionGroup(parser, 'Compare options',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    75
                    'The generate function will create target files from a specific configuration.'\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    76
                    'The generate will always work with read-only mode of the project, so no changes are saved to project')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    77
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    78
    group.add_option("-s", "--sourceconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    79
                        dest="sourceconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    80
                        help="defines the name of the sourceconfiguration for the compare action. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    81
                             "The configuration is expected to be located current storage.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    82
                        metavar="CONFIG")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    83
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    84
    group.add_option("-t", "--targetconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    85
                        dest="targetconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    86
                        help="defines the name of the target configuration for the compare action. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    87
                             "The configuration can be located in the current storage or it the configuration"\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    88
                             "definition can contain a path to a storage. The storage definition is given as a path"\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    89
                             "before semicolon. e.g. x:\data\configproject;productx.confml, test.cpf;root.confml",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    90
                        metavar="CONFIG")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    91
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    92
#    group.add_option("--compare-dict",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    93
#                   dest="compare_dict",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    94
#                   action="store",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    95
#                   type="string",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    96
#                   help="Compare elements as a dictionary",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    97
#                   metavar="DICT",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    98
#                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    99
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   100
    group.add_option("--report",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   101
                   dest="report_file",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   102
                   action="store",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   103
                   type="string",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   104
                   help="The file where the comparison report is written."\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   105
                        "By default this value is determined by the used "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   106
                        "report type. Example: --report report.html.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   107
                   metavar="FILE",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   108
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   109
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   110
    group.add_option("--template",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   111
                   dest="template",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   112
                   action="store",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   113
                   type="string",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   114
                   help="Template used in a report generation. By default "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   115
                        "this value is determined by the used report type. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   116
                        "Example: --template report_template.html.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   117
                   metavar="FILE",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   118
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   119
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   120
    group.add_option("--report-type",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   121
                   dest="report_type",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   122
                   action="store",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   123
                   type="string",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   124
                   help="The type of the report to generate. This is a convenience "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   125
                        "switch for setting the used template.                     "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   126
                        "Possible values:\n                                      "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   127
                        + shortcut_container.get_shortcut_help_text(),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   128
                   metavar="TYPE",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   129
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   130
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   131
    group.add_option("--impl-filter",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   132
                   dest="impl_filter",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   133
                   action="store",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   134
                   type="string",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   135
                   help="The pattern used for filtering implementations for the "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   136
                        "comparison. See the switch --impl in action generate for "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   137
                        "more info. ",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   138
                   metavar="PATTERN",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   139
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   140
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   141
    start_time = time.time()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   142
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   143
    parser.add_option_group(group)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   144
    (options, args) = parser.parse_args()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   145
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   146
    cone_common.handle_common_options(options, settings=gset)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   147
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   148
    if not options.sourceconfiguration: parser.error("sourceconfiguration must be given")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   149
    if not options.targetconfiguration: parser.error("targetconfiguration must be given")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   150
    if options.report_type and options.template:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   151
        parser.error("both --report-type and --template supplied; use only one of them")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   152
    if not shortcut_container.is_valid_shortcut(options.report_type):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   153
        parser.error("Invalid report type: %s" % options.report_type)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   154
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   155
    template_file, report_file = shortcut_container.determine_template_and_report(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   156
        options.report_type,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   157
        options.template,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   158
        options.report_file,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   159
        'comparison')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   160
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   161
    action = CompareAction(options.project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   162
                           options.sourceconfiguration,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   163
                           options.targetconfiguration,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   164
                           template    = template_file,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   165
                           report_file = report_file,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   166
                           impl_filter = options.impl_filter)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   167
    result = action.run_action()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   168
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   169
    resmap = {True: 0, False: 1}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   170
    sys.exit(resmap[result])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   171
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   172
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   173
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   174
class CompareAction(object):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   175
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   176
    def __init__(self, current, sourceconfig, targetconfig, **kwargs):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   177
        self.current = current
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   178
        self.sourceconfig = sourceconfig
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   179
        self.targetconfig = targetconfig
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   180
        self.reportfile = kwargs.get("report_file", 'compare.html')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   181
        self.reporttemplate = kwargs.get('template', '')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   182
        self.columns = kwargs.get('columns', None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   183
        self.impl_filter = kwargs.get('impl_filter',)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   184
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   185
    def run_action(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   186
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   187
        Run the action.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   188
        @return: True if successful, False if not.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   189
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   190
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   191
        currentprj = api.Project(api.Storage.open(self.current,"r"))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   192
        targetprj = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   193
        (targetproject,targetconf) = self.parse_target_configuration(self.targetconfig)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   194
        print "Compare %s <> %s in %s" % (self.sourceconfig, targetconf, targetproject)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   195
        if targetproject != '':
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   196
            targetprj = api.Project(api.Storage.open(targetproject,"r"))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   197
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   198
            # The comparison doesn't seem to work if the same project
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   199
            #    object is used
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   200
            #targetprj = currentprj
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   201
            targetprj = api.Project(api.Storage.open(self.current,"r"))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   202
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   203
        source = currentprj.get_configuration(self.sourceconfig)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   204
        target = targetprj.get_configuration(targetconf)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   205
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   206
        print "Writing report to %s" % self.reportfile
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   207
        sourcedata = {}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   208
        targetdata = {}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   209
        sourcedata['name'] = self.sourceconfig
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   210
        targetdata['name'] = self.targetconfig
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   211
        sourcedata['features'] = self.get_feature_api_data(source)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   212
        targetdata['features'] = self.get_feature_api_data(target)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   213
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   214
        impl_comp_data_proxy = ImplComparisonDataProxy(source,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   215
                                                       target,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   216
                                                       self.impl_filter)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   217
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   218
        template_data = {'sourcedata': sourcedata,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   219
                         'targetdata': targetdata,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   220
                         'impl_data':  impl_comp_data_proxy}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   221
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   222
        result = report_util.generate_report(self.reporttemplate,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   223
                                             self.reportfile,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   224
                                             {'data': template_data})
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   225
        print "Done."
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   226
        return result
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   227
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   228
    def parse_target_configuration(self,configpath):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   229
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   230
        return tuple (storage, configpath) from storagepath;root.confml.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   231
        returns ('', 'root.confml') from root.confml 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   232
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   233
        elems = configpath.rsplit(';',2)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   234
        if len(elems) > 1:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   235
            return (elems[0],elems[1])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   236
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   237
            return ('',elems[0])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   238
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   239
    def get_feature_api_data(self,config):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   240
        # Traverse through all features in the api
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   241
        # and construct the data rows
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   242
        data = {}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   243
        for elem in config.get_default_view().get_features('**'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   244
            data[elem.fqr] = elem._obj
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   245
        return data
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   246
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   247
class ImplComparisonDataProxy(object):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   248
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   249
    Proxy object for loading implementation comparison data on demand.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   250
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   251
    def __init__(self, sourceconfig, targetconfig, impl_filter):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   252
        self.sourceconfig = sourceconfig
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   253
        self.targetconfig = targetconfig
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   254
        if impl_filter is None: self.impl_filter = '.*'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   255
        else:                   self.impl_filter = impl_filter
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   256
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   257
        self._flat_data = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   258
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   259
    @property
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   260
    def flat(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   261
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   262
            if self._flat_data is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   263
                self._flat_data = self._get_flat_comparison_data()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   264
            return self._flat_data
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   265
        except Exception, e:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   266
            utils.log_exception(log, 'Error retrieving ImplComparisonDataProxy.flat!')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   267
            raise
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   268
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   269
    def _get_flat_comparison_data(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   270
        log.debug("Loading implementations for comparison (impl filter = '%s')..." % (self.impl_filter))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   271
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   272
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   273
            source_impls = plugin.get_impl_set(self.sourceconfig, self.impl_filter)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   274
            target_impls = plugin.get_impl_set(self.targetconfig, self.impl_filter)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   275
        except Exception, e:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   276
            utils.log_exception(log, 'Failed to load implementations!')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   277
            raise
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   278
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   279
        log.debug("%d impl(s) in source." % len(source_impls))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   280
        log.debug("%d impl(s) in target." % len(target_impls))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   281
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   282
        log.debug("Generating flat comparison results...")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   283
        result = source_impls.flat_compare(target_impls)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   284
        log.debug("Generated %d result row(s)" % len(result))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   285
        return result
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   286
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   287
if __name__ == "__main__":
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   288
    main()