blob: fc5ea43063b83105a7c601a854e4fb82dcc1b123 [file] [log] [blame]
Chris Masone6fed6462011-10-20 16:36:43 -07001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import common
6import compiler, logging, os, random, re, time
7from autotest_lib.client.common_lib import control_data, error, utils
8
9
10class ControlFileGetter(object):
11 """
12 Interface for classes that can list and fetch known control files.
13
14 @var _CONTROL_PATTERN: control file name format to match.
15 """
16
17 _CONTROL_PATTERN = '^control(?:\..+)?$'
18
19 def __init__(self):
20 pass
21
22
23 def get_control_file_list(self):
24 """
25 Gather a list of paths to control files matching |_CONTROL_PATTERN|.
26
27 @return A list of files that match regexp
28 """
29 pass
30
31
32 def get_control_file_contents(self, test_path):
33 """
34 Given a path to a control file, return its contents.
35
36 @param test_path: the path to the control file
37 @return the contents of the control file specified by the path.
38 """
39 pass
40
41
42 def get_control_file_contents_by_name(self, test_name):
43 """
44 Given the name of a control file, return its contents.
45
46 @param test_name: the path to the control file.
47 @return the contents of the control file specified by the path.
48 """
49 pass
50
51
52 def _is_useful_file(self, name):
53 return '__init__.py' not in name and '.svn' not in name
54
55
56class FileSystemGetter(ControlFileGetter):
57 def __init__(self, paths):
58 """
59 @param paths: base directories to start search.
60 """
61 self._paths = paths
62 self._files = []
63
64
65 def get_control_file_list(self):
66 """
67 Gather a list of paths to control files under |_paths|.
68
69 @return A list of files that match |_CONTROL_PATTERN|.
70 """
71 regexp = re.compile(self._CONTROL_PATTERN)
72 directories = self._paths
73 while len(directories) > 0:
74 directory = directories.pop()
75 if not os.path.exists(directory):
76 continue
77 for name in os.listdir(directory):
78 fullpath = os.path.join(directory, name)
79 if os.path.isfile(fullpath):
80 if regexp.search(name):
81 # if we are a control file
82 self._files.append(fullpath)
83 elif os.path.isdir(fullpath):
84 directories.append(fullpath)
85 return [f for f in self._files if self._is_useful_file(f)]
86
87
88 def get_control_file_contents(self, test_path):
89 return utils.read_file(test_path)
90
91
92 def get_control_file_contents_by_name(self, test_name):
93 if not self._files:
94 self.get_control_file_list()
95 regexp = re.compile(os.path.join(test_name, 'control'))
96 candidates = filter(regexp.search, self._files)
97 if not candidates or len(candidates) > 1:
98 raise error.TestError(test_name + ' is not unique.')
99 return self.get_control_file_contents(candidates[0])