LitConfig.py revision 3d4a973ee4a2a134dd3df34a64a0a16256a5306c
1class 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 LitFormats 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                 noExecute, ignoreStdErr, debug, isWindows,
23                 params, config_prefix = None):
24        # The name of the test runner.
25        self.progname = progname
26        # The items to add to the PATH environment variable.
27        self.path = list(map(str, path))
28        self.quiet = bool(quiet)
29        self.useValgrind = bool(useValgrind)
30        self.valgrindLeakCheck = bool(valgrindLeakCheck)
31        self.valgrindUserArgs = list(valgrindArgs)
32        self.noExecute = noExecute
33        self.ignoreStdErr = ignoreStdErr
34        self.debug = debug
35        self.isWindows = bool(isWindows)
36        self.params = dict(params)
37        self.bashPath = None
38
39        # Configuration files to look for when discovering test suites.
40        self.config_prefix = config_prefix or 'lit'
41        self.config_name = '%s.cfg' % (self.config_prefix,)
42        self.site_config_name = '%s.site.cfg' % (self.config_prefix,)
43        self.local_config_name = '%s.local.cfg' % (self.config_prefix,)
44
45        self.numErrors = 0
46        self.numWarnings = 0
47
48        self.valgrindArgs = []
49        if self.useValgrind:
50            self.valgrindArgs = ['valgrind', '-q', '--run-libc-freeres=no',
51                                 '--tool=memcheck', '--trace-children=yes',
52                                 '--error-exitcode=123']
53            if self.valgrindLeakCheck:
54                self.valgrindArgs.append('--leak-check=full')
55            else:
56                # The default is 'summary'.
57                self.valgrindArgs.append('--leak-check=no')
58            self.valgrindArgs.extend(self.valgrindUserArgs)
59
60
61    def load_config(self, config, path):
62        """load_config(config, path) - Load a config object from an alternate
63        path."""
64        from TestingConfig import TestingConfig
65        if self.debug:
66            self.note('load_config from %r' % path)
67        return TestingConfig.frompath(path, config.parent, self,
68                                      mustExist = True,
69                                      config = config)
70
71    def getBashPath(self):
72        """getBashPath - Get the path to 'bash'"""
73        import os, Util
74
75        if self.bashPath is not None:
76            return self.bashPath
77
78        self.bashPath = Util.which('bash', os.pathsep.join(self.path))
79        if self.bashPath is None:
80            # Check some known paths.
81            for path in ('/bin/bash', '/usr/bin/bash', '/usr/local/bin/bash'):
82                if os.path.exists(path):
83                    self.bashPath = path
84                    break
85
86        if self.bashPath is None:
87            self.warning("Unable to find 'bash'.")
88            self.bashPath = ''
89
90        return self.bashPath
91
92    def getToolsPath(self, dir, paths, tools):
93        import os, Util
94        if dir is not None and os.path.isabs(dir) and os.path.isdir(dir):
95            if not Util.checkToolsPath(dir, tools):
96                return None
97        else:
98            dir = Util.whichTools(tools, paths)
99
100        # bash
101        self.bashPath = Util.which('bash', dir)
102        if self.bashPath is None:
103            self.note("Unable to find 'bash.exe'.")
104            self.bashPath = ''
105
106        return dir
107
108    def _write_message(self, kind, message):
109        import inspect, os, sys
110
111        # Get the file/line where this message was generated.
112        f = inspect.currentframe()
113        # Step out of _write_message, and then out of wrapper.
114        f = f.f_back.f_back
115        file,line,_,_,_ = inspect.getframeinfo(f)
116        location = '%s:%d' % (os.path.basename(file), line)
117
118        print >>sys.stderr, '%s: %s: %s: %s' % (self.progname, location,
119                                                kind, message)
120
121    def note(self, message):
122        self._write_message('note', message)
123
124    def warning(self, message):
125        self._write_message('warning', message)
126        self.numWarnings += 1
127
128    def error(self, message):
129        self._write_message('error', message)
130        self.numErrors += 1
131
132    def fatal(self, message):
133        import sys
134        self._write_message('fatal', message)
135        sys.exit(2)
136