Mercurial > ~astiob > upreckon > hgweb
annotate config.py @ 89:3ae6cb69e4ef
Sample total is no longer printed if there are no samples
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Mon, 28 Feb 2011 15:10:40 +0000 |
parents | cd347cfca272 |
children | c62c9bfd614a |
rev | line source |
---|---|
78 | 1 # Copyright (c) 2010-2011 Chortos-2 <chortos@inbox.lv> |
16 | 2 |
21 | 3 from __future__ import division, with_statement |
4 | |
5 try: | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
6 from compat import * |
21 | 7 import files |
8 except ImportError: | |
9 import __main__ | |
10 __main__.import_error(sys.exc_info()[1]) | |
11 else: | |
12 from __main__ import options | |
13 | |
14 if files.ZipArchive: | |
15 try: | |
16 import zipimport | |
17 except ImportError: | |
18 zipimport = None | |
19 else: | |
20 zipimport = None | |
21 | |
22 | 22 import imp, os, sys, tempfile |
21 | 23 |
24 __all__ = 'load_problem', 'load_global', 'globalconf' | |
25 | |
74 | 26 defaults_problem = {'kind': 'batch', |
27 'usegroups': False, | |
82
06356af50bf9
Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
79
diff
changeset
|
28 'maxcputime': None, |
06356af50bf9
Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
79
diff
changeset
|
29 'maxwalltime': None, |
21 | 30 'maxmemory': None, |
31 'dummies': {}, | |
32 'testsexcluded': (), | |
33 'padtests': 0, | |
34 'paddummies': 0, | |
35 'taskweight': 100, | |
76
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
36 'groupweight': {}, |
21 | 37 'pointmap': {}, |
38 'stdio': False, | |
39 'dummyinname': '', | |
40 'dummyoutname': '', | |
41 'tester': None, | |
42 'maxexitcode': 0, | |
43 'inname': '', | |
44 'ansname': ''} | |
79
ee8a99dcaaed
Renamed configuration variable tasknames to problems
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
45 defaults_global = {'problems': None, |
22 | 46 'force_zero_exitcode': True} |
43 | 47 defaults_noerase = {'inname': '%.in', |
48 'outname': '%.out', | |
49 'ansname': '%.ans'} | |
21 | 50 patterns = ('inname', 'outname', 'ansname', 'testcaseinname', |
51 'testcaseoutname', 'dummyinname', 'dummyoutname') | |
52 | |
53 class Config(object): | |
54 __slots__ = 'modules', '__dict__' | |
55 | |
56 def __init__(self, *modules): | |
57 self.modules = modules | |
58 | |
59 def __getattr__(self, name): | |
60 for module in self.modules: | |
61 try: | |
62 return getattr(module, name) | |
63 except AttributeError: | |
64 pass | |
65 # TODO: provide a message | |
66 raise AttributeError(name) | |
16 | 67 |
22 | 68 # A helper context manager |
69 class ReadDeleting(object): | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
70 __slots__ = 'name', 'file' |
22 | 71 |
72 def __init__(self, name): | |
73 self.name = name | |
74 | |
75 def __enter__(self): | |
76 try: | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
77 self.file = open(self.name, 'rU') |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
78 return self.file |
22 | 79 except: |
80 try: | |
81 self.__exit__(None, None, None) | |
82 except: | |
83 pass | |
84 raise | |
85 | |
86 def __exit__(self, exc_type, exc_val, exc_tb): | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
87 self.file.close() |
22 | 88 os.remove(self.name) |
89 | |
21 | 90 def load_problem(problem_name): |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
91 global builtins |
83 | 92 try: |
93 dwb = sys.dont_write_bytecode | |
94 sys.dont_write_bytecode = True | |
95 except AttributeError: | |
96 pass | |
21 | 97 metafile = files.File('/'.join((problem_name, 'testconf.py')), True, 'configuration') |
98 module = None | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
99 with CompatBuiltins() as builtins: |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
100 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
101 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
102 module = zipimport.zipimporter(os.path.dirname(metafile.full_real_path)).load_module('testconf') |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
103 except zipimport.ZipImportError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
104 pass |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
105 else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
106 del sys.modules['testconf'] |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
107 if not module: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
108 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
109 with metafile.open() as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
110 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
111 # Handle the case when f is not a true file object but imp requires one |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
112 except ValueError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
113 # FIXME: 2.5 lacks the delete parameter |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
114 with tempfile.NamedTemporaryFile(delete=False) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
115 inputdatafname = f.name |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
116 metafile.copy(inputdatafname) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
117 with ReadDeleting(inputdatafname) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
118 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
21 | 119 del sys.modules['testconf'] |
120 if hasattr(module, 'padwithzeroestolength'): | |
121 if not hasattr(module, 'padtests'): | |
122 try: | |
123 module.padtests = module.padwithzeroestolength[0] | |
124 except TypeError: | |
125 module.padtests = module.padwithzeroestolength | |
126 if not hasattr(module, 'paddummies'): | |
127 try: | |
128 module.paddummies = module.padwithzeroestolength[1] | |
129 except TypeError: | |
130 module.paddummies = module.padwithzeroestolength | |
131 for name in defaults_problem: | |
132 if not hasattr(globalconf, name): | |
133 setattr(module, name, getattr(module, name, defaults_problem[name])) | |
38
a6d554679ce8
Fixed a bug with nested configuration namespaces in config.py
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
134 module = Config(module, globalconf) |
27 | 135 if not module.dummyinname: |
136 module.dummyinname = getattr(module, 'testcaseinname', module.dummyinname) | |
137 if not module.dummyoutname: | |
138 module.dummyoutname = getattr(module, 'testcaseoutname', module.dummyoutname) | |
21 | 139 if not hasattr(module, 'path'): |
140 if hasattr(module, 'name'): | |
141 module.path = module.name | |
142 elif sys.platform != 'win32': | |
143 module.path = os.path.join(os.path.curdir, problem_name) | |
144 else: | |
145 module.path = problem_name | |
76
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
146 for name in 'pointmap', 'groupweight': |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
147 oldmap = getattr(module, name) |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
148 if isinstance(oldmap, dict): |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
149 newmap = {} |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
150 for key in oldmap: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
151 if not options.legacy and isinstance(key, basestring): |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
152 newmap[key] = oldmap[key] |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
153 else: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
154 try: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
155 for k in key: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
156 newmap[k] = oldmap[key] |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
157 except TypeError: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
158 newmap[key] = oldmap[key] |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
159 setattr(module, name, newmap) |
21 | 160 if options.no_maxtime: |
82
06356af50bf9
Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
79
diff
changeset
|
161 module.maxcputime = module.maxwalltime = 0 |
83 | 162 try: |
163 sys.dont_write_bytecode = dwb | |
164 except NameError: | |
165 pass | |
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
166 for name in patterns: |
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
167 if hasattr(module, name): |
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
168 setattr(module, name, getattr(module, name).replace('%', problem_name)) |
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
169 return module |
21 | 170 |
171 def load_global(): | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
172 global builtins |
83 | 173 try: |
174 dwb = sys.dont_write_bytecode | |
175 sys.dont_write_bytecode = True | |
176 except AttributeError: | |
177 pass | |
21 | 178 metafile = files.File('testconf.py', True, 'configuration') |
179 module = None | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
180 with CompatBuiltins() as builtins: |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
181 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
182 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
183 module = zipimport.zipimporter(os.path.dirname(metafile.full_real_path)).load_module('testconf') |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
184 except zipimport.ZipImportError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
185 pass |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
186 else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
187 del sys.modules['testconf'] |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
188 if not module: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
189 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
190 with metafile.open() as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
191 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
192 # Handle the case when f is not a true file object but imp requires one |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
193 except ValueError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
194 # FIXME: 2.5 lacks the delete parameter |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
195 with tempfile.NamedTemporaryFile(delete=False) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
196 inputdatafname = f.name |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
197 metafile.copy(inputdatafname) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
198 with ReadDeleting(inputdatafname) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
199 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
21 | 200 del sys.modules['testconf'] |
201 for name in defaults_global: | |
202 setattr(module, name, getattr(module, name, defaults_global[name])) | |
43 | 203 if not options.erase: |
204 for name in defaults_noerase: | |
205 setattr(module, name, getattr(module, name, defaults_noerase[name])) | |
79
ee8a99dcaaed
Renamed configuration variable tasknames to problems
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
206 if hasattr(module, 'tasknames'): |
ee8a99dcaaed
Renamed configuration variable tasknames to problems
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
207 module.problems = module.tasknames |
21 | 208 global globalconf |
209 globalconf = module | |
83 | 210 try: |
211 sys.dont_write_bytecode = dwb | |
212 except NameError: | |
213 pass | |
21 | 214 return module |