configurationengine/source/scripts/conesub_merge.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 sys
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    18
import logging
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    19
from optparse import OptionParser, OptionGroup
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    20
import cone_common
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    21
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    22
from cone.public import api, utils, exceptions
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    23
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    24
VERSION = '1.0'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    25
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    26
logger    = logging.getLogger('cone')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    27
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    28
class MergeFailedException(Exception):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    29
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    30
    Exception raised if the merge failed for some reason.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    31
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    32
    pass
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    33
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    34
class MergePolicy(object):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    35
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    36
    Merge policy constants.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    37
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    38
    #: Replace/add files from the source layer into the
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    39
    #: target layer, preserving files in the target layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    40
    #: that do not exist in the source layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    41
    REPLACE_ADD = 'replace-add'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    42
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    43
    #: Overwrite the entire target layer, so that it contains
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    44
    #: only the contents of the source layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    45
    OVERWRITE_LAYER = 'overwrite-layer'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    46
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    47
    ALL = (REPLACE_ADD, OVERWRITE_LAYER)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    48
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    49
    @classmethod
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    50
    def is_valid(cls, policy):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    51
        return policy in cls.ALL
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    52
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    53
def get_new_layer_name(oldname,rootconfig):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    54
    newpath = utils.resourceref.get_path(oldname)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    55
    newpath+= "_" + utils.resourceref.remove_ext(utils.resourceref.get_filename(rootconfig))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    56
    newpath+= "/" + utils.resourceref.get_filename(oldname)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    57
    return newpath
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    58
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    59
def merge_configuration_layer(sourceconfig, targetconfig, merge_policy):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    60
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    61
    Merge the contents of the source layer into the target layer.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    62
    @param sourceconfig: Source layer root configuration object.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    63
    @param targetconfig: Target layer root configuration object.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    64
    @param merge_policy: The used layer merge policy.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    65
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    66
    # If policy tells to entirely overwrite the layer, remove all
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    67
    # resources from the layer first
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    68
    if merge_policy == MergePolicy.OVERWRITE_LAYER:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    69
        # Remove configurations from the layer root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    70
        confs = targetconfig.list_configurations()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    71
        for conf in confs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    72
            targetconfig.remove_configuration(conf)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    73
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    74
        # Remove all related resources
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    75
        layerobj = targetconfig.get_layer()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    76
        # Round one: remove all files
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    77
        resources = layerobj.list_all_related(empty_folders=False)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    78
        for res in resources: layerobj.delete_resource(res)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    79
        # Round two: remove any remaining empty directories
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    80
        resources = layerobj.list_all_related(empty_folders=True)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    81
        for res in resources: layerobj.delete_folder(res)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    82
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    83
    # Find all ConfML files
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    84
    confml_resources = sourceconfig.list_configurations()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    85
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    86
    # Find all other related files and folders (content/, doc/ etc.)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    87
    layerobj = sourceconfig.get_layer()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    88
    targetobj = targetconfig.get_layer()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    89
    other_resources = layerobj.list_all_related(empty_folders=True)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    90
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    91
    # Copy the resources to the target configuration
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    92
    for res_path in confml_resources + other_resources:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    93
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    94
            rres = layerobj.open_resource(res_path,"rb")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    95
            wres = targetobj.open_resource(res_path,"wb")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    96
            print "Copying %s" % rres.path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    97
            logger.info('Copying layer resource %s to %s' % (rres.path, wres.path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    98
            wres.write(rres.read())
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    99
            wres.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   100
            rres.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   101
        except exceptions.NotResource:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   102
            # If it isn't a resource (file), it's a folder
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   103
            targetobj.create_folder(res_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   104
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   105
    # Remove all configurations from the target layer root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   106
    already_included = targetconfig.list_configurations()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   107
    for confml_path in already_included:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   108
        targetconfig.remove_configuration(confml_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   109
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   110
    # Include them back
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   111
    for confml_path in already_included:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   112
        targetconfig.include_configuration(confml_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   113
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   114
    # Include all added configurations
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   115
    for confml_path in confml_resources:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   116
        if confml_path not in already_included:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   117
            print "Including %s in layer root %s" % (confml_path, targetconfig.path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   118
            targetconfig.include_configuration(confml_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   119
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   120
def find_layers_to_merge(layer_indices, rename, sourceconfig, targetconfig):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   121
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   122
    Return a list of layers to merge.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   123
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   124
    @param layer_indices: List of layer indices to merge, can also be
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   125
        None to indicate that all layers are to be merged.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   126
    @param rename: True if the layers should be renamed in the target
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   127
        config, False if not.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   128
    @return: A list of tuples (layer_root, target_layer_root), where
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   129
        layer_root is the path to the layer root in the source
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   130
        configuration and target_layer_root the one in the target
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   131
        configuration.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   132
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   133
    # Get a list of all configurations to merge
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   134
    if layer_indices is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   135
        mergeconfigs = sourceconfig.list_configurations()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   136
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   137
        mergeconfigs = sort_mergeconfigs(layer_indices, sourceconfig.list_configurations())
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   138
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   139
    result = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   140
    if rename:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   141
        for source_path in mergeconfigs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   142
            target_path = get_new_layer_name(source_path, targetconfig.path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   143
            result.append((source_path, target_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   144
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   145
        for source_path in mergeconfigs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   146
            result.append((source_path, source_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   147
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   148
    return result
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   149
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   150
def sort_mergeconfigs(layers, sourceconfigs):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   151
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   152
    Return a correctly sorted list of source configuration layers.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   153
    @param layers: List of the indices of the layers to merg. Can be None, in
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   154
        which case all layers are returned.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   155
    @param sourceconfigs: List of all configuration layer root paths in the
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   156
        source project.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   157
    @return: List of configuration layer root paths.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   158
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   159
    sorted_configs = [None for _ in xrange(len(sourceconfigs))]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   160
    for layer in layers:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   161
        sorted_configs[layer]=sourceconfigs[layer]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   162
    sorted_configs = filter(lambda x: x != None, sorted_configs)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   163
    return sorted_configs
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   164
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   165
def merge_config_root_to_config_root(source_project, target_project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   166
                                     source_config, target_config,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   167
                                     layer_indices, rename,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   168
                                     merge_policy):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   169
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   170
    Merge the source configuration root to the target configuration root.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   171
    @param layer_indices: List of layer indices to specify the layers
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   172
        to merge, can be None.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   173
    @param rename: If True, the merged layers are renamed based on the
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   174
        name of the target configuration root.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   175
    @param merge_policy: The used merge policy.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   176
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   177
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   178
    def get_active_root_if_necessary(project, configuration, name):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   179
        if configuration:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   180
            return configuration
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   181
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   182
            active_root = project.get_storage().get_active_configuration()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   183
            if active_root == "":
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   184
                raise MergeFailedException("No %s configuration given and the project does not have an active root" % name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   185
            else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   186
                return active_root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   187
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   188
    target_root = get_active_root_if_necessary(target_project, target_config, 'target')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   189
    source_root = get_active_root_if_necessary(source_project, source_config, 'source')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   190
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   191
    print "Target config:  %s" % target_root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   192
    print "Source config:  %s" % source_root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   193
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   194
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   195
        source_config = source_project.get_configuration(source_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   196
    except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   197
        raise MergeFailedException("Configuration root '%s' not found in source project" % source_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   198
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   199
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   200
    # Create or get the target configuration root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   201
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   202
        target_config = target_project.get_configuration(target_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   203
    except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   204
        logger.info('Creating new root configuration %s' % (target_config))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   205
        target_config  = target_project.create_configuration(target_config)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   206
        for sourcelayer_path in source_config.list_configurations():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   207
            sourcelayer = source_config.get_configuration(sourcelayer_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   208
            sourcelayer_path = sourcelayer.path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   209
            if target_config.get_storage().is_resource(sourcelayer.path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   210
                logger.info('Including layer %s to root %s' % (sourcelayer_path, target_config.path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   211
                target_config.include_configuration(sourcelayer_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   212
            else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   213
                logger.info('Creating new layer %s to root %s' % (sourcelayer_path, target_config.path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   214
                target_config.create_configuration(sourcelayer_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   215
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   216
    # Collect a correctly sorted list of all layer paths to merge
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   217
    layers_to_merge = find_layers_to_merge(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   218
        layer_indices   = layer_indices,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   219
        rename          = rename,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   220
        sourceconfig    = source_config,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   221
        targetconfig    = target_config)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   222
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   223
    print "Merging %d layer(s)..." % len(layers_to_merge)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   224
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   225
    # Merge the layers
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   226
    for source_path, target_path in layers_to_merge:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   227
        print "Merging %s -> %s" % (source_path, target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   228
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   229
        source_layer = source_project.get_configuration(source_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   230
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   231
        if source_path != target_path:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   232
            try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   233
                target_config.remove_configuration(source_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   234
            except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   235
                pass
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   236
            target_config.create_configuration(target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   237
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   238
        # Get or create the target configuration layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   239
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   240
            target_layer = target_config.get_configuration(target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   241
        except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   242
            logger.info('Creating new layer configuration %s' % (target_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   243
            target_layer = target_config.create_configuration(target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   244
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   245
        merge_configuration_layer(source_layer, target_layer, merge_policy)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   246
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   247
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   248
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   249
def main(argv=sys.argv):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   250
    parser = OptionParser(version="%%prog %s" % VERSION)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   251
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   252
    parser.add_options(cone_common.COMMON_OPTIONS)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   253
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   254
    parser.add_option("-c", "--configuration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   255
                        dest="configuration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   256
                        help="defines the name of the target configuration for the action",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   257
                        metavar="CONFIG")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   258
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   259
    parser.add_option("-p", "--project",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   260
                       dest="project",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   261
                       help="defines the location of current project. Default is the current working directory.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   262
                       default=".",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   263
                       metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   264
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   265
    group = OptionGroup(parser, 'Merge options',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   266
                        'The merge functionality is meant to merge configurations/layers '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   267
                        'from a remote project (defined with -r) to the current project (defined with -p). '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   268
                        'Default value for the current project is the currently working directory. '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   269
                        'A project can be either a folder or a cpf/zip file. There are two ways to '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   270
                        'use merge: merge configuration roots (multiple layers), or specific layers. '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   271
                        'See the ConE documentation for details and examples.')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   272
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   273
    group.add_option("-r", "--remote",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   274
                   dest="remote",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   275
                   help="defines the location of remote storage",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   276
                   metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   277
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   278
    group.add_option("-s", "--sourceconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   279
                        dest="sourceconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   280
                        help="defines the name of the remote configuration inside the remote storage for the merge action. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   281
                             "Default is the active root of the remote project.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   282
                        metavar="CONFIG")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   283
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   284
    group.add_option("--sourcelayer",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   285
                     help="Defines a specific layer to use as the layer to merge "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   286
                          "from the remote project. Must be the layer root (ConfML file)."\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   287
                          "For example: --sourcelayer assets/somelayer/root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   288
                     metavar="LAYER_ROOT",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   289
                     default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   290
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   291
    group.add_option("--targetlayer",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   292
                     help="Defines a specific layer (root) to use as the layer to merge "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   293
                          "into the target project. Must be the layer root (ConfML file)."\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   294
                          "For example: --targetlayer assets/somelayer/root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   295
                     metavar="LAYER_ROOT",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   296
                     default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   297
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   298
    group.add_option("--rename",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   299
                        action="store_true", 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   300
                        dest="rename",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   301
                        help="defines that the merged layers need to be renamed",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   302
                        default=False)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   303
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   304
    group.add_option("--all",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   305
                        action="store_true", 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   306
                        dest="all",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   307
                        help="Defines that the entire configuration (all layers) needs to be merged. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   308
                             "This has no effect when merging layers directly using --sourcelayer and --targetlayer.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   309
                        default=False)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   310
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   311
    group.add_option("-l", "--layer",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   312
                   dest="layers",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   313
                   type="int",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   314
                   action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   315
                   help="Define the layers of the source configuration that are included to merge action. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   316
                        "The layer operation can be used several times in a single command. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   317
                        "Note that this can only be used when merging configuration roots, not "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   318
                        "specific layers using --sourcelayer and --targetlayer. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   319
                        "Example -l -1 --layer=-2, which would append a layers -1 and -2 to the layers => layers = -1,-2",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   320
                   metavar="LAYERS",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   321
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   322
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   323
    group.add_option("--merge-policy",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   324
                     help="Specifies the merge policy to use when merging layers. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   325
                          "Possible values:                                                         "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   326
                          "replace-add - Add/replace files from source layer, but leave other files in the target as they are. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   327
                          "                                                         "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   328
                          "overwrite-layer - Overwrite the entire layer (remove all previous content).",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   329
                     default=MergePolicy.REPLACE_ADD)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   330
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   331
    parser.add_option_group(group)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   332
    (options, _) = parser.parse_args(argv)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   333
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   334
    cone_common.handle_common_options(options)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   335
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   336
    # Check the passed options
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   337
    if not MergePolicy.is_valid(options.merge_policy):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   338
        parser.error("Invalid merge policy: %s\nMust be one of %s" % (options.merge_policy, '\n'.join(MergePolicy.ALL)))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   339
    if not options.remote: parser.error("Remote project must be given")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   340
    if options.layers and (options.sourcelayer or options.targetlayer):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   341
        parser.error("Specifying layer indices using --layer is not supported when using --sourcelayer or --targetlayer!")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   342
    if options.sourcelayer and not options.targetlayer:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   343
        parser.error("Merging a layer into a configuration is not supported at the moment!")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   344
    if options.sourcelayer and not options.sourcelayer.lower().endswith('.confml'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   345
        parser.error("Source layer root should be a .confml file")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   346
    if options.targetlayer and not options.targetlayer.lower().endswith('.confml'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   347
        parser.error("Target layer root should be a .confml file")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   348
    if not options.sourcelayer and options.targetlayer:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   349
        parser.error("Cannot merge a configuration into a layer!")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   350
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   351
    # If layers for configuration root merging are not specifically given,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   352
    # the default is the last layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   353
    if options.layers is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   354
        options.layers = [-1]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   355
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   356
    target_project = api.Project(api.Storage.open(options.project,"a", username=options.username, password=options.password))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   357
    source_project = api.Project(api.Storage.open(options.remote,"r", username=options.username, password=options.password))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   358
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   359
    print "Target project: %s" % options.project
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   360
    print "Source project: %s" % options.remote
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   361
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   362
    target_config = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   363
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   364
        if options.sourcelayer and options.targetlayer:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   365
            print "Target layer:   %s" % options.targetlayer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   366
            print "Source layer:   %s" % options.sourcelayer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   367
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   368
            try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   369
                source_config = source_project.get_configuration(options.sourcelayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   370
            except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   371
                raise MergeFailedException("Layer root '%s' not found in source project" % options.sourcelayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   372
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   373
            try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   374
                target_config = target_project.get_configuration(options.targetlayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   375
            except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   376
                logger.info('Creating new layer %s' % (options.targetlayer))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   377
                target_config  = target_project.create_configuration(options.targetlayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   378
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   379
            print "Merging layers..."
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   380
            merge_configuration_layer(source_config, target_config, options.merge_policy)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   381
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   382
            # Merging a configuration root into a configuration root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   383
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   384
            if options.all: layer_indices = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   385
            else:           layer_indices = utils.distinct_array(options.layers)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   386
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   387
            merge_config_root_to_config_root(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   388
                source_project = source_project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   389
                target_project = target_project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   390
                source_config  = options.sourceconfiguration,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   391
                target_config  = options.configuration,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   392
                layer_indices  = layer_indices,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   393
                rename         = options.rename,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   394
                merge_policy   = options.merge_policy)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   395
    except MergeFailedException, e:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   396
        print "Could not merge: %s" % e
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   397
        sys.exit(2)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   398
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   399
        # Merge successful, so save the target configuration and project
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   400
        # to persist the changes
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   401
        if target_config: target_config.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   402
        target_project.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   403
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   404
    target_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   405
    source_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   406
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   407
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   408
if __name__ == "__main__":
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   409
    main()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   410
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   411
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   412