configurationengine/source/scripts/conesub_export.py
author terytkon
Thu, 11 Mar 2010 17:04:37 +0200
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
permissions -rw-r--r--
Adding EPL version of configurationengine.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     1
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     2
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     3
# All rights reserved.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     4
# This component and the accompanying materials are made available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     5
# under the terms of "Eclipse Public License v1.0"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     6
# which accompanies this distribution, and is available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     8
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     9
# Initial Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    11
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    12
# Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    13
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    14
# Description: 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    15
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    16
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    17
import os
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    18
import fnmatch
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    19
import logging
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    20
from optparse import OptionParser, OptionGroup
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    21
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    22
import cone_common
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    23
from cone.public import api, plugin, utils, exceptions
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    24
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    25
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    26
VERSION     = '1.0'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    27
DEFAULT_EXT = '.cpf'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    28
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    29
logger    = logging.getLogger('cone')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    30
DATA_NAME = 'confml/data.confml'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    31
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    32
def main():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    33
    parser = OptionParser(version="%%prog %s" % VERSION)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    34
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    35
    parser.add_options(cone_common.COMMON_OPTIONS)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    36
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    37
    parser.add_option("-c", "--configuration",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    38
                        dest="configs",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    39
                        action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    40
                        help="Defines the name of the configuration for the action, can be "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    41
                             "specified multiple times to include multiple configurations.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    42
                        metavar="CONFIG",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    43
                        default=[])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    44
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    45
    parser.add_option("--config-wildcard",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    46
                      action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    47
                      dest="config_wildcards",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    48
                      help="Wildcard pattern for including configurations, e.g. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    49
                           "product_langpack_*_root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    50
                      metavar="WILDCARD",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    51
                      default=[])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    52
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    53
    parser.add_option("--config-regex",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    54
                      action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    55
                      dest="config_regexes",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    56
                      help="Regular expression for including configurations, e.g. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    57
                           "product_langpack_\\d{2}_root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    58
                      metavar="REGEX",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    59
                      default=[])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    60
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    61
    parser.add_option("-p", "--project",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    62
                       dest="project",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    63
                       help="defines the location of current project. Default is the "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    64
                            "current working directory.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    65
                       default=".",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    66
                       metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    67
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    68
    group = OptionGroup(parser, 'Export options',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    69
                        'The export action is intended for exporting configurations '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    70
                        'from one project (storage) to another. A project can be a '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    71
                        'folder, a CPF or ZIP file, or a Carbon web storage URL. '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    72
                        # An ugly way to make newlines, someone should look into
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    73
                        # sub-classing optparse.HelpFormatter... 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    74
                        '                                                                          '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    75
                        'Two different ways of exporting are supported: '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    76
                        '                                                                          '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    77
                        '1. Exporting multiple configurations into one new project using --remote '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    78
                        '                                                                          '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    79
                        '2. Exporting configurations into a number of new projects using --export-dir')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    80
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    81
    group.add_option("-r", "--remote",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    82
                   dest="remote",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    83
                   help="Defines the location of remote storage. All configurations included using "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    84
                        "--configuration, --config-wildcard and --config-regex are exported into "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    85
                        "the storage. If the remote storage location is not given, the default "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    86
                        "location is determined based on the first included source configuration name. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    87
                        "E.g. 'example.confml' would be exported into 'example.cpf'",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    88
                   metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    89
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    90
    group.add_option("--export-dir",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    91
                     help="Defines the directory where each included configuration is exported "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    92
                          "as a new project.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    93
                     default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    94
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    95
    group.add_option("--export-format",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    96
                     help="Defines the format into which projects are exported when using "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    97
                          "--export-dir. Possible values are 'cpf' (the default) and 'dir'.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    98
                     default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    99
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   100
    group.add_option("-a","--add",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   101
                   dest="added",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   102
                   action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   103
                   type="string",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   104
                   help="Adds a configuration layer to the given configuration as last element. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   105
                        "The add operation can be used several times in a single command and it "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   106
                        "can create even an empty layer. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   107
                        "Example --add foo/root.confml --add bar/root-confml.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   108
                   metavar="CONFIG",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   109
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   110
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   111
    group.add_option("--exclude-folders",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   112
                        dest="exclude_empty_folders",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   113
                        action="store_true",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   114
                        help="Excludes empty folders from export",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   115
                        default=False)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   116
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   117
    parser.add_option_group(group)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   118
    (options, args) = parser.parse_args()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   119
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   120
    cone_common.handle_common_options(options)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   121
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   122
    # Check options
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   123
    if options.export_format and options.export_dir is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   124
        parser.error("--export-format can only be used in conjunction with --export-dir")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   125
    if options.export_dir and options.remote:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   126
        parser.error("--export-dir and --remote cannot be used at the same time")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   127
    if options.export_format and options.export_format.lower() not in ('dir', 'cpf'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   128
        parser.error("Invalid export format '%s'" % options.export_format)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   129
    if options.export_dir and not (options.configs or options.config_wildcards or options.config_regexes):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   130
        parser.error("Use of --export-dir requires at least one configuration to be specified")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   131
    if options.export_dir and os.path.isfile(options.export_dir):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   132
        parser.error("Given export directory '%s' is a file")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   133
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   134
    # Open the project and find out the active configuration
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   135
    project = api.Project(api.Storage.open(options.project, "r"))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   136
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   137
        active_root = project.get_storage().get_active_configuration()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   138
    except AttributeError:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   139
        active_root = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   140
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   141
    # Collect the list of configurations specified from the command line
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   142
    config_list = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   143
    if options.configs or options.config_wildcards or options.config_regexes:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   144
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   145
            config_list = cone_common.get_config_list_from_project(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   146
                project          = project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   147
                configs          = options.configs,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   148
                config_wildcards = options.config_wildcards,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   149
                config_regexes   = options.config_regexes)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   150
        except cone_common.ConfigurationNotFoundError, e:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   151
            parser.error(str(e))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   152
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   153
    # Use the active configuration if no configurations are specifically given
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   154
    if len(config_list) == 0:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   155
        if active_root is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   156
            parser.error("No configurations given and the project does not have an active root")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   157
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   158
            logger.info('No configurations given! Using active root configuration %s' % active_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   159
            config_list = [active_root]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   160
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   161
    # Perform the export
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   162
    if options.export_dir:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   163
        _export_to_dir(project       = project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   164
                       export_dir    = options.export_dir,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   165
                       export_format = options.export_format or 'cpf',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   166
                       configs       = config_list,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   167
                       added_layers  = options.added,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   168
                       empty_folders = not options.exclude_empty_folders)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   169
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   170
        _export_to_storage(project                 = project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   171
                           remote_project_location = options.remote,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   172
                           configs                 = config_list,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   173
                           added_layers            = options.added,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   174
                           empty_folders           = not options.exclude_empty_folders)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   175
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   176
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   177
def _export_to_storage(project, remote_project_location, configs, added_layers, empty_folders):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   178
    assert len(configs) > 0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   179
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   180
    # If the remote storage is not given, determine it automatically based
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   181
    # on the first specified configuration name
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   182
    if not remote_project_location:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   183
        remotename, ext = os.path.splitext(os.path.basename(configs[0]))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   184
        remotename += DEFAULT_EXT
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   185
        logger.info('No remote storage given! Using source configuration name %s' % remotename)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   186
        remote_project_location = remotename
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   187
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   188
    remote_project = api.Project(api.Storage.open(remote_project_location, "w"))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   189
    for config_path in configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   190
        config = project.get_configuration(config_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   191
        project.export_configuration(config,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   192
                                     remote_project.storage,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   193
                                     empty_folders = empty_folders)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   194
        print "Export %s to %s done!" % (config_path, remote_project_location)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   195
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   196
    # Setting first as active configuration if there are more than one configuration defined.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   197
    configs = remote_project.list_configurations()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   198
    if len(configs):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   199
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   200
            remote_project.get_storage().set_active_configuration(configs[0])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   201
        except AttributeError:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   202
            pass
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   203
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   204
    remote_project.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   205
    remote_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   206
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   207
    _add_layers(project, remote_project_location, added_layers, empty_folders)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   208
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   209
def _add_layers(source_project, remote_project_location, added_configs, empty_folders):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   210
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   211
    Add new configuration layers from source_project into 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   212
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   213
    if not added_configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   214
        return
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   215
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   216
    target_project = api.Project(api.Storage.open(remote_project_location, "a"))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   217
    for target_config_name in target_project.list_configurations():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   218
        target_config = target_project.get_configuration(target_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   219
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   220
        for added_config_name in added_configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   221
            # Add layers only once
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   222
            if not target_project.storage.is_resource(added_config_name):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   223
                logger.info('Adding configuration %s' % added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   224
                
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   225
                if source_project.storage.is_resource(added_config_name):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   226
                    # The configuration exists in the source project, export it from there
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   227
                    existing_config = source_project.get_configuration(added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   228
                    source_project.export_configuration(existing_config,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   229
                                                        target_project.storage,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   230
                                                        empty_folders = empty_folders)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   231
                else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   232
                    # The given configuration does not exist in the source project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   233
                    # create a new empty layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   234
                    logger.info("Creating new layer %s." % added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   235
                    new_config = target_project.create_configuration(added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   236
                    new_config.create_configuration(DATA_NAME)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   237
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   238
            # Include the added configuration in the configuration root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   239
            target_config.include_configuration(utils.resourceref.norm(added_config_name))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   240
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   241
    target_project.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   242
    target_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   243
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   244
def _export_to_dir(project, export_dir, export_format, configs, added_layers, empty_folders):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   245
    if not os.path.exists(export_dir):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   246
        os.makedirs(export_dir)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   247
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   248
    for config in configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   249
        remote_name, _ = os.path.splitext(os.path.basename(config))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   250
        if export_format.lower() == 'cpf':
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   251
            remote_name += '.cpf'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   252
        elif export_format.lower() == 'dir':
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   253
            remote_name += '/'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   254
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   255
        remote_name = os.path.join(export_dir, remote_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   256
        _export_to_storage(project, remote_name, [config], added_layers, empty_folders)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   257
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   258
if __name__ == "__main__":
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   259
    main()