Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/config.py @ 24:c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
TestCase.__call__() now returns the fraction (a number from 0 to 1) of case.points that is to be awarded.
Bug fix: %-patterns in configuration variables common to all problems are now substituted.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Thu, 23 Sep 2010 00:11:24 +0000 |
parents | f07b7a431ea6 |
children | b500e117080e |
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: | |
7 import files | |
8 except ImportError: | |
9 import __main__ | |
10 __main__.import_error(sys.exc_info()[1]) | |
11 else: | |
12 from __main__ import options | |
13 | |
14 if files.ZipArchive: | |
15 try: | |
16 import zipimport | |
17 except ImportError: | |
18 zipimport = None | |
19 else: | |
20 zipimport = None | |
21 | |
22 | 22 import imp, os, sys, tempfile |
21 | 23 |
24 __all__ = 'load_problem', 'load_global', 'globalconf' | |
25 | |
26 defaults_problem = {'usegroups': False, | |
27 'maxtime': None, | |
28 'maxmemory': None, | |
29 'dummies': {}, | |
30 'testsexcluded': (), | |
31 'padtests': 0, | |
32 'paddummies': 0, | |
33 'taskweight': 100, | |
34 'pointmap': {}, | |
35 'stdio': False, | |
36 'dummyinname': '', | |
37 'dummyoutname': '', | |
38 'tester': None, | |
39 'maxexitcode': 0, | |
40 'inname': '', | |
41 'ansname': ''} | |
22 | 42 defaults_global = {'tasknames': None, |
43 'force_zero_exitcode': True} | |
21 | 44 patterns = ('inname', 'outname', 'ansname', 'testcaseinname', |
45 'testcaseoutname', 'dummyinname', 'dummyoutname') | |
46 | |
47 class Config(object): | |
48 __slots__ = 'modules', '__dict__' | |
49 | |
50 def __init__(self, *modules): | |
51 self.modules = modules | |
52 | |
53 def __getattr__(self, name): | |
54 for module in self.modules: | |
55 try: | |
56 return getattr(module, name) | |
57 except AttributeError: | |
58 pass | |
59 # TODO: provide a message | |
60 raise AttributeError(name) | |
16 | 61 |
22 | 62 # A helper context manager |
63 class ReadDeleting(object): | |
64 __slots__ = 'name' | |
65 | |
66 def __init__(self, name): | |
67 self.name = name | |
68 | |
69 def __enter__(self): | |
70 try: | |
71 return open(self.name, 'rU') | |
72 except: | |
73 try: | |
74 self.__exit__(None, None, None) | |
75 except: | |
76 pass | |
77 raise | |
78 | |
79 def __exit__(self, exc_type, exc_val, exc_tb): | |
80 os.remove(self.name) | |
81 | |
21 | 82 def load_problem(problem_name): |
83 dwb = sys.dont_write_bytecode | |
84 sys.dont_write_bytecode = True | |
85 metafile = files.File('/'.join((problem_name, 'testconf.py')), True, 'configuration') | |
86 module = None | |
87 if zipimport and isinstance(metafile.archive, files.ZipArchive): | |
88 try: | |
89 module = zipimport.zipimporter(os.path.dirname(metafile.full_real_path)).load_module('testconf') | |
90 except zipimport.ZipImportError: | |
91 pass | |
92 else: | |
93 del sys.modules['testconf'] | |
94 if not module: | |
22 | 95 try: |
96 with metafile.open() as f: | |
97 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) | |
98 # Handle the case when f is not a true file object but imp requires one | |
99 except ValueError: | |
100 # FIXME: 2.5 lacks the delete parameter | |
101 with tempfile.NamedTemporaryFile(delete=False) as f: | |
102 inputdatafname = f.name | |
103 metafile.extract(inputdatafname) | |
104 with ReadDeleting(inputdatafname) as f: | |
105 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) | |
106 del sys.modules['testconf'] | |
21 | 107 if hasattr(module, 'padwithzeroestolength'): |
108 if not hasattr(module, 'padtests'): | |
109 try: | |
110 module.padtests = module.padwithzeroestolength[0] | |
111 except TypeError: | |
112 module.padtests = module.padwithzeroestolength | |
113 if not hasattr(module, 'paddummies'): | |
114 try: | |
115 module.paddummies = module.padwithzeroestolength[1] | |
116 except TypeError: | |
117 module.paddummies = module.padwithzeroestolength | |
118 for name in defaults_problem: | |
119 if not hasattr(globalconf, name): | |
120 setattr(module, name, getattr(module, name, defaults_problem[name])) | |
121 if not hasattr(module, 'path'): | |
122 if hasattr(module, 'name'): | |
123 module.path = module.name | |
124 elif sys.platform != 'win32': | |
125 module.path = os.path.join(os.path.curdir, problem_name) | |
126 else: | |
127 module.path = problem_name | |
128 if options.no_maxtime: | |
129 module.maxtime = 0 | |
130 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
|
131 module = Config(module, globalconf) |
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 return module |
21 | 136 |
137 def load_global(): | |
138 dwb = sys.dont_write_bytecode | |
139 sys.dont_write_bytecode = True | |
140 metafile = files.File('testconf.py', True, 'configuration') | |
141 module = None | |
142 if zipimport and isinstance(metafile.archive, files.ZipArchive): | |
143 try: | |
144 module = zipimport.zipimporter(os.path.dirname(metafile.full_real_path)).load_module('testconf') | |
145 except zipimport.ZipImportError: | |
146 pass | |
147 else: | |
148 del sys.modules['testconf'] | |
149 if not module: | |
22 | 150 try: |
151 with metafile.open() as f: | |
152 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) | |
153 # Handle the case when f is not a true file object but imp requires one | |
154 except ValueError: | |
155 # FIXME: 2.5 lacks the delete parameter | |
156 with tempfile.NamedTemporaryFile(delete=False) as f: | |
157 inputdatafname = f.name | |
158 metafile.extract(inputdatafname) | |
159 with ReadDeleting(inputdatafname) as f: | |
160 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) | |
161 del sys.modules['testconf'] | |
21 | 162 for name in defaults_global: |
163 setattr(module, name, getattr(module, name, defaults_global[name])) | |
164 global globalconf | |
165 globalconf = module | |
166 sys.dont_write_bytecode = dwb | |
167 return module |