Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/config.py @ 42:44609b41868b
Added .hgignore for pyc files
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Sun, 05 Dec 2010 15:29:17 +0100 |
| parents | a6d554679ce8 |
| children | 81f58c938ec5 |
| 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} | |
| 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): |
| 86 dwb = sys.dont_write_bytecode | |
| 87 sys.dont_write_bytecode = True | |
| 88 metafile = files.File('/'.join((problem_name, 'testconf.py')), True, 'configuration') | |
| 89 module = None | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
90 with CompatBuiltins(): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
91 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
92 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
93 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
|
94 except zipimport.ZipImportError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
95 pass |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
96 else: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
97 del sys.modules['testconf'] |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
98 if not module: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
99 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
100 with metafile.open() as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
101 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
|
102 # 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
|
103 except ValueError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
104 # FIXME: 2.5 lacks the delete parameter |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
105 with tempfile.NamedTemporaryFile(delete=False) as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
106 inputdatafname = f.name |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
107 metafile.copy(inputdatafname) |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
108 with ReadDeleting(inputdatafname) as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
109 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
| 21 | 110 del sys.modules['testconf'] |
| 111 if hasattr(module, 'padwithzeroestolength'): | |
| 112 if not hasattr(module, 'padtests'): | |
| 113 try: | |
| 114 module.padtests = module.padwithzeroestolength[0] | |
| 115 except TypeError: | |
| 116 module.padtests = module.padwithzeroestolength | |
| 117 if not hasattr(module, 'paddummies'): | |
| 118 try: | |
| 119 module.paddummies = module.padwithzeroestolength[1] | |
| 120 except TypeError: | |
| 121 module.paddummies = module.padwithzeroestolength | |
| 122 for name in defaults_problem: | |
| 123 if not hasattr(globalconf, name): | |
| 124 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
|
125 module = Config(module, globalconf) |
| 27 | 126 if not module.dummyinname: |
| 127 module.dummyinname = getattr(module, 'testcaseinname', module.dummyinname) | |
| 128 if not module.dummyoutname: | |
| 129 module.dummyoutname = getattr(module, 'testcaseoutname', module.dummyoutname) | |
| 21 | 130 if not hasattr(module, 'path'): |
| 131 if hasattr(module, 'name'): | |
| 132 module.path = module.name | |
| 133 elif sys.platform != 'win32': | |
| 134 module.path = os.path.join(os.path.curdir, problem_name) | |
| 135 else: | |
| 136 module.path = problem_name | |
| 137 if options.no_maxtime: | |
| 138 module.maxtime = 0 | |
| 139 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
|
140 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
|
141 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
|
142 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
|
143 return module |
| 21 | 144 |
| 145 def load_global(): | |
| 146 dwb = sys.dont_write_bytecode | |
| 147 sys.dont_write_bytecode = True | |
| 148 metafile = files.File('testconf.py', True, 'configuration') | |
| 149 module = None | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
150 with CompatBuiltins(): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
151 if zipimport and isinstance(metafile.archive, files.ZipArchive): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
152 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
153 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
|
154 except zipimport.ZipImportError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
155 pass |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
156 else: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
157 del sys.modules['testconf'] |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
158 if not module: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
159 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
160 with metafile.open() as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
161 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
|
162 # 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
|
163 except ValueError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
164 # FIXME: 2.5 lacks the delete parameter |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
165 with tempfile.NamedTemporaryFile(delete=False) as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
166 inputdatafname = f.name |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
167 metafile.copy(inputdatafname) |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
168 with ReadDeleting(inputdatafname) as f: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
169 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
| 21 | 170 del sys.modules['testconf'] |
| 171 for name in defaults_global: | |
| 172 setattr(module, name, getattr(module, name, defaults_global[name])) | |
| 173 global globalconf | |
| 174 globalconf = module | |
| 175 sys.dont_write_bytecode = dwb | |
| 176 return module |
