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