Mercurial > ~astiob > upreckon > hgweb
annotate config.py @ 74:aea4fc87698a
Minimal code clean-up
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Thu, 06 Jan 2011 20:06:42 +0200 |
| parents | b071ef77377c |
| children | 0e5ae28e0b2b |
| rev | line source |
|---|---|
| 21 | 1 #! /usr/bin/env python |
| 16 | 2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> |
| 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, | |
| 21 | 29 'maxtime': None, |
| 30 'maxmemory': None, | |
| 31 'dummies': {}, | |
| 32 'testsexcluded': (), | |
| 33 'padtests': 0, | |
| 34 'paddummies': 0, | |
| 35 'taskweight': 100, | |
| 36 'pointmap': {}, | |
| 37 'stdio': False, | |
| 38 'dummyinname': '', | |
| 39 'dummyoutname': '', | |
| 40 'tester': None, | |
| 41 'maxexitcode': 0, | |
| 42 'inname': '', | |
| 43 'ansname': ''} | |
| 22 | 44 defaults_global = {'tasknames': None, |
| 45 'force_zero_exitcode': True} | |
| 43 | 46 defaults_noerase = {'inname': '%.in', |
| 47 'outname': '%.out', | |
| 48 'ansname': '%.ans'} | |
| 21 | 49 patterns = ('inname', 'outname', 'ansname', 'testcaseinname', |
| 50 'testcaseoutname', 'dummyinname', 'dummyoutname') | |
| 51 | |
| 52 class Config(object): | |
| 53 __slots__ = 'modules', '__dict__' | |
| 54 | |
| 55 def __init__(self, *modules): | |
| 56 self.modules = modules | |
| 57 | |
| 58 def __getattr__(self, name): | |
| 59 for module in self.modules: | |
| 60 try: | |
| 61 return getattr(module, name) | |
| 62 except AttributeError: | |
| 63 pass | |
| 64 # TODO: provide a message | |
| 65 raise AttributeError(name) | |
| 16 | 66 |
| 22 | 67 # A helper context manager |
| 68 class ReadDeleting(object): | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
69 __slots__ = 'name', 'file' |
| 22 | 70 |
| 71 def __init__(self, name): | |
| 72 self.name = name | |
| 73 | |
| 74 def __enter__(self): | |
| 75 try: | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
76 self.file = open(self.name, 'rU') |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
77 return self.file |
| 22 | 78 except: |
| 79 try: | |
| 80 self.__exit__(None, None, None) | |
| 81 except: | |
| 82 pass | |
| 83 raise | |
| 84 | |
| 85 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
|
86 self.file.close() |
| 22 | 87 os.remove(self.name) |
| 88 | |
| 21 | 89 def load_problem(problem_name): |
|
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
90 global builtins |
| 21 | 91 dwb = sys.dont_write_bytecode |
| 92 sys.dont_write_bytecode = True | |
| 93 metafile = files.File('/'.join((problem_name, 'testconf.py')), True, 'configuration') | |
| 94 module = None | |
|
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
95 with CompatBuiltins() as builtins: |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
96 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
97 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
98 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
|
99 except zipimport.ZipImportError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
100 pass |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
101 else: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
102 del sys.modules['testconf'] |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
103 if not module: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
104 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
105 with metafile.open() as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
106 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
|
107 # 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
|
108 except ValueError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
109 # FIXME: 2.5 lacks the delete parameter |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
110 with tempfile.NamedTemporaryFile(delete=False) as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
111 inputdatafname = f.name |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
112 metafile.copy(inputdatafname) |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
113 with ReadDeleting(inputdatafname) as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
114 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
| 21 | 115 del sys.modules['testconf'] |
| 116 if hasattr(module, 'padwithzeroestolength'): | |
| 117 if not hasattr(module, 'padtests'): | |
| 118 try: | |
| 119 module.padtests = module.padwithzeroestolength[0] | |
| 120 except TypeError: | |
| 121 module.padtests = module.padwithzeroestolength | |
| 122 if not hasattr(module, 'paddummies'): | |
| 123 try: | |
| 124 module.paddummies = module.padwithzeroestolength[1] | |
| 125 except TypeError: | |
| 126 module.paddummies = module.padwithzeroestolength | |
| 127 for name in defaults_problem: | |
| 128 if not hasattr(globalconf, name): | |
| 129 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
|
130 module = Config(module, globalconf) |
| 27 | 131 if not module.dummyinname: |
| 132 module.dummyinname = getattr(module, 'testcaseinname', module.dummyinname) | |
| 133 if not module.dummyoutname: | |
| 134 module.dummyoutname = getattr(module, 'testcaseoutname', module.dummyoutname) | |
| 21 | 135 if not hasattr(module, 'path'): |
| 136 if hasattr(module, 'name'): | |
| 137 module.path = module.name | |
| 138 elif sys.platform != 'win32': | |
| 139 module.path = os.path.join(os.path.curdir, problem_name) | |
| 140 else: | |
| 141 module.path = problem_name | |
|
60
7c6dba0b84f2
Restored support for iterable keys in the pointmap configuration variable
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
142 newpointmap = {} |
|
7c6dba0b84f2
Restored support for iterable keys in the pointmap configuration variable
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
143 for key in module.pointmap: |
|
73
b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
Oleg Oshmyan <chortos@inbox.lv>
parents:
70
diff
changeset
|
144 if not options.legacy and isinstance(key, basestring): |
|
60
7c6dba0b84f2
Restored support for iterable keys in the pointmap configuration variable
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
145 newpointmap[key] = module.pointmap[key] |
|
73
b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
Oleg Oshmyan <chortos@inbox.lv>
parents:
70
diff
changeset
|
146 else: |
|
b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
Oleg Oshmyan <chortos@inbox.lv>
parents:
70
diff
changeset
|
147 try: |
|
b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
Oleg Oshmyan <chortos@inbox.lv>
parents:
70
diff
changeset
|
148 for k in key: |
|
b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
Oleg Oshmyan <chortos@inbox.lv>
parents:
70
diff
changeset
|
149 newpointmap[k] = module.pointmap[key] |
|
b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
Oleg Oshmyan <chortos@inbox.lv>
parents:
70
diff
changeset
|
150 except TypeError: |
|
b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
Oleg Oshmyan <chortos@inbox.lv>
parents:
70
diff
changeset
|
151 newpointmap[key] = module.pointmap[key] |
|
60
7c6dba0b84f2
Restored support for iterable keys in the pointmap configuration variable
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
152 module.pointmap = newpointmap |
| 21 | 153 if options.no_maxtime: |
| 154 module.maxtime = 0 | |
| 155 sys.dont_write_bytecode = dwb | |
|
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 return module |
| 21 | 160 |
| 161 def load_global(): | |
|
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
162 global builtins |
| 21 | 163 dwb = sys.dont_write_bytecode |
| 164 sys.dont_write_bytecode = True | |
| 165 metafile = files.File('testconf.py', True, 'configuration') | |
| 166 module = None | |
|
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
167 with CompatBuiltins() as builtins: |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
168 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
169 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
170 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
|
171 except zipimport.ZipImportError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
172 pass |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
173 else: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
174 del sys.modules['testconf'] |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
175 if not module: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
176 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
177 with metafile.open() as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
178 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
|
179 # 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
|
180 except ValueError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
181 # FIXME: 2.5 lacks the delete parameter |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
182 with tempfile.NamedTemporaryFile(delete=False) as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
183 inputdatafname = f.name |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
184 metafile.copy(inputdatafname) |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
185 with ReadDeleting(inputdatafname) 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)) |
| 21 | 187 del sys.modules['testconf'] |
| 188 for name in defaults_global: | |
| 189 setattr(module, name, getattr(module, name, defaults_global[name])) | |
| 43 | 190 if not options.erase: |
| 191 for name in defaults_noerase: | |
| 192 setattr(module, name, getattr(module, name, defaults_noerase[name])) | |
| 21 | 193 global globalconf |
| 194 globalconf = module | |
| 195 sys.dont_write_bytecode = dwb | |
| 196 return module |
