blob: 5609064b2c6c0e72374c38cd7c0277ea3ba865cc [file] [log] [blame]
mbligh7c8ea992009-06-22 19:03:08 +00001#!/usr/bin/python
showard909c7a62008-07-15 21:52:38 +00002#
3# Copyright 2008 Google Inc. All Rights Reserved.
4"""
5This utility allows for easy updating, removing and importing
showardeab66ce2009-12-23 00:03:56 +00006of tests into the autotest_web afe_autotests table.
showard909c7a62008-07-15 21:52:38 +00007
8Example of updating client side tests:
mbligh4b5c31e2009-07-11 00:55:34 +00009./test_importer.py -t /usr/local/autotest/client/tests
showard909c7a62008-07-15 21:52:38 +000010
mbligh4b5c31e2009-07-11 00:55:34 +000011If, for example, not all of your control files adhere to the standard outlined
12at http://autotest.kernel.org/wiki/ControlRequirements, you can force options:
showard909c7a62008-07-15 21:52:38 +000013
mbligh4b5c31e2009-07-11 00:55:34 +000014./test_importer.py --test-type server -t /usr/local/autotest/server/tests
showard909c7a62008-07-15 21:52:38 +000015
mbligh4b5c31e2009-07-11 00:55:34 +000016You would need to pass --add-noncompliant to include such control files,
17however. An easy way to check for compliance is to run in dry mode:
showard909c7a62008-07-15 21:52:38 +000018
mbligh4b5c31e2009-07-11 00:55:34 +000019./test_importer.py --dry-run -t /usr/local/autotest/server/tests/mytest
20
21Or to check a single control file, you can use check_control_file_vars.py.
22
23Running with no options is equivalent to --add-all --db-clear-tests.
24
25Most options should be fairly self explanatory, use --help to display them.
showard909c7a62008-07-15 21:52:38 +000026"""
27
28
showard909c7a62008-07-15 21:52:38 +000029import common
showardcd5131c2010-01-12 18:54:33 +000030import logging, re, os, sys, optparse, compiler
Aviv Keshet6f455262013-03-01 16:02:29 -080031#pylint: disable-msg=W0611
showardcd5131c2010-01-12 18:54:33 +000032from autotest_lib.frontend import setup_django_environment
33from autotest_lib.frontend.afe import models
mbligh61be4cd2010-03-30 02:02:40 +000034from autotest_lib.client.common_lib import control_data, utils
lmrc781f552010-05-20 00:36:37 +000035from autotest_lib.client.common_lib import logging_config, logging_manager
showard909c7a62008-07-15 21:52:38 +000036
showard909c7a62008-07-15 21:52:38 +000037
lmrc781f552010-05-20 00:36:37 +000038class TestImporterLoggingConfig(logging_config.LoggingConfig):
Aviv Keshet6f455262013-03-01 16:02:29 -080039 #pylint: disable-msg=C0111
lmrc781f552010-05-20 00:36:37 +000040 def configure_logging(self, results_dir=None, verbose=False):
41 super(TestImporterLoggingConfig, self).configure_logging(
42 use_console=True,
43 verbose=verbose)
44
45
showard909c7a62008-07-15 21:52:38 +000046# Global
47DRY_RUN = False
showard989f25d2008-10-01 11:38:11 +000048DEPENDENCIES_NOT_FOUND = set()
showard909c7a62008-07-15 21:52:38 +000049
showardcd5131c2010-01-12 18:54:33 +000050
mbligh61be4cd2010-03-30 02:02:40 +000051def update_all(autotest_dir, add_noncompliant, add_experimental):
52 """
53 Function to scan through all tests and add them to the database.
54
55 This function invoked when no parameters supplied to the command line.
56 It 'synchronizes' the test database with the current contents of the
57 client and server test directories. When test code is discovered
58 in the file system new tests may be added to the db. Likewise,
59 if test code is not found in the filesystem, tests may be removed
60 from the db. The base test directories are hard-coded to client/tests,
61 client/site_tests, server/tests and server/site_tests.
62
63 @param autotest_dir: prepended to path strings (/usr/local/autotest).
64 @param add_noncompliant: attempt adding test with invalid control files.
65 @param add_experimental: add tests with experimental attribute set.
66 """
showardcd5131c2010-01-12 18:54:33 +000067 for path in [ 'server/tests', 'server/site_tests', 'client/tests',
68 'client/site_tests']:
69 test_path = os.path.join(autotest_dir, path)
70 if not os.path.exists(test_path):
71 continue
mbligh61be4cd2010-03-30 02:02:40 +000072 logging.info("Scanning %s", test_path)
showardcd5131c2010-01-12 18:54:33 +000073 tests = []
74 tests = get_tests_from_fs(test_path, "^control.*",
75 add_noncompliant=add_noncompliant)
76 update_tests_in_db(tests, add_experimental=add_experimental,
77 add_noncompliant=add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +000078 autotest_dir=autotest_dir)
showardcd5131c2010-01-12 18:54:33 +000079 test_suite_path = os.path.join(autotest_dir, 'test_suites')
80 if os.path.exists(test_suite_path):
mbligh61be4cd2010-03-30 02:02:40 +000081 logging.info("Scanning %s", test_suite_path)
showardcd5131c2010-01-12 18:54:33 +000082 tests = get_tests_from_fs(test_suite_path, '.*',
83 add_noncompliant=add_noncompliant)
84 update_tests_in_db(tests, add_experimental=add_experimental,
85 add_noncompliant=add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +000086 autotest_dir=autotest_dir)
showardcd5131c2010-01-12 18:54:33 +000087
88 profilers_path = os.path.join(autotest_dir, "client/profilers")
89 if os.path.exists(profilers_path):
mbligh61be4cd2010-03-30 02:02:40 +000090 logging.info("Scanning %s", profilers_path)
showardcd5131c2010-01-12 18:54:33 +000091 profilers = get_tests_from_fs(profilers_path, '.*py$')
mbligh61be4cd2010-03-30 02:02:40 +000092 update_profilers_in_db(profilers, add_noncompliant=add_noncompliant,
showardcd5131c2010-01-12 18:54:33 +000093 description='NA')
94 # Clean bad db entries
mbligh61be4cd2010-03-30 02:02:40 +000095 db_clean_broken(autotest_dir)
showardcd5131c2010-01-12 18:54:33 +000096
97
mbligh61be4cd2010-03-30 02:02:40 +000098def update_samples(autotest_dir, add_noncompliant, add_experimental):
99 """
100 Add only sample tests to the database from the filesystem.
101
102 This function invoked when -S supplied on command line.
103 Only adds tests to the database - does not delete any.
104 Samples tests are formatted slightly differently than other tests.
105
106 @param autotest_dir: prepended to path strings (/usr/local/autotest).
107 @param add_noncompliant: attempt adding test with invalid control files.
108 @param add_experimental: add tests with experimental attribute set.
109 """
showardcd5131c2010-01-12 18:54:33 +0000110 sample_path = os.path.join(autotest_dir, 'server/samples')
111 if os.path.exists(sample_path):
mbligh61be4cd2010-03-30 02:02:40 +0000112 logging.info("Scanning %s", sample_path)
showardcd5131c2010-01-12 18:54:33 +0000113 tests = get_tests_from_fs(sample_path, '.*srv$',
114 add_noncompliant=add_noncompliant)
115 update_tests_in_db(tests, add_experimental=add_experimental,
116 add_noncompliant=add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000117 autotest_dir=autotest_dir)
showardcd5131c2010-01-12 18:54:33 +0000118
119
mbligh61be4cd2010-03-30 02:02:40 +0000120def db_clean_broken(autotest_dir):
121 """
122 Remove tests from autotest_web that do not have valid control files
showardcd5131c2010-01-12 18:54:33 +0000123
mbligh61be4cd2010-03-30 02:02:40 +0000124 This function invoked when -c supplied on the command line and when
125 running update_all(). Removes tests from database which are not
126 found in the filesystem. Also removes profilers which are just
127 a special case of tests.
128
129 @param autotest_dir: prepended to path strings (/usr/local/autotest).
showardcd5131c2010-01-12 18:54:33 +0000130 """
131 for test in models.Test.objects.all():
132 full_path = os.path.join(autotest_dir, test.path)
133 if not os.path.isfile(full_path):
mbligh61be4cd2010-03-30 02:02:40 +0000134 logging.info("Removing %s", test.path)
showardcd5131c2010-01-12 18:54:33 +0000135 _log_or_execute(repr(test), test.delete)
136
137 # Find profilers that are no longer present
138 for profiler in models.Profiler.objects.all():
139 full_path = os.path.join(autotest_dir, "client", "profilers",
140 profiler.name)
141 if not os.path.exists(full_path):
mbligh61be4cd2010-03-30 02:02:40 +0000142 logging.info("Removing %s", profiler.name)
showardcd5131c2010-01-12 18:54:33 +0000143 _log_or_execute(repr(profiler), profiler.delete)
144
145
mbligh61be4cd2010-03-30 02:02:40 +0000146def db_clean_all(autotest_dir):
147 """
148 Remove all tests from autotest_web - very destructive
149
150 This function invoked when -C supplied on the command line.
151 Removes ALL tests from the database.
152
153 @param autotest_dir: prepended to path strings (/usr/local/autotest).
154 """
155 for test in models.Test.objects.all():
156 full_path = os.path.join(autotest_dir, test.path)
157 logging.info("Removing %s", test.path)
158 _log_or_execute(repr(test), test.delete)
159
160 # Find profilers that are no longer present
161 for profiler in models.Profiler.objects.all():
162 full_path = os.path.join(autotest_dir, "client", "profilers",
163 profiler.name)
164 logging.info("Removing %s", profiler.name)
165 _log_or_execute(repr(profiler), profiler.delete)
166
167
168def update_profilers_in_db(profilers, description='NA',
showardcd5131c2010-01-12 18:54:33 +0000169 add_noncompliant=False):
mbligh61be4cd2010-03-30 02:02:40 +0000170 """
171 Add only profilers to the database from the filesystem.
172
173 This function invoked when -p supplied on command line.
174 Only adds profilers to the database - does not delete any.
175 Profilers are formatted slightly differently than tests.
176
177 @param profilers: list of profilers found in the file system.
178 @param description: simple text to satisfy docstring.
179 @param add_noncompliant: attempt adding test with invalid control files.
180 """
showardcd5131c2010-01-12 18:54:33 +0000181 for profiler in profilers:
mblighed0526a2010-04-22 18:26:17 +0000182 name = os.path.basename(profiler)
183 if name.endswith('.py'):
184 name = name[:-3]
showardcd5131c2010-01-12 18:54:33 +0000185 if not profilers[profiler]:
186 if add_noncompliant:
187 doc = description
188 else:
jamesrenfed94092010-05-03 20:52:47 +0000189 logging.warn("Skipping %s, missing docstring", profiler)
mbligh781d2692010-04-21 01:04:28 +0000190 continue
showardcd5131c2010-01-12 18:54:33 +0000191 else:
192 doc = profilers[profiler]
193
194 model = models.Profiler.objects.get_or_create(name=name)[0]
195 model.description = doc
196 _log_or_execute(repr(model), model.save)
197
198
199def update_tests_in_db(tests, dry_run=False, add_experimental=False,
mbligh61be4cd2010-03-30 02:02:40 +0000200 add_noncompliant=False, autotest_dir=None):
201 """
202 Scans through all tests and add them to the database.
203
204 This function invoked when -t supplied and for update_all.
205 When test code is discovered in the file system new tests may be added
206
207 @param tests: list of tests found in the filesystem.
208 @param dry_run: not used at this time.
209 @param add_experimental: add tests with experimental attribute set.
210 @param add_noncompliant: attempt adding test with invalid control files.
211 @param autotest_dir: prepended to path strings (/usr/local/autotest).
212 """
213 site_set_attributes_module = utils.import_site_module(
214 __file__, 'autotest_lib.utils.site_test_importer_attributes')
215
showardcd5131c2010-01-12 18:54:33 +0000216 for test in tests:
217 new_test = models.Test.objects.get_or_create(
218 path=test.replace(autotest_dir, '').lstrip('/'))[0]
mbligh61be4cd2010-03-30 02:02:40 +0000219 logging.info("Processing %s", new_test.path)
showardcd5131c2010-01-12 18:54:33 +0000220
221 # Set the test's attributes
222 data = tests[test]
223 _set_attributes_clean(new_test, data)
224
mbligh61be4cd2010-03-30 02:02:40 +0000225 # Custom Attribute Update
226 if site_set_attributes_module:
227 site_set_attributes_module._set_attributes_custom(new_test, data)
228
showardcd5131c2010-01-12 18:54:33 +0000229 # This only takes place if --add-noncompliant is provided on the CLI
230 if not new_test.name:
231 test_new_test = test.split('/')
232 if test_new_test[-1] == 'control':
233 new_test.name = test_new_test[-2]
234 else:
235 control_name = "%s:%s"
236 control_name %= (test_new_test[-2],
237 test_new_test[-1])
238 new_test.name = control_name.replace('control.', '')
239
240 # Experimental Check
241 if not add_experimental and new_test.experimental:
242 continue
243
244 _log_or_execute(repr(new_test), new_test.save)
245 add_label_dependencies(new_test)
246
Eric Li861b2d52011-02-04 14:50:35 -0800247 # save TestParameter
248 for para_name in data.test_parameters:
249 test_parameter = models.TestParameter.objects.get_or_create(
250 test=new_test, name=para_name)[0]
251 test_parameter.save()
252
showardcd5131c2010-01-12 18:54:33 +0000253
254def _set_attributes_clean(test, data):
mbligh61be4cd2010-03-30 02:02:40 +0000255 """
256 First pass sets the attributes of the Test object from file system.
showardcd5131c2010-01-12 18:54:33 +0000257
mbligh61be4cd2010-03-30 02:02:40 +0000258 @param test: a test object to be populated for the database.
259 @param data: object with test data from the file system.
260 """
showardcd5131c2010-01-12 18:54:33 +0000261 test_type = { 'client' : 1,
262 'server' : 2, }
263 test_time = { 'short' : 1,
264 'medium' : 2,
265 'long' : 3, }
266
267
268 string_attributes = ('name', 'author', 'test_class', 'test_category',
269 'test_category', 'sync_count')
270 for attribute in string_attributes:
271 setattr(test, attribute, getattr(data, attribute))
272
273 test.description = data.doc
274 test.dependencies = ", ".join(data.dependencies)
275
276 int_attributes = ('experimental', 'run_verify')
277 for attribute in int_attributes:
278 setattr(test, attribute, int(getattr(data, attribute)))
279
280 try:
281 test.test_type = int(data.test_type)
282 if test.test_type != 1 and test.test_type != 2:
283 raise Exception('Incorrect number %d for test_type' %
284 test.test_type)
285 except ValueError:
286 pass
287 try:
288 test.test_time = int(data.time)
289 if test.test_time < 1 or test.time > 3:
290 raise Exception('Incorrect number %d for time' % test.time)
291 except ValueError:
292 pass
293
294 if not test.test_time and str == type(data.time):
295 test.test_time = test_time[data.time.lower()]
296 if not test.test_type and str == type(data.test_type):
297 test.test_type = test_type[data.test_type.lower()]
298
Aviv Keshet6f455262013-03-01 16:02:29 -0800299 test.test_retry = data.retries
300
showardcd5131c2010-01-12 18:54:33 +0000301
302def add_label_dependencies(test):
303 """
mbligh61be4cd2010-03-30 02:02:40 +0000304 Add proper many-to-many relationships from DEPENDENCIES field.
305
306 @param test: test object for the database.
showardcd5131c2010-01-12 18:54:33 +0000307 """
mbligh61be4cd2010-03-30 02:02:40 +0000308
showardcd5131c2010-01-12 18:54:33 +0000309 # clear out old relationships
310 _log_or_execute(repr(test), test.dependency_labels.clear,
311 subject='clear dependencies from')
312
313 for label_name in test.dependencies.split(','):
314 label_name = label_name.strip().lower()
315 if not label_name:
316 continue
317
318 try:
319 label = models.Label.objects.get(name=label_name)
320 except models.Label.DoesNotExist:
321 log_dependency_not_found(label_name)
322 continue
323
324 _log_or_execute(repr(label), test.dependency_labels.add, label,
325 subject='add dependency to %s' % test.name)
326
327
328def log_dependency_not_found(label_name):
mbligh61be4cd2010-03-30 02:02:40 +0000329 """
330 Exception processing when label not found in database.
331
332 @param label_name: from test dependencies.
333 """
showardcd5131c2010-01-12 18:54:33 +0000334 if label_name in DEPENDENCIES_NOT_FOUND:
335 return
mbligh61be4cd2010-03-30 02:02:40 +0000336 logging.info("Dependency %s not found", label_name)
showardcd5131c2010-01-12 18:54:33 +0000337 DEPENDENCIES_NOT_FOUND.add(label_name)
338
339
340def get_tests_from_fs(parent_dir, control_pattern, add_noncompliant=False):
mbligh61be4cd2010-03-30 02:02:40 +0000341 """
342 Find control files in file system and load a list with their info.
showardcd5131c2010-01-12 18:54:33 +0000343
mbligh61be4cd2010-03-30 02:02:40 +0000344 @param parent_dir: directory to search recursively.
345 @param control_pattern: name format of control file.
346 @param add_noncompliant: ignore control file parse errors.
347
mblighed0526a2010-04-22 18:26:17 +0000348 @return dictionary of the form: tests[file_path] = parsed_object
showardcd5131c2010-01-12 18:54:33 +0000349 """
350 tests = {}
351 profilers = False
352 if 'client/profilers' in parent_dir:
353 profilers = True
354 for dir in [ parent_dir ]:
355 files = recursive_walk(dir, control_pattern)
356 for file in files:
357 if '__init__.py' in file or '.svn' in file:
358 continue
359 if not profilers:
360 if not add_noncompliant:
361 try:
362 found_test = control_data.parse_control(file,
363 raise_warnings=True)
364 tests[file] = found_test
365 except control_data.ControlVariableException, e:
jamesrenfed94092010-05-03 20:52:47 +0000366 logging.warn("Skipping %s\n%s", file, e)
jamesren6e3e9bd2010-04-28 18:07:05 +0000367 except Exception, e:
368 logging.error("Bad %s\n%s", file, e)
showardcd5131c2010-01-12 18:54:33 +0000369 else:
370 found_test = control_data.parse_control(file)
371 tests[file] = found_test
372 else:
showardcd5131c2010-01-12 18:54:33 +0000373 tests[file] = compiler.parseFile(file).doc
374 return tests
375
376
377def recursive_walk(path, wildcard):
mbligh61be4cd2010-03-30 02:02:40 +0000378 """
379 Recursively go through a directory.
380
381 This function invoked by get_tests_from_fs().
382
383 @param path: base directory to start search.
384 @param wildcard: name format to match.
385
mblighed0526a2010-04-22 18:26:17 +0000386 @return A list of files that match wildcard
showardcd5131c2010-01-12 18:54:33 +0000387 """
388 files = []
389 directories = [ path ]
390 while len(directories)>0:
391 directory = directories.pop()
392 for name in os.listdir(directory):
393 fullpath = os.path.join(directory, name)
394 if os.path.isfile(fullpath):
395 # if we are a control file
396 if re.search(wildcard, name):
397 files.append(fullpath)
398 elif os.path.isdir(fullpath):
399 directories.append(fullpath)
400 return files
401
402
403def _log_or_execute(content, func, *args, **kwargs):
mbligh61be4cd2010-03-30 02:02:40 +0000404 """
405 Log a message if dry_run is enabled, or execute the given function.
showardcd5131c2010-01-12 18:54:33 +0000406
mbligh61be4cd2010-03-30 02:02:40 +0000407 Relies on the DRY_RUN global variable.
408
409 @param content: the actual log message.
410 @param func: function to execute if dry_run is not enabled.
411 @param subject: (Optional) The type of log being written. Defaults to
412 the name of the provided function.
showardcd5131c2010-01-12 18:54:33 +0000413 """
414 subject = kwargs.get('subject', func.__name__)
415
416 if DRY_RUN:
mbligh61be4cd2010-03-30 02:02:40 +0000417 logging.info("Would %s: %s", subject, content)
showardcd5131c2010-01-12 18:54:33 +0000418 else:
419 func(*args)
420
421
mbligh61be4cd2010-03-30 02:02:40 +0000422def _create_whitelist_set(whitelist_path):
423 """
424 Create a set with contents from a whitelist file for membership testing.
425
426 @param whitelist_path: full path to the whitelist file.
427
mblighed0526a2010-04-22 18:26:17 +0000428 @return set with files listed one/line - newlines included.
mbligh61be4cd2010-03-30 02:02:40 +0000429 """
430 f = open(whitelist_path, 'r')
431 whitelist_set = set([line.strip() for line in f])
432 f.close()
433 return whitelist_set
434
435
436def update_from_whitelist(whitelist_set, add_experimental, add_noncompliant,
437 autotest_dir):
438 """
439 Scans through all tests in the whitelist and add them to the database.
440
441 This function invoked when -w supplied.
442
443 @param whitelist_set: set of tests in full-path form from a whitelist.
444 @param add_experimental: add tests with experimental attribute set.
445 @param add_noncompliant: attempt adding test with invalid control files.
446 @param autotest_dir: prepended to path strings (/usr/local/autotest).
447 """
448 tests = {}
449 profilers = {}
450 for file_path in whitelist_set:
451 if file_path.find('client/profilers') == -1:
452 try:
453 found_test = control_data.parse_control(file_path,
454 raise_warnings=True)
455 tests[file_path] = found_test
456 except control_data.ControlVariableException, e:
jamesrenfed94092010-05-03 20:52:47 +0000457 logging.warn("Skipping %s\n%s", file, e)
mbligh61be4cd2010-03-30 02:02:40 +0000458 else:
459 profilers[file_path] = compiler.parseFile(file_path).doc
460
461 if len(tests) > 0:
462 update_tests_in_db(tests, add_experimental=add_experimental,
463 add_noncompliant=add_noncompliant,
464 autotest_dir=autotest_dir)
465 if len(profilers) > 0:
466 update_profilers_in_db(profilers, add_noncompliant=add_noncompliant,
467 description='NA')
468
469
showard909c7a62008-07-15 21:52:38 +0000470def main(argv):
Aviv Keshet6f455262013-03-01 16:02:29 -0800471 """Main function
472 @param argv: List of command line parameters.
473 """
mbligh61be4cd2010-03-30 02:02:40 +0000474
showard909c7a62008-07-15 21:52:38 +0000475 global DRY_RUN
476 parser = optparse.OptionParser()
mbligh61be4cd2010-03-30 02:02:40 +0000477 parser.add_option('-c', '--db-clean-tests',
478 dest='clean_tests', action='store_true',
showard909c7a62008-07-15 21:52:38 +0000479 default=False,
mbligh61be4cd2010-03-30 02:02:40 +0000480 help='Clean client and server tests with invalid control files')
481 parser.add_option('-C', '--db-clear-all-tests',
482 dest='clear_all_tests', action='store_true',
483 default=False,
484 help='Clear ALL client and server tests')
showard909c7a62008-07-15 21:52:38 +0000485 parser.add_option('-d', '--dry-run',
486 dest='dry_run', action='store_true', default=False,
487 help='Dry run for operation')
mbligh322ec1a2008-09-26 16:48:10 +0000488 parser.add_option('-A', '--add-all',
489 dest='add_all', action='store_true',
490 default=False,
mbligh25d656c2009-08-24 22:04:02 +0000491 help='Add site_tests, tests, and test_suites')
492 parser.add_option('-S', '--add-samples',
493 dest='add_samples', action='store_true',
494 default=False,
495 help='Add samples.')
mbligh322ec1a2008-09-26 16:48:10 +0000496 parser.add_option('-E', '--add-experimental',
showard909c7a62008-07-15 21:52:38 +0000497 dest='add_experimental', action='store_true',
mbligh79410e12008-11-20 17:59:15 +0000498 default=True,
showard909c7a62008-07-15 21:52:38 +0000499 help='Add experimental tests to frontend')
500 parser.add_option('-N', '--add-noncompliant',
501 dest='add_noncompliant', action='store_true',
502 default=False,
mbligh4b5c31e2009-07-11 00:55:34 +0000503 help='Add non-compliant tests (i.e. tests that do not '
504 'define all required control variables)')
mbligh322ec1a2008-09-26 16:48:10 +0000505 parser.add_option('-p', '--profile-dir', dest='profile_dir',
506 help='Directory to recursively check for profiles')
showard909c7a62008-07-15 21:52:38 +0000507 parser.add_option('-t', '--tests-dir', dest='tests_dir',
508 help='Directory to recursively check for control.*')
showard909c7a62008-07-15 21:52:38 +0000509 parser.add_option('-r', '--control-pattern', dest='control_pattern',
510 default='^control.*',
511 help='The pattern to look for in directories for control files')
mbligh322ec1a2008-09-26 16:48:10 +0000512 parser.add_option('-v', '--verbose',
513 dest='verbose', action='store_true', default=False,
514 help='Run in verbose mode')
mbligh61be4cd2010-03-30 02:02:40 +0000515 parser.add_option('-w', '--whitelist-file', dest='whitelist_file',
516 help='Filename for list of test names that must match')
517 parser.add_option('-z', '--autotest-dir', dest='autotest_dir',
mbligh322ec1a2008-09-26 16:48:10 +0000518 default=os.path.join(os.path.dirname(__file__), '..'),
showard909c7a62008-07-15 21:52:38 +0000519 help='Autotest directory root')
520 options, args = parser.parse_args()
lmrc781f552010-05-20 00:36:37 +0000521
522 logging_manager.configure_logging(TestImporterLoggingConfig(),
523 verbose=options.verbose)
524
showard909c7a62008-07-15 21:52:38 +0000525 DRY_RUN = options.dry_run
jamesrenfed94092010-05-03 20:52:47 +0000526 if DRY_RUN:
527 logging.getLogger().setLevel(logging.WARN)
528
mbligh322ec1a2008-09-26 16:48:10 +0000529 # Make sure autotest_dir is the absolute path
530 options.autotest_dir = os.path.abspath(options.autotest_dir)
531
532 if len(args) > 0:
mbligh61be4cd2010-03-30 02:02:40 +0000533 logging.error("Invalid option(s) provided: %s", args)
showard909c7a62008-07-15 21:52:38 +0000534 parser.print_help()
535 return 1
536
mbligh61be4cd2010-03-30 02:02:40 +0000537 if options.verbose:
538 logging.getLogger().setLevel(logging.DEBUG)
539
540 if len(argv) == 1 or (len(argv) == 2 and options.verbose):
mbligh322ec1a2008-09-26 16:48:10 +0000541 update_all(options.autotest_dir, options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000542 options.add_experimental)
543 db_clean_broken(options.autotest_dir)
mbligh322ec1a2008-09-26 16:48:10 +0000544 return 0
545
mbligh61be4cd2010-03-30 02:02:40 +0000546 if options.clear_all_tests:
547 if (options.clean_tests or options.add_all or options.add_samples or
548 options.add_noncompliant):
549 logging.error(
550 "Can only pass --autotest-dir, --dry-run and --verbose with "
551 "--db-clear-all-tests")
552 return 1
553 db_clean_all(options.autotest_dir)
554
555 whitelist_set = None
556 if options.whitelist_file:
557 if options.add_all:
558 logging.error("Cannot pass both --add-all and --whitelist-file")
559 return 1
560 whitelist_path = os.path.abspath(options.whitelist_file)
561 if not os.path.isfile(whitelist_path):
562 logging.error("--whitelist-file (%s) not found", whitelist_path)
563 return 1
564 logging.info("Using whitelist file %s", whitelist_path)
565 whitelist_set = _create_whitelist_set(whitelist_path)
566 update_from_whitelist(whitelist_set,
567 add_experimental=options.add_experimental,
568 add_noncompliant=options.add_noncompliant,
569 autotest_dir=options.autotest_dir)
mbligh322ec1a2008-09-26 16:48:10 +0000570 if options.add_all:
571 update_all(options.autotest_dir, options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000572 options.add_experimental)
mbligh25d656c2009-08-24 22:04:02 +0000573 if options.add_samples:
574 update_samples(options.autotest_dir, options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000575 options.add_experimental)
showard909c7a62008-07-15 21:52:38 +0000576 if options.tests_dir:
mbligh4b5c31e2009-07-11 00:55:34 +0000577 options.tests_dir = os.path.abspath(options.tests_dir)
showard909c7a62008-07-15 21:52:38 +0000578 tests = get_tests_from_fs(options.tests_dir, options.control_pattern,
579 add_noncompliant=options.add_noncompliant)
mbligh322ec1a2008-09-26 16:48:10 +0000580 update_tests_in_db(tests, add_experimental=options.add_experimental,
showard909c7a62008-07-15 21:52:38 +0000581 add_noncompliant=options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000582 autotest_dir=options.autotest_dir)
mbligh322ec1a2008-09-26 16:48:10 +0000583 if options.profile_dir:
584 profilers = get_tests_from_fs(options.profile_dir, '.*py$')
mbligh61be4cd2010-03-30 02:02:40 +0000585 update_profilers_in_db(profilers,
mbligh322ec1a2008-09-26 16:48:10 +0000586 add_noncompliant=options.add_noncompliant,
587 description='NA')
mbligh61be4cd2010-03-30 02:02:40 +0000588 if options.clean_tests:
589 db_clean_broken(options.autotest_dir)
showard909c7a62008-07-15 21:52:38 +0000590
591
showard909c7a62008-07-15 21:52:38 +0000592if __name__ == "__main__":
593 main(sys.argv)