Mercurial > ~astiob > upreckon > hgweb
annotate config.py @ 73:b071ef77377c
String keys in pointmap are no longer treated as iterables except in legacy mode
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Thu, 06 Jan 2011 19:58:45 +0200 |
parents | b9d5857f7b9a |
children | aea4fc87698a |
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 | |
27 defaults_problem = {'usegroups': False, | |
28 'maxtime': None, | |
29 'maxmemory': None, | |
30 'dummies': {}, | |
31 'testsexcluded': (), | |
32 'padtests': 0, | |
33 'paddummies': 0, | |
34 'taskweight': 100, | |
35 'pointmap': {}, | |
36 'stdio': False, | |
37 'dummyinname': '', | |
38 'dummyoutname': '', | |
39 'tester': None, | |
40 'maxexitcode': 0, | |
41 'inname': '', | |
42 'ansname': ''} | |
22 | 43 defaults_global = {'tasknames': None, |
44 'force_zero_exitcode': True} | |
43 | 45 defaults_noerase = {'inname': '%.in', |
46 'outname': '%.out', | |
47 'ansname': '%.ans'} | |
21 | 48 patterns = ('inname', 'outname', 'ansname', 'testcaseinname', |
49 'testcaseoutname', 'dummyinname', 'dummyoutname') | |
50 | |
51 class Config(object): | |
52 __slots__ = 'modules', '__dict__' | |
53 | |
54 def __init__(self, *modules): | |
55 self.modules = modules | |
56 | |
57 def __getattr__(self, name): | |
58 for module in self.modules: | |
59 try: | |
60 return getattr(module, name) | |
61 except AttributeError: | |
62 pass | |
63 # TODO: provide a message | |
64 raise AttributeError(name) | |
16 | 65 |
22 | 66 # A helper context manager |
67 class ReadDeleting(object): | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
68 __slots__ = 'name', 'file' |
22 | 69 |
70 def __init__(self, name): | |
71 self.name = name | |
72 | |
73 def __enter__(self): | |
74 try: | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
75 self.file = open(self.name, 'rU') |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
76 return self.file |
22 | 77 except: |
78 try: | |
79 self.__exit__(None, None, None) | |
80 except: | |
81 pass | |
82 raise | |
83 | |
84 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
|
85 self.file.close() |
22 | 86 os.remove(self.name) |
87 | |
21 | 88 def load_problem(problem_name): |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
89 global builtins |
21 | 90 dwb = sys.dont_write_bytecode |
91 sys.dont_write_bytecode = True | |
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 | |
60
7c6dba0b84f2
Restored support for iterable keys in the pointmap configuration variable
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
141 newpointmap = {} |
7c6dba0b84f2
Restored support for iterable keys in the pointmap configuration variable
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 module.pointmap = newpointmap |
21 | 152 if options.no_maxtime: |
153 module.maxtime = 0 | |
154 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
|
155 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
|
156 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
|
157 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
|
158 return module |
21 | 159 |
160 def load_global(): | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
161 global builtins |
21 | 162 dwb = sys.dont_write_bytecode |
163 sys.dont_write_bytecode = True | |
164 metafile = files.File('testconf.py', True, 'configuration') | |
165 module = None | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
166 with CompatBuiltins() as builtins: |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
167 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
168 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
169 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
|
170 except zipimport.ZipImportError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
171 pass |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
172 else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
173 del sys.modules['testconf'] |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
174 if not module: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
175 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
176 with metafile.open() as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
177 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
|
178 # 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
|
179 except ValueError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
180 # FIXME: 2.5 lacks the delete parameter |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
181 with tempfile.NamedTemporaryFile(delete=False) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
182 inputdatafname = f.name |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
183 metafile.copy(inputdatafname) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
184 with ReadDeleting(inputdatafname) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
185 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
21 | 186 del sys.modules['testconf'] |
187 for name in defaults_global: | |
188 setattr(module, name, getattr(module, name, defaults_global[name])) | |
43 | 189 if not options.erase: |
190 for name in defaults_noerase: | |
191 setattr(module, name, getattr(module, name, defaults_noerase[name])) | |
21 | 192 global globalconf |
193 globalconf = module | |
194 sys.dont_write_bytecode = dwb | |
195 return module |