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