configurationengine/source/scripts/conesub_export.py
author m2lahtel
Thu, 21 Oct 2010 16:36:53 +0300
changeset 5 d2c80f5cab53
parent 3 e7e0ae78773e
permissions -rw-r--r--
Updated to version 1.2.14
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
import cone_common
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    22
from cone.public import api, plugin, utils, exceptions
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    23
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    24
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    25
VERSION     = '1.0'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    26
DEFAULT_EXT = '.cpf'
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
    27
CONE_SCRIPT_PATTERN = 'conesub_*.py'
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
    28
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    29
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    30
logger    = logging.getLogger('cone')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    31
DATA_NAME = 'confml/data.confml'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    32
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
    33
class  ExportFailedException(Exception):
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
    34
    pass    
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
    35
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    36
def main():
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
    37
    """ Export configurations. """
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    38
    parser = OptionParser(version="%%prog %s" % VERSION)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    39
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    40
    parser.add_options(cone_common.COMMON_OPTIONS)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    41
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    42
    parser.add_option("-c", "--configuration",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    43
                        dest="configs",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    44
                        action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    45
                        help="Defines the name of the configuration for the action, can be "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    46
                             "specified multiple times to include multiple configurations.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    47
                        metavar="CONFIG",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    48
                        default=[])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    49
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    50
    parser.add_option("--config-wildcard",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    51
                      action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    52
                      dest="config_wildcards",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    53
                      help="Wildcard pattern for including configurations, e.g. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    54
                           "product_langpack_*_root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    55
                      metavar="WILDCARD",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    56
                      default=[])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    57
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    58
    parser.add_option("--config-regex",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    59
                      action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    60
                      dest="config_regexes",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    61
                      help="Regular expression for including configurations, e.g. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    62
                           "product_langpack_\\d{2}_root.confml",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    63
                      metavar="REGEX",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    64
                      default=[])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    65
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    66
    parser.add_option("-p", "--project",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    67
                       dest="project",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    68
                       help="defines the location of current project. Default is the "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    69
                            "current working directory.",\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    70
                       default=".",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    71
                       metavar="STORAGE")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    72
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    73
    group = OptionGroup(parser, 'Export options',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    74
                        'The export action is intended for exporting configurations '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    75
                        'from one project (storage) to another. A project can be a '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    76
                        'folder, a CPF or ZIP file, or a Carbon web storage URL. '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    77
                        # An ugly way to make newlines, someone should look into
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    78
                        # sub-classing optparse.HelpFormatter... 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    79
                        '                                                                          '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    80
                        'Two different ways of exporting are supported: '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    81
                        '                                                                          '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    82
                        '1. Exporting multiple configurations into one new project using --remote '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    83
                        '                                                                          '\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    84
                        '2. Exporting configurations into a number of new projects using --export-dir')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    85
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    86
    group.add_option("-r", "--remote",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    87
                   dest="remote",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    88
                   help="Defines the location of remote storage. All configurations included using "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    89
                        "--configuration, --config-wildcard and --config-regex are exported into "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    90
                        "the storage. If the remote storage location is not given, the default "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    91
                        "location is determined based on the first included source configuration name. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    92
                        "E.g. 'example.confml' would be exported into 'example.cpf'",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    93
                   metavar="STORAGE")
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-dir",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    96
                     help="Defines the directory where each included configuration is exported "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    97
                          "as a new project.",
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("--export-format",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   101
                     help="Defines the format into which projects are exported when using "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   102
                          "--export-dir. Possible values are 'cpf' (the default) and 'dir'.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   103
                     default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   104
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   105
    group.add_option("-a","--add",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   106
                   dest="added",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   107
                   action="append",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   108
                   type="string",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   109
                   help="Adds a configuration layer to the given configuration as last element. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   110
                        "The add operation can be used several times in a single command and it "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   111
                        "can create even an empty layer. "\
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   112
                        "Example --add foo/root.confml --add bar/root-confml.",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   113
                   metavar="CONFIG",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   114
                   default=None)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   115
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   116
    group.add_option("--run-action",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   117
                   dest="action",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   118
                   action="append",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   119
                   type="string",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   120
                   help="Adds a execution of extra step that can manipulate the configuration before it is exported to external storage. "\
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   121
                        "The --run-action operation can be used several times in a single command and it "\
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   122
                        "will execute the actions in given order."\
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   123
                        "Example --run-action=fix, which would execute fix action during export.",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   124
                   metavar="PLUGIN",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   125
                   default=None)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   126
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   127
    group.add_option("--exclude-folders",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   128
                        dest="exclude_empty_folders",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   129
                        action="store_true",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   130
                        help="Excludes empty folders from export",
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   131
                        default=False)
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   132
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   133
    group.add_option("--exclude-content-filter",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   134
                        dest="exclude_content_filter",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   135
                        help="Filters out files and folders from content folder which matches with "\
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   136
                             "the given regular expression. Matching is case-insensitive. "\
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   137
                             "Example --exclude-content-filter=\".*\.sisx|.*\.sis|.*\.wgz|.*wgt\" "\
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   138
                             "Filters out all files with extension sis, sisx, wgz or wgt.",
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   139
                        default=None)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   140
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   141
    group.add_option("--include-content-filter",
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   142
                        dest="include_content_filter",
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   143
                        help="Filters out files and folders from content folder which don't match with "\
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   144
                             "the given regular expression. Matching is case-insensitive. "\
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   145
                             "Example --include-content-filter=\".*(manual|configurator).*\" "\
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   146
                             "Filters out all content files which don't have manual or configurator in their path.",
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   147
                        default=None)
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   148
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   149
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   150
    parser.add_option_group(group)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   151
    (options, args) = parser.parse_args()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   152
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   153
    cone_common.handle_common_options(options)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   154
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   155
    # Check options
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   156
    if options.export_format and options.export_dir is None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   157
        parser.error("--export-format can only be used in conjunction with --export-dir")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   158
    if options.export_dir and options.remote:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   159
        parser.error("--export-dir and --remote cannot be used at the same time")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   160
    if options.export_format and options.export_format.lower() not in ('dir', 'cpf'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   161
        parser.error("Invalid export format '%s'" % options.export_format)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   162
    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
   163
        parser.error("Use of --export-dir requires at least one configuration to be specified")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   164
    if options.export_dir and os.path.isfile(options.export_dir):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   165
        parser.error("Given export directory '%s' is a file")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   166
    
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   167
    try:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   168
        run_export(options)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   169
    except ExportFailedException, e:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   170
        parser.error(str(e))
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   171
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   172
    
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 run_export(options):
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   175
    # Open the project and find out the active configuration
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   176
    storage = api.Storage.open(options.project, "r", username=options.username, password=options.password)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   177
    project = api.Project(storage)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   178
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   179
        active_root = project.get_storage().get_active_configuration()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   180
    except AttributeError:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   181
        active_root = None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   182
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   183
    # Collect the list of configurations specified from the command line
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   184
    config_list = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   185
    if options.configs or options.config_wildcards or options.config_regexes:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   186
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   187
            config_list = cone_common.get_config_list_from_project(
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   188
                project          = project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   189
                configs          = options.configs,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   190
                config_wildcards = options.config_wildcards,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   191
                config_regexes   = options.config_regexes)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   192
        except cone_common.ConfigurationNotFoundError, e:
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   193
            raise ExportFailedException(str(e))
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   194
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   195
    # Use the active configuration if no configurations are specifically given
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   196
    if len(config_list) == 0:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   197
        if active_root is None:
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   198
            raise ExportFailedException("No configurations given and the project does not have an active root")
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   199
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   200
            logger.info('No configurations given! Using active root configuration %s' % active_root)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   201
            config_list = [active_root]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   202
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   203
    # Perform the export
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   204
    if options.export_dir:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   205
        _export_to_dir(project       = project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   206
                       export_dir    = options.export_dir,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   207
                       export_format = options.export_format or 'cpf',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   208
                       configs       = config_list,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   209
                       added_layers  = options.added,
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   210
                       empty_folders = not options.exclude_empty_folders,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   211
                       exclude_filters       = {"content": options.exclude_content_filter},
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   212
                       include_filters       = {"content": options.include_content_filter},
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   213
                       options               = options)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   214
    else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   215
        _export_to_storage(project                 = project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   216
                           remote_project_location = options.remote,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   217
                           configs                 = config_list,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   218
                           added_layers            = options.added,
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   219
                           empty_folders           = not options.exclude_empty_folders,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   220
                           exclude_filters         = {"content": options.exclude_content_filter},
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   221
						   include_filters         = {"content": options.include_content_filter},
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   222
                           options                 = options)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   223
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   224
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   225
def _export_to_storage(project, remote_project_location, configs, added_layers, empty_folders, exclude_filters, include_filters, options):
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   226
    assert len(configs) > 0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   227
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   228
    # If the remote storage is not given, determine it automatically based
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   229
    # on the first specified configuration name
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   230
    if not remote_project_location:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   231
        remotename, ext = os.path.splitext(os.path.basename(configs[0]))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   232
        remotename += DEFAULT_EXT
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   233
        logger.info('No remote storage given! Using source configuration name %s' % remotename)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   234
        remote_project_location = remotename
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   235
    
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   236
    remote_project = api.Project(api.Storage.open(remote_project_location, "w", username=options.username, password=options.password))
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   237
    for config_path in configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   238
        config = project.get_configuration(config_path)
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   239
        # immediate solution to run fixes during export
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   240
        if options.action:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   241
            from cone.action import loader
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   242
            for action_name in options.action:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   243
                try:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   244
                    action_class = loader.get_class(action_name)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   245
                    action = action_class(configuration=config,
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   246
                                          project=project)
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   247
                    action.run()
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   248
                except Exception,e:
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   249
                    logger.warning('Running plugin %s threw an exception: %s' % (action_name, e))
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   250
        project.export_configuration(config,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   251
                                     remote_project.storage,
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   252
                                     empty_folders = empty_folders,
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   253
                                     exclude_filters = exclude_filters,
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   254
									 include_filters = include_filters)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   255
        print "Export %s to %s done!" % (config_path, remote_project_location)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   256
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   257
    # Setting first as active configuration if there are more than one configuration defined.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   258
    configs = remote_project.list_configurations()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   259
    if len(configs):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   260
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   261
            remote_project.get_storage().set_active_configuration(configs[0])
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   262
        except AttributeError:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   263
            pass
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   264
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   265
    remote_project.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   266
    remote_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   267
    
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   268
    _add_layers(project, remote_project_location, added_layers, empty_folders, exclude_filters, include_filters, options)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   269
    
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   270
def _add_layers(source_project, remote_project_location, added_configs, empty_folders, exclude_filters, include_filters, options):
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   271
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   272
    Add new configuration layers from source_project into 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   273
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   274
    if not added_configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   275
        return
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   276
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   277
    target_project = api.Project(api.Storage.open(remote_project_location, "a"))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   278
    for target_config_name in target_project.list_configurations():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   279
        target_config = target_project.get_configuration(target_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   280
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   281
        for added_config_name in added_configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   282
            # Add layers only once
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   283
            if not target_project.storage.is_resource(added_config_name):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   284
                logger.info('Adding configuration %s' % added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   285
                
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   286
                if source_project.storage.is_resource(added_config_name):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   287
                    # The configuration exists in the source project, export it from there
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   288
                    existing_config = source_project.get_configuration(added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   289
                    source_project.export_configuration(existing_config,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   290
                                                        target_project.storage,
3
e7e0ae78773e ConE 1.2.11 release
m2lahtel
parents: 0
diff changeset
   291
                                                        empty_folders = empty_folders,
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   292
                                                        exclude_filters = exclude_filters,
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   293
                                                        include_filters = include_filters)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   294
                else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   295
                    # The given configuration does not exist in the source project,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   296
                    # create a new empty layer
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   297
                    logger.info("Creating new layer %s." % added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   298
                    new_config = target_project.create_configuration(added_config_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   299
                    new_config.create_configuration(DATA_NAME)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   300
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   301
            # Include the added configuration in the configuration root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   302
            target_config.include_configuration(utils.resourceref.norm(added_config_name))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   303
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   304
    target_project.save()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   305
    target_project.close()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   306
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   307
def _export_to_dir(project, export_dir, export_format, configs, added_layers, empty_folders, exclude_filters, include_filters, options):
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   308
    if not os.path.exists(export_dir):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   309
        os.makedirs(export_dir)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   310
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   311
    for config in configs:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   312
        remote_name, _ = os.path.splitext(os.path.basename(config))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   313
        if export_format.lower() == 'cpf':
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   314
            remote_name += '.cpf'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   315
        elif export_format.lower() == 'dir':
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   316
            remote_name += '/'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   317
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   318
        remote_name = os.path.join(export_dir, remote_name)
5
d2c80f5cab53 Updated to version 1.2.14
m2lahtel
parents: 3
diff changeset
   319
        _export_to_storage(project, remote_name, [config], added_layers, empty_folders, exclude_filters, include_filters, options)
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   320
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   321
if __name__ == "__main__":
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   322
    main()