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