|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import sys, os, unittest, re |
|
18 import build_egg_info |
|
19 |
|
20 # Path to the directory where this file is located |
|
21 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
22 |
|
23 |
|
24 |
|
25 def plugin_test_init(root_path, plugin_source_relative_path='../..'): |
|
26 """ |
|
27 Initialize things so that plug-in unit tests can be run. |
|
28 |
|
29 @param root_path: Path of the __init__.py file calling this function. |
|
30 @param plugin_source_relative_path: Path to the plug-in's source root relative |
|
31 to root_path. Usually this should be '../..', since this function is intended |
|
32 to be called from a plug-in's tests/__init__.py file. |
|
33 |
|
34 """ |
|
35 |
|
36 # Generate egg-info for the plug-in. |
|
37 # The egg-info needs to be up-to-date, or the plug-in framework will not be able |
|
38 # to find the plug-in's ImplML reader classes |
|
39 plugin_path = os.path.normpath(os.path.join(root_path, plugin_source_relative_path)) |
|
40 assert 'setup.py' in os.listdir(plugin_path), "Path '%s' does not contain 'setup.py" % plugin_path |
|
41 build_egg_info.generate_egg_info(plugin_path) |
|
42 |
|
43 #def integration_test_init(root_path): |
|
44 # """ |
|
45 # Initialize things so that integration tests can be run. |
|
46 # |
|
47 # This function is intended to be called from a sub-package's integration-test/__init__.py file. |
|
48 # @param root_path: Path of the __init__.py file calling this function. |
|
49 # """ |
|
50 # |
|
51 # |
|
52 # # Generate egg-info for the plug-ins. |
|
53 # # The egg-info needs to be up-to-date, or the plug-in framework will not be able |
|
54 # # to find the plug-in's ImplML reader classes |
|
55 # for p in plugin_paths: |
|
56 # assert 'setup.py' in os.listdir(p), "Path '%s' does not contain 'setup.py" % p |
|
57 # build_egg_info.generate_egg_info(p) |
|
58 |
|
59 #def init_all(): |
|
60 # """ |
|
61 # Add all plug-ins to sys.path and PYTHONPATH so that running all |
|
62 # plug-in unit tests and integration tests work. |
|
63 # """ |
|
64 # # Find all plug-in source directories |
|
65 # plugin_paths = [] |
|
66 # temp = find_all_plugin_sources(os.path.join(PLUGIN_SOURCE_ROOT)) |
|
67 # for package_name, sources in temp.iteritems(): |
|
68 # for path, modname in sources: |
|
69 # plugin_paths.append(path) |
|
70 # |
|
71 # # Add paths so that the unit tests work |
|
72 # # ------------------------------------- |
|
73 # extra_paths = [ |
|
74 # # For module 'cone' (may be needed in some tests for e.g. asserts) |
|
75 # SOURCE_ROOT, |
|
76 # |
|
77 # # For module 'testautomation' |
|
78 # os.path.join(SOURCE_ROOT, 'testautomation'), |
|
79 # ] + plugin_paths |
|
80 # for p in extra_paths: |
|
81 # p = os.path.normpath(p) |
|
82 # if p not in sys.path: sys.path.append(p) |
|
83 # |
|
84 # # Add things to PYTHONPATH so that running cone_tool.py works |
|
85 # paths = [] |
|
86 # paths.append(SOURCE_ROOT) # For module 'cone' |
|
87 # paths.extend(plugin_paths) |
|
88 # os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + ';' + ';'.join(paths) |
|
89 # |
|
90 # # Generate egg-info for the plug-ins. |
|
91 # # The egg-info needs to be up-to-date, or the plug-in framework will not be able |
|
92 # # to find the plug-in's ImplML reader classes |
|
93 # for p in plugin_paths: |
|
94 # assert 'setup.py' in os.listdir(p), "Path '%s' does not contain 'setup.py" % p |
|
95 # build_egg_info.generate_egg_info(p) |
|
96 |
|
97 |
|
98 def get_plugin_module_name(plugin_src_path): |
|
99 """ |
|
100 Return the name of the plug-in's module under given plug-in source path. |
|
101 |
|
102 >>> get_tests_module('source/plugins/common/ConeContentPlugin') |
|
103 'contentplugin' |
|
104 >>> get_tests_module('foo') |
|
105 None |
|
106 """ |
|
107 if not os.path.isdir(plugin_src_path): |
|
108 return None |
|
109 |
|
110 for name in os.listdir(plugin_src_path): |
|
111 path = os.path.join(plugin_src_path, name) |
|
112 if os.path.isdir(path) and '__init__.py' in os.listdir(path): |
|
113 return name |
|
114 return None |
|
115 |
|
116 def find_plugin_sources(subpackage_root_path): |
|
117 """ |
|
118 Return ConE plug-in source directories from the given path. |
|
119 |
|
120 All sub-directories in subpackage_root_path of the form Cone*Plugin/ containing |
|
121 a sub-directory that is a python module are returned. |
|
122 |
|
123 @param path: The directory from where to find the entries. |
|
124 @return: A list of tuples, each containing (<source_dir>, <module_name>). |
|
125 """ |
|
126 pattern = re.compile(r'^Cone.*Plugin$') |
|
127 |
|
128 # Collect a list of plug-in source paths and plug-in module names |
|
129 result = [] |
|
130 for name in os.listdir(subpackage_root_path): |
|
131 path = os.path.join(subpackage_root_path, name) |
|
132 if pattern.match(name) is not None: |
|
133 modname = get_plugin_module_name(path) |
|
134 if modname is not None: |
|
135 result.append((path, modname)) |
|
136 return result |
|
137 |
|
138 def find_all_plugin_sources(plugins_root_path): |
|
139 """ |
|
140 Return all ConE plug-in source directories from the plugins/ root |
|
141 directory. |
|
142 @return: A dictionary containing the output of find_plugin_sources() by |
|
143 plug-in sub-packages. |
|
144 >>> find_all_plugin_sources('C:/work/cone-trunk/source/plugins') |
|
145 {'common': [('C:/work/cone-trunk/source/plugins/common/ConeContentPlugin', 'contentplugin'), |
|
146 ('C:/work/cone-trunk/source/plugins/common/ConeTemplatePlugin', 'templatemlplugin')], |
|
147 'example': [('C:/work/cone-trunk/source/plugins/example/ConeExamplePlugin', 'examplemlplugin')]} |
|
148 """ |
|
149 result = {} |
|
150 for name in os.listdir(plugins_root_path): |
|
151 path = os.path.join(plugins_root_path, name) |
|
152 if os.path.isdir(path): |
|
153 sources = find_plugin_sources(path) |
|
154 if sources: result[name] = sources |
|
155 return result |
|
156 |
|
157 def find_plugin_package_subpaths(plugin_source_root, subpath, package_name=None): |
|
158 """ |
|
159 Return a list of plug-in package sub-paths based on the given plug-in package name. |
|
160 |
|
161 The returned list always contains the sub-path for common plug-ins, and |
|
162 additionally for an extra plug-in package based on the given package name. |
|
163 |
|
164 This function can be used to find specifically named files or directories |
|
165 under the plug-in paths. E.g. find all 'integration-test' directories: |
|
166 |
|
167 >>> find_plugin_package_subpaths('integration-test', 'symbian') |
|
168 [('common', 'C:\\cone\\trunk\\sources\\plugins\\common\\integration-test'), |
|
169 ('symbian', 'C:\\cone\\trunk\\sources\\plugins\\symbian\\integration-test')] |
|
170 |
|
171 @param package_name: Name of the extra plug-in package. Can be None, '', |
|
172 'common' or an existing plug-in package. |
|
173 @return: List of tuples (package_name, subpath). |
|
174 |
|
175 @raise ValueError: The given package_name was invalid. |
|
176 """ |
|
177 result = [] |
|
178 |
|
179 def add(package_name): |
|
180 package_dir = os.path.join(plugin_source_root, package_name) |
|
181 |
|
182 if not os.path.exists(package_dir): |
|
183 raise ValueError("Invalid plug-in package name: '%s'" % package_name) |
|
184 |
|
185 path = os.path.normpath(os.path.join(package_dir, subpath)) |
|
186 if os.path.exists(path): |
|
187 result.append((package_name, path)) |
|
188 |
|
189 add('common') |
|
190 if package_name not in (None, '', 'common'): |
|
191 add(package_name) |
|
192 |
|
193 return result |
|
194 |
|
195 |
|
196 def find_plugin_sources_by_package(plugin_source_root, package_name=None): |
|
197 """ |
|
198 Return a list of plug-in source paths based on the given plug-in package name. |
|
199 |
|
200 The returned list always contains sources of common plug-ins, and |
|
201 additionally extra plug-in sources based on the given package name. |
|
202 |
|
203 @param package_name: Name of the extra plug-in package. Can be None, '', |
|
204 'common' or an existing plug-in package. |
|
205 @raise ValueError: The given package_name was invalid. |
|
206 """ |
|
207 result = [] |
|
208 |
|
209 # Find all plug-in sources |
|
210 sources = find_all_plugin_sources(plugin_source_root) |
|
211 |
|
212 # Always return common plug-ins |
|
213 if 'common' in sources: |
|
214 for path, modname in sources['common']: |
|
215 result.append(path) |
|
216 |
|
217 # Return extra plug-ins if necessary |
|
218 if package_name not in (None, '', 'common'): |
|
219 if package_name not in sources: |
|
220 raise ValueError("Invalid plug-in package name: '%s'" % package_name) |
|
221 else: |
|
222 for path, modname in sources[package_name]: |
|
223 result.append(path) |
|
224 |
|
225 return result |