95405e932441451ad5b8fdcd71a07b6fd76eeff3
[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 fromdefaults(litConfig):
13         """
14         fromdefaults(litConfig) -> TestingConfig
15
16         Create a TestingConfig object with default values.
17         """
18         # Set the environment based on the command line arguments.
19         environment = {
20             'LIBRARY_PATH' : os.environ.get('LIBRARY_PATH',''),
21             'LD_LIBRARY_PATH' : os.environ.get('LD_LIBRARY_PATH',''),
22             'PATH' : os.pathsep.join(litConfig.path +
23                                      [os.environ.get('PATH','')]),
24             'SYSTEMROOT' : os.environ.get('SYSTEMROOT',''),
25             'TERM' : os.environ.get('TERM',''),
26             'LLVM_DISABLE_CRASH_REPORT' : '1',
27             }
28
29         if sys.platform == 'win32':
30             environment.update({
31                     'INCLUDE' : os.environ.get('INCLUDE',''),
32                     'PATHEXT' : os.environ.get('PATHEXT',''),
33                     'PYTHONUNBUFFERED' : '1',
34                     'TEMP' : os.environ.get('TEMP',''),
35                     'TMP' : os.environ.get('TMP',''),
36                     })
37
38         # The option to preserve TEMP, TMP, and TMPDIR.
39         # This is intended to check how many temporary files would be generated
40         # (and be not cleaned up) in automated builders.
41         if 'LIT_PRESERVES_TMP' in os.environ:
42             environment.update({
43                     'TEMP' : os.environ.get('TEMP',''),
44                     'TMP' : os.environ.get('TMP',''),
45                     'TMPDIR' : os.environ.get('TMPDIR',''),
46                     })
47
48         # Set the default available features based on the LitConfig.
49         available_features = []
50         if litConfig.useValgrind:
51             available_features.append('valgrind')
52             if litConfig.valgrindLeakCheck:
53                 available_features.append('vg_leak')
54
55         return TestingConfig(None,
56                              name = '<unnamed>',
57                              suffixes = set(),
58                              test_format = None,
59                              environment = environment,
60                              substitutions = [],
61                              unsupported = False,
62                              test_exec_root = None,
63                              test_source_root = None,
64                              excludes = [],
65                              available_features = available_features,
66                              pipefail = True)
67
68     def load_from_path(self, path, litConfig):
69         """
70         load_from_path(path, litConfig)
71
72         Load the configuration module at the provided path into the given config
73         object.
74         """
75
76         # Load the config script data.
77         f = open(path)
78         try:
79             data = f.read()
80         except:
81             litConfig.fatal('unable to load config file: %r' % (path,))
82         f.close()
83
84         # Execute the config script to initialize the object.
85         cfg_globals = dict(globals())
86         cfg_globals['config'] = self
87         cfg_globals['lit_config'] = litConfig
88         cfg_globals['__file__'] = path
89         try:
90             if PY2:
91                 exec("exec data in cfg_globals")
92             else:
93                 exec(data, cfg_globals)
94             if litConfig.debug:
95                 litConfig.note('... loaded config %r' % path)
96         except SystemExit:
97             e = sys.exc_info()[1]
98             # We allow normal system exit inside a config file to just
99             # return control without error.
100             if e.args:
101                 raise
102         except:
103             import traceback
104             litConfig.fatal(
105                 'unable to parse config file %r, traceback: %s' % (
106                     path, traceback.format_exc()))
107
108         self.finish(litConfig)
109
110     def __init__(self, parent, name, suffixes, test_format,
111                  environment, substitutions, unsupported,
112                  test_exec_root, test_source_root, excludes,
113                  available_features, pipefail):
114         self.parent = parent
115         self.name = str(name)
116         self.suffixes = set(suffixes)
117         self.test_format = test_format
118         self.environment = dict(environment)
119         self.substitutions = list(substitutions)
120         self.unsupported = unsupported
121         self.test_exec_root = test_exec_root
122         self.test_source_root = test_source_root
123         self.excludes = set(excludes)
124         self.available_features = set(available_features)
125         self.pipefail = pipefail
126
127     def finish(self, litConfig):
128         """finish() - Finish this config object, after loading is complete."""
129
130         self.name = str(self.name)
131         self.suffixes = set(self.suffixes)
132         self.environment = dict(self.environment)
133         self.substitutions = list(self.substitutions)
134         if self.test_exec_root is not None:
135             # FIXME: This should really only be suite in test suite config
136             # files. Should we distinguish them?
137             self.test_exec_root = str(self.test_exec_root)
138         if self.test_source_root is not None:
139             # FIXME: This should really only be suite in test suite config
140             # files. Should we distinguish them?
141             self.test_source_root = str(self.test_source_root)
142         self.excludes = set(self.excludes)
143
144     @property
145     def root(self):
146         """root attribute - The root configuration for the test suite."""
147         if self.parent is None:
148             return self
149         else:
150             return self.parent.root
151