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