mbligh | 7c8ea99 | 2009-06-22 19:03:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 4 | """ |
| 5 | This utility allows for easy updating, removing and importing |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 6 | of tests into the autotest_web afe_autotests table. |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 7 | |
| 8 | Example of updating client side tests: |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 9 | ./test_importer.py -t /usr/local/autotest/client/tests |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 10 | |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 11 | If, for example, not all of your control files adhere to the standard outlined |
| 12 | at http://autotest.kernel.org/wiki/ControlRequirements, you can force options: |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 13 | |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 14 | ./test_importer.py --test-type server -t /usr/local/autotest/server/tests |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 15 | |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 16 | You would need to pass --add-noncompliant to include such control files, |
| 17 | however. An easy way to check for compliance is to run in dry mode: |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 18 | |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 19 | ./test_importer.py --dry-run -t /usr/local/autotest/server/tests/mytest |
| 20 | |
| 21 | Or to check a single control file, you can use check_control_file_vars.py. |
| 22 | |
| 23 | Running with no options is equivalent to --add-all --db-clear-tests. |
| 24 | |
| 25 | Most options should be fairly self explanatory, use --help to display them. |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 26 | """ |
| 27 | |
| 28 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 29 | import common |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 30 | import logging, re, os, sys, optparse, compiler |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 31 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 32 | from autotest_lib.frontend import setup_django_environment |
| 33 | from autotest_lib.frontend.afe import models |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 34 | from autotest_lib.client.common_lib import control_data, utils |
lmr | c781f55 | 2010-05-20 00:36:37 +0000 | [diff] [blame] | 35 | from autotest_lib.client.common_lib import logging_config, logging_manager |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 36 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 37 | |
lmr | c781f55 | 2010-05-20 00:36:37 +0000 | [diff] [blame] | 38 | class TestImporterLoggingConfig(logging_config.LoggingConfig): |
Aviv Keshet | 6f45526 | 2013-03-01 16:02:29 -0800 | [diff] [blame] | 39 | #pylint: disable-msg=C0111 |
lmr | c781f55 | 2010-05-20 00:36:37 +0000 | [diff] [blame] | 40 | def configure_logging(self, results_dir=None, verbose=False): |
| 41 | super(TestImporterLoggingConfig, self).configure_logging( |
| 42 | use_console=True, |
| 43 | verbose=verbose) |
| 44 | |
| 45 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 46 | # Global |
| 47 | DRY_RUN = False |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 48 | DEPENDENCIES_NOT_FOUND = set() |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 49 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 50 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 51 | def 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 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 67 | 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 |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 72 | logging.info("Scanning %s", test_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 73 | 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, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 78 | autotest_dir=autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 79 | test_suite_path = os.path.join(autotest_dir, 'test_suites') |
| 80 | if os.path.exists(test_suite_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 81 | logging.info("Scanning %s", test_suite_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 82 | 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, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 86 | autotest_dir=autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 87 | |
| 88 | profilers_path = os.path.join(autotest_dir, "client/profilers") |
| 89 | if os.path.exists(profilers_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 90 | logging.info("Scanning %s", profilers_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 91 | profilers = get_tests_from_fs(profilers_path, '.*py$') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 92 | update_profilers_in_db(profilers, add_noncompliant=add_noncompliant, |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 93 | description='NA') |
| 94 | # Clean bad db entries |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 95 | db_clean_broken(autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 96 | |
| 97 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 98 | def 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 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 110 | sample_path = os.path.join(autotest_dir, 'server/samples') |
| 111 | if os.path.exists(sample_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 112 | logging.info("Scanning %s", sample_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 113 | 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, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 117 | autotest_dir=autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 118 | |
| 119 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 120 | def db_clean_broken(autotest_dir): |
| 121 | """ |
| 122 | Remove tests from autotest_web that do not have valid control files |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 123 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 124 | 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). |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 130 | """ |
| 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): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 134 | logging.info("Removing %s", test.path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 135 | _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): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 142 | logging.info("Removing %s", profiler.name) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 143 | _log_or_execute(repr(profiler), profiler.delete) |
| 144 | |
| 145 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 146 | def 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 | |
| 168 | def update_profilers_in_db(profilers, description='NA', |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 169 | add_noncompliant=False): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 170 | """ |
| 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 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 181 | for profiler in profilers: |
mbligh | ed0526a | 2010-04-22 18:26:17 +0000 | [diff] [blame] | 182 | name = os.path.basename(profiler) |
| 183 | if name.endswith('.py'): |
| 184 | name = name[:-3] |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 185 | if not profilers[profiler]: |
| 186 | if add_noncompliant: |
| 187 | doc = description |
| 188 | else: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 189 | logging.warning("Skipping %s, missing docstring", profiler) |
mbligh | 781d269 | 2010-04-21 01:04:28 +0000 | [diff] [blame] | 190 | continue |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 191 | 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 | |
| 199 | def update_tests_in_db(tests, dry_run=False, add_experimental=False, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 200 | 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 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 216 | for test in tests: |
| 217 | new_test = models.Test.objects.get_or_create( |
| 218 | path=test.replace(autotest_dir, '').lstrip('/'))[0] |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 219 | logging.info("Processing %s", new_test.path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 220 | |
| 221 | # Set the test's attributes |
| 222 | data = tests[test] |
| 223 | _set_attributes_clean(new_test, data) |
| 224 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 225 | # Custom Attribute Update |
| 226 | if site_set_attributes_module: |
| 227 | site_set_attributes_module._set_attributes_custom(new_test, data) |
| 228 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 229 | # 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 Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 247 | # 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 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 253 | |
| 254 | def _set_attributes_clean(test, data): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 255 | """ |
| 256 | First pass sets the attributes of the Test object from file system. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 257 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 258 | @param test: a test object to be populated for the database. |
| 259 | @param data: object with test data from the file system. |
| 260 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 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 | |
Aviv Keshet | 3dd8beb | 2013-05-13 17:36:04 -0700 | [diff] [blame] | 274 | try: |
| 275 | test.test_type = control_data.CONTROL_TYPE.get_value(data.test_type) |
| 276 | except AttributeError: |
| 277 | raise Exception('Unknown test_type %s for test %s', data.test_type, |
| 278 | data.name) |
| 279 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 280 | int_attributes = ('experimental', 'run_verify') |
| 281 | for attribute in int_attributes: |
| 282 | setattr(test, attribute, int(getattr(data, attribute))) |
| 283 | |
| 284 | try: |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 285 | test.test_time = int(data.time) |
| 286 | if test.test_time < 1 or test.time > 3: |
| 287 | raise Exception('Incorrect number %d for time' % test.time) |
| 288 | except ValueError: |
| 289 | pass |
| 290 | |
| 291 | if not test.test_time and str == type(data.time): |
| 292 | test.test_time = test_time[data.time.lower()] |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 293 | |
Aviv Keshet | 6f45526 | 2013-03-01 16:02:29 -0800 | [diff] [blame] | 294 | test.test_retry = data.retries |
| 295 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 296 | |
| 297 | def add_label_dependencies(test): |
| 298 | """ |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 299 | Add proper many-to-many relationships from DEPENDENCIES field. |
| 300 | |
| 301 | @param test: test object for the database. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 302 | """ |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 303 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 304 | # clear out old relationships |
| 305 | _log_or_execute(repr(test), test.dependency_labels.clear, |
| 306 | subject='clear dependencies from') |
| 307 | |
| 308 | for label_name in test.dependencies.split(','): |
| 309 | label_name = label_name.strip().lower() |
| 310 | if not label_name: |
| 311 | continue |
| 312 | |
| 313 | try: |
| 314 | label = models.Label.objects.get(name=label_name) |
| 315 | except models.Label.DoesNotExist: |
| 316 | log_dependency_not_found(label_name) |
| 317 | continue |
| 318 | |
| 319 | _log_or_execute(repr(label), test.dependency_labels.add, label, |
| 320 | subject='add dependency to %s' % test.name) |
| 321 | |
| 322 | |
| 323 | def log_dependency_not_found(label_name): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 324 | """ |
| 325 | Exception processing when label not found in database. |
| 326 | |
| 327 | @param label_name: from test dependencies. |
| 328 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 329 | if label_name in DEPENDENCIES_NOT_FOUND: |
| 330 | return |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 331 | logging.info("Dependency %s not found", label_name) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 332 | DEPENDENCIES_NOT_FOUND.add(label_name) |
| 333 | |
| 334 | |
| 335 | def get_tests_from_fs(parent_dir, control_pattern, add_noncompliant=False): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 336 | """ |
| 337 | Find control files in file system and load a list with their info. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 338 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 339 | @param parent_dir: directory to search recursively. |
| 340 | @param control_pattern: name format of control file. |
| 341 | @param add_noncompliant: ignore control file parse errors. |
| 342 | |
mbligh | ed0526a | 2010-04-22 18:26:17 +0000 | [diff] [blame] | 343 | @return dictionary of the form: tests[file_path] = parsed_object |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 344 | """ |
| 345 | tests = {} |
| 346 | profilers = False |
| 347 | if 'client/profilers' in parent_dir: |
| 348 | profilers = True |
| 349 | for dir in [ parent_dir ]: |
| 350 | files = recursive_walk(dir, control_pattern) |
| 351 | for file in files: |
| 352 | if '__init__.py' in file or '.svn' in file: |
| 353 | continue |
| 354 | if not profilers: |
| 355 | if not add_noncompliant: |
| 356 | try: |
| 357 | found_test = control_data.parse_control(file, |
| 358 | raise_warnings=True) |
| 359 | tests[file] = found_test |
| 360 | except control_data.ControlVariableException, e: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 361 | logging.warning("Skipping %s\n%s", file, e) |
jamesren | 6e3e9bd | 2010-04-28 18:07:05 +0000 | [diff] [blame] | 362 | except Exception, e: |
| 363 | logging.error("Bad %s\n%s", file, e) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 364 | else: |
| 365 | found_test = control_data.parse_control(file) |
| 366 | tests[file] = found_test |
| 367 | else: |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 368 | tests[file] = compiler.parseFile(file).doc |
| 369 | return tests |
| 370 | |
| 371 | |
| 372 | def recursive_walk(path, wildcard): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 373 | """ |
| 374 | Recursively go through a directory. |
| 375 | |
| 376 | This function invoked by get_tests_from_fs(). |
| 377 | |
| 378 | @param path: base directory to start search. |
| 379 | @param wildcard: name format to match. |
| 380 | |
mbligh | ed0526a | 2010-04-22 18:26:17 +0000 | [diff] [blame] | 381 | @return A list of files that match wildcard |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 382 | """ |
| 383 | files = [] |
| 384 | directories = [ path ] |
| 385 | while len(directories)>0: |
| 386 | directory = directories.pop() |
| 387 | for name in os.listdir(directory): |
| 388 | fullpath = os.path.join(directory, name) |
| 389 | if os.path.isfile(fullpath): |
| 390 | # if we are a control file |
| 391 | if re.search(wildcard, name): |
| 392 | files.append(fullpath) |
| 393 | elif os.path.isdir(fullpath): |
| 394 | directories.append(fullpath) |
| 395 | return files |
| 396 | |
| 397 | |
| 398 | def _log_or_execute(content, func, *args, **kwargs): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 399 | """ |
| 400 | Log a message if dry_run is enabled, or execute the given function. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 401 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 402 | Relies on the DRY_RUN global variable. |
| 403 | |
| 404 | @param content: the actual log message. |
| 405 | @param func: function to execute if dry_run is not enabled. |
| 406 | @param subject: (Optional) The type of log being written. Defaults to |
| 407 | the name of the provided function. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 408 | """ |
| 409 | subject = kwargs.get('subject', func.__name__) |
| 410 | |
| 411 | if DRY_RUN: |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 412 | logging.info("Would %s: %s", subject, content) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 413 | else: |
| 414 | func(*args) |
| 415 | |
| 416 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 417 | def _create_whitelist_set(whitelist_path): |
| 418 | """ |
| 419 | Create a set with contents from a whitelist file for membership testing. |
| 420 | |
| 421 | @param whitelist_path: full path to the whitelist file. |
| 422 | |
mbligh | ed0526a | 2010-04-22 18:26:17 +0000 | [diff] [blame] | 423 | @return set with files listed one/line - newlines included. |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 424 | """ |
| 425 | f = open(whitelist_path, 'r') |
| 426 | whitelist_set = set([line.strip() for line in f]) |
| 427 | f.close() |
| 428 | return whitelist_set |
| 429 | |
| 430 | |
| 431 | def update_from_whitelist(whitelist_set, add_experimental, add_noncompliant, |
| 432 | autotest_dir): |
| 433 | """ |
| 434 | Scans through all tests in the whitelist and add them to the database. |
| 435 | |
| 436 | This function invoked when -w supplied. |
| 437 | |
| 438 | @param whitelist_set: set of tests in full-path form from a whitelist. |
| 439 | @param add_experimental: add tests with experimental attribute set. |
| 440 | @param add_noncompliant: attempt adding test with invalid control files. |
| 441 | @param autotest_dir: prepended to path strings (/usr/local/autotest). |
| 442 | """ |
| 443 | tests = {} |
| 444 | profilers = {} |
| 445 | for file_path in whitelist_set: |
| 446 | if file_path.find('client/profilers') == -1: |
| 447 | try: |
| 448 | found_test = control_data.parse_control(file_path, |
| 449 | raise_warnings=True) |
| 450 | tests[file_path] = found_test |
| 451 | except control_data.ControlVariableException, e: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 452 | logging.warning("Skipping %s\n%s", file, e) |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 453 | else: |
| 454 | profilers[file_path] = compiler.parseFile(file_path).doc |
| 455 | |
| 456 | if len(tests) > 0: |
| 457 | update_tests_in_db(tests, add_experimental=add_experimental, |
| 458 | add_noncompliant=add_noncompliant, |
| 459 | autotest_dir=autotest_dir) |
| 460 | if len(profilers) > 0: |
| 461 | update_profilers_in_db(profilers, add_noncompliant=add_noncompliant, |
| 462 | description='NA') |
| 463 | |
| 464 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 465 | def main(argv): |
Aviv Keshet | 6f45526 | 2013-03-01 16:02:29 -0800 | [diff] [blame] | 466 | """Main function |
| 467 | @param argv: List of command line parameters. |
| 468 | """ |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 469 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 470 | global DRY_RUN |
| 471 | parser = optparse.OptionParser() |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 472 | parser.add_option('-c', '--db-clean-tests', |
| 473 | dest='clean_tests', action='store_true', |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 474 | default=False, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 475 | help='Clean client and server tests with invalid control files') |
| 476 | parser.add_option('-C', '--db-clear-all-tests', |
| 477 | dest='clear_all_tests', action='store_true', |
| 478 | default=False, |
| 479 | help='Clear ALL client and server tests') |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 480 | parser.add_option('-d', '--dry-run', |
| 481 | dest='dry_run', action='store_true', default=False, |
| 482 | help='Dry run for operation') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 483 | parser.add_option('-A', '--add-all', |
| 484 | dest='add_all', action='store_true', |
| 485 | default=False, |
mbligh | 25d656c | 2009-08-24 22:04:02 +0000 | [diff] [blame] | 486 | help='Add site_tests, tests, and test_suites') |
| 487 | parser.add_option('-S', '--add-samples', |
| 488 | dest='add_samples', action='store_true', |
| 489 | default=False, |
| 490 | help='Add samples.') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 491 | parser.add_option('-E', '--add-experimental', |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 492 | dest='add_experimental', action='store_true', |
mbligh | 79410e1 | 2008-11-20 17:59:15 +0000 | [diff] [blame] | 493 | default=True, |
Dan Shi | 5cbd0f8 | 2013-03-29 09:22:55 -0700 | [diff] [blame] | 494 | help='Add experimental tests to frontend, works only ' |
| 495 | 'with -A (--add-all) option') |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 496 | parser.add_option('-N', '--add-noncompliant', |
| 497 | dest='add_noncompliant', action='store_true', |
| 498 | default=False, |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 499 | help='Add non-compliant tests (i.e. tests that do not ' |
Dan Shi | 5cbd0f8 | 2013-03-29 09:22:55 -0700 | [diff] [blame] | 500 | 'define all required control variables), works ' |
| 501 | 'only with -A (--add-all) option') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 502 | parser.add_option('-p', '--profile-dir', dest='profile_dir', |
| 503 | help='Directory to recursively check for profiles') |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 504 | parser.add_option('-t', '--tests-dir', dest='tests_dir', |
| 505 | help='Directory to recursively check for control.*') |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 506 | parser.add_option('-r', '--control-pattern', dest='control_pattern', |
| 507 | default='^control.*', |
| 508 | help='The pattern to look for in directories for control files') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 509 | parser.add_option('-v', '--verbose', |
| 510 | dest='verbose', action='store_true', default=False, |
| 511 | help='Run in verbose mode') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 512 | parser.add_option('-w', '--whitelist-file', dest='whitelist_file', |
| 513 | help='Filename for list of test names that must match') |
| 514 | parser.add_option('-z', '--autotest-dir', dest='autotest_dir', |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 515 | default=os.path.join(os.path.dirname(__file__), '..'), |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 516 | help='Autotest directory root') |
| 517 | options, args = parser.parse_args() |
lmr | c781f55 | 2010-05-20 00:36:37 +0000 | [diff] [blame] | 518 | |
| 519 | logging_manager.configure_logging(TestImporterLoggingConfig(), |
| 520 | verbose=options.verbose) |
| 521 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 522 | DRY_RUN = options.dry_run |
jamesren | fed9409 | 2010-05-03 20:52:47 +0000 | [diff] [blame] | 523 | if DRY_RUN: |
| 524 | logging.getLogger().setLevel(logging.WARN) |
| 525 | |
Dan Shi | 5cbd0f8 | 2013-03-29 09:22:55 -0700 | [diff] [blame] | 526 | if len(argv) > 1 and options.add_noncompliant and not options.add_all: |
| 527 | logging.error('-N (--add-noncompliant) must be ran with option -A ' |
| 528 | '(--add-All).') |
| 529 | return 1 |
| 530 | |
| 531 | if len(argv) > 1 and options.add_experimental and not options.add_all: |
| 532 | logging.error('-E (--add-experimental) must be ran with option -A ' |
| 533 | '(--add-All).') |
| 534 | return 1 |
| 535 | |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 536 | # Make sure autotest_dir is the absolute path |
| 537 | options.autotest_dir = os.path.abspath(options.autotest_dir) |
| 538 | |
| 539 | if len(args) > 0: |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 540 | logging.error("Invalid option(s) provided: %s", args) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 541 | parser.print_help() |
| 542 | return 1 |
| 543 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 544 | if options.verbose: |
| 545 | logging.getLogger().setLevel(logging.DEBUG) |
| 546 | |
| 547 | if len(argv) == 1 or (len(argv) == 2 and options.verbose): |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 548 | update_all(options.autotest_dir, options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 549 | options.add_experimental) |
| 550 | db_clean_broken(options.autotest_dir) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 551 | return 0 |
| 552 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 553 | if options.clear_all_tests: |
| 554 | if (options.clean_tests or options.add_all or options.add_samples or |
| 555 | options.add_noncompliant): |
| 556 | logging.error( |
| 557 | "Can only pass --autotest-dir, --dry-run and --verbose with " |
| 558 | "--db-clear-all-tests") |
| 559 | return 1 |
| 560 | db_clean_all(options.autotest_dir) |
| 561 | |
| 562 | whitelist_set = None |
| 563 | if options.whitelist_file: |
| 564 | if options.add_all: |
| 565 | logging.error("Cannot pass both --add-all and --whitelist-file") |
| 566 | return 1 |
| 567 | whitelist_path = os.path.abspath(options.whitelist_file) |
| 568 | if not os.path.isfile(whitelist_path): |
| 569 | logging.error("--whitelist-file (%s) not found", whitelist_path) |
| 570 | return 1 |
| 571 | logging.info("Using whitelist file %s", whitelist_path) |
| 572 | whitelist_set = _create_whitelist_set(whitelist_path) |
| 573 | update_from_whitelist(whitelist_set, |
| 574 | add_experimental=options.add_experimental, |
| 575 | add_noncompliant=options.add_noncompliant, |
| 576 | autotest_dir=options.autotest_dir) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 577 | if options.add_all: |
| 578 | update_all(options.autotest_dir, options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 579 | options.add_experimental) |
mbligh | 25d656c | 2009-08-24 22:04:02 +0000 | [diff] [blame] | 580 | if options.add_samples: |
| 581 | update_samples(options.autotest_dir, options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 582 | options.add_experimental) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 583 | if options.tests_dir: |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 584 | options.tests_dir = os.path.abspath(options.tests_dir) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 585 | tests = get_tests_from_fs(options.tests_dir, options.control_pattern, |
| 586 | add_noncompliant=options.add_noncompliant) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 587 | update_tests_in_db(tests, add_experimental=options.add_experimental, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 588 | add_noncompliant=options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 589 | autotest_dir=options.autotest_dir) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 590 | if options.profile_dir: |
| 591 | profilers = get_tests_from_fs(options.profile_dir, '.*py$') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 592 | update_profilers_in_db(profilers, |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 593 | add_noncompliant=options.add_noncompliant, |
| 594 | description='NA') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame] | 595 | if options.clean_tests: |
| 596 | db_clean_broken(options.autotest_dir) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 597 | |
| 598 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 599 | if __name__ == "__main__": |
Alex Miller | 6215493 | 2013-08-12 11:04:31 -0700 | [diff] [blame] | 600 | sys.exit(main(sys.argv)) |