1#!/usr/bin/python
2# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6""" Utility code adapted from test_importer.py for test doc generation.
7
8These routines are modified versions of those in test_importer.py. If the
9docgen code is ever merged into Autotest, this code should be factored out
10of test_importer.py and combined with this.
11"""
12
13import fnmatch
14import os
15
16import common
17from autotest_lib.client.common_lib import control_data
18
19
20def GetTestsFromFS(parent_dir, logger):
21    """
22    Find control files in file system and load a list with their info.
23
24    @param parent_dir: directory to search recursively.
25    @param logger: Python logger for logging.
26
27    @return dictionary of the form: tests[file_path] = parsed_object
28    """
29    tests = {}
30    tests_src = {}
31    for root, dirnames, filenames in os.walk(parent_dir):
32        for filename in fnmatch.filter(filenames, 'control*'):
33            test_name = os.path.basename(root)
34            if test_name[:5].lower() == 'suite' or '.svn' in filename:
35                continue
36            full_name = os.path.join(root, filename)
37            try:
38                found_test = control_data.parse_control(full_name,
39                                                        raise_warnings=True)
40                tests[test_name] = ''
41                tests_src[test_name] = parent_dir
42            except control_data.ControlVariableException, e:
43                logger.warn("Skipping %s\n%s", full_name, e)
44            except Exception, e:
45                logger.error("Bad %s\n%s", full_name, e)
46    return tests, tests_src
47
48
49