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 |
| 11 | infrastructure. |
| 12 | |
| 13 | Data will be written to stdout (or, optionally a file). Format will be: |
| 14 | |
| 15 | {'suite1': {'path/to/test1/control': set(['dep1', 'dep2']), |
| 16 | 'path/to/test2/control': set(['dep2', 'dep3'])}, |
| 17 | 'suite2': {'path/to/test4/control': set(['dep6']), |
| 18 | 'path/to/test5/control': set(['dep7', 'dep3'])}} |
| 19 | |
| 20 | This is intended for use only with Chrome OS test suits that leverage the |
| 21 | dynamic suite infrastructure in server/cros/dynamic_suite.py. |
| 22 | """ |
| 23 | |
| 24 | import optparse, os, sys |
| 25 | import common |
| 26 | from autotest_lib.client.common_lib import control_data |
| 27 | from autotest_lib.server.cros import control_file_getter, dynamic_suite |
| 28 | |
| 29 | def parse_options(): |
| 30 | parser = optparse.OptionParser() |
| 31 | parser.add_option('-a', '--autotest_dir', dest='autotest_dir', |
| 32 | default=os.path.abspath( |
| 33 | os.path.join(os.path.dirname(__file__), '..')), |
| 34 | help="Directory under which to search for tests."\ |
| 35 | " (e.g. /usr/local/autotest). Defaults to '..'") |
| 36 | parser.add_option('-o', '--output_file', dest='output_file', |
| 37 | default=None, |
| 38 | help='File into which to write collected test info.'\ |
| 39 | ' Defaults to stdout.') |
| 40 | options, _ = parser.parse_args() |
| 41 | return options |
| 42 | |
| 43 | |
| 44 | def main(): |
| 45 | options = parse_options() |
| 46 | |
| 47 | fs_getter = dynamic_suite.Suite.create_fs_getter(options.autotest_dir) |
| 48 | predicate = lambda t: hasattr(t, 'suite') # Filter for tests in suites. |
| 49 | test_deps = {} # Format will be {suite: {test: [dep, dep]}}. |
| 50 | for test in dynamic_suite.Suite.find_and_parse_tests(fs_getter, |
| 51 | predicate, |
| 52 | True): |
| 53 | if test.dependencies: |
| 54 | for suite in dynamic_suite.Suite.parse_tag(test.suite): |
| 55 | suite_deps = test_deps.setdefault(suite, {}) |
| 56 | suite_deps[test.path] = test.dependencies |
| 57 | |
| 58 | if options.output_file: |
| 59 | with open(options.output_file, 'w+') as fd: |
| 60 | fd.write('%r' % test_deps) |
| 61 | else: |
| 62 | print '%r' % test_deps |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | sys.exit(main()) |