[lit] Eliminate mustExist parameter from TestingConfig.frompath().
[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(path, config, self)
76
77     def getBashPath(self):
78         """getBashPath - Get the path to 'bash'"""
79         if self.bashPath is not None:
80             return self.bashPath
81
82         self.bashPath = lit.Util.which('bash', os.pathsep.join(self.path))
83         if self.bashPath is None:
84             # Check some known paths.
85             for path in ('/bin/bash', '/usr/bin/bash', '/usr/local/bin/bash'):
86                 if os.path.exists(path):
87                     self.bashPath = path
88                     break
89
90         if self.bashPath is None:
91             self.warning("Unable to find 'bash'.")
92             self.bashPath = ''
93
94         return self.bashPath
95
96     def getToolsPath(self, dir, paths, tools):
97         if dir is not None and os.path.isabs(dir) and os.path.isdir(dir):
98             if not lit.Util.checkToolsPath(dir, tools):
99                 return None
100         else:
101             dir = lit.Util.whichTools(tools, paths)
102
103         # bash
104         self.bashPath = lit.Util.which('bash', dir)
105         if self.bashPath is None:
106             self.note("Unable to find 'bash.exe'.")
107             self.bashPath = ''
108
109         return dir
110
111     def _write_message(self, kind, message):
112         # Get the file/line where this message was generated.
113         f = inspect.currentframe()
114         # Step out of _write_message, and then out of wrapper.
115         f = f.f_back.f_back
116         file,line,_,_,_ = inspect.getframeinfo(f)
117         location = '%s:%d' % (os.path.basename(file), line)
118
119         sys.stderr.write('%s: %s: %s: %s\n' % (self.progname, location,
120                                                kind, message))
121
122     def note(self, message):
123         self._write_message('note', message)
124
125     def warning(self, message):
126         self._write_message('warning', message)
127         self.numWarnings += 1
128
129     def error(self, message):
130         self._write_message('error', message)
131         self.numErrors += 1
132
133     def fatal(self, message):
134         self._write_message('fatal', message)
135         sys.exit(2)