Mercurial > ~astiob > upreckon > hgweb
annotate upreckon/config.py @ 171:161d78f52cc3 2.01
Fixed crashing on callable values of the tester conf. var.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Sat, 18 Jun 2011 01:57:35 +0100 |
parents | 43e1f2d5d5a4 |
children |
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 | |
146
d5b6708c1955
Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents:
107
diff
changeset
|
5 from .compat import * |
d5b6708c1955
Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents:
107
diff
changeset
|
6 from . import files |
151
6261eea8a975
Positional cmdline arguments now override testconf.tests and disable groups
Oleg Oshmyan <chortos@inbox.lv>
parents:
150
diff
changeset
|
7 from __main__ import options, args |
21 | 8 |
9 if files.ZipArchive: | |
10 try: | |
11 import zipimport | |
12 except ImportError: | |
13 zipimport = None | |
14 else: | |
15 zipimport = None | |
16 | |
150
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
17 import imp, os, posixpath, sys, tempfile |
21 | 18 |
150
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
19 __all__ = 'load_problem', 'load_global', 'globalconf', 'nativize_path' |
21 | 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, |
96
c3afa2b0c14c
Fixed a typo (did not affect anything)
Oleg Oshmyan <chortos@inbox.lv>
parents:
92
diff
changeset
|
26 'dummies': (), |
21 | 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 | |
150
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
85 def nativize_path(portable_path): |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
86 if portable_path.startswith('//:'): |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
87 return portable_path[3:] |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
88 comps = portable_path.split('/') |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
89 for i, comp in enumerate(comps): |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
90 if comp == '..': |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
91 comps[i] = os.path.pardir |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
92 elif comp == '.': |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
93 comps[i] = os.path.curdir |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
94 native_path = os.path.join(*comps) |
156
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
95 if posixpath.isabs(portable_path) and not os.path.isabs(native_path): |
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
96 abspath = os.path.abspath(native_path) |
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
97 parent = os.path.dirname(abspath) |
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
98 while parent != abspath: |
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
99 abspath, parent = parent, os.path.dirname(parent) |
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
100 native_path = os.path.join(parent, native_path) |
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
101 elif not posixpath.isabs(portable_path) and os.path.isabs(native_path): |
150
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
102 native_path = os.path.sep + native_path |
156
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
103 if posixpath.isabs(portable_path) != os.path.isabs(native_path): |
2db236d25ecd
Improved the nativization of absolute paths by config.nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
152
diff
changeset
|
104 raise ValueError('cannot make native path relative/absolute') |
150
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
105 return native_path |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
106 |
21 | 107 def load_problem(problem_name): |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
108 global builtins |
83 | 109 try: |
110 dwb = sys.dont_write_bytecode | |
111 sys.dont_write_bytecode = True | |
112 except AttributeError: | |
113 pass | |
150
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
114 metafile = files.File(problem_name + '/testconf.py', True, 'configuration') |
21 | 115 module = None |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
116 with CompatBuiltins() as builtins: |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
117 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
118 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
119 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
|
120 except zipimport.ZipImportError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
121 pass |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
122 else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
123 del sys.modules['testconf'] |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
124 if not module: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
125 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
126 with metafile.open() as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
127 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
|
128 # 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
|
129 except ValueError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
130 # FIXME: 2.5 lacks the delete parameter |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
131 with tempfile.NamedTemporaryFile(delete=False) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
132 inputdatafname = f.name |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
133 metafile.copy(inputdatafname) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
134 with ReadDeleting(inputdatafname) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
135 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
21 | 136 del sys.modules['testconf'] |
107
6589511f5418
Fixed mishandling of some variables in multi-problem legacy configurations
Oleg Oshmyan <chortos@inbox.lv>
parents:
96
diff
changeset
|
137 module = Config(module, globalconf) |
21 | 138 if hasattr(module, 'padwithzeroestolength'): |
139 if not hasattr(module, 'padtests'): | |
140 try: | |
141 module.padtests = module.padwithzeroestolength[0] | |
142 except TypeError: | |
143 module.padtests = module.padwithzeroestolength | |
144 if not hasattr(module, 'paddummies'): | |
145 try: | |
146 module.paddummies = module.padwithzeroestolength[1] | |
147 except TypeError: | |
148 module.paddummies = module.padwithzeroestolength | |
92
17041a71bc02
maxtime is now auto-converted to maxcputime
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
149 if (not hasattr(module, 'maxcputime') and |
17041a71bc02
maxtime is now auto-converted to maxcputime
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
150 not hasattr(module, 'maxwalltime') and |
17041a71bc02
maxtime is now auto-converted to maxcputime
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
151 hasattr(module, 'maxtime')): |
17041a71bc02
maxtime is now auto-converted to maxcputime
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
152 module.maxcputime = module.maxtime |
21 | 153 for name in defaults_problem: |
107
6589511f5418
Fixed mishandling of some variables in multi-problem legacy configurations
Oleg Oshmyan <chortos@inbox.lv>
parents:
96
diff
changeset
|
154 setattr(module, name, getattr(module, name, defaults_problem[name])) |
27 | 155 if not module.dummyinname: |
156 module.dummyinname = getattr(module, 'testcaseinname', module.dummyinname) | |
157 if not module.dummyoutname: | |
158 module.dummyoutname = getattr(module, 'testcaseoutname', module.dummyoutname) | |
150
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
159 if hasattr(module, 'testee'): |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
160 if isinstance(module.testee, basestring): |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
161 module.path = nativize_path(module.testee) |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
162 else: |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
163 testee = tuple(module.testee) |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
164 module.path = (nativize_path(testee[0]),) + testee[1:] |
006dce02752c
Added the testee configuration variable and the config.nativize_path function
Oleg Oshmyan <chortos@inbox.lv>
parents:
146
diff
changeset
|
165 elif not hasattr(module, 'path'): |
21 | 166 if hasattr(module, 'name'): |
167 module.path = module.name | |
168 elif sys.platform != 'win32': | |
169 module.path = os.path.join(os.path.curdir, problem_name) | |
170 else: | |
171 module.path = problem_name | |
161
43e1f2d5d5a4
The first/only item of testconf.tester is now passed through nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
156
diff
changeset
|
172 if module.tester: |
43e1f2d5d5a4
The first/only item of testconf.tester is now passed through nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
156
diff
changeset
|
173 if isinstance(module.tester, basestring): |
43e1f2d5d5a4
The first/only item of testconf.tester is now passed through nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
156
diff
changeset
|
174 module.tester = nativize_path(module.tester) |
171
161d78f52cc3
Fixed crashing on callable values of the tester conf. var.
Oleg Oshmyan <chortos@inbox.lv>
parents:
161
diff
changeset
|
175 elif not callable(module.tester): |
161
43e1f2d5d5a4
The first/only item of testconf.tester is now passed through nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
156
diff
changeset
|
176 tester = tuple(module.tester) |
43e1f2d5d5a4
The first/only item of testconf.tester is now passed through nativize_path
Oleg Oshmyan <chortos@inbox.lv>
parents:
156
diff
changeset
|
177 module.tester = (nativize_path(tester[0]),) + tester[1:] |
76
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
178 for name in 'pointmap', 'groupweight': |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
179 oldmap = getattr(module, name) |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
180 if isinstance(oldmap, dict): |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
181 newmap = {} |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
182 for key in oldmap: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
183 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
|
184 newmap[key] = oldmap[key] |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
185 else: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
186 try: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
187 for k in key: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
188 newmap[k] = oldmap[key] |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
189 except TypeError: |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
190 newmap[key] = oldmap[key] |
0e5ae28e0b2b
Points are now weighted on a test context basis
Oleg Oshmyan <chortos@inbox.lv>
parents:
74
diff
changeset
|
191 setattr(module, name, newmap) |
21 | 192 if options.no_maxtime: |
82
06356af50bf9
Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
79
diff
changeset
|
193 module.maxcputime = module.maxwalltime = 0 |
151
6261eea8a975
Positional cmdline arguments now override testconf.tests and disable groups
Oleg Oshmyan <chortos@inbox.lv>
parents:
150
diff
changeset
|
194 if args: |
6261eea8a975
Positional cmdline arguments now override testconf.tests and disable groups
Oleg Oshmyan <chortos@inbox.lv>
parents:
150
diff
changeset
|
195 module.usegroups = False |
6261eea8a975
Positional cmdline arguments now override testconf.tests and disable groups
Oleg Oshmyan <chortos@inbox.lv>
parents:
150
diff
changeset
|
196 module.tests = args |
152
7951219d9866
Dummies are no longer tested when positional cmdline arguments are given
Oleg Oshmyan <chortos@inbox.lv>
parents:
151
diff
changeset
|
197 module.dummies = () |
83 | 198 try: |
199 sys.dont_write_bytecode = dwb | |
200 except NameError: | |
201 pass | |
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
202 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
|
203 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
|
204 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
|
205 return module |
21 | 206 |
207 def load_global(): | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
208 global builtins |
83 | 209 try: |
210 dwb = sys.dont_write_bytecode | |
211 sys.dont_write_bytecode = True | |
212 except AttributeError: | |
213 pass | |
21 | 214 metafile = files.File('testconf.py', True, 'configuration') |
215 module = None | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
60
diff
changeset
|
216 with CompatBuiltins() as builtins: |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
217 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
218 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
219 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
|
220 except zipimport.ZipImportError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
221 pass |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
222 else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
223 del sys.modules['testconf'] |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
224 if not module: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
225 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
226 with metafile.open() as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
227 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
|
228 # 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
|
229 except ValueError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
230 # FIXME: 2.5 lacks the delete parameter |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
231 with tempfile.NamedTemporaryFile(delete=False) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
232 inputdatafname = f.name |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
233 metafile.copy(inputdatafname) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
234 with ReadDeleting(inputdatafname) as f: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
235 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
21 | 236 del sys.modules['testconf'] |
237 for name in defaults_global: | |
238 setattr(module, name, getattr(module, name, defaults_global[name])) | |
43 | 239 if not options.erase: |
240 for name in defaults_noerase: | |
241 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
|
242 if hasattr(module, 'tasknames'): |
ee8a99dcaaed
Renamed configuration variable tasknames to problems
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
243 module.problems = module.tasknames |
21 | 244 global globalconf |
245 globalconf = module | |
83 | 246 try: |
247 sys.dont_write_bytecode = dwb | |
248 except NameError: | |
249 pass | |
21 | 250 return module |