configurationengine/source/plugins/plugin_utils.py
author terytkon
Thu, 11 Mar 2010 17:04:37 +0200
changeset 0 2e8eeb919028
permissions -rw-r--r--
Adding EPL version of configurationengine.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     1
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     2
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     3
# All rights reserved.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     4
# This component and the accompanying materials are made available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     5
# under the terms of "Eclipse Public License v1.0"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     6
# which accompanies this distribution, and is available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     8
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     9
# Initial Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    11
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    12
# Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    13
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    14
# Description:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    15
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    16
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    17
import sys, os, unittest, re
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    18
import build_egg_info
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    19
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    20
# Path to the directory where this file is located
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    21
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    22
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    23
# Path to the source/ directory
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    24
SOURCE_ROOT = os.path.normpath(os.path.join(ROOT_PATH, '..'))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    25
assert os.path.split(SOURCE_ROOT)[1] == 'source'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    26
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    27
# Path to the source/plugins directory
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    28
PLUGIN_SOURCE_ROOT = os.path.normpath(os.path.join(SOURCE_ROOT, 'plugins'))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    29
assert os.path.isdir(PLUGIN_SOURCE_ROOT)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    30
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    31
def plugin_test_init(root_path, plugin_source_relative_path='../..'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    32
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    33
    Initialize things so that plug-in unit tests can be run.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    34
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    35
    @param root_path: Path of the __init__.py file calling this function. 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    36
    @param plugin_source_relative_path: Path to the plug-in's source root relative
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    37
        to root_path. Usually this should be '../..', since this function is intended
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    38
        to be called from a plug-in's tests/__init__.py file.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    39
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    40
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    41
    # Add paths so that the unit tests work
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    42
    # -------------------------------------
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    43
    extra_paths = [
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    44
        # For module 'cone'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    45
        SOURCE_ROOT,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    46
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    47
        # For module 'testautomation'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    48
        os.path.join(SOURCE_ROOT, 'testautomation'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    49
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    50
        # For the plug-in module.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    51
        # Since the method is expected to be called from e.g.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    52
        # source/plugins/ConeSomePlugin/someplugin/tests/__init__.py, this will then be
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    53
        # source/plugins/ConeSomePlugin, in which case the module 'someplugin' can be
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    54
        # imported
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    55
        os.path.join(root_path, plugin_source_relative_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    56
    ]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    57
    for p in extra_paths:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    58
        p = os.path.normpath(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    59
        if p not in sys.path: sys.path.append(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    60
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    61
    # Generate egg-info for the plug-in.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    62
    # The egg-info needs to be up-to-date, or the plug-in framework will not be able
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    63
    # to find the plug-in's ImplML reader classes
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    64
    plugin_path = os.path.normpath(os.path.join(root_path, plugin_source_relative_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    65
    assert 'setup.py' in os.listdir(plugin_path), "Path '%s' does not contain 'setup.py" % plugin_path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    66
    build_egg_info.generate_egg_info(plugin_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    67
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    68
def integration_test_init(root_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    69
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    70
    Initialize things so that integration tests can be run.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    71
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    72
    This function is intended to be called from a sub-package's integration-test/__init__.py file.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    73
    @param root_path: Path of the __init__.py file calling this function.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    74
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    75
    # Add paths so that the unit tests work
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    76
    # -------------------------------------
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    77
    extra_paths = [
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    78
        # For module 'cone' (may be needed in some tests for e.g. asserts)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    79
        SOURCE_ROOT,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    80
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    81
        # For module 'testautomation'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    82
        os.path.join(SOURCE_ROOT, 'testautomation'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    83
    ]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    84
    for p in extra_paths:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    85
        p = os.path.normpath(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    86
        if p not in sys.path: sys.path.append(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    87
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    88
    # Collect plug-in source paths (common + current)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    89
    temp = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    90
    temp += [path for path, _ in find_plugin_sources(os.path.join(PLUGIN_SOURCE_ROOT, 'common'))]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    91
    temp += [path for path, _ in find_plugin_sources(os.path.normpath(os.path.join(root_path, '..')))]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    92
    plugin_paths = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    93
    for p in temp:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    94
        if p not in plugin_paths: plugin_paths.append(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    95
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    96
    # Add things to PYTHONPATH so that running cone_tool.py works
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    97
    paths = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    98
    paths.append(SOURCE_ROOT) # For module 'cone'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    99
    paths.extend(plugin_paths)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   100
    os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + ';' + ';'.join(paths)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   101
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   102
    # Generate egg-info for the plug-ins.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   103
    # The egg-info needs to be up-to-date, or the plug-in framework will not be able
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   104
    # to find the plug-in's ImplML reader classes
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   105
    for p in plugin_paths:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   106
        assert 'setup.py' in os.listdir(p), "Path '%s' does not contain 'setup.py" % p
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   107
        build_egg_info.generate_egg_info(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   108
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   109
def init_all():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   110
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   111
    Add all plug-ins to sys.path and PYTHONPATH so that running all
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   112
    plug-in unit tests and integration tests work.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   113
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   114
    # Find all plug-in source directories
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   115
    plugin_paths = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   116
    temp = find_all_plugin_sources(os.path.join(PLUGIN_SOURCE_ROOT))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   117
    for package_name, sources in temp.iteritems():
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   118
        for path, modname in sources:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   119
            plugin_paths.append(path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   120
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   121
    # Add paths so that the unit tests work
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   122
    # -------------------------------------
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   123
    extra_paths = [
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   124
        # For module 'cone' (may be needed in some tests for e.g. asserts)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   125
        SOURCE_ROOT,
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   126
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   127
        # For module 'testautomation'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   128
        os.path.join(SOURCE_ROOT, 'testautomation'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   129
    ] + plugin_paths
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   130
    for p in extra_paths:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   131
        p = os.path.normpath(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   132
        if p not in sys.path: sys.path.append(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   133
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   134
    # Add things to PYTHONPATH so that running cone_tool.py works
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   135
    paths = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   136
    paths.append(SOURCE_ROOT) # For module 'cone'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   137
    paths.extend(plugin_paths)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   138
    os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + ';' + ';'.join(paths)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   139
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   140
    # Generate egg-info for the plug-ins.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   141
    # The egg-info needs to be up-to-date, or the plug-in framework will not be able
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   142
    # to find the plug-in's ImplML reader classes
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   143
    for p in plugin_paths:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   144
        assert 'setup.py' in os.listdir(p), "Path '%s' does not contain 'setup.py" % p
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   145
        build_egg_info.generate_egg_info(p)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   146
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   147
def collect_test_suite_from_dir(root_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   148
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   149
    Return a test suite containing all tests in 'unittest_*.py' files under the given directory.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   150
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   151
    # Find all unittest_*.py files in the folder
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   152
    test_modules = filter(lambda name: re.match(r'^unittest_.*\.py$', name) != None, os.listdir(root_path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   153
    # Strip .py endings
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   154
    test_modules = map(lambda name: name[:-3], test_modules)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   155
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   156
    # Add the root path as the first one in sys.path, so that
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   157
    # __import__() checks that first when attempting to import a module 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   158
    sys.path.insert(0, root_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   159
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   160
    try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   161
        suite = unittest.TestSuite()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   162
        for test_module in test_modules:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   163
            # Load the test module dynamically and add it to the test suite
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   164
            module = __import__(test_module)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   165
            suite.addTests(unittest.TestLoader().loadTestsFromModule(module))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   166
        return suite
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   167
    finally:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   168
        # Remove root_path from sys.path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   169
        del sys.path[0]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   170
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   171
def collect_suite_from_source_list(paths_and_modnames):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   172
    suite = unittest.TestSuite()
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   173
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   174
    # Add the source paths to sys.path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   175
    for path, modname in paths_and_modnames:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   176
        if path not in sys.path:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   177
            sys.path.append(path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   178
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   179
    # Import the tests in a separate loop, because otherwise the ImplML reader classes
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   180
    # could be loaded before all plug-ins are in sys.path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   181
    for path, modname in paths_and_modnames:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   182
        try:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   183
            m = __import__(modname + '.tests')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   184
            suite.addTests(m.tests.collect_suite())
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   185
        except ImportError:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   186
            print "Tests for '%s' not added, no 'tests' module found" % path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   187
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   188
    return suite
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   189
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   190
def get_plugin_module_name(plugin_src_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   191
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   192
    Return the name of the plug-in's module under given plug-in source path.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   193
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   194
    >>> get_tests_module('source/plugins/common/ConeContentPlugin')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   195
    'contentplugin'
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   196
    >>> get_tests_module('foo')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   197
    None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   198
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   199
    if not os.path.isdir(plugin_src_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   200
        return None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   201
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   202
    for name in os.listdir(plugin_src_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   203
        path = os.path.join(plugin_src_path, name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   204
        if os.path.isdir(path) and '__init__.py' in os.listdir(path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   205
            return name
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   206
    return None
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   207
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   208
def find_plugin_sources(subpackage_root_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   209
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   210
    Return ConE plug-in source directories from the given path.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   211
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   212
    All sub-directories in subpackage_root_path of the form Cone*Plugin/ containing
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   213
    a sub-directory that is a python module are returned.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   214
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   215
    @param path: The directory from where to find the entries.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   216
    @return: A list of tuples, each containing (<source_dir>, <module_name>).
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   217
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   218
    pattern = re.compile(r'^Cone.*Plugin$')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   219
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   220
    # Collect a list of plug-in source paths and plug-in module names
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   221
    result = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   222
    for name in os.listdir(subpackage_root_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   223
        path = os.path.join(subpackage_root_path, name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   224
        if pattern.match(name) is not None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   225
            modname = get_plugin_module_name(path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   226
            if modname is not None:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   227
                result.append((path, modname))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   228
    return result
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   229
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   230
def find_all_plugin_sources(plugins_root_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   231
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   232
    Return all ConE plug-in source directories from the plugins/ root
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   233
    directory.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   234
    @return: A dictionary containing the output of find_plugin_sources() by
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   235
        plug-in sub-packages.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   236
    >>> find_all_plugin_sources('C:/work/cone-trunk/source/plugins')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   237
    {'common': [('C:/work/cone-trunk/source/plugins/common/ConeContentPlugin', 'contentplugin'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   238
                ('C:/work/cone-trunk/source/plugins/common/ConeTemplatePlugin', 'templatemlplugin')],
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   239
     'example': [('C:/work/cone-trunk/source/plugins/example/ConeExamplePlugin', 'examplemlplugin')]}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   240
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   241
    result = {}
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   242
    for name in os.listdir(plugins_root_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   243
        path = os.path.join(plugins_root_path, name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   244
        if os.path.isdir(path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   245
            sources = find_plugin_sources(path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   246
            if sources: result[name] = sources
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   247
    return result
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   248
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   249
def find_plugin_package_subpaths(subpath, package_name=None):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   250
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   251
    Return a list of plug-in package sub-paths based on the given plug-in package name.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   252
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   253
    The returned list always contains the sub-path for common plug-ins, and
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   254
    additionally for an extra plug-in package based on the given package name.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   255
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   256
    This function can be used to find specifically named files or directories
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   257
    under the plug-in paths. E.g. find all 'integration-test' directories:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   258
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   259
    >>> find_plugin_package_subpaths('integration-test', 'symbian')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   260
    [('common',  'C:\\cone\\trunk\\sources\\plugins\\common\\integration-test'),
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   261
     ('symbian', 'C:\\cone\\trunk\\sources\\plugins\\symbian\\integration-test')]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   262
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   263
    @param package_name: Name of the extra plug-in package. Can be None, '',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   264
        'common' or an existing plug-in package.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   265
    @return: List of tuples (package_name, subpath).
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   266
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   267
    @raise ValueError: The given package_name was invalid.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   268
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   269
    result = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   270
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   271
    def add(package_name):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   272
        package_dir = os.path.join(PLUGIN_SOURCE_ROOT, package_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   273
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   274
        if not os.path.exists(package_dir):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   275
            raise ValueError("Invalid plug-in package name: '%s'" % package_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   276
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   277
        path = os.path.normpath(os.path.join(package_dir, subpath))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   278
        if os.path.exists(path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   279
            result.append((package_name, path))
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   280
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   281
    add('common')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   282
    if package_name not in (None, '', 'common'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   283
        add(package_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   284
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   285
    return result
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   286
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   287
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   288
def find_plugin_sources_by_package(package_name=None):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   289
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   290
    Return a list of plug-in source paths based on the given plug-in package name.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   291
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   292
    The returned list always contains sources of common plug-ins, and
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   293
    additionally extra plug-in sources based on the given package name.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   294
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   295
    @param package_name: Name of the extra plug-in package. Can be None, '',
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   296
        'common' or an existing plug-in package.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   297
    @raise ValueError: The given package_name was invalid.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   298
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   299
    result = []
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   300
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   301
    # Find all plug-in sources
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   302
    sources = find_all_plugin_sources(PLUGIN_SOURCE_ROOT)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   303
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   304
    # Always return common plug-ins
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   305
    if 'common' in sources:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   306
        for path, modname in sources['common']:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   307
            result.append(path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   308
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   309
    # Return extra plug-ins if necessary
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   310
    if package_name not in (None, '', 'common'):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   311
        if package_name not in sources:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   312
            raise ValueError("Invalid plug-in package name: '%s'" % package_name)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   313
        else:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   314
            for path, modname in sources[package_name]:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   315
                result.append(path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   316
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   317
    return result