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