Home | History | Annotate | Download | only in site_utils
      1 #!/usr/bin/python
      2 #
      3 # Copyright (c) 20123 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 control files to build a suite to control files map.
      8 
      9 Given an autotest root directory, this tool will bucket tests accroding to
     10 their suite.Data will be written to stdout (or, optionally a file), eg:
     11 
     12 {'suite1': ['path/to/test1/control', 'path/to/test2/control'],
     13  'suite2': ['path/to/test4/control', 'path/to/test5/control']}
     14 
     15 This is intended for use only with Chrome OS test suites that leverage the
     16 dynamic suite infrastructure in server/cros/dynamic_suite.py. It is invoked
     17 at build time to generate said suite to control files map, which dynamic_suite
     18 consults at run time to determine which tests belong to a suite.
     19 """
     20 
     21 
     22 import collections, json, os, sys
     23 
     24 import common
     25 from autotest_lib.server.cros.dynamic_suite import suite
     26 from autotest_lib.site_utils import suite_preprocessor
     27 
     28 
     29 # A set of SUITES that we choose not to preprocess as they might have tests
     30 # added later.
     31 SUITE_BLACKLIST = set(['au'])
     32 
     33 
     34 def _get_control_files_to_process(autotest_dir):
     35     """Find all control files in autotest_dir that have 'SUITE='
     36 
     37     @param autotest_dir: The directory to search for control files.
     38     @return: All control files in autotest_dir that have a suite attribute.
     39     """
     40     fs_getter = suite.Suite.create_fs_getter(autotest_dir)
     41     predicate = lambda t: hasattr(t, 'suite')
     42     return suite.Suite.find_and_parse_tests(fs_getter, predicate,
     43                                             add_experimental=True)
     44 
     45 
     46 def get_suite_control_files(autotest_dir):
     47     """
     48     Partition all control files in autotest_dir based on suite.
     49 
     50     @param autotest_dir: Directory to walk looking for control files.
     51     @return suite_control_files: A dictionary mapping suite->[control files]
     52                                  as described in this files docstring.
     53     @raise ValueError: If autotest_dir doesn't exist.
     54     """
     55     if not os.path.exists(autotest_dir):
     56       raise ValueError('Could not find directory: %s, failed to map suites to'
     57                        ' their control files.' % autotest_dir)
     58 
     59     autotest_dir = autotest_dir.rstrip('/')
     60     suite_control_files = collections.defaultdict(list)
     61 
     62     for test in _get_control_files_to_process(autotest_dir):
     63         for suite_name in suite.Suite.parse_tag(test.suite):
     64             if suite_name in SUITE_BLACKLIST:
     65                 continue
     66 
     67             suite_control_files[suite_name].append(
     68                 test.path.replace('%s/' % autotest_dir, ''))
     69     return suite_control_files
     70 
     71 
     72 def main():
     73     """
     74     Main function.
     75     """
     76     options = suite_preprocessor.parse_options()
     77 
     78     suite_control_files = get_suite_control_files(options.autotest_dir)
     79     if options.output_file:
     80         with open(options.output_file, 'w') as file_obj:
     81             json.dump(suite_control_files, file_obj)
     82     else:
     83         print suite_control_files
     84 
     85 
     86 if __name__ == '__main__':
     87     sys.exit(main())
     88