configurationengine/source/scripts/conesub_merge.py
author m2lahtel
Tue, 10 Aug 2010 14:29:28 +0300
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
child 5 d2c80f5cab53
permissions -rw-r--r--
ConE 1.2.11 release
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)
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   119
    
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   120
    merge_metadata(sourceconfig, targetconfig)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   121
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   122
def find_layers_to_merge(layer_indices, rename, sourceconfig, targetconfig):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   123
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   124
    Return a list of layers to merge.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   125
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   126
    @param layer_indices: List of layer indices to merge, can also be
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   127
        None to indicate that all layers are to be merged.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   128
    @param rename: True if the layers should be renamed in the target
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   129
        config, False if not.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   130
    @return: A list of tuples (layer_root, target_layer_root), where
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   131
        layer_root is the path to the layer root in the source
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   132
        configuration and target_layer_root the one in the target
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   133
        configuration.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   134
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   135
    # Get a list of all configurations to merge
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   136
    if layer_indices is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   137
        mergeconfigs = sourceconfig.list_configurations()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   138
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   139
        mergeconfigs = sort_mergeconfigs(layer_indices, sourceconfig.list_configurations())
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   140
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   141
    result = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   142
    if rename:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   143
        for source_path in mergeconfigs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   144
            target_path = get_new_layer_name(source_path, targetconfig.path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   145
            result.append((source_path, target_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   146
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   147
        for source_path in mergeconfigs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   148
            result.append((source_path, source_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   149
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   150
    return result
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   151
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   152
def sort_mergeconfigs(layers, sourceconfigs):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   153
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   154
    Return a correctly sorted list of source configuration layers.
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   155
    @param layers: List of the indices of the layers to merge. Can be None, in
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   156
        which case all layers are returned.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   157
    @param sourceconfigs: List of all configuration layer root paths in the
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   158
        source project.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   159
    @return: List of configuration layer root paths.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   160
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   161
    sorted_configs = [None for _ in xrange(len(sourceconfigs))]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   162
    for layer in layers:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   163
        sorted_configs[layer]=sourceconfigs[layer]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   164
    sorted_configs = filter(lambda x: x != None, sorted_configs)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   165
    return sorted_configs
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   166
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   167
def merge_metadata(source_config, target_config):
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   168
    # Merge (well, replace) configuration meta-data
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   169
    if source_config.meta:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   170
        target_config.meta = source_config.meta._clone(recursion=True)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   171
    else:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   172
        del target_config.meta
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   173
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   174
def get_active_root_if_necessary(project, configuration, name):
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   175
    if configuration:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   176
        return configuration
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   177
    else:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   178
        active_root = project.get_storage().get_active_configuration()
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   179
        if active_root == "":
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   180
            raise MergeFailedException("No %s configuration given and the project does not have an active root" % name)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   181
        else:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   182
            return active_root
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   183
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   184
def merge_config_root_to_config_root(source_project, target_project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   185
                                     source_config, target_config,
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   186
                                     layer_finder_func,
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   187
                                     merge_policy):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   188
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   189
    Merge the source configuration root to the target configuration root.
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   190
    
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   191
    @param source_config: Name of the source configuration.
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   192
    @param target_config: Name of the target configuration.
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   193
    @param layer_finder_func: Function for finding the layers that are to be
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   194
        merged. It should take source and target configuration objects as
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   195
        arguments and return a list of tuples (layer_root, target_layer_root),
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   196
        where layer_root is the path to the layer root in the source
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   197
        configuration and target_layer_root the one in the target
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   198
        configuration.
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   199
    @param merge_policy: The used merge policy.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   200
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   201
    target_root = get_active_root_if_necessary(target_project, target_config, 'target')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   202
    source_root = get_active_root_if_necessary(source_project, source_config, 'source')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   203
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   204
    print "Target config:  %s" % target_root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   205
    print "Source config:  %s" % source_root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   206
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   207
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   208
        source_config = source_project.get_configuration(source_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   209
    except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   210
        raise MergeFailedException("Configuration root '%s' not found in source project" % source_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   211
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   212
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   213
    # Create or get the target configuration root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   214
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   215
        target_config = target_project.get_configuration(target_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   216
    except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   217
        logger.info('Creating new root configuration %s' % (target_config))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   218
        target_config  = target_project.create_configuration(target_config)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   219
        for sourcelayer_path in source_config.list_configurations():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   220
            sourcelayer = source_config.get_configuration(sourcelayer_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   221
            sourcelayer_path = sourcelayer.path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   222
            if target_config.get_storage().is_resource(sourcelayer.path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   223
                logger.info('Including layer %s to root %s' % (sourcelayer_path, target_config.path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   224
                target_config.include_configuration(sourcelayer_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   225
            else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   226
                logger.info('Creating new layer %s to root %s' % (sourcelayer_path, target_config.path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   227
                target_config.create_configuration(sourcelayer_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   228
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   229
    # Collect a correctly sorted list of all layer paths to merge
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   230
    layers_to_merge = layer_finder_func(source_config, target_config)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   231
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   232
    print "Merging %d layer(s)..." % len(layers_to_merge)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   233
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   234
    # Merge the layers
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   235
    for source_path, target_path in layers_to_merge:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   236
        print "Merging %s -> %s" % (source_path, target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   237
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   238
        source_layer = source_project.get_configuration(source_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   239
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   240
        if source_path != target_path:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   241
            try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   242
                target_config.remove_configuration(source_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   243
            except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   244
                pass
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   245
            target_config.create_configuration(target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   246
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   247
        # Get or create the target configuration layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   248
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   249
            target_layer = target_config.get_configuration(target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   250
        except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   251
            logger.info('Creating new layer configuration %s' % (target_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   252
            target_layer = target_config.create_configuration(target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   253
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   254
        merge_configuration_layer(source_layer, target_layer, merge_policy)
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   255
    
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   256
    merge_metadata(source_config, target_config)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   257
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   258
def main(argv=sys.argv):
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   259
    """ Merge a configuration/layer to the project. """
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   260
    parser = OptionParser(version="%%prog %s" % VERSION)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   261
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   262
    parser.add_options(cone_common.COMMON_OPTIONS)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   263
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   264
    parser.add_option("-c", "--configuration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   265
                        dest="configuration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   266
                        help="defines the name of the target configuration for the action",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   267
                        metavar="CONFIG")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   268
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   269
    parser.add_option("-p", "--project",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   270
                       dest="project",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   271
                       help="defines the location of current project. Default is the current working directory.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   272
                       default=".",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   273
                       metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   274
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   275
    group = OptionGroup(parser, 'Merge options',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   276
                        'The merge functionality is meant to merge configurations/layers '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   277
                        'from a remote project (defined with -r) to the current project (defined with -p). '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   278
                        'Default value for the current project is the currently working directory. '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   279
                        '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
   280
                        'use merge: merge configuration roots (multiple layers), or specific layers. '
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   281
                        'See the ConE documentation for details and examples.')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   282
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   283
    group.add_option("-r", "--remote",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   284
                   dest="remote",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   285
                   help="defines the location of remote storage",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   286
                   metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   287
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   288
    group.add_option("-s", "--sourceconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   289
                        dest="sourceconfiguration",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   290
                        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
   291
                             "Default is the active root of the remote project.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   292
                        metavar="CONFIG")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   293
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   294
    group.add_option("--sourcelayer",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   295
                     help="Defines a specific layer to use as the layer to merge "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   296
                          "from the remote project. Must be the layer root (ConfML file)."\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   297
                          "For example: --sourcelayer assets/somelayer/root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   298
                     metavar="LAYER_ROOT",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   299
                     default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   300
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   301
    group.add_option("--targetlayer",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   302
                     help="Defines a specific layer (root) to use as the layer to merge "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   303
                          "into the target project. Must be the layer root (ConfML file)."\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   304
                          "For example: --targetlayer assets/somelayer/root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   305
                     metavar="LAYER_ROOT",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   306
                     default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   307
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   308
    group.add_option("--rename",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   309
                        action="store_true", 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   310
                        dest="rename",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   311
                        help="defines that the merged layers need to be renamed",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   312
                        default=False)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   313
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   314
    group.add_option("--all",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   315
                        action="store_true", 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   316
                        dest="all",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   317
                        help="Defines that the entire configuration (all layers) needs to be merged. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   318
                             "This has no effect when merging layers directly using --sourcelayer and --targetlayer.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   319
                        default=False)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   320
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   321
    group.add_option("-l", "--layer",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   322
                   dest="layers",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   323
                   type="int",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   324
                   action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   325
                   help="Define the layers of the source configuration that are included to merge action. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   326
                        "The layer operation can be used several times in a single command. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   327
                        "Note that this can only be used when merging configuration roots, not "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   328
                        "specific layers using --sourcelayer and --targetlayer. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   329
                        "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
   330
                   metavar="LAYERS",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   331
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   332
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   333
    group.add_option("--merge-policy",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   334
                     help="Specifies the merge policy to use when merging layers. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   335
                          "Possible values:                                                         "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   336
                          "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
   337
                          "                                                         "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   338
                          "overwrite-layer - Overwrite the entire layer (remove all previous content).",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   339
                     default=MergePolicy.REPLACE_ADD)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   340
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   341
    parser.add_option_group(group)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   342
    (options, _) = parser.parse_args(argv)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   343
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   344
    cone_common.handle_common_options(options)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   345
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   346
    # Check the passed options
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   347
    if not MergePolicy.is_valid(options.merge_policy):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   348
        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
   349
    if not options.remote: parser.error("Remote project must be given")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   350
    if options.layers and (options.sourcelayer or options.targetlayer):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   351
        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
   352
    if options.sourcelayer and not options.targetlayer:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   353
        parser.error("Merging a layer into a configuration is not supported at the moment!")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   354
    if options.sourcelayer and not options.sourcelayer.lower().endswith('.confml'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   355
        parser.error("Source layer root should be a .confml file")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   356
    if options.targetlayer and not options.targetlayer.lower().endswith('.confml'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   357
        parser.error("Target layer root should be a .confml file")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   358
    if not options.sourcelayer and options.targetlayer:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   359
        parser.error("Cannot merge a configuration into a layer!")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   360
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   361
    # If layers for configuration root merging are not specifically given,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   362
    # the default is the last layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   363
    if options.layers is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   364
        options.layers = [-1]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   365
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   366
    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
   367
    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
   368
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   369
    print "Target project: %s" % options.project
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   370
    print "Source project: %s" % options.remote
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   371
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   372
    target_config = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   373
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   374
        if options.sourcelayer and options.targetlayer:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   375
            print "Target layer:   %s" % options.targetlayer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   376
            print "Source layer:   %s" % options.sourcelayer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   377
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   378
            try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   379
                source_config = source_project.get_configuration(options.sourcelayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   380
            except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   381
                raise MergeFailedException("Layer root '%s' not found in source project" % options.sourcelayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   382
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   383
            try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   384
                target_config = target_project.get_configuration(options.targetlayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   385
            except exceptions.NotFound:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   386
                logger.info('Creating new layer %s' % (options.targetlayer))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   387
                target_config  = target_project.create_configuration(options.targetlayer)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   388
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   389
            print "Merging layers..."
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   390
            merge_configuration_layer(source_config, target_config, options.merge_policy)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   391
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   392
            # Merging a configuration root into a configuration root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   393
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   394
            if options.all: layer_indices = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   395
            else:           layer_indices = utils.distinct_array(options.layers)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   396
            
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   397
            def find_layers(source_config, target_config):
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   398
                return find_layers_to_merge(
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   399
                    layer_indices   = layer_indices,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   400
                    rename          = options.rename,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   401
                    sourceconfig    = source_config,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   402
                    targetconfig    = target_config)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   403
            
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   404
            merge_config_root_to_config_root(
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   405
                source_project      = source_project,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   406
                target_project      = target_project,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   407
                source_config       = options.sourceconfiguration,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   408
                target_config       = options.configuration,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   409
                layer_finder_func   = find_layers,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   410
                merge_policy        = options.merge_policy)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   411
    except MergeFailedException, e:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   412
        print "Could not merge: %s" % e
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   413
        sys.exit(2)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   414
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   415
        # Merge successful, so save the target configuration and project
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   416
        # to persist the changes
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   417
        if target_config: target_config.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   418
        target_project.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   419
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   420
    target_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   421
    source_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   422
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   423
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   424
if __name__ == "__main__":
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   425
    main()