[lit] Eliminate some nested imports.
[oota-llvm.git] / utils / lit / lit / LitConfig.py
1 from __future__ import absolute_import
2 import inspect
3 import os
4 import sys
5
6 import lit.Test
7 import lit.TestFormats
8 import lit.TestingConfig
9 import lit.Util
10
11 class LitConfig:
12     """LitConfig - Configuration data for a 'lit' test runner instance, shared
13     across all tests.
14
15     The LitConfig object is also used to communicate with client configuration
16     files, it is always passed in as the global variable 'lit' so that
17     configuration files can access common functionality and internal components
18     easily.
19     """
20
21     # Provide access to Test module.
22     Test = lit.Test
23
24     # Provide access to built-in formats.
25     formats = lit.TestFormats
26
27     # Provide access to built-in utility functions.
28     util = lit.Util
29
30     def __init__(self, progname, path, quiet,
31                  useValgrind, valgrindLeakCheck, valgrindArgs,
32                  noExecute, debug, isWindows,
33                  params, config_prefix = None):
34         # The name of the test runner.
35         self.progname = progname
36         # The items to add to the PATH environment variable.
37         self.path = [str(p) for p in path]
38         self.quiet = bool(quiet)
39         self.useValgrind = bool(useValgrind)
40         self.valgrindLeakCheck = bool(valgrindLeakCheck)
41         self.valgrindUserArgs = list(valgrindArgs)
42         self.noExecute = noExecute
43         self.debug = debug
44         self.isWindows = bool(isWindows)
45         self.params = dict(params)
46         self.bashPath = None
47
48         # Configuration files to look for when discovering test suites.
49         self.config_prefix = config_prefix or 'lit'
50         self.config_name = '%s.cfg' % (self.config_prefix,)
51         self.site_config_name = '%s.site.cfg' % (self.config_prefix,)
52         self.local_config_name = '%s.local.cfg' % (self.config_prefix,)
53
54         self.numErrors = 0
55         self.numWarnings = 0
56
57         self.valgrindArgs = []
58         if self.useValgrind:
59             self.valgrindArgs = ['valgrind', '-q', '--run-libc-freeres=no',
60                                  '--tool=memcheck', '--trace-children=yes',
61                                  '--error-exitcode=123']
62             if self.valgrindLeakCheck:
63                 self.valgrindArgs.append('--leak-check=full')
64             else:
65                 # The default is 'summary'.
66                 self.valgrindArgs.append('--leak-check=no')
67             self.valgrindArgs.extend(self.valgrindUserArgs)
68
69
70     def load_config(self, config, path):
71         """load_config(config, path) - Load a config object from an alternate
72         path."""
73         if self.debug:
74             self.note('load_config from %r' % path)
75         return lit.TestingConfig.TestingConfig.frompath(
76             path, config.parent, self, mustExist = True, config = config)
77
78     def getBashPath(self):
79         """getBashPath - Get the path to 'bash'"""
80         if self.bashPath is not None:
81             return self.bashPath
82
83         self.bashPath = lit.Util.which('bash', os.pathsep.join(self.path))
84         if self.bashPath is None:
85             # Check some known paths.
86             for path in ('/bin/bash', '/usr/bin/bash', '/usr/local/bin/bash'):
87                 if os.path.exists(path):
88                     self.bashPath = path
89                     break
90
91         if self.bashPath is None:
92             self.warning("Unable to find 'bash'.")
93             self.bashPath = ''
94
95         return self.bashPath
96
97     def getToolsPath(self, dir, paths, tools):
98         if dir is not None and os.path.isabs(dir) and os.path.isdir(dir):
99             if not lit.Util.checkToolsPath(dir, tools):
100                 return None
101         else:
102             dir = lit.Util.whichTools(tools, paths)
103
104         # bash
105         self.bashPath = lit.Util.which('bash', dir)
106         if self.bashPath is None:
107             self.note("Unable to find 'bash.exe'.")
108             self.bashPath = ''
109
110         return dir
111
112     def _write_message(self, kind, message):
113         # Get the file/line where this message was generated.
114         f = inspect.currentframe()
115         # Step out of _write_message, and then out of wrapper.
116         f = f.f_back.f_back
117         file,line,_,_,_ = inspect.getframeinfo(f)
118         location = '%s:%d' % (os.path.basename(file), line)
119
120         sys.stderr.write('%s: %s: %s: %s\n' % (self.progname, location,
121                                                kind, message))
122
123     def note(self, message):
124         self._write_message('note', message)
125
126     def warning(self, message):
127         self._write_message('warning', message)
128         self.numWarnings += 1
129
130     def error(self, message):
131         self._write_message('error', message)
132         self.numErrors += 1
133
134     def fatal(self, message):
135         self._write_message('fatal', message)
136         sys.exit(2)