Chris Masone | c80af5b | 2012-05-30 14:18:22 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Tool for preprocessing tests to determine their DEPENDENCIES. |
| 8 | |
| 9 | Given an autotest root directory, this tool will aggregate the DEPENDENCIES of |
| 10 | all tests into a single file ready for later consumption by the dynamic suite |
Chris Masone | a3d98c8 | 2012-06-04 11:07:35 -0700 | [diff] [blame] | 11 | infrastructure. ALL DATA MUST BE STORED IN LITERAL TYPES! Dicts, lists, |
| 12 | strings, etc. |
Chris Masone | c80af5b | 2012-05-30 14:18:22 -0700 | [diff] [blame] | 13 | |
| 14 | Data will be written to stdout (or, optionally a file). Format will be: |
| 15 | |
| 16 | {'suite1': {'path/to/test1/control': set(['dep1', 'dep2']), |
| 17 | 'path/to/test2/control': set(['dep2', 'dep3'])}, |
| 18 | 'suite2': {'path/to/test4/control': set(['dep6']), |
| 19 | 'path/to/test5/control': set(['dep7', 'dep3'])}} |
| 20 | |
| 21 | This is intended for use only with Chrome OS test suits that leverage the |
| 22 | dynamic suite infrastructure in server/cros/dynamic_suite.py. |
| 23 | """ |
| 24 | |
| 25 | import optparse, os, sys |
| 26 | import common |
| 27 | from autotest_lib.client.common_lib import control_data |
| 28 | from autotest_lib.server.cros import control_file_getter, dynamic_suite |
| 29 | |
| 30 | def parse_options(): |
| 31 | parser = optparse.OptionParser() |
| 32 | parser.add_option('-a', '--autotest_dir', dest='autotest_dir', |
| 33 | default=os.path.abspath( |
| 34 | os.path.join(os.path.dirname(__file__), '..')), |
| 35 | help="Directory under which to search for tests."\ |
| 36 | " (e.g. /usr/local/autotest). Defaults to '..'") |
| 37 | parser.add_option('-o', '--output_file', dest='output_file', |
| 38 | default=None, |
| 39 | help='File into which to write collected test info.'\ |
| 40 | ' Defaults to stdout.') |
| 41 | options, _ = parser.parse_args() |
| 42 | return options |
| 43 | |
| 44 | |
| 45 | def main(): |
| 46 | options = parse_options() |
| 47 | |
| 48 | fs_getter = dynamic_suite.Suite.create_fs_getter(options.autotest_dir) |
Chris Masone | 4ac6c73 | 2012-06-04 15:25:51 -0700 | [diff] [blame] | 49 | predicate = lambda t: hasattr(t, 'suite') |
Chris Masone | c80af5b | 2012-05-30 14:18:22 -0700 | [diff] [blame] | 50 | test_deps = {} # Format will be {suite: {test: [dep, dep]}}. |
| 51 | for test in dynamic_suite.Suite.find_and_parse_tests(fs_getter, |
| 52 | predicate, |
| 53 | True): |
Chris Masone | 4ac6c73 | 2012-06-04 15:25:51 -0700 | [diff] [blame] | 54 | for suite in dynamic_suite.Suite.parse_tag(test.suite): |
| 55 | suite_deps = test_deps.setdefault(suite, {}) |
| 56 | # Force this to a list so that we can parse it later with |
| 57 | # ast.literal_eval() -- which is MUCH safer than eval() |
| 58 | if test.dependencies: |
Chris Masone | a3d98c8 | 2012-06-04 11:07:35 -0700 | [diff] [blame] | 59 | suite_deps[test.path] = list(test.dependencies) |
Chris Masone | 4ac6c73 | 2012-06-04 15:25:51 -0700 | [diff] [blame] | 60 | else: |
| 61 | suite_deps[test.path] = [] |
Chris Masone | c80af5b | 2012-05-30 14:18:22 -0700 | [diff] [blame] | 62 | |
| 63 | if options.output_file: |
| 64 | with open(options.output_file, 'w+') as fd: |
| 65 | fd.write('%r' % test_deps) |
| 66 | else: |
| 67 | print '%r' % test_deps |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | sys.exit(main()) |