blob: a6768ea4224cb2bc3220d4503b51bedcc01bc6bf [file] [log] [blame]
Chris Masonec80af5b2012-05-30 14:18:22 -07001#!/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
9Given an autotest root directory, this tool will aggregate the DEPENDENCIES of
10all tests into a single file ready for later consumption by the dynamic suite
Chris Masonea3d98c82012-06-04 11:07:35 -070011infrastructure. ALL DATA MUST BE STORED IN LITERAL TYPES! Dicts, lists,
12strings, etc.
Chris Masonec80af5b2012-05-30 14:18:22 -070013
14Data 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
21This is intended for use only with Chrome OS test suits that leverage the
22dynamic suite infrastructure in server/cros/dynamic_suite.py.
23"""
24
25import optparse, os, sys
26import common
27from autotest_lib.client.common_lib import control_data
28from autotest_lib.server.cros import control_file_getter, dynamic_suite
29
30def 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
45def main():
46 options = parse_options()
47
48 fs_getter = dynamic_suite.Suite.create_fs_getter(options.autotest_dir)
Chris Masone4ac6c732012-06-04 15:25:51 -070049 predicate = lambda t: hasattr(t, 'suite')
Chris Masonec80af5b2012-05-30 14:18:22 -070050 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 Masone4ac6c732012-06-04 15:25:51 -070054 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 Masonea3d98c82012-06-04 11:07:35 -070059 suite_deps[test.path] = list(test.dependencies)
Chris Masone4ac6c732012-06-04 15:25:51 -070060 else:
61 suite_deps[test.path] = []
Chris Masonec80af5b2012-05-30 14:18:22 -070062
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
69if __name__ == "__main__":
70 sys.exit(main())