blob: 6a63b1ef8f3592eaa543759b75d99fede3428b71 [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
31from autotest_lib.frontend import setup_django_environment
32from autotest_lib.frontend.afe import models
mbligh61be4cd2010-03-30 02:02:40 +000033from autotest_lib.client.common_lib import control_data, utils
lmrc781f552010-05-20 00:36:37 +000034from autotest_lib.client.common_lib import logging_config, logging_manager
showard909c7a62008-07-15 21:52:38 +000035
showard909c7a62008-07-15 21:52:38 +000036
lmrc781f552010-05-20 00:36:37 +000037class TestImporterLoggingConfig(logging_config.LoggingConfig):
38 def configure_logging(self, results_dir=None, verbose=False):
39 super(TestImporterLoggingConfig, self).configure_logging(
40 use_console=True,
41 verbose=verbose)
42
43
showard909c7a62008-07-15 21:52:38 +000044# Global
45DRY_RUN = False
showard989f25d2008-10-01 11:38:11 +000046DEPENDENCIES_NOT_FOUND = set()
showard909c7a62008-07-15 21:52:38 +000047
showardcd5131c2010-01-12 18:54:33 +000048
mbligh61be4cd2010-03-30 02:02:40 +000049def update_all(autotest_dir, add_noncompliant, add_experimental):
50 """
51 Function to scan through all tests and add them to the database.
52
53 This function invoked when no parameters supplied to the command line.
54 It 'synchronizes' the test database with the current contents of the
55 client and server test directories. When test code is discovered
56 in the file system new tests may be added to the db. Likewise,
57 if test code is not found in the filesystem, tests may be removed
58 from the db. The base test directories are hard-coded to client/tests,
59 client/site_tests, server/tests and server/site_tests.
60
61 @param autotest_dir: prepended to path strings (/usr/local/autotest).
62 @param add_noncompliant: attempt adding test with invalid control files.
63 @param add_experimental: add tests with experimental attribute set.
64 """
showardcd5131c2010-01-12 18:54:33 +000065 for path in [ 'server/tests', 'server/site_tests', 'client/tests',
66 'client/site_tests']:
67 test_path = os.path.join(autotest_dir, path)
68 if not os.path.exists(test_path):
69 continue
mbligh61be4cd2010-03-30 02:02:40 +000070 logging.info("Scanning %s", test_path)
showardcd5131c2010-01-12 18:54:33 +000071 tests = []
72 tests = get_tests_from_fs(test_path, "^control.*",
73 add_noncompliant=add_noncompliant)
74 update_tests_in_db(tests, add_experimental=add_experimental,
75 add_noncompliant=add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +000076 autotest_dir=autotest_dir)
showardcd5131c2010-01-12 18:54:33 +000077 test_suite_path = os.path.join(autotest_dir, 'test_suites')
78 if os.path.exists(test_suite_path):
mbligh61be4cd2010-03-30 02:02:40 +000079 logging.info("Scanning %s", test_suite_path)
showardcd5131c2010-01-12 18:54:33 +000080 tests = get_tests_from_fs(test_suite_path, '.*',
81 add_noncompliant=add_noncompliant)
82 update_tests_in_db(tests, add_experimental=add_experimental,
83 add_noncompliant=add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +000084 autotest_dir=autotest_dir)
showardcd5131c2010-01-12 18:54:33 +000085
86 profilers_path = os.path.join(autotest_dir, "client/profilers")
87 if os.path.exists(profilers_path):
mbligh61be4cd2010-03-30 02:02:40 +000088 logging.info("Scanning %s", profilers_path)
showardcd5131c2010-01-12 18:54:33 +000089 profilers = get_tests_from_fs(profilers_path, '.*py$')
mbligh61be4cd2010-03-30 02:02:40 +000090 update_profilers_in_db(profilers, add_noncompliant=add_noncompliant,
showardcd5131c2010-01-12 18:54:33 +000091 description='NA')
92 # Clean bad db entries
mbligh61be4cd2010-03-30 02:02:40 +000093 db_clean_broken(autotest_dir)
showardcd5131c2010-01-12 18:54:33 +000094
95
mbligh61be4cd2010-03-30 02:02:40 +000096def update_samples(autotest_dir, add_noncompliant, add_experimental):
97 """
98 Add only sample tests to the database from the filesystem.
99
100 This function invoked when -S supplied on command line.
101 Only adds tests to the database - does not delete any.
102 Samples tests are formatted slightly differently than other tests.
103
104 @param autotest_dir: prepended to path strings (/usr/local/autotest).
105 @param add_noncompliant: attempt adding test with invalid control files.
106 @param add_experimental: add tests with experimental attribute set.
107 """
showardcd5131c2010-01-12 18:54:33 +0000108 sample_path = os.path.join(autotest_dir, 'server/samples')
109 if os.path.exists(sample_path):
mbligh61be4cd2010-03-30 02:02:40 +0000110 logging.info("Scanning %s", sample_path)
showardcd5131c2010-01-12 18:54:33 +0000111 tests = get_tests_from_fs(sample_path, '.*srv$',
112 add_noncompliant=add_noncompliant)
113 update_tests_in_db(tests, add_experimental=add_experimental,
114 add_noncompliant=add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000115 autotest_dir=autotest_dir)
showardcd5131c2010-01-12 18:54:33 +0000116
117
mbligh61be4cd2010-03-30 02:02:40 +0000118def db_clean_broken(autotest_dir):
119 """
120 Remove tests from autotest_web that do not have valid control files
showardcd5131c2010-01-12 18:54:33 +0000121
mbligh61be4cd2010-03-30 02:02:40 +0000122 This function invoked when -c supplied on the command line and when
123 running update_all(). Removes tests from database which are not
124 found in the filesystem. Also removes profilers which are just
125 a special case of tests.
126
127 @param autotest_dir: prepended to path strings (/usr/local/autotest).
showardcd5131c2010-01-12 18:54:33 +0000128 """
129 for test in models.Test.objects.all():
130 full_path = os.path.join(autotest_dir, test.path)
131 if not os.path.isfile(full_path):
mbligh61be4cd2010-03-30 02:02:40 +0000132 logging.info("Removing %s", test.path)
showardcd5131c2010-01-12 18:54:33 +0000133 _log_or_execute(repr(test), test.delete)
134
135 # Find profilers that are no longer present
136 for profiler in models.Profiler.objects.all():
137 full_path = os.path.join(autotest_dir, "client", "profilers",
138 profiler.name)
139 if not os.path.exists(full_path):
mbligh61be4cd2010-03-30 02:02:40 +0000140 logging.info("Removing %s", profiler.name)
showardcd5131c2010-01-12 18:54:33 +0000141 _log_or_execute(repr(profiler), profiler.delete)
142
143
mbligh61be4cd2010-03-30 02:02:40 +0000144def db_clean_all(autotest_dir):
145 """
146 Remove all tests from autotest_web - very destructive
147
148 This function invoked when -C supplied on the command line.
149 Removes ALL tests from the database.
150
151 @param autotest_dir: prepended to path strings (/usr/local/autotest).
152 """
153 for test in models.Test.objects.all():
154 full_path = os.path.join(autotest_dir, test.path)
155 logging.info("Removing %s", test.path)
156 _log_or_execute(repr(test), test.delete)
157
158 # Find profilers that are no longer present
159 for profiler in models.Profiler.objects.all():
160 full_path = os.path.join(autotest_dir, "client", "profilers",
161 profiler.name)
162 logging.info("Removing %s", profiler.name)
163 _log_or_execute(repr(profiler), profiler.delete)
164
165
166def update_profilers_in_db(profilers, description='NA',
showardcd5131c2010-01-12 18:54:33 +0000167 add_noncompliant=False):
mbligh61be4cd2010-03-30 02:02:40 +0000168 """
169 Add only profilers to the database from the filesystem.
170
171 This function invoked when -p supplied on command line.
172 Only adds profilers to the database - does not delete any.
173 Profilers are formatted slightly differently than tests.
174
175 @param profilers: list of profilers found in the file system.
176 @param description: simple text to satisfy docstring.
177 @param add_noncompliant: attempt adding test with invalid control files.
178 """
showardcd5131c2010-01-12 18:54:33 +0000179 for profiler in profilers:
mblighed0526a2010-04-22 18:26:17 +0000180 name = os.path.basename(profiler)
181 if name.endswith('.py'):
182 name = name[:-3]
showardcd5131c2010-01-12 18:54:33 +0000183 if not profilers[profiler]:
184 if add_noncompliant:
185 doc = description
186 else:
jamesrenfed94092010-05-03 20:52:47 +0000187 logging.warn("Skipping %s, missing docstring", profiler)
mbligh781d2692010-04-21 01:04:28 +0000188 continue
showardcd5131c2010-01-12 18:54:33 +0000189 else:
190 doc = profilers[profiler]
191
192 model = models.Profiler.objects.get_or_create(name=name)[0]
193 model.description = doc
194 _log_or_execute(repr(model), model.save)
195
196
197def update_tests_in_db(tests, dry_run=False, add_experimental=False,
mbligh61be4cd2010-03-30 02:02:40 +0000198 add_noncompliant=False, autotest_dir=None):
199 """
200 Scans through all tests and add them to the database.
201
202 This function invoked when -t supplied and for update_all.
203 When test code is discovered in the file system new tests may be added
204
205 @param tests: list of tests found in the filesystem.
206 @param dry_run: not used at this time.
207 @param add_experimental: add tests with experimental attribute set.
208 @param add_noncompliant: attempt adding test with invalid control files.
209 @param autotest_dir: prepended to path strings (/usr/local/autotest).
210 """
211 site_set_attributes_module = utils.import_site_module(
212 __file__, 'autotest_lib.utils.site_test_importer_attributes')
213
showardcd5131c2010-01-12 18:54:33 +0000214 for test in tests:
215 new_test = models.Test.objects.get_or_create(
216 path=test.replace(autotest_dir, '').lstrip('/'))[0]
mbligh61be4cd2010-03-30 02:02:40 +0000217 logging.info("Processing %s", new_test.path)
showardcd5131c2010-01-12 18:54:33 +0000218
219 # Set the test's attributes
220 data = tests[test]
221 _set_attributes_clean(new_test, data)
222
mbligh61be4cd2010-03-30 02:02:40 +0000223 # Custom Attribute Update
224 if site_set_attributes_module:
225 site_set_attributes_module._set_attributes_custom(new_test, data)
226
showardcd5131c2010-01-12 18:54:33 +0000227 # This only takes place if --add-noncompliant is provided on the CLI
228 if not new_test.name:
229 test_new_test = test.split('/')
230 if test_new_test[-1] == 'control':
231 new_test.name = test_new_test[-2]
232 else:
233 control_name = "%s:%s"
234 control_name %= (test_new_test[-2],
235 test_new_test[-1])
236 new_test.name = control_name.replace('control.', '')
237
238 # Experimental Check
239 if not add_experimental and new_test.experimental:
240 continue
241
242 _log_or_execute(repr(new_test), new_test.save)
243 add_label_dependencies(new_test)
Eric Li7edb3042011-01-06 17:57:17 -0800244
245 # save TestParameter
246 for para_name in data.test_parameters:
247 test_parameter = models.TestParameter.objects.get_or_create(
248 test=new_test, name=para_name)[0]
249 test_parameter.save()
showardcd5131c2010-01-12 18:54:33 +0000250
251
252def _set_attributes_clean(test, data):
mbligh61be4cd2010-03-30 02:02:40 +0000253 """
254 First pass sets the attributes of the Test object from file system.
showardcd5131c2010-01-12 18:54:33 +0000255
mbligh61be4cd2010-03-30 02:02:40 +0000256 @param test: a test object to be populated for the database.
257 @param data: object with test data from the file system.
258 """
showardcd5131c2010-01-12 18:54:33 +0000259 test_type = { 'client' : 1,
260 'server' : 2, }
261 test_time = { 'short' : 1,
262 'medium' : 2,
263 'long' : 3, }
264
265
266 string_attributes = ('name', 'author', 'test_class', 'test_category',
267 'test_category', 'sync_count')
268 for attribute in string_attributes:
269 setattr(test, attribute, getattr(data, attribute))
270
271 test.description = data.doc
272 test.dependencies = ", ".join(data.dependencies)
273
274 int_attributes = ('experimental', 'run_verify')
275 for attribute in int_attributes:
276 setattr(test, attribute, int(getattr(data, attribute)))
277
278 try:
279 test.test_type = int(data.test_type)
280 if test.test_type != 1 and test.test_type != 2:
281 raise Exception('Incorrect number %d for test_type' %
282 test.test_type)
283 except ValueError:
284 pass
285 try:
286 test.test_time = int(data.time)
287 if test.test_time < 1 or test.time > 3:
288 raise Exception('Incorrect number %d for time' % test.time)
289 except ValueError:
290 pass
291
292 if not test.test_time and str == type(data.time):
293 test.test_time = test_time[data.time.lower()]
294 if not test.test_type and str == type(data.test_type):
295 test.test_type = test_type[data.test_type.lower()]
296
297
298def add_label_dependencies(test):
299 """
mbligh61be4cd2010-03-30 02:02:40 +0000300 Add proper many-to-many relationships from DEPENDENCIES field.
301
302 @param test: test object for the database.
showardcd5131c2010-01-12 18:54:33 +0000303 """
mbligh61be4cd2010-03-30 02:02:40 +0000304
showardcd5131c2010-01-12 18:54:33 +0000305 # clear out old relationships
306 _log_or_execute(repr(test), test.dependency_labels.clear,
307 subject='clear dependencies from')
308
309 for label_name in test.dependencies.split(','):
310 label_name = label_name.strip().lower()
311 if not label_name:
312 continue
313
314 try:
315 label = models.Label.objects.get(name=label_name)
316 except models.Label.DoesNotExist:
317 log_dependency_not_found(label_name)
318 continue
319
320 _log_or_execute(repr(label), test.dependency_labels.add, label,
321 subject='add dependency to %s' % test.name)
322
323
324def log_dependency_not_found(label_name):
mbligh61be4cd2010-03-30 02:02:40 +0000325 """
326 Exception processing when label not found in database.
327
328 @param label_name: from test dependencies.
329 """
showardcd5131c2010-01-12 18:54:33 +0000330 if label_name in DEPENDENCIES_NOT_FOUND:
331 return
mbligh61be4cd2010-03-30 02:02:40 +0000332 logging.info("Dependency %s not found", label_name)
showardcd5131c2010-01-12 18:54:33 +0000333 DEPENDENCIES_NOT_FOUND.add(label_name)
334
335
336def get_tests_from_fs(parent_dir, control_pattern, add_noncompliant=False):
mbligh61be4cd2010-03-30 02:02:40 +0000337 """
338 Find control files in file system and load a list with their info.
showardcd5131c2010-01-12 18:54:33 +0000339
mbligh61be4cd2010-03-30 02:02:40 +0000340 @param parent_dir: directory to search recursively.
341 @param control_pattern: name format of control file.
342 @param add_noncompliant: ignore control file parse errors.
343
mblighed0526a2010-04-22 18:26:17 +0000344 @return dictionary of the form: tests[file_path] = parsed_object
showardcd5131c2010-01-12 18:54:33 +0000345 """
346 tests = {}
347 profilers = False
348 if 'client/profilers' in parent_dir:
349 profilers = True
350 for dir in [ parent_dir ]:
351 files = recursive_walk(dir, control_pattern)
352 for file in files:
353 if '__init__.py' in file or '.svn' in file:
354 continue
355 if not profilers:
356 if not add_noncompliant:
357 try:
358 found_test = control_data.parse_control(file,
359 raise_warnings=True)
360 tests[file] = found_test
361 except control_data.ControlVariableException, e:
jamesrenfed94092010-05-03 20:52:47 +0000362 logging.warn("Skipping %s\n%s", file, e)
jamesren6e3e9bd2010-04-28 18:07:05 +0000363 except Exception, e:
364 logging.error("Bad %s\n%s", file, e)
showardcd5131c2010-01-12 18:54:33 +0000365 else:
366 found_test = control_data.parse_control(file)
367 tests[file] = found_test
368 else:
showardcd5131c2010-01-12 18:54:33 +0000369 tests[file] = compiler.parseFile(file).doc
370 return tests
371
372
373def recursive_walk(path, wildcard):
mbligh61be4cd2010-03-30 02:02:40 +0000374 """
375 Recursively go through a directory.
376
377 This function invoked by get_tests_from_fs().
378
379 @param path: base directory to start search.
380 @param wildcard: name format to match.
381
mblighed0526a2010-04-22 18:26:17 +0000382 @return A list of files that match wildcard
showardcd5131c2010-01-12 18:54:33 +0000383 """
384 files = []
385 directories = [ path ]
386 while len(directories)>0:
387 directory = directories.pop()
388 for name in os.listdir(directory):
389 fullpath = os.path.join(directory, name)
390 if os.path.isfile(fullpath):
391 # if we are a control file
392 if re.search(wildcard, name):
393 files.append(fullpath)
394 elif os.path.isdir(fullpath):
395 directories.append(fullpath)
396 return files
397
398
399def _log_or_execute(content, func, *args, **kwargs):
mbligh61be4cd2010-03-30 02:02:40 +0000400 """
401 Log a message if dry_run is enabled, or execute the given function.
showardcd5131c2010-01-12 18:54:33 +0000402
mbligh61be4cd2010-03-30 02:02:40 +0000403 Relies on the DRY_RUN global variable.
404
405 @param content: the actual log message.
406 @param func: function to execute if dry_run is not enabled.
407 @param subject: (Optional) The type of log being written. Defaults to
408 the name of the provided function.
showardcd5131c2010-01-12 18:54:33 +0000409 """
410 subject = kwargs.get('subject', func.__name__)
411
412 if DRY_RUN:
mbligh61be4cd2010-03-30 02:02:40 +0000413 logging.info("Would %s: %s", subject, content)
showardcd5131c2010-01-12 18:54:33 +0000414 else:
415 func(*args)
416
417
mbligh61be4cd2010-03-30 02:02:40 +0000418def _create_whitelist_set(whitelist_path):
419 """
420 Create a set with contents from a whitelist file for membership testing.
421
422 @param whitelist_path: full path to the whitelist file.
423
mblighed0526a2010-04-22 18:26:17 +0000424 @return set with files listed one/line - newlines included.
mbligh61be4cd2010-03-30 02:02:40 +0000425 """
426 f = open(whitelist_path, 'r')
427 whitelist_set = set([line.strip() for line in f])
428 f.close()
429 return whitelist_set
430
431
432def update_from_whitelist(whitelist_set, add_experimental, add_noncompliant,
433 autotest_dir):
434 """
435 Scans through all tests in the whitelist and add them to the database.
436
437 This function invoked when -w supplied.
438
439 @param whitelist_set: set of tests in full-path form from a whitelist.
440 @param add_experimental: add tests with experimental attribute set.
441 @param add_noncompliant: attempt adding test with invalid control files.
442 @param autotest_dir: prepended to path strings (/usr/local/autotest).
443 """
444 tests = {}
445 profilers = {}
446 for file_path in whitelist_set:
447 if file_path.find('client/profilers') == -1:
448 try:
449 found_test = control_data.parse_control(file_path,
450 raise_warnings=True)
451 tests[file_path] = found_test
452 except control_data.ControlVariableException, e:
jamesrenfed94092010-05-03 20:52:47 +0000453 logging.warn("Skipping %s\n%s", file, e)
mbligh61be4cd2010-03-30 02:02:40 +0000454 else:
455 profilers[file_path] = compiler.parseFile(file_path).doc
456
457 if len(tests) > 0:
458 update_tests_in_db(tests, add_experimental=add_experimental,
459 add_noncompliant=add_noncompliant,
460 autotest_dir=autotest_dir)
461 if len(profilers) > 0:
462 update_profilers_in_db(profilers, add_noncompliant=add_noncompliant,
463 description='NA')
464
465
showard909c7a62008-07-15 21:52:38 +0000466def main(argv):
467 """Main function"""
mbligh61be4cd2010-03-30 02:02:40 +0000468
showard909c7a62008-07-15 21:52:38 +0000469 global DRY_RUN
470 parser = optparse.OptionParser()
mbligh61be4cd2010-03-30 02:02:40 +0000471 parser.add_option('-c', '--db-clean-tests',
472 dest='clean_tests', action='store_true',
showard909c7a62008-07-15 21:52:38 +0000473 default=False,
mbligh61be4cd2010-03-30 02:02:40 +0000474 help='Clean client and server tests with invalid control files')
475 parser.add_option('-C', '--db-clear-all-tests',
476 dest='clear_all_tests', action='store_true',
477 default=False,
478 help='Clear ALL client and server tests')
showard909c7a62008-07-15 21:52:38 +0000479 parser.add_option('-d', '--dry-run',
480 dest='dry_run', action='store_true', default=False,
481 help='Dry run for operation')
mbligh322ec1a2008-09-26 16:48:10 +0000482 parser.add_option('-A', '--add-all',
483 dest='add_all', action='store_true',
484 default=False,
mbligh25d656c2009-08-24 22:04:02 +0000485 help='Add site_tests, tests, and test_suites')
486 parser.add_option('-S', '--add-samples',
487 dest='add_samples', action='store_true',
488 default=False,
489 help='Add samples.')
mbligh322ec1a2008-09-26 16:48:10 +0000490 parser.add_option('-E', '--add-experimental',
showard909c7a62008-07-15 21:52:38 +0000491 dest='add_experimental', action='store_true',
mbligh79410e12008-11-20 17:59:15 +0000492 default=True,
showard909c7a62008-07-15 21:52:38 +0000493 help='Add experimental tests to frontend')
494 parser.add_option('-N', '--add-noncompliant',
495 dest='add_noncompliant', action='store_true',
496 default=False,
mbligh4b5c31e2009-07-11 00:55:34 +0000497 help='Add non-compliant tests (i.e. tests that do not '
498 'define all required control variables)')
mbligh322ec1a2008-09-26 16:48:10 +0000499 parser.add_option('-p', '--profile-dir', dest='profile_dir',
500 help='Directory to recursively check for profiles')
showard909c7a62008-07-15 21:52:38 +0000501 parser.add_option('-t', '--tests-dir', dest='tests_dir',
502 help='Directory to recursively check for control.*')
showard909c7a62008-07-15 21:52:38 +0000503 parser.add_option('-r', '--control-pattern', dest='control_pattern',
504 default='^control.*',
505 help='The pattern to look for in directories for control files')
mbligh322ec1a2008-09-26 16:48:10 +0000506 parser.add_option('-v', '--verbose',
507 dest='verbose', action='store_true', default=False,
508 help='Run in verbose mode')
mbligh61be4cd2010-03-30 02:02:40 +0000509 parser.add_option('-w', '--whitelist-file', dest='whitelist_file',
510 help='Filename for list of test names that must match')
511 parser.add_option('-z', '--autotest-dir', dest='autotest_dir',
mbligh322ec1a2008-09-26 16:48:10 +0000512 default=os.path.join(os.path.dirname(__file__), '..'),
showard909c7a62008-07-15 21:52:38 +0000513 help='Autotest directory root')
514 options, args = parser.parse_args()
lmrc781f552010-05-20 00:36:37 +0000515
516 logging_manager.configure_logging(TestImporterLoggingConfig(),
517 verbose=options.verbose)
518
showard909c7a62008-07-15 21:52:38 +0000519 DRY_RUN = options.dry_run
jamesrenfed94092010-05-03 20:52:47 +0000520 if DRY_RUN:
521 logging.getLogger().setLevel(logging.WARN)
522
mbligh322ec1a2008-09-26 16:48:10 +0000523 # Make sure autotest_dir is the absolute path
524 options.autotest_dir = os.path.abspath(options.autotest_dir)
525
526 if len(args) > 0:
mbligh61be4cd2010-03-30 02:02:40 +0000527 logging.error("Invalid option(s) provided: %s", args)
showard909c7a62008-07-15 21:52:38 +0000528 parser.print_help()
529 return 1
530
mbligh61be4cd2010-03-30 02:02:40 +0000531 if options.verbose:
532 logging.getLogger().setLevel(logging.DEBUG)
533
534 if len(argv) == 1 or (len(argv) == 2 and options.verbose):
mbligh322ec1a2008-09-26 16:48:10 +0000535 update_all(options.autotest_dir, options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000536 options.add_experimental)
537 db_clean_broken(options.autotest_dir)
mbligh322ec1a2008-09-26 16:48:10 +0000538 return 0
539
mbligh61be4cd2010-03-30 02:02:40 +0000540 if options.clear_all_tests:
541 if (options.clean_tests or options.add_all or options.add_samples or
542 options.add_noncompliant):
543 logging.error(
544 "Can only pass --autotest-dir, --dry-run and --verbose with "
545 "--db-clear-all-tests")
546 return 1
547 db_clean_all(options.autotest_dir)
548
549 whitelist_set = None
550 if options.whitelist_file:
551 if options.add_all:
552 logging.error("Cannot pass both --add-all and --whitelist-file")
553 return 1
554 whitelist_path = os.path.abspath(options.whitelist_file)
555 if not os.path.isfile(whitelist_path):
556 logging.error("--whitelist-file (%s) not found", whitelist_path)
557 return 1
558 logging.info("Using whitelist file %s", whitelist_path)
559 whitelist_set = _create_whitelist_set(whitelist_path)
560 update_from_whitelist(whitelist_set,
561 add_experimental=options.add_experimental,
562 add_noncompliant=options.add_noncompliant,
563 autotest_dir=options.autotest_dir)
mbligh322ec1a2008-09-26 16:48:10 +0000564 if options.add_all:
565 update_all(options.autotest_dir, options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000566 options.add_experimental)
mbligh25d656c2009-08-24 22:04:02 +0000567 if options.add_samples:
568 update_samples(options.autotest_dir, options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000569 options.add_experimental)
showard909c7a62008-07-15 21:52:38 +0000570 if options.tests_dir:
mbligh4b5c31e2009-07-11 00:55:34 +0000571 options.tests_dir = os.path.abspath(options.tests_dir)
showard909c7a62008-07-15 21:52:38 +0000572 tests = get_tests_from_fs(options.tests_dir, options.control_pattern,
573 add_noncompliant=options.add_noncompliant)
mbligh322ec1a2008-09-26 16:48:10 +0000574 update_tests_in_db(tests, add_experimental=options.add_experimental,
showard909c7a62008-07-15 21:52:38 +0000575 add_noncompliant=options.add_noncompliant,
mbligh61be4cd2010-03-30 02:02:40 +0000576 autotest_dir=options.autotest_dir)
mbligh322ec1a2008-09-26 16:48:10 +0000577 if options.profile_dir:
578 profilers = get_tests_from_fs(options.profile_dir, '.*py$')
mbligh61be4cd2010-03-30 02:02:40 +0000579 update_profilers_in_db(profilers,
mbligh322ec1a2008-09-26 16:48:10 +0000580 add_noncompliant=options.add_noncompliant,
581 description='NA')
mbligh61be4cd2010-03-30 02:02:40 +0000582 if options.clean_tests:
583 db_clean_broken(options.autotest_dir)
showard909c7a62008-07-15 21:52:38 +0000584
585
showard909c7a62008-07-15 21:52:38 +0000586if __name__ == "__main__":
587 main(sys.argv)