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