[lit] Report the traceback when config import fails.
[oota-llvm.git] / utils / lit / lit / TestingConfig.py
1 import os
2 import sys
3
4 PY2 = sys.version_info[0] < 3
5
6 class TestingConfig:
7     """"
8     TestingConfig - Information on the tests inside a suite.
9     """
10
11     @staticmethod
12     def frompath(path, parent, litConfig, mustExist, config = None):
13         if config is None:
14             # Set the environment based on the command line arguments.
15             environment = {
16                 'LIBRARY_PATH' : os.environ.get('LIBRARY_PATH',''),
17                 'LD_LIBRARY_PATH' : os.environ.get('LD_LIBRARY_PATH',''),
18                 'PATH' : os.pathsep.join(litConfig.path +
19                                          [os.environ.get('PATH','')]),
20                 'SYSTEMROOT' : os.environ.get('SYSTEMROOT',''),
21                 'TERM' : os.environ.get('TERM',''),
22                 'LLVM_DISABLE_CRASH_REPORT' : '1',
23                 }
24
25             if sys.platform == 'win32':
26                 environment.update({
27                         'INCLUDE' : os.environ.get('INCLUDE',''),
28                         'PATHEXT' : os.environ.get('PATHEXT',''),
29                         'PYTHONUNBUFFERED' : '1',
30                         'TEMP' : os.environ.get('TEMP',''),
31                         'TMP' : os.environ.get('TMP',''),
32                         })
33
34             # Set the default available features based on the LitConfig.
35             available_features = []
36             if litConfig.useValgrind:
37                 available_features.append('valgrind')
38                 if litConfig.valgrindLeakCheck:
39                     available_features.append('vg_leak')
40
41             config = TestingConfig(parent,
42                                    name = '<unnamed>',
43                                    suffixes = set(),
44                                    test_format = None,
45                                    environment = environment,
46                                    substitutions = [],
47                                    unsupported = False,
48                                    on_clone = None,
49                                    test_exec_root = None,
50                                    test_source_root = None,
51                                    excludes = [],
52                                    available_features = available_features,
53                                    pipefail = True)
54
55         if os.path.exists(path):
56             # FIXME: Improve detection and error reporting of errors in the
57             # config file.
58             f = open(path)
59             cfg_globals = dict(globals())
60             cfg_globals['config'] = config
61             cfg_globals['lit'] = litConfig
62             cfg_globals['__file__'] = path
63             try:
64                 data = f.read()
65                 if PY2:
66                     exec("exec data in cfg_globals")
67                 else:
68                     exec(data, cfg_globals)
69                 if litConfig.debug:
70                     litConfig.note('... loaded config %r' % path)
71             except SystemExit:
72                 e = sys.exc_info()[1]
73                 # We allow normal system exit inside a config file to just
74                 # return control without error.
75                 if e.args:
76                     raise
77             except:
78                 import traceback
79                 litConfig.fatal(
80                     'unable to parse config file %r, traceback: %s' % (
81                         path, traceback.format_exc()))
82             f.close()
83         else:
84             if mustExist:
85                 litConfig.fatal('unable to load config from %r ' % path)
86             elif litConfig.debug:
87                 litConfig.note('... config not found  - %r' %path)
88
89         config.finish(litConfig)
90         return config
91
92     def __init__(self, parent, name, suffixes, test_format,
93                  environment, substitutions, unsupported, on_clone,
94                  test_exec_root, test_source_root, excludes,
95                  available_features, pipefail):
96         self.parent = parent
97         self.name = str(name)
98         self.suffixes = set(suffixes)
99         self.test_format = test_format
100         self.environment = dict(environment)
101         self.substitutions = list(substitutions)
102         self.unsupported = unsupported
103         self.on_clone = on_clone
104         self.test_exec_root = test_exec_root
105         self.test_source_root = test_source_root
106         self.excludes = set(excludes)
107         self.available_features = set(available_features)
108         self.pipefail = pipefail
109
110     def clone(self, path):
111         # FIXME: Chain implementations?
112         #
113         # FIXME: Allow extra parameters?
114         cfg = TestingConfig(self, self.name, self.suffixes, self.test_format,
115                             self.environment, self.substitutions,
116                             self.unsupported, self.on_clone,
117                             self.test_exec_root, self.test_source_root,
118                             self.excludes, self.available_features,
119                             self.pipefail)
120         if cfg.on_clone:
121             cfg.on_clone(self, cfg, path)
122         return cfg
123
124     def finish(self, litConfig):
125         """finish() - Finish this config object, after loading is complete."""
126
127         self.name = str(self.name)
128         self.suffixes = set(self.suffixes)
129         self.environment = dict(self.environment)
130         self.substitutions = list(self.substitutions)
131         if self.test_exec_root is not None:
132             # FIXME: This should really only be suite in test suite config
133             # files. Should we distinguish them?
134             self.test_exec_root = str(self.test_exec_root)
135         if self.test_source_root is not None:
136             # FIXME: This should really only be suite in test suite config
137             # files. Should we distinguish them?
138             self.test_source_root = str(self.test_source_root)
139         self.excludes = set(self.excludes)
140
141     @property
142     def root(self):
143         """root attribute - The root configuration for the test suite."""
144         if self.parent is None:
145             return self
146         else:
147             return self.parent.root
148