17c8ea99fecf140093f2fe3dcd0ab6b5bf65b1bbembligh#!/usr/bin/python
2909c7a661e6739c110690e70e2f4421ffc4e5433showard#
3909c7a661e6739c110690e70e2f4421ffc4e5433showard# Copyright 2008 Google Inc. All Rights Reserved.
4909c7a661e6739c110690e70e2f4421ffc4e5433showard"""
5909c7a661e6739c110690e70e2f4421ffc4e5433showardThis utility allows for easy updating, removing and importing
6eab66ce582bfe05076ff096c3a044d8f0497bbcashowardof tests into the autotest_web afe_autotests table.
7909c7a661e6739c110690e70e2f4421ffc4e5433showard
8909c7a661e6739c110690e70e2f4421ffc4e5433showardExample of updating client side tests:
94b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh./test_importer.py -t /usr/local/autotest/client/tests
10909c7a661e6739c110690e70e2f4421ffc4e5433showard
114b5c31e1821307b880b57a41f6d8ea21948fc9e8mblighIf, for example, not all of your control files adhere to the standard outlined
124b5c31e1821307b880b57a41f6d8ea21948fc9e8mblighat http://autotest.kernel.org/wiki/ControlRequirements, you can force options:
13909c7a661e6739c110690e70e2f4421ffc4e5433showard
144b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh./test_importer.py --test-type server -t /usr/local/autotest/server/tests
15909c7a661e6739c110690e70e2f4421ffc4e5433showard
164b5c31e1821307b880b57a41f6d8ea21948fc9e8mblighYou would need to pass --add-noncompliant to include such control files,
174b5c31e1821307b880b57a41f6d8ea21948fc9e8mblighhowever.  An easy way to check for compliance is to run in dry mode:
18909c7a661e6739c110690e70e2f4421ffc4e5433showard
194b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh./test_importer.py --dry-run -t /usr/local/autotest/server/tests/mytest
204b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh
214b5c31e1821307b880b57a41f6d8ea21948fc9e8mblighOr to check a single control file, you can use check_control_file_vars.py.
224b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh
234b5c31e1821307b880b57a41f6d8ea21948fc9e8mblighRunning with no options is equivalent to --add-all --db-clear-tests.
244b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh
254b5c31e1821307b880b57a41f6d8ea21948fc9e8mblighMost options should be fairly self explanatory, use --help to display them.
26909c7a661e6739c110690e70e2f4421ffc4e5433showard"""
27909c7a661e6739c110690e70e2f4421ffc4e5433showard
28909c7a661e6739c110690e70e2f4421ffc4e5433showard
29909c7a661e6739c110690e70e2f4421ffc4e5433showardimport common
30cd5131c818ea432d21413694c4ff53c03c4f9379showardimport logging, re, os, sys, optparse, compiler
31e19d303a6718cbd9995e27485554212b63a0a701beeps
32cd5131c818ea432d21413694c4ff53c03c4f9379showardfrom autotest_lib.frontend import setup_django_environment
33cd5131c818ea432d21413694c4ff53c03c4f9379showardfrom autotest_lib.frontend.afe import models
3461be4cd430df250187506331b131c245d0f28b1amblighfrom autotest_lib.client.common_lib import control_data, utils
35c781f55093bf5e78645d13c08447bc8fb0f49c1clmrfrom autotest_lib.client.common_lib import logging_config, logging_manager
36c781f55093bf5e78645d13c08447bc8fb0f49c1clmr
37c781f55093bf5e78645d13c08447bc8fb0f49c1clmr
38c781f55093bf5e78645d13c08447bc8fb0f49c1clmrclass TestImporterLoggingConfig(logging_config.LoggingConfig):
396f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet    #pylint: disable-msg=C0111
40c781f55093bf5e78645d13c08447bc8fb0f49c1clmr    def configure_logging(self, results_dir=None, verbose=False):
41c781f55093bf5e78645d13c08447bc8fb0f49c1clmr        super(TestImporterLoggingConfig, self).configure_logging(
42c781f55093bf5e78645d13c08447bc8fb0f49c1clmr                                                               use_console=True,
43c781f55093bf5e78645d13c08447bc8fb0f49c1clmr                                                               verbose=verbose)
44909c7a661e6739c110690e70e2f4421ffc4e5433showard
45909c7a661e6739c110690e70e2f4421ffc4e5433showard
46909c7a661e6739c110690e70e2f4421ffc4e5433showard# Global
47909c7a661e6739c110690e70e2f4421ffc4e5433showardDRY_RUN = False
48989f25dcbb6361218f0f84d1c8404761b4c39d96showardDEPENDENCIES_NOT_FOUND = set()
49909c7a661e6739c110690e70e2f4421ffc4e5433showard
50322ec1afe58a974fba2e969d260d50f4de278a92mbligh
5161be4cd430df250187506331b131c245d0f28b1amblighdef update_all(autotest_dir, add_noncompliant, add_experimental):
5261be4cd430df250187506331b131c245d0f28b1ambligh    """
5361be4cd430df250187506331b131c245d0f28b1ambligh    Function to scan through all tests and add them to the database.
5461be4cd430df250187506331b131c245d0f28b1ambligh
5561be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked when no parameters supplied to the command line.
5661be4cd430df250187506331b131c245d0f28b1ambligh    It 'synchronizes' the test database with the current contents of the
5761be4cd430df250187506331b131c245d0f28b1ambligh    client and server test directories.  When test code is discovered
5861be4cd430df250187506331b131c245d0f28b1ambligh    in the file system new tests may be added to the db.  Likewise,
5961be4cd430df250187506331b131c245d0f28b1ambligh    if test code is not found in the filesystem, tests may be removed
6061be4cd430df250187506331b131c245d0f28b1ambligh    from the db.  The base test directories are hard-coded to client/tests,
6161be4cd430df250187506331b131c245d0f28b1ambligh    client/site_tests, server/tests and server/site_tests.
6261be4cd430df250187506331b131c245d0f28b1ambligh
6361be4cd430df250187506331b131c245d0f28b1ambligh    @param autotest_dir: prepended to path strings (/usr/local/autotest).
6461be4cd430df250187506331b131c245d0f28b1ambligh    @param add_noncompliant: attempt adding test with invalid control files.
6561be4cd430df250187506331b131c245d0f28b1ambligh    @param add_experimental: add tests with experimental attribute set.
6661be4cd430df250187506331b131c245d0f28b1ambligh    """
67322ec1afe58a974fba2e969d260d50f4de278a92mbligh    for path in [ 'server/tests', 'server/site_tests', 'client/tests',
68322ec1afe58a974fba2e969d260d50f4de278a92mbligh                  'client/site_tests']:
69322ec1afe58a974fba2e969d260d50f4de278a92mbligh        test_path = os.path.join(autotest_dir, path)
70322ec1afe58a974fba2e969d260d50f4de278a92mbligh        if not os.path.exists(test_path):
71322ec1afe58a974fba2e969d260d50f4de278a92mbligh            continue
7261be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Scanning %s", test_path)
73322ec1afe58a974fba2e969d260d50f4de278a92mbligh        tests = []
74322ec1afe58a974fba2e969d260d50f4de278a92mbligh        tests = get_tests_from_fs(test_path, "^control.*",
75322ec1afe58a974fba2e969d260d50f4de278a92mbligh                                 add_noncompliant=add_noncompliant)
76322ec1afe58a974fba2e969d260d50f4de278a92mbligh        update_tests_in_db(tests, add_experimental=add_experimental,
77322ec1afe58a974fba2e969d260d50f4de278a92mbligh                           add_noncompliant=add_noncompliant,
7861be4cd430df250187506331b131c245d0f28b1ambligh                           autotest_dir=autotest_dir)
79322ec1afe58a974fba2e969d260d50f4de278a92mbligh    test_suite_path = os.path.join(autotest_dir, 'test_suites')
80322ec1afe58a974fba2e969d260d50f4de278a92mbligh    if os.path.exists(test_suite_path):
8161be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Scanning %s", test_suite_path)
82322ec1afe58a974fba2e969d260d50f4de278a92mbligh        tests = get_tests_from_fs(test_suite_path, '.*',
83322ec1afe58a974fba2e969d260d50f4de278a92mbligh                                 add_noncompliant=add_noncompliant)
84322ec1afe58a974fba2e969d260d50f4de278a92mbligh        update_tests_in_db(tests, add_experimental=add_experimental,
85322ec1afe58a974fba2e969d260d50f4de278a92mbligh                           add_noncompliant=add_noncompliant,
8661be4cd430df250187506331b131c245d0f28b1ambligh                           autotest_dir=autotest_dir)
871ef218db76c473c28627377d8f50d6e6c6743289mbligh
88322ec1afe58a974fba2e969d260d50f4de278a92mbligh    profilers_path = os.path.join(autotest_dir, "client/profilers")
89322ec1afe58a974fba2e969d260d50f4de278a92mbligh    if os.path.exists(profilers_path):
9061be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Scanning %s", profilers_path)
91322ec1afe58a974fba2e969d260d50f4de278a92mbligh        profilers = get_tests_from_fs(profilers_path, '.*py$')
9261be4cd430df250187506331b131c245d0f28b1ambligh        update_profilers_in_db(profilers, add_noncompliant=add_noncompliant,
93322ec1afe58a974fba2e969d260d50f4de278a92mbligh                               description='NA')
94b6f95534ef8ef87144491d6ec14f4f3182b47bc8showard    # Clean bad db entries
9561be4cd430df250187506331b131c245d0f28b1ambligh    db_clean_broken(autotest_dir)
9661be4cd430df250187506331b131c245d0f28b1ambligh
9761be4cd430df250187506331b131c245d0f28b1ambligh
9861be4cd430df250187506331b131c245d0f28b1amblighdef update_samples(autotest_dir, add_noncompliant, add_experimental):
9961be4cd430df250187506331b131c245d0f28b1ambligh    """
10061be4cd430df250187506331b131c245d0f28b1ambligh    Add only sample tests to the database from the filesystem.
1011ef218db76c473c28627377d8f50d6e6c6743289mbligh
10261be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked when -S supplied on command line.
10361be4cd430df250187506331b131c245d0f28b1ambligh    Only adds tests to the database - does not delete any.
10461be4cd430df250187506331b131c245d0f28b1ambligh    Samples tests are formatted slightly differently than other tests.
105322ec1afe58a974fba2e969d260d50f4de278a92mbligh
10661be4cd430df250187506331b131c245d0f28b1ambligh    @param autotest_dir: prepended to path strings (/usr/local/autotest).
10761be4cd430df250187506331b131c245d0f28b1ambligh    @param add_noncompliant: attempt adding test with invalid control files.
10861be4cd430df250187506331b131c245d0f28b1ambligh    @param add_experimental: add tests with experimental attribute set.
10961be4cd430df250187506331b131c245d0f28b1ambligh    """
11025d656cafb0e9f731e405dfb79bb714c8421b4edmbligh    sample_path = os.path.join(autotest_dir, 'server/samples')
11125d656cafb0e9f731e405dfb79bb714c8421b4edmbligh    if os.path.exists(sample_path):
11261be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Scanning %s", sample_path)
11325d656cafb0e9f731e405dfb79bb714c8421b4edmbligh        tests = get_tests_from_fs(sample_path, '.*srv$',
11425d656cafb0e9f731e405dfb79bb714c8421b4edmbligh                                  add_noncompliant=add_noncompliant)
11525d656cafb0e9f731e405dfb79bb714c8421b4edmbligh        update_tests_in_db(tests, add_experimental=add_experimental,
11625d656cafb0e9f731e405dfb79bb714c8421b4edmbligh                           add_noncompliant=add_noncompliant,
11761be4cd430df250187506331b131c245d0f28b1ambligh                           autotest_dir=autotest_dir)
11861be4cd430df250187506331b131c245d0f28b1ambligh
11925d656cafb0e9f731e405dfb79bb714c8421b4edmbligh
12061be4cd430df250187506331b131c245d0f28b1amblighdef db_clean_broken(autotest_dir):
12161be4cd430df250187506331b131c245d0f28b1ambligh    """
12261be4cd430df250187506331b131c245d0f28b1ambligh    Remove tests from autotest_web that do not have valid control files
12325d656cafb0e9f731e405dfb79bb714c8421b4edmbligh
12461be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked when -c supplied on the command line and when
12561be4cd430df250187506331b131c245d0f28b1ambligh    running update_all().  Removes tests from database which are not
12661be4cd430df250187506331b131c245d0f28b1ambligh    found in the filesystem.  Also removes profilers which are just
12761be4cd430df250187506331b131c245d0f28b1ambligh    a special case of tests.
128909c7a661e6739c110690e70e2f4421ffc4e5433showard
12961be4cd430df250187506331b131c245d0f28b1ambligh    @param autotest_dir: prepended to path strings (/usr/local/autotest).
130909c7a661e6739c110690e70e2f4421ffc4e5433showard    """
131cd5131c818ea432d21413694c4ff53c03c4f9379showard    for test in models.Test.objects.all():
132cd5131c818ea432d21413694c4ff53c03c4f9379showard        full_path = os.path.join(autotest_dir, test.path)
133322ec1afe58a974fba2e969d260d50f4de278a92mbligh        if not os.path.isfile(full_path):
13461be4cd430df250187506331b131c245d0f28b1ambligh            logging.info("Removing %s", test.path)
135cd5131c818ea432d21413694c4ff53c03c4f9379showard            _log_or_execute(repr(test), test.delete)
136322ec1afe58a974fba2e969d260d50f4de278a92mbligh
137322ec1afe58a974fba2e969d260d50f4de278a92mbligh    # Find profilers that are no longer present
138cd5131c818ea432d21413694c4ff53c03c4f9379showard    for profiler in models.Profiler.objects.all():
139cd5131c818ea432d21413694c4ff53c03c4f9379showard        full_path = os.path.join(autotest_dir, "client", "profilers",
140cd5131c818ea432d21413694c4ff53c03c4f9379showard                                 profiler.name)
141322ec1afe58a974fba2e969d260d50f4de278a92mbligh        if not os.path.exists(full_path):
14261be4cd430df250187506331b131c245d0f28b1ambligh            logging.info("Removing %s", profiler.name)
143cd5131c818ea432d21413694c4ff53c03c4f9379showard            _log_or_execute(repr(profiler), profiler.delete)
144322ec1afe58a974fba2e969d260d50f4de278a92mbligh
145322ec1afe58a974fba2e969d260d50f4de278a92mbligh
14661be4cd430df250187506331b131c245d0f28b1amblighdef db_clean_all(autotest_dir):
14761be4cd430df250187506331b131c245d0f28b1ambligh    """
14861be4cd430df250187506331b131c245d0f28b1ambligh    Remove all tests from autotest_web - very destructive
14961be4cd430df250187506331b131c245d0f28b1ambligh
15061be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked when -C supplied on the command line.
15161be4cd430df250187506331b131c245d0f28b1ambligh    Removes ALL tests from the database.
15261be4cd430df250187506331b131c245d0f28b1ambligh
15361be4cd430df250187506331b131c245d0f28b1ambligh    @param autotest_dir: prepended to path strings (/usr/local/autotest).
15461be4cd430df250187506331b131c245d0f28b1ambligh    """
15561be4cd430df250187506331b131c245d0f28b1ambligh    for test in models.Test.objects.all():
15661be4cd430df250187506331b131c245d0f28b1ambligh        full_path = os.path.join(autotest_dir, test.path)
15761be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Removing %s", test.path)
15861be4cd430df250187506331b131c245d0f28b1ambligh        _log_or_execute(repr(test), test.delete)
15961be4cd430df250187506331b131c245d0f28b1ambligh
16061be4cd430df250187506331b131c245d0f28b1ambligh    # Find profilers that are no longer present
16161be4cd430df250187506331b131c245d0f28b1ambligh    for profiler in models.Profiler.objects.all():
16261be4cd430df250187506331b131c245d0f28b1ambligh        full_path = os.path.join(autotest_dir, "client", "profilers",
16361be4cd430df250187506331b131c245d0f28b1ambligh                                 profiler.name)
16461be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Removing %s", profiler.name)
16561be4cd430df250187506331b131c245d0f28b1ambligh        _log_or_execute(repr(profiler), profiler.delete)
16661be4cd430df250187506331b131c245d0f28b1ambligh
16761be4cd430df250187506331b131c245d0f28b1ambligh
16861be4cd430df250187506331b131c245d0f28b1amblighdef update_profilers_in_db(profilers, description='NA',
169322ec1afe58a974fba2e969d260d50f4de278a92mbligh                           add_noncompliant=False):
17061be4cd430df250187506331b131c245d0f28b1ambligh    """
17161be4cd430df250187506331b131c245d0f28b1ambligh    Add only profilers to the database from the filesystem.
17261be4cd430df250187506331b131c245d0f28b1ambligh
17361be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked when -p supplied on command line.
17461be4cd430df250187506331b131c245d0f28b1ambligh    Only adds profilers to the database - does not delete any.
17561be4cd430df250187506331b131c245d0f28b1ambligh    Profilers are formatted slightly differently than tests.
17661be4cd430df250187506331b131c245d0f28b1ambligh
17761be4cd430df250187506331b131c245d0f28b1ambligh    @param profilers: list of profilers found in the file system.
17861be4cd430df250187506331b131c245d0f28b1ambligh    @param description: simple text to satisfy docstring.
17961be4cd430df250187506331b131c245d0f28b1ambligh    @param add_noncompliant: attempt adding test with invalid control files.
18061be4cd430df250187506331b131c245d0f28b1ambligh    """
181322ec1afe58a974fba2e969d260d50f4de278a92mbligh    for profiler in profilers:
182ed0526aba7b4000c4558111bf42f8e3e7dbbe450mbligh        name = os.path.basename(profiler)
183ed0526aba7b4000c4558111bf42f8e3e7dbbe450mbligh        if name.endswith('.py'):
184ed0526aba7b4000c4558111bf42f8e3e7dbbe450mbligh            name = name[:-3]
185322ec1afe58a974fba2e969d260d50f4de278a92mbligh        if not profilers[profiler]:
186322ec1afe58a974fba2e969d260d50f4de278a92mbligh            if add_noncompliant:
187322ec1afe58a974fba2e969d260d50f4de278a92mbligh                doc = description
188322ec1afe58a974fba2e969d260d50f4de278a92mbligh            else:
18904be2bd5e4666a5c253e9c30ab20555e04286032Ilja H. Friedel                logging.warning("Skipping %s, missing docstring", profiler)
190781d269bb961a7ea7bfb61a9f8c1d87320ed8ad0mbligh                continue
191322ec1afe58a974fba2e969d260d50f4de278a92mbligh        else:
192322ec1afe58a974fba2e969d260d50f4de278a92mbligh            doc = profilers[profiler]
193909c7a661e6739c110690e70e2f4421ffc4e5433showard
194cd5131c818ea432d21413694c4ff53c03c4f9379showard        model = models.Profiler.objects.get_or_create(name=name)[0]
195cd5131c818ea432d21413694c4ff53c03c4f9379showard        model.description = doc
196cd5131c818ea432d21413694c4ff53c03c4f9379showard        _log_or_execute(repr(model), model.save)
197909c7a661e6739c110690e70e2f4421ffc4e5433showard
198909c7a661e6739c110690e70e2f4421ffc4e5433showard
199909c7a661e6739c110690e70e2f4421ffc4e5433showarddef update_tests_in_db(tests, dry_run=False, add_experimental=False,
20061be4cd430df250187506331b131c245d0f28b1ambligh                       add_noncompliant=False, autotest_dir=None):
20161be4cd430df250187506331b131c245d0f28b1ambligh    """
20261be4cd430df250187506331b131c245d0f28b1ambligh    Scans through all tests and add them to the database.
20361be4cd430df250187506331b131c245d0f28b1ambligh
20461be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked when -t supplied and for update_all.
20561be4cd430df250187506331b131c245d0f28b1ambligh    When test code is discovered in the file system new tests may be added
20661be4cd430df250187506331b131c245d0f28b1ambligh
20761be4cd430df250187506331b131c245d0f28b1ambligh    @param tests: list of tests found in the filesystem.
20861be4cd430df250187506331b131c245d0f28b1ambligh    @param dry_run: not used at this time.
20961be4cd430df250187506331b131c245d0f28b1ambligh    @param add_experimental: add tests with experimental attribute set.
21061be4cd430df250187506331b131c245d0f28b1ambligh    @param add_noncompliant: attempt adding test with invalid control files.
21161be4cd430df250187506331b131c245d0f28b1ambligh    @param autotest_dir: prepended to path strings (/usr/local/autotest).
21261be4cd430df250187506331b131c245d0f28b1ambligh    """
21361be4cd430df250187506331b131c245d0f28b1ambligh    site_set_attributes_module = utils.import_site_module(
21461be4cd430df250187506331b131c245d0f28b1ambligh        __file__, 'autotest_lib.utils.site_test_importer_attributes')
21561be4cd430df250187506331b131c245d0f28b1ambligh
216909c7a661e6739c110690e70e2f4421ffc4e5433showard    for test in tests:
217cd5131c818ea432d21413694c4ff53c03c4f9379showard        new_test = models.Test.objects.get_or_create(
218cd5131c818ea432d21413694c4ff53c03c4f9379showard                path=test.replace(autotest_dir, '').lstrip('/'))[0]
21961be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Processing %s", new_test.path)
220cd5131c818ea432d21413694c4ff53c03c4f9379showard
221cd5131c818ea432d21413694c4ff53c03c4f9379showard        # Set the test's attributes
222cd5131c818ea432d21413694c4ff53c03c4f9379showard        data = tests[test]
223cd5131c818ea432d21413694c4ff53c03c4f9379showard        _set_attributes_clean(new_test, data)
224cd5131c818ea432d21413694c4ff53c03c4f9379showard
22561be4cd430df250187506331b131c245d0f28b1ambligh        # Custom Attribute Update
22661be4cd430df250187506331b131c245d0f28b1ambligh        if site_set_attributes_module:
22761be4cd430df250187506331b131c245d0f28b1ambligh            site_set_attributes_module._set_attributes_custom(new_test, data)
22861be4cd430df250187506331b131c245d0f28b1ambligh
229909c7a661e6739c110690e70e2f4421ffc4e5433showard        # This only takes place if --add-noncompliant is provided on the CLI
230cd5131c818ea432d21413694c4ff53c03c4f9379showard        if not new_test.name:
231909c7a661e6739c110690e70e2f4421ffc4e5433showard            test_new_test = test.split('/')
232909c7a661e6739c110690e70e2f4421ffc4e5433showard            if test_new_test[-1] == 'control':
233cd5131c818ea432d21413694c4ff53c03c4f9379showard                new_test.name = test_new_test[-2]
234909c7a661e6739c110690e70e2f4421ffc4e5433showard            else:
235909c7a661e6739c110690e70e2f4421ffc4e5433showard                control_name = "%s:%s"
236909c7a661e6739c110690e70e2f4421ffc4e5433showard                control_name %= (test_new_test[-2],
2371ef218db76c473c28627377d8f50d6e6c6743289mbligh                                 test_new_test[-1])
238cd5131c818ea432d21413694c4ff53c03c4f9379showard                new_test.name = control_name.replace('control.', '')
239909c7a661e6739c110690e70e2f4421ffc4e5433showard
240cd5131c818ea432d21413694c4ff53c03c4f9379showard        # Experimental Check
241cd5131c818ea432d21413694c4ff53c03c4f9379showard        if not add_experimental and new_test.experimental:
242cd5131c818ea432d21413694c4ff53c03c4f9379showard            continue
243989f25dcbb6361218f0f84d1c8404761b4c39d96showard
244cd5131c818ea432d21413694c4ff53c03c4f9379showard        _log_or_execute(repr(new_test), new_test.save)
245cd5131c818ea432d21413694c4ff53c03c4f9379showard        add_label_dependencies(new_test)
246909c7a661e6739c110690e70e2f4421ffc4e5433showard
247861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li        # save TestParameter
248861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li        for para_name in data.test_parameters:
249861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li            test_parameter = models.TestParameter.objects.get_or_create(
250861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li                test=new_test, name=para_name)[0]
251861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li            test_parameter.save()
252861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li
253909c7a661e6739c110690e70e2f4421ffc4e5433showard
254cd5131c818ea432d21413694c4ff53c03c4f9379showarddef _set_attributes_clean(test, data):
25561be4cd430df250187506331b131c245d0f28b1ambligh    """
25661be4cd430df250187506331b131c245d0f28b1ambligh    First pass sets the attributes of the Test object from file system.
257909c7a661e6739c110690e70e2f4421ffc4e5433showard
25861be4cd430df250187506331b131c245d0f28b1ambligh    @param test: a test object to be populated for the database.
25961be4cd430df250187506331b131c245d0f28b1ambligh    @param data: object with test data from the file system.
26061be4cd430df250187506331b131c245d0f28b1ambligh    """
261909c7a661e6739c110690e70e2f4421ffc4e5433showard    test_time = { 'short' : 1,
262909c7a661e6739c110690e70e2f4421ffc4e5433showard                  'medium' : 2,
263909c7a661e6739c110690e70e2f4421ffc4e5433showard                  'long' : 3, }
264909c7a661e6739c110690e70e2f4421ffc4e5433showard
265cd5131c818ea432d21413694c4ff53c03c4f9379showard
266cd5131c818ea432d21413694c4ff53c03c4f9379showard    string_attributes = ('name', 'author', 'test_class', 'test_category',
267cd5131c818ea432d21413694c4ff53c03c4f9379showard                         'test_category', 'sync_count')
268cd5131c818ea432d21413694c4ff53c03c4f9379showard    for attribute in string_attributes:
269cd5131c818ea432d21413694c4ff53c03c4f9379showard        setattr(test, attribute, getattr(data, attribute))
270cd5131c818ea432d21413694c4ff53c03c4f9379showard
271cd5131c818ea432d21413694c4ff53c03c4f9379showard    test.description = data.doc
272cd5131c818ea432d21413694c4ff53c03c4f9379showard    test.dependencies = ", ".join(data.dependencies)
273cd5131c818ea432d21413694c4ff53c03c4f9379showard
2743dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet    try:
2753dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet        test.test_type = control_data.CONTROL_TYPE.get_value(data.test_type)
2763dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet    except AttributeError:
2773dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet        raise Exception('Unknown test_type %s for test %s', data.test_type,
2783dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet                        data.name)
2793dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet
280cd5131c818ea432d21413694c4ff53c03c4f9379showard    int_attributes = ('experimental', 'run_verify')
281cd5131c818ea432d21413694c4ff53c03c4f9379showard    for attribute in int_attributes:
282cd5131c818ea432d21413694c4ff53c03c4f9379showard        setattr(test, attribute, int(getattr(data, attribute)))
283cd5131c818ea432d21413694c4ff53c03c4f9379showard
284909c7a661e6739c110690e70e2f4421ffc4e5433showard    try:
285cd5131c818ea432d21413694c4ff53c03c4f9379showard        test.test_time = int(data.time)
286cd5131c818ea432d21413694c4ff53c03c4f9379showard        if test.test_time < 1 or test.time > 3:
287cd5131c818ea432d21413694c4ff53c03c4f9379showard            raise Exception('Incorrect number %d for time' % test.time)
288909c7a661e6739c110690e70e2f4421ffc4e5433showard    except ValueError:
289909c7a661e6739c110690e70e2f4421ffc4e5433showard        pass
290909c7a661e6739c110690e70e2f4421ffc4e5433showard
291cd5131c818ea432d21413694c4ff53c03c4f9379showard    if not test.test_time and str == type(data.time):
292cd5131c818ea432d21413694c4ff53c03c4f9379showard        test.test_time = test_time[data.time.lower()]
293909c7a661e6739c110690e70e2f4421ffc4e5433showard
2946f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet    test.test_retry = data.retries
2956f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet
296909c7a661e6739c110690e70e2f4421ffc4e5433showard
297cd5131c818ea432d21413694c4ff53c03c4f9379showarddef add_label_dependencies(test):
298989f25dcbb6361218f0f84d1c8404761b4c39d96showard    """
29961be4cd430df250187506331b131c245d0f28b1ambligh    Add proper many-to-many relationships from DEPENDENCIES field.
30061be4cd430df250187506331b131c245d0f28b1ambligh
30161be4cd430df250187506331b131c245d0f28b1ambligh    @param test: test object for the database.
302989f25dcbb6361218f0f84d1c8404761b4c39d96showard    """
30361be4cd430df250187506331b131c245d0f28b1ambligh
304989f25dcbb6361218f0f84d1c8404761b4c39d96showard    # clear out old relationships
305cd5131c818ea432d21413694c4ff53c03c4f9379showard    _log_or_execute(repr(test), test.dependency_labels.clear,
306cd5131c818ea432d21413694c4ff53c03c4f9379showard                    subject='clear dependencies from')
307989f25dcbb6361218f0f84d1c8404761b4c39d96showard
308cd5131c818ea432d21413694c4ff53c03c4f9379showard    for label_name in test.dependencies.split(','):
309cd5131c818ea432d21413694c4ff53c03c4f9379showard        label_name = label_name.strip().lower()
310cd5131c818ea432d21413694c4ff53c03c4f9379showard        if not label_name:
311cd5131c818ea432d21413694c4ff53c03c4f9379showard            continue
312989f25dcbb6361218f0f84d1c8404761b4c39d96showard
313cd5131c818ea432d21413694c4ff53c03c4f9379showard        try:
314cd5131c818ea432d21413694c4ff53c03c4f9379showard            label = models.Label.objects.get(name=label_name)
315cd5131c818ea432d21413694c4ff53c03c4f9379showard        except models.Label.DoesNotExist:
316cd5131c818ea432d21413694c4ff53c03c4f9379showard            log_dependency_not_found(label_name)
317cd5131c818ea432d21413694c4ff53c03c4f9379showard            continue
318989f25dcbb6361218f0f84d1c8404761b4c39d96showard
319cd5131c818ea432d21413694c4ff53c03c4f9379showard        _log_or_execute(repr(label), test.dependency_labels.add, label,
320cd5131c818ea432d21413694c4ff53c03c4f9379showard                        subject='add dependency to %s' % test.name)
321989f25dcbb6361218f0f84d1c8404761b4c39d96showard
322989f25dcbb6361218f0f84d1c8404761b4c39d96showard
323989f25dcbb6361218f0f84d1c8404761b4c39d96showarddef log_dependency_not_found(label_name):
32461be4cd430df250187506331b131c245d0f28b1ambligh    """
32561be4cd430df250187506331b131c245d0f28b1ambligh    Exception processing when label not found in database.
32661be4cd430df250187506331b131c245d0f28b1ambligh
32761be4cd430df250187506331b131c245d0f28b1ambligh    @param label_name: from test dependencies.
32861be4cd430df250187506331b131c245d0f28b1ambligh    """
329989f25dcbb6361218f0f84d1c8404761b4c39d96showard    if label_name in DEPENDENCIES_NOT_FOUND:
330989f25dcbb6361218f0f84d1c8404761b4c39d96showard        return
33161be4cd430df250187506331b131c245d0f28b1ambligh    logging.info("Dependency %s not found", label_name)
332989f25dcbb6361218f0f84d1c8404761b4c39d96showard    DEPENDENCIES_NOT_FOUND.add(label_name)
333989f25dcbb6361218f0f84d1c8404761b4c39d96showard
334989f25dcbb6361218f0f84d1c8404761b4c39d96showard
335909c7a661e6739c110690e70e2f4421ffc4e5433showarddef get_tests_from_fs(parent_dir, control_pattern, add_noncompliant=False):
33661be4cd430df250187506331b131c245d0f28b1ambligh    """
33761be4cd430df250187506331b131c245d0f28b1ambligh    Find control files in file system and load a list with their info.
338909c7a661e6739c110690e70e2f4421ffc4e5433showard
33961be4cd430df250187506331b131c245d0f28b1ambligh    @param parent_dir: directory to search recursively.
34061be4cd430df250187506331b131c245d0f28b1ambligh    @param control_pattern: name format of control file.
34161be4cd430df250187506331b131c245d0f28b1ambligh    @param add_noncompliant: ignore control file parse errors.
34261be4cd430df250187506331b131c245d0f28b1ambligh
343ed0526aba7b4000c4558111bf42f8e3e7dbbe450mbligh    @return dictionary of the form: tests[file_path] = parsed_object
344909c7a661e6739c110690e70e2f4421ffc4e5433showard    """
345909c7a661e6739c110690e70e2f4421ffc4e5433showard    tests = {}
346322ec1afe58a974fba2e969d260d50f4de278a92mbligh    profilers = False
347322ec1afe58a974fba2e969d260d50f4de278a92mbligh    if 'client/profilers' in parent_dir:
348322ec1afe58a974fba2e969d260d50f4de278a92mbligh        profilers = True
349909c7a661e6739c110690e70e2f4421ffc4e5433showard    for dir in [ parent_dir ]:
350909c7a661e6739c110690e70e2f4421ffc4e5433showard        files = recursive_walk(dir, control_pattern)
351909c7a661e6739c110690e70e2f4421ffc4e5433showard        for file in files:
352e8474bbe43eecca1ca18e1e4de4e71883fd4fdf1mbligh            if '__init__.py' in file or '.svn' in file:
353322ec1afe58a974fba2e969d260d50f4de278a92mbligh                continue
354322ec1afe58a974fba2e969d260d50f4de278a92mbligh            if not profilers:
355322ec1afe58a974fba2e969d260d50f4de278a92mbligh                if not add_noncompliant:
356322ec1afe58a974fba2e969d260d50f4de278a92mbligh                    try:
357322ec1afe58a974fba2e969d260d50f4de278a92mbligh                        found_test = control_data.parse_control(file,
358909c7a661e6739c110690e70e2f4421ffc4e5433showard                                                            raise_warnings=True)
359322ec1afe58a974fba2e969d260d50f4de278a92mbligh                        tests[file] = found_test
360322ec1afe58a974fba2e969d260d50f4de278a92mbligh                    except control_data.ControlVariableException, e:
36104be2bd5e4666a5c253e9c30ab20555e04286032Ilja H. Friedel                        logging.warning("Skipping %s\n%s", file, e)
3626e3e9bd25bcbe16ecc6e14fee07adab01536e4fcjamesren                    except Exception, e:
3636e3e9bd25bcbe16ecc6e14fee07adab01536e4fcjamesren                        logging.error("Bad %s\n%s", file, e)
364322ec1afe58a974fba2e969d260d50f4de278a92mbligh                else:
365322ec1afe58a974fba2e969d260d50f4de278a92mbligh                    found_test = control_data.parse_control(file)
366909c7a661e6739c110690e70e2f4421ffc4e5433showard                    tests[file] = found_test
367909c7a661e6739c110690e70e2f4421ffc4e5433showard            else:
368322ec1afe58a974fba2e969d260d50f4de278a92mbligh                tests[file] = compiler.parseFile(file).doc
369909c7a661e6739c110690e70e2f4421ffc4e5433showard    return tests
370909c7a661e6739c110690e70e2f4421ffc4e5433showard
371909c7a661e6739c110690e70e2f4421ffc4e5433showard
372909c7a661e6739c110690e70e2f4421ffc4e5433showarddef recursive_walk(path, wildcard):
37361be4cd430df250187506331b131c245d0f28b1ambligh    """
37461be4cd430df250187506331b131c245d0f28b1ambligh    Recursively go through a directory.
37561be4cd430df250187506331b131c245d0f28b1ambligh
37661be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked by get_tests_from_fs().
37761be4cd430df250187506331b131c245d0f28b1ambligh
37861be4cd430df250187506331b131c245d0f28b1ambligh    @param path: base directory to start search.
37961be4cd430df250187506331b131c245d0f28b1ambligh    @param wildcard: name format to match.
38061be4cd430df250187506331b131c245d0f28b1ambligh
381ed0526aba7b4000c4558111bf42f8e3e7dbbe450mbligh    @return A list of files that match wildcard
382909c7a661e6739c110690e70e2f4421ffc4e5433showard    """
383909c7a661e6739c110690e70e2f4421ffc4e5433showard    files = []
384909c7a661e6739c110690e70e2f4421ffc4e5433showard    directories = [ path ]
385909c7a661e6739c110690e70e2f4421ffc4e5433showard    while len(directories)>0:
386909c7a661e6739c110690e70e2f4421ffc4e5433showard        directory = directories.pop()
387909c7a661e6739c110690e70e2f4421ffc4e5433showard        for name in os.listdir(directory):
388909c7a661e6739c110690e70e2f4421ffc4e5433showard            fullpath = os.path.join(directory, name)
389909c7a661e6739c110690e70e2f4421ffc4e5433showard            if os.path.isfile(fullpath):
390909c7a661e6739c110690e70e2f4421ffc4e5433showard                # if we are a control file
391909c7a661e6739c110690e70e2f4421ffc4e5433showard                if re.search(wildcard, name):
392909c7a661e6739c110690e70e2f4421ffc4e5433showard                    files.append(fullpath)
393909c7a661e6739c110690e70e2f4421ffc4e5433showard            elif os.path.isdir(fullpath):
394909c7a661e6739c110690e70e2f4421ffc4e5433showard                directories.append(fullpath)
395909c7a661e6739c110690e70e2f4421ffc4e5433showard    return files
396909c7a661e6739c110690e70e2f4421ffc4e5433showard
397909c7a661e6739c110690e70e2f4421ffc4e5433showard
398cd5131c818ea432d21413694c4ff53c03c4f9379showarddef _log_or_execute(content, func, *args, **kwargs):
39961be4cd430df250187506331b131c245d0f28b1ambligh    """
40061be4cd430df250187506331b131c245d0f28b1ambligh    Log a message if dry_run is enabled, or execute the given function.
401909c7a661e6739c110690e70e2f4421ffc4e5433showard
40261be4cd430df250187506331b131c245d0f28b1ambligh    Relies on the DRY_RUN global variable.
40361be4cd430df250187506331b131c245d0f28b1ambligh
40461be4cd430df250187506331b131c245d0f28b1ambligh    @param content: the actual log message.
40561be4cd430df250187506331b131c245d0f28b1ambligh    @param func: function to execute if dry_run is not enabled.
40661be4cd430df250187506331b131c245d0f28b1ambligh    @param subject: (Optional) The type of log being written. Defaults to
40761be4cd430df250187506331b131c245d0f28b1ambligh                     the name of the provided function.
408cd5131c818ea432d21413694c4ff53c03c4f9379showard    """
409cd5131c818ea432d21413694c4ff53c03c4f9379showard    subject = kwargs.get('subject', func.__name__)
410909c7a661e6739c110690e70e2f4421ffc4e5433showard
411909c7a661e6739c110690e70e2f4421ffc4e5433showard    if DRY_RUN:
41261be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Would %s: %s",  subject, content)
413909c7a661e6739c110690e70e2f4421ffc4e5433showard    else:
414cd5131c818ea432d21413694c4ff53c03c4f9379showard        func(*args)
415cd5131c818ea432d21413694c4ff53c03c4f9379showard
416cd5131c818ea432d21413694c4ff53c03c4f9379showard
41761be4cd430df250187506331b131c245d0f28b1amblighdef _create_whitelist_set(whitelist_path):
41861be4cd430df250187506331b131c245d0f28b1ambligh    """
41961be4cd430df250187506331b131c245d0f28b1ambligh    Create a set with contents from a whitelist file for membership testing.
42061be4cd430df250187506331b131c245d0f28b1ambligh
42161be4cd430df250187506331b131c245d0f28b1ambligh    @param whitelist_path: full path to the whitelist file.
42261be4cd430df250187506331b131c245d0f28b1ambligh
423ed0526aba7b4000c4558111bf42f8e3e7dbbe450mbligh    @return set with files listed one/line - newlines included.
42461be4cd430df250187506331b131c245d0f28b1ambligh    """
42561be4cd430df250187506331b131c245d0f28b1ambligh    f = open(whitelist_path, 'r')
42661be4cd430df250187506331b131c245d0f28b1ambligh    whitelist_set = set([line.strip() for line in f])
42761be4cd430df250187506331b131c245d0f28b1ambligh    f.close()
42861be4cd430df250187506331b131c245d0f28b1ambligh    return whitelist_set
42961be4cd430df250187506331b131c245d0f28b1ambligh
43061be4cd430df250187506331b131c245d0f28b1ambligh
43161be4cd430df250187506331b131c245d0f28b1amblighdef update_from_whitelist(whitelist_set, add_experimental, add_noncompliant,
43261be4cd430df250187506331b131c245d0f28b1ambligh                          autotest_dir):
43361be4cd430df250187506331b131c245d0f28b1ambligh    """
43461be4cd430df250187506331b131c245d0f28b1ambligh    Scans through all tests in the whitelist and add them to the database.
43561be4cd430df250187506331b131c245d0f28b1ambligh
43661be4cd430df250187506331b131c245d0f28b1ambligh    This function invoked when -w supplied.
43761be4cd430df250187506331b131c245d0f28b1ambligh
43861be4cd430df250187506331b131c245d0f28b1ambligh    @param whitelist_set: set of tests in full-path form from a whitelist.
43961be4cd430df250187506331b131c245d0f28b1ambligh    @param add_experimental: add tests with experimental attribute set.
44061be4cd430df250187506331b131c245d0f28b1ambligh    @param add_noncompliant: attempt adding test with invalid control files.
44161be4cd430df250187506331b131c245d0f28b1ambligh    @param autotest_dir: prepended to path strings (/usr/local/autotest).
44261be4cd430df250187506331b131c245d0f28b1ambligh    """
44361be4cd430df250187506331b131c245d0f28b1ambligh    tests = {}
44461be4cd430df250187506331b131c245d0f28b1ambligh    profilers = {}
44561be4cd430df250187506331b131c245d0f28b1ambligh    for file_path in whitelist_set:
44661be4cd430df250187506331b131c245d0f28b1ambligh        if file_path.find('client/profilers') == -1:
44761be4cd430df250187506331b131c245d0f28b1ambligh            try:
44861be4cd430df250187506331b131c245d0f28b1ambligh                found_test = control_data.parse_control(file_path,
44961be4cd430df250187506331b131c245d0f28b1ambligh                                                        raise_warnings=True)
45061be4cd430df250187506331b131c245d0f28b1ambligh                tests[file_path] = found_test
45161be4cd430df250187506331b131c245d0f28b1ambligh            except control_data.ControlVariableException, e:
45204be2bd5e4666a5c253e9c30ab20555e04286032Ilja H. Friedel                logging.warning("Skipping %s\n%s", file, e)
45361be4cd430df250187506331b131c245d0f28b1ambligh        else:
45461be4cd430df250187506331b131c245d0f28b1ambligh            profilers[file_path] = compiler.parseFile(file_path).doc
45561be4cd430df250187506331b131c245d0f28b1ambligh
45661be4cd430df250187506331b131c245d0f28b1ambligh    if len(tests) > 0:
45761be4cd430df250187506331b131c245d0f28b1ambligh        update_tests_in_db(tests, add_experimental=add_experimental,
45861be4cd430df250187506331b131c245d0f28b1ambligh                           add_noncompliant=add_noncompliant,
45961be4cd430df250187506331b131c245d0f28b1ambligh                           autotest_dir=autotest_dir)
46061be4cd430df250187506331b131c245d0f28b1ambligh    if len(profilers) > 0:
46161be4cd430df250187506331b131c245d0f28b1ambligh        update_profilers_in_db(profilers, add_noncompliant=add_noncompliant,
46261be4cd430df250187506331b131c245d0f28b1ambligh                               description='NA')
46361be4cd430df250187506331b131c245d0f28b1ambligh
46461be4cd430df250187506331b131c245d0f28b1ambligh
465cd5131c818ea432d21413694c4ff53c03c4f9379showarddef main(argv):
4666f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet    """Main function
4676f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet    @param argv: List of command line parameters.
4686f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet    """
46961be4cd430df250187506331b131c245d0f28b1ambligh
470cd5131c818ea432d21413694c4ff53c03c4f9379showard    global DRY_RUN
471cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser = optparse.OptionParser()
47261be4cd430df250187506331b131c245d0f28b1ambligh    parser.add_option('-c', '--db-clean-tests',
47361be4cd430df250187506331b131c245d0f28b1ambligh                      dest='clean_tests', action='store_true',
474cd5131c818ea432d21413694c4ff53c03c4f9379showard                      default=False,
47561be4cd430df250187506331b131c245d0f28b1ambligh                help='Clean client and server tests with invalid control files')
47661be4cd430df250187506331b131c245d0f28b1ambligh    parser.add_option('-C', '--db-clear-all-tests',
47761be4cd430df250187506331b131c245d0f28b1ambligh                      dest='clear_all_tests', action='store_true',
47861be4cd430df250187506331b131c245d0f28b1ambligh                      default=False,
47961be4cd430df250187506331b131c245d0f28b1ambligh                help='Clear ALL client and server tests')
480cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-d', '--dry-run',
481cd5131c818ea432d21413694c4ff53c03c4f9379showard                      dest='dry_run', action='store_true', default=False,
482cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Dry run for operation')
483cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-A', '--add-all',
484cd5131c818ea432d21413694c4ff53c03c4f9379showard                      dest='add_all', action='store_true',
485cd5131c818ea432d21413694c4ff53c03c4f9379showard                      default=False,
486cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Add site_tests, tests, and test_suites')
487cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-S', '--add-samples',
488cd5131c818ea432d21413694c4ff53c03c4f9379showard                      dest='add_samples', action='store_true',
489cd5131c818ea432d21413694c4ff53c03c4f9379showard                      default=False,
490cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Add samples.')
491cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-E', '--add-experimental',
492cd5131c818ea432d21413694c4ff53c03c4f9379showard                      dest='add_experimental', action='store_true',
493cd5131c818ea432d21413694c4ff53c03c4f9379showard                      default=True,
4945cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi                      help='Add experimental tests to frontend, works only '
4955cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi                           'with -A (--add-all) option')
496cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-N', '--add-noncompliant',
497cd5131c818ea432d21413694c4ff53c03c4f9379showard                      dest='add_noncompliant', action='store_true',
498cd5131c818ea432d21413694c4ff53c03c4f9379showard                      default=False,
499cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Add non-compliant tests (i.e. tests that do not '
5005cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi                           'define all required control variables), works '
5015cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi                           'only with -A (--add-all) option')
502cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-p', '--profile-dir', dest='profile_dir',
503cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Directory to recursively check for profiles')
504cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-t', '--tests-dir', dest='tests_dir',
505cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Directory to recursively check for control.*')
506cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-r', '--control-pattern', dest='control_pattern',
507cd5131c818ea432d21413694c4ff53c03c4f9379showard                      default='^control.*',
508cd5131c818ea432d21413694c4ff53c03c4f9379showard               help='The pattern to look for in directories for control files')
509cd5131c818ea432d21413694c4ff53c03c4f9379showard    parser.add_option('-v', '--verbose',
510cd5131c818ea432d21413694c4ff53c03c4f9379showard                      dest='verbose', action='store_true', default=False,
511cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Run in verbose mode')
51261be4cd430df250187506331b131c245d0f28b1ambligh    parser.add_option('-w', '--whitelist-file', dest='whitelist_file',
51361be4cd430df250187506331b131c245d0f28b1ambligh                      help='Filename for list of test names that must match')
51461be4cd430df250187506331b131c245d0f28b1ambligh    parser.add_option('-z', '--autotest-dir', dest='autotest_dir',
515cd5131c818ea432d21413694c4ff53c03c4f9379showard                      default=os.path.join(os.path.dirname(__file__), '..'),
516cd5131c818ea432d21413694c4ff53c03c4f9379showard                      help='Autotest directory root')
517cd5131c818ea432d21413694c4ff53c03c4f9379showard    options, args = parser.parse_args()
518c781f55093bf5e78645d13c08447bc8fb0f49c1clmr
519c781f55093bf5e78645d13c08447bc8fb0f49c1clmr    logging_manager.configure_logging(TestImporterLoggingConfig(),
520c781f55093bf5e78645d13c08447bc8fb0f49c1clmr                                      verbose=options.verbose)
521c781f55093bf5e78645d13c08447bc8fb0f49c1clmr
522cd5131c818ea432d21413694c4ff53c03c4f9379showard    DRY_RUN = options.dry_run
523fed94091229cb4ea3bc2371c13e6f6846fce1a47jamesren    if DRY_RUN:
524fed94091229cb4ea3bc2371c13e6f6846fce1a47jamesren        logging.getLogger().setLevel(logging.WARN)
525fed94091229cb4ea3bc2371c13e6f6846fce1a47jamesren
5265cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi    if len(argv) > 1 and options.add_noncompliant and not options.add_all:
5275cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi        logging.error('-N (--add-noncompliant) must be ran with option -A '
5285cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi                      '(--add-All).')
5295cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi        return 1
5305cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi
5315cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi    if len(argv) > 1 and options.add_experimental and not options.add_all:
5325cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi        logging.error('-E (--add-experimental) must be ran with option -A '
5335cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi                      '(--add-All).')
5345cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi        return 1
5355cbd0f81babd0737799dfb447b53f04c35b9004dDan Shi
536cd5131c818ea432d21413694c4ff53c03c4f9379showard    # Make sure autotest_dir is the absolute path
537cd5131c818ea432d21413694c4ff53c03c4f9379showard    options.autotest_dir = os.path.abspath(options.autotest_dir)
538cd5131c818ea432d21413694c4ff53c03c4f9379showard
539cd5131c818ea432d21413694c4ff53c03c4f9379showard    if len(args) > 0:
54061be4cd430df250187506331b131c245d0f28b1ambligh        logging.error("Invalid option(s) provided: %s", args)
541cd5131c818ea432d21413694c4ff53c03c4f9379showard        parser.print_help()
542cd5131c818ea432d21413694c4ff53c03c4f9379showard        return 1
543cd5131c818ea432d21413694c4ff53c03c4f9379showard
54461be4cd430df250187506331b131c245d0f28b1ambligh    if options.verbose:
54561be4cd430df250187506331b131c245d0f28b1ambligh        logging.getLogger().setLevel(logging.DEBUG)
54661be4cd430df250187506331b131c245d0f28b1ambligh
54761be4cd430df250187506331b131c245d0f28b1ambligh    if len(argv) == 1 or (len(argv) == 2 and options.verbose):
548cd5131c818ea432d21413694c4ff53c03c4f9379showard        update_all(options.autotest_dir, options.add_noncompliant,
54961be4cd430df250187506331b131c245d0f28b1ambligh                   options.add_experimental)
55061be4cd430df250187506331b131c245d0f28b1ambligh        db_clean_broken(options.autotest_dir)
551cd5131c818ea432d21413694c4ff53c03c4f9379showard        return 0
552cd5131c818ea432d21413694c4ff53c03c4f9379showard
55361be4cd430df250187506331b131c245d0f28b1ambligh    if options.clear_all_tests:
55461be4cd430df250187506331b131c245d0f28b1ambligh        if (options.clean_tests or options.add_all or options.add_samples or
55561be4cd430df250187506331b131c245d0f28b1ambligh            options.add_noncompliant):
55661be4cd430df250187506331b131c245d0f28b1ambligh            logging.error(
55761be4cd430df250187506331b131c245d0f28b1ambligh                "Can only pass --autotest-dir, --dry-run and --verbose with "
55861be4cd430df250187506331b131c245d0f28b1ambligh                "--db-clear-all-tests")
55961be4cd430df250187506331b131c245d0f28b1ambligh            return 1
56061be4cd430df250187506331b131c245d0f28b1ambligh        db_clean_all(options.autotest_dir)
56161be4cd430df250187506331b131c245d0f28b1ambligh
56261be4cd430df250187506331b131c245d0f28b1ambligh    whitelist_set = None
56361be4cd430df250187506331b131c245d0f28b1ambligh    if options.whitelist_file:
56461be4cd430df250187506331b131c245d0f28b1ambligh        if options.add_all:
56561be4cd430df250187506331b131c245d0f28b1ambligh            logging.error("Cannot pass both --add-all and --whitelist-file")
56661be4cd430df250187506331b131c245d0f28b1ambligh            return 1
56761be4cd430df250187506331b131c245d0f28b1ambligh        whitelist_path = os.path.abspath(options.whitelist_file)
56861be4cd430df250187506331b131c245d0f28b1ambligh        if not os.path.isfile(whitelist_path):
56961be4cd430df250187506331b131c245d0f28b1ambligh            logging.error("--whitelist-file (%s) not found", whitelist_path)
57061be4cd430df250187506331b131c245d0f28b1ambligh            return 1
57161be4cd430df250187506331b131c245d0f28b1ambligh        logging.info("Using whitelist file %s", whitelist_path)
57261be4cd430df250187506331b131c245d0f28b1ambligh        whitelist_set =  _create_whitelist_set(whitelist_path)
57361be4cd430df250187506331b131c245d0f28b1ambligh        update_from_whitelist(whitelist_set,
57461be4cd430df250187506331b131c245d0f28b1ambligh                              add_experimental=options.add_experimental,
57561be4cd430df250187506331b131c245d0f28b1ambligh                              add_noncompliant=options.add_noncompliant,
57661be4cd430df250187506331b131c245d0f28b1ambligh                              autotest_dir=options.autotest_dir)
577cd5131c818ea432d21413694c4ff53c03c4f9379showard    if options.add_all:
578cd5131c818ea432d21413694c4ff53c03c4f9379showard        update_all(options.autotest_dir, options.add_noncompliant,
57961be4cd430df250187506331b131c245d0f28b1ambligh                   options.add_experimental)
580cd5131c818ea432d21413694c4ff53c03c4f9379showard    if options.add_samples:
581cd5131c818ea432d21413694c4ff53c03c4f9379showard        update_samples(options.autotest_dir, options.add_noncompliant,
58261be4cd430df250187506331b131c245d0f28b1ambligh                       options.add_experimental)
583cd5131c818ea432d21413694c4ff53c03c4f9379showard    if options.tests_dir:
584cd5131c818ea432d21413694c4ff53c03c4f9379showard        options.tests_dir = os.path.abspath(options.tests_dir)
585cd5131c818ea432d21413694c4ff53c03c4f9379showard        tests = get_tests_from_fs(options.tests_dir, options.control_pattern,
586cd5131c818ea432d21413694c4ff53c03c4f9379showard                                  add_noncompliant=options.add_noncompliant)
587cd5131c818ea432d21413694c4ff53c03c4f9379showard        update_tests_in_db(tests, add_experimental=options.add_experimental,
588cd5131c818ea432d21413694c4ff53c03c4f9379showard                           add_noncompliant=options.add_noncompliant,
58961be4cd430df250187506331b131c245d0f28b1ambligh                           autotest_dir=options.autotest_dir)
590cd5131c818ea432d21413694c4ff53c03c4f9379showard    if options.profile_dir:
591cd5131c818ea432d21413694c4ff53c03c4f9379showard        profilers = get_tests_from_fs(options.profile_dir, '.*py$')
59261be4cd430df250187506331b131c245d0f28b1ambligh        update_profilers_in_db(profilers,
593cd5131c818ea432d21413694c4ff53c03c4f9379showard                               add_noncompliant=options.add_noncompliant,
594cd5131c818ea432d21413694c4ff53c03c4f9379showard                               description='NA')
59561be4cd430df250187506331b131c245d0f28b1ambligh    if options.clean_tests:
59661be4cd430df250187506331b131c245d0f28b1ambligh        db_clean_broken(options.autotest_dir)
597909c7a661e6739c110690e70e2f4421ffc4e5433showard
598909c7a661e6739c110690e70e2f4421ffc4e5433showard
599909c7a661e6739c110690e70e2f4421ffc4e5433showardif __name__ == "__main__":
600621549355424e1a39529c9fd24d2a1486d79426aAlex Miller    sys.exit(main(sys.argv))
601