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 |
| 31 | from autotest_lib.frontend import setup_django_environment |
| 32 | from autotest_lib.frontend.afe import models |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 33 | from autotest_lib.client.common_lib import control_data, utils |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 34 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 35 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 36 | logging.basicConfig(level=logging.ERROR) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 37 | # Global |
| 38 | DRY_RUN = False |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 39 | DEPENDENCIES_NOT_FOUND = set() |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 40 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 41 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 42 | def update_all(autotest_dir, add_noncompliant, add_experimental): |
| 43 | """ |
| 44 | Function to scan through all tests and add them to the database. |
| 45 | |
| 46 | This function invoked when no parameters supplied to the command line. |
| 47 | It 'synchronizes' the test database with the current contents of the |
| 48 | client and server test directories. When test code is discovered |
| 49 | in the file system new tests may be added to the db. Likewise, |
| 50 | if test code is not found in the filesystem, tests may be removed |
| 51 | from the db. The base test directories are hard-coded to client/tests, |
| 52 | client/site_tests, server/tests and server/site_tests. |
| 53 | |
| 54 | @param autotest_dir: prepended to path strings (/usr/local/autotest). |
| 55 | @param add_noncompliant: attempt adding test with invalid control files. |
| 56 | @param add_experimental: add tests with experimental attribute set. |
| 57 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 58 | for path in [ 'server/tests', 'server/site_tests', 'client/tests', |
| 59 | 'client/site_tests']: |
| 60 | test_path = os.path.join(autotest_dir, path) |
| 61 | if not os.path.exists(test_path): |
| 62 | continue |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 63 | logging.info("Scanning %s", test_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 64 | tests = [] |
| 65 | tests = get_tests_from_fs(test_path, "^control.*", |
| 66 | add_noncompliant=add_noncompliant) |
| 67 | update_tests_in_db(tests, add_experimental=add_experimental, |
| 68 | add_noncompliant=add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 69 | autotest_dir=autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 70 | test_suite_path = os.path.join(autotest_dir, 'test_suites') |
| 71 | if os.path.exists(test_suite_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 72 | logging.info("Scanning %s", test_suite_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 73 | tests = get_tests_from_fs(test_suite_path, '.*', |
| 74 | add_noncompliant=add_noncompliant) |
| 75 | update_tests_in_db(tests, add_experimental=add_experimental, |
| 76 | add_noncompliant=add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 77 | autotest_dir=autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 78 | |
| 79 | profilers_path = os.path.join(autotest_dir, "client/profilers") |
| 80 | if os.path.exists(profilers_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 81 | logging.info("Scanning %s", profilers_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 82 | profilers = get_tests_from_fs(profilers_path, '.*py$') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 83 | update_profilers_in_db(profilers, add_noncompliant=add_noncompliant, |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 84 | description='NA') |
| 85 | # Clean bad db entries |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 86 | db_clean_broken(autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 87 | |
| 88 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 89 | def update_samples(autotest_dir, add_noncompliant, add_experimental): |
| 90 | """ |
| 91 | Add only sample tests to the database from the filesystem. |
| 92 | |
| 93 | This function invoked when -S supplied on command line. |
| 94 | Only adds tests to the database - does not delete any. |
| 95 | Samples tests are formatted slightly differently than other tests. |
| 96 | |
| 97 | @param autotest_dir: prepended to path strings (/usr/local/autotest). |
| 98 | @param add_noncompliant: attempt adding test with invalid control files. |
| 99 | @param add_experimental: add tests with experimental attribute set. |
| 100 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 101 | sample_path = os.path.join(autotest_dir, 'server/samples') |
| 102 | if os.path.exists(sample_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 103 | logging.info("Scanning %s", sample_path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 104 | tests = get_tests_from_fs(sample_path, '.*srv$', |
| 105 | add_noncompliant=add_noncompliant) |
| 106 | update_tests_in_db(tests, add_experimental=add_experimental, |
| 107 | add_noncompliant=add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 108 | autotest_dir=autotest_dir) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 109 | |
| 110 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 111 | def db_clean_broken(autotest_dir): |
| 112 | """ |
| 113 | Remove tests from autotest_web that do not have valid control files |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 114 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 115 | This function invoked when -c supplied on the command line and when |
| 116 | running update_all(). Removes tests from database which are not |
| 117 | found in the filesystem. Also removes profilers which are just |
| 118 | a special case of tests. |
| 119 | |
| 120 | @param autotest_dir: prepended to path strings (/usr/local/autotest). |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 121 | """ |
| 122 | for test in models.Test.objects.all(): |
| 123 | full_path = os.path.join(autotest_dir, test.path) |
| 124 | if not os.path.isfile(full_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 125 | logging.info("Removing %s", test.path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 126 | _log_or_execute(repr(test), test.delete) |
| 127 | |
| 128 | # Find profilers that are no longer present |
| 129 | for profiler in models.Profiler.objects.all(): |
| 130 | full_path = os.path.join(autotest_dir, "client", "profilers", |
| 131 | profiler.name) |
| 132 | if not os.path.exists(full_path): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 133 | logging.info("Removing %s", profiler.name) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 134 | _log_or_execute(repr(profiler), profiler.delete) |
| 135 | |
| 136 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 137 | def db_clean_all(autotest_dir): |
| 138 | """ |
| 139 | Remove all tests from autotest_web - very destructive |
| 140 | |
| 141 | This function invoked when -C supplied on the command line. |
| 142 | Removes ALL tests from the database. |
| 143 | |
| 144 | @param autotest_dir: prepended to path strings (/usr/local/autotest). |
| 145 | """ |
| 146 | for test in models.Test.objects.all(): |
| 147 | full_path = os.path.join(autotest_dir, test.path) |
| 148 | logging.info("Removing %s", test.path) |
| 149 | _log_or_execute(repr(test), test.delete) |
| 150 | |
| 151 | # Find profilers that are no longer present |
| 152 | for profiler in models.Profiler.objects.all(): |
| 153 | full_path = os.path.join(autotest_dir, "client", "profilers", |
| 154 | profiler.name) |
| 155 | logging.info("Removing %s", profiler.name) |
| 156 | _log_or_execute(repr(profiler), profiler.delete) |
| 157 | |
| 158 | |
| 159 | def update_profilers_in_db(profilers, description='NA', |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 160 | add_noncompliant=False): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 161 | """ |
| 162 | Add only profilers to the database from the filesystem. |
| 163 | |
| 164 | This function invoked when -p supplied on command line. |
| 165 | Only adds profilers to the database - does not delete any. |
| 166 | Profilers are formatted slightly differently than tests. |
| 167 | |
| 168 | @param profilers: list of profilers found in the file system. |
| 169 | @param description: simple text to satisfy docstring. |
| 170 | @param add_noncompliant: attempt adding test with invalid control files. |
| 171 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 172 | for profiler in profilers: |
| 173 | name = os.path.basename(profiler).rstrip(".py") |
| 174 | if not profilers[profiler]: |
| 175 | if add_noncompliant: |
| 176 | doc = description |
| 177 | else: |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 178 | logging.info("Skipping %s, missing docstring", profiler) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 179 | else: |
| 180 | doc = profilers[profiler] |
| 181 | |
| 182 | model = models.Profiler.objects.get_or_create(name=name)[0] |
| 183 | model.description = doc |
| 184 | _log_or_execute(repr(model), model.save) |
| 185 | |
| 186 | |
| 187 | def update_tests_in_db(tests, dry_run=False, add_experimental=False, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 188 | add_noncompliant=False, autotest_dir=None): |
| 189 | """ |
| 190 | Scans through all tests and add them to the database. |
| 191 | |
| 192 | This function invoked when -t supplied and for update_all. |
| 193 | When test code is discovered in the file system new tests may be added |
| 194 | |
| 195 | @param tests: list of tests found in the filesystem. |
| 196 | @param dry_run: not used at this time. |
| 197 | @param add_experimental: add tests with experimental attribute set. |
| 198 | @param add_noncompliant: attempt adding test with invalid control files. |
| 199 | @param autotest_dir: prepended to path strings (/usr/local/autotest). |
| 200 | """ |
| 201 | site_set_attributes_module = utils.import_site_module( |
| 202 | __file__, 'autotest_lib.utils.site_test_importer_attributes') |
| 203 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 204 | for test in tests: |
| 205 | new_test = models.Test.objects.get_or_create( |
| 206 | path=test.replace(autotest_dir, '').lstrip('/'))[0] |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 207 | logging.info("Processing %s", new_test.path) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 208 | |
| 209 | # Set the test's attributes |
| 210 | data = tests[test] |
| 211 | _set_attributes_clean(new_test, data) |
| 212 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 213 | # Custom Attribute Update |
| 214 | if site_set_attributes_module: |
| 215 | site_set_attributes_module._set_attributes_custom(new_test, data) |
| 216 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 217 | # This only takes place if --add-noncompliant is provided on the CLI |
| 218 | if not new_test.name: |
| 219 | test_new_test = test.split('/') |
| 220 | if test_new_test[-1] == 'control': |
| 221 | new_test.name = test_new_test[-2] |
| 222 | else: |
| 223 | control_name = "%s:%s" |
| 224 | control_name %= (test_new_test[-2], |
| 225 | test_new_test[-1]) |
| 226 | new_test.name = control_name.replace('control.', '') |
| 227 | |
| 228 | # Experimental Check |
| 229 | if not add_experimental and new_test.experimental: |
| 230 | continue |
| 231 | |
| 232 | _log_or_execute(repr(new_test), new_test.save) |
| 233 | add_label_dependencies(new_test) |
| 234 | |
| 235 | |
| 236 | def _set_attributes_clean(test, data): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 237 | """ |
| 238 | First pass sets the attributes of the Test object from file system. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 239 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 240 | @param test: a test object to be populated for the database. |
| 241 | @param data: object with test data from the file system. |
| 242 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 243 | test_type = { 'client' : 1, |
| 244 | 'server' : 2, } |
| 245 | test_time = { 'short' : 1, |
| 246 | 'medium' : 2, |
| 247 | 'long' : 3, } |
| 248 | |
| 249 | |
| 250 | string_attributes = ('name', 'author', 'test_class', 'test_category', |
| 251 | 'test_category', 'sync_count') |
| 252 | for attribute in string_attributes: |
| 253 | setattr(test, attribute, getattr(data, attribute)) |
| 254 | |
| 255 | test.description = data.doc |
| 256 | test.dependencies = ", ".join(data.dependencies) |
| 257 | |
| 258 | int_attributes = ('experimental', 'run_verify') |
| 259 | for attribute in int_attributes: |
| 260 | setattr(test, attribute, int(getattr(data, attribute))) |
| 261 | |
| 262 | try: |
| 263 | test.test_type = int(data.test_type) |
| 264 | if test.test_type != 1 and test.test_type != 2: |
| 265 | raise Exception('Incorrect number %d for test_type' % |
| 266 | test.test_type) |
| 267 | except ValueError: |
| 268 | pass |
| 269 | try: |
| 270 | test.test_time = int(data.time) |
| 271 | if test.test_time < 1 or test.time > 3: |
| 272 | raise Exception('Incorrect number %d for time' % test.time) |
| 273 | except ValueError: |
| 274 | pass |
| 275 | |
| 276 | if not test.test_time and str == type(data.time): |
| 277 | test.test_time = test_time[data.time.lower()] |
| 278 | if not test.test_type and str == type(data.test_type): |
| 279 | test.test_type = test_type[data.test_type.lower()] |
| 280 | |
| 281 | |
| 282 | def add_label_dependencies(test): |
| 283 | """ |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 284 | Add proper many-to-many relationships from DEPENDENCIES field. |
| 285 | |
| 286 | @param test: test object for the database. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 287 | """ |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 288 | |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 289 | # clear out old relationships |
| 290 | _log_or_execute(repr(test), test.dependency_labels.clear, |
| 291 | subject='clear dependencies from') |
| 292 | |
| 293 | for label_name in test.dependencies.split(','): |
| 294 | label_name = label_name.strip().lower() |
| 295 | if not label_name: |
| 296 | continue |
| 297 | |
| 298 | try: |
| 299 | label = models.Label.objects.get(name=label_name) |
| 300 | except models.Label.DoesNotExist: |
| 301 | log_dependency_not_found(label_name) |
| 302 | continue |
| 303 | |
| 304 | _log_or_execute(repr(label), test.dependency_labels.add, label, |
| 305 | subject='add dependency to %s' % test.name) |
| 306 | |
| 307 | |
| 308 | def log_dependency_not_found(label_name): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 309 | """ |
| 310 | Exception processing when label not found in database. |
| 311 | |
| 312 | @param label_name: from test dependencies. |
| 313 | """ |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 314 | if label_name in DEPENDENCIES_NOT_FOUND: |
| 315 | return |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 316 | logging.info("Dependency %s not found", label_name) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 317 | DEPENDENCIES_NOT_FOUND.add(label_name) |
| 318 | |
| 319 | |
| 320 | def get_tests_from_fs(parent_dir, control_pattern, add_noncompliant=False): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 321 | """ |
| 322 | Find control files in file system and load a list with their info. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 323 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 324 | @param parent_dir: directory to search recursively. |
| 325 | @param control_pattern: name format of control file. |
| 326 | @param add_noncompliant: ignore control file parse errors. |
| 327 | |
| 328 | @return: dictionary of the form: tests[file_path] = parsed_object |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 329 | """ |
| 330 | tests = {} |
| 331 | profilers = False |
| 332 | if 'client/profilers' in parent_dir: |
| 333 | profilers = True |
| 334 | for dir in [ parent_dir ]: |
| 335 | files = recursive_walk(dir, control_pattern) |
| 336 | for file in files: |
| 337 | if '__init__.py' in file or '.svn' in file: |
| 338 | continue |
| 339 | if not profilers: |
| 340 | if not add_noncompliant: |
| 341 | try: |
| 342 | found_test = control_data.parse_control(file, |
| 343 | raise_warnings=True) |
| 344 | tests[file] = found_test |
| 345 | except control_data.ControlVariableException, e: |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 346 | logging.info("Skipping %s\n%s", file, e) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 347 | pass |
| 348 | else: |
| 349 | found_test = control_data.parse_control(file) |
| 350 | tests[file] = found_test |
| 351 | else: |
| 352 | script = file.rstrip(".py") |
| 353 | tests[file] = compiler.parseFile(file).doc |
| 354 | return tests |
| 355 | |
| 356 | |
| 357 | def recursive_walk(path, wildcard): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 358 | """ |
| 359 | Recursively go through a directory. |
| 360 | |
| 361 | This function invoked by get_tests_from_fs(). |
| 362 | |
| 363 | @param path: base directory to start search. |
| 364 | @param wildcard: name format to match. |
| 365 | |
| 366 | @return: A list of files that match wildcard |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 367 | """ |
| 368 | files = [] |
| 369 | directories = [ path ] |
| 370 | while len(directories)>0: |
| 371 | directory = directories.pop() |
| 372 | for name in os.listdir(directory): |
| 373 | fullpath = os.path.join(directory, name) |
| 374 | if os.path.isfile(fullpath): |
| 375 | # if we are a control file |
| 376 | if re.search(wildcard, name): |
| 377 | files.append(fullpath) |
| 378 | elif os.path.isdir(fullpath): |
| 379 | directories.append(fullpath) |
| 380 | return files |
| 381 | |
| 382 | |
| 383 | def _log_or_execute(content, func, *args, **kwargs): |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 384 | """ |
| 385 | Log a message if dry_run is enabled, or execute the given function. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 386 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 387 | Relies on the DRY_RUN global variable. |
| 388 | |
| 389 | @param content: the actual log message. |
| 390 | @param func: function to execute if dry_run is not enabled. |
| 391 | @param subject: (Optional) The type of log being written. Defaults to |
| 392 | the name of the provided function. |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 393 | """ |
| 394 | subject = kwargs.get('subject', func.__name__) |
| 395 | |
| 396 | if DRY_RUN: |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 397 | logging.info("Would %s: %s", subject, content) |
showard | cd5131c | 2010-01-12 18:54:33 +0000 | [diff] [blame] | 398 | else: |
| 399 | func(*args) |
| 400 | |
| 401 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 402 | def _create_whitelist_set(whitelist_path): |
| 403 | """ |
| 404 | Create a set with contents from a whitelist file for membership testing. |
| 405 | |
| 406 | @param whitelist_path: full path to the whitelist file. |
| 407 | |
| 408 | @return: set with files listed one/line - newlines included. |
| 409 | """ |
| 410 | f = open(whitelist_path, 'r') |
| 411 | whitelist_set = set([line.strip() for line in f]) |
| 412 | f.close() |
| 413 | return whitelist_set |
| 414 | |
| 415 | |
| 416 | def update_from_whitelist(whitelist_set, add_experimental, add_noncompliant, |
| 417 | autotest_dir): |
| 418 | """ |
| 419 | Scans through all tests in the whitelist and add them to the database. |
| 420 | |
| 421 | This function invoked when -w supplied. |
| 422 | |
| 423 | @param whitelist_set: set of tests in full-path form from a whitelist. |
| 424 | @param add_experimental: add tests with experimental attribute set. |
| 425 | @param add_noncompliant: attempt adding test with invalid control files. |
| 426 | @param autotest_dir: prepended to path strings (/usr/local/autotest). |
| 427 | """ |
| 428 | tests = {} |
| 429 | profilers = {} |
| 430 | for file_path in whitelist_set: |
| 431 | if file_path.find('client/profilers') == -1: |
| 432 | try: |
| 433 | found_test = control_data.parse_control(file_path, |
| 434 | raise_warnings=True) |
| 435 | tests[file_path] = found_test |
| 436 | except control_data.ControlVariableException, e: |
| 437 | logging.info("Skipping %s\n%s", file, e) |
| 438 | pass |
| 439 | else: |
| 440 | profilers[file_path] = compiler.parseFile(file_path).doc |
| 441 | |
| 442 | if len(tests) > 0: |
| 443 | update_tests_in_db(tests, add_experimental=add_experimental, |
| 444 | add_noncompliant=add_noncompliant, |
| 445 | autotest_dir=autotest_dir) |
| 446 | if len(profilers) > 0: |
| 447 | update_profilers_in_db(profilers, add_noncompliant=add_noncompliant, |
| 448 | description='NA') |
| 449 | |
| 450 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 451 | def main(argv): |
| 452 | """Main function""" |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 453 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 454 | global DRY_RUN |
| 455 | parser = optparse.OptionParser() |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 456 | parser.add_option('-c', '--db-clean-tests', |
| 457 | dest='clean_tests', action='store_true', |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 458 | default=False, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 459 | help='Clean client and server tests with invalid control files') |
| 460 | parser.add_option('-C', '--db-clear-all-tests', |
| 461 | dest='clear_all_tests', action='store_true', |
| 462 | default=False, |
| 463 | help='Clear ALL client and server tests') |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 464 | parser.add_option('-d', '--dry-run', |
| 465 | dest='dry_run', action='store_true', default=False, |
| 466 | help='Dry run for operation') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 467 | parser.add_option('-A', '--add-all', |
| 468 | dest='add_all', action='store_true', |
| 469 | default=False, |
mbligh | 25d656c | 2009-08-24 22:04:02 +0000 | [diff] [blame] | 470 | help='Add site_tests, tests, and test_suites') |
| 471 | parser.add_option('-S', '--add-samples', |
| 472 | dest='add_samples', action='store_true', |
| 473 | default=False, |
| 474 | help='Add samples.') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 475 | parser.add_option('-E', '--add-experimental', |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 476 | dest='add_experimental', action='store_true', |
mbligh | 79410e1 | 2008-11-20 17:59:15 +0000 | [diff] [blame] | 477 | default=True, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 478 | help='Add experimental tests to frontend') |
| 479 | parser.add_option('-N', '--add-noncompliant', |
| 480 | dest='add_noncompliant', action='store_true', |
| 481 | default=False, |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 482 | help='Add non-compliant tests (i.e. tests that do not ' |
| 483 | 'define all required control variables)') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 484 | parser.add_option('-p', '--profile-dir', dest='profile_dir', |
| 485 | help='Directory to recursively check for profiles') |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 486 | parser.add_option('-t', '--tests-dir', dest='tests_dir', |
| 487 | help='Directory to recursively check for control.*') |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 488 | parser.add_option('-r', '--control-pattern', dest='control_pattern', |
| 489 | default='^control.*', |
| 490 | help='The pattern to look for in directories for control files') |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 491 | parser.add_option('-v', '--verbose', |
| 492 | dest='verbose', action='store_true', default=False, |
| 493 | help='Run in verbose mode') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 494 | parser.add_option('-w', '--whitelist-file', dest='whitelist_file', |
| 495 | help='Filename for list of test names that must match') |
| 496 | parser.add_option('-z', '--autotest-dir', dest='autotest_dir', |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 497 | default=os.path.join(os.path.dirname(__file__), '..'), |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 498 | help='Autotest directory root') |
| 499 | options, args = parser.parse_args() |
| 500 | DRY_RUN = options.dry_run |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 501 | # Make sure autotest_dir is the absolute path |
| 502 | options.autotest_dir = os.path.abspath(options.autotest_dir) |
| 503 | |
| 504 | if len(args) > 0: |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 505 | logging.error("Invalid option(s) provided: %s", args) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 506 | parser.print_help() |
| 507 | return 1 |
| 508 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 509 | if options.verbose: |
| 510 | logging.getLogger().setLevel(logging.DEBUG) |
| 511 | |
| 512 | if len(argv) == 1 or (len(argv) == 2 and options.verbose): |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 513 | update_all(options.autotest_dir, options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 514 | options.add_experimental) |
| 515 | db_clean_broken(options.autotest_dir) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 516 | return 0 |
| 517 | |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 518 | if options.clear_all_tests: |
| 519 | if (options.clean_tests or options.add_all or options.add_samples or |
| 520 | options.add_noncompliant): |
| 521 | logging.error( |
| 522 | "Can only pass --autotest-dir, --dry-run and --verbose with " |
| 523 | "--db-clear-all-tests") |
| 524 | return 1 |
| 525 | db_clean_all(options.autotest_dir) |
| 526 | |
| 527 | whitelist_set = None |
| 528 | if options.whitelist_file: |
| 529 | if options.add_all: |
| 530 | logging.error("Cannot pass both --add-all and --whitelist-file") |
| 531 | return 1 |
| 532 | whitelist_path = os.path.abspath(options.whitelist_file) |
| 533 | if not os.path.isfile(whitelist_path): |
| 534 | logging.error("--whitelist-file (%s) not found", whitelist_path) |
| 535 | return 1 |
| 536 | logging.info("Using whitelist file %s", whitelist_path) |
| 537 | whitelist_set = _create_whitelist_set(whitelist_path) |
| 538 | update_from_whitelist(whitelist_set, |
| 539 | add_experimental=options.add_experimental, |
| 540 | add_noncompliant=options.add_noncompliant, |
| 541 | autotest_dir=options.autotest_dir) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 542 | if options.add_all: |
| 543 | update_all(options.autotest_dir, options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 544 | options.add_experimental) |
mbligh | 25d656c | 2009-08-24 22:04:02 +0000 | [diff] [blame] | 545 | if options.add_samples: |
| 546 | update_samples(options.autotest_dir, options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 547 | options.add_experimental) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 548 | if options.tests_dir: |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 549 | options.tests_dir = os.path.abspath(options.tests_dir) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 550 | tests = get_tests_from_fs(options.tests_dir, options.control_pattern, |
| 551 | add_noncompliant=options.add_noncompliant) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 552 | update_tests_in_db(tests, add_experimental=options.add_experimental, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 553 | add_noncompliant=options.add_noncompliant, |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 554 | autotest_dir=options.autotest_dir) |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 555 | if options.profile_dir: |
| 556 | profilers = get_tests_from_fs(options.profile_dir, '.*py$') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 557 | update_profilers_in_db(profilers, |
mbligh | 322ec1a | 2008-09-26 16:48:10 +0000 | [diff] [blame] | 558 | add_noncompliant=options.add_noncompliant, |
| 559 | description='NA') |
mbligh | 61be4cd | 2010-03-30 02:02:40 +0000 | [diff] [blame^] | 560 | if options.clean_tests: |
| 561 | db_clean_broken(options.autotest_dir) |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 562 | |
| 563 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 564 | if __name__ == "__main__": |
| 565 | main(sys.argv) |