Mercurial > ~astiob > upreckon > hgweb
comparison 2.00/config.py @ 22:f07b7a431ea6
Further 2.00 work
Testconfs in all supported kinds of archives should now work.
Test runs are now cancelled by pressing Escape rather than Ctrl+C.
Improved time control.
Greatly improved temporary and helper file cleanup.
The pause configuration variable can now be a callable and is now processed using subprocess rather than system().
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Wed, 22 Sep 2010 22:01:56 +0000 |
| parents | ec6f1a132109 |
| children | c23d81f4a1a3 |
comparison
equal
deleted
inserted
replaced
| 21:ec6f1a132109 | 22:f07b7a431ea6 |
|---|---|
| 17 except ImportError: | 17 except ImportError: |
| 18 zipimport = None | 18 zipimport = None |
| 19 else: | 19 else: |
| 20 zipimport = None | 20 zipimport = None |
| 21 | 21 |
| 22 import imp, os, sys | 22 import imp, os, sys, tempfile |
| 23 | 23 |
| 24 __all__ = 'load_problem', 'load_global', 'globalconf' | 24 __all__ = 'load_problem', 'load_global', 'globalconf' |
| 25 | 25 |
| 26 defaults_problem = {'usegroups': False, | 26 defaults_problem = {'usegroups': False, |
| 27 'maxtime': None, | 27 'maxtime': None, |
| 37 'dummyoutname': '', | 37 'dummyoutname': '', |
| 38 'tester': None, | 38 'tester': None, |
| 39 'maxexitcode': 0, | 39 'maxexitcode': 0, |
| 40 'inname': '', | 40 'inname': '', |
| 41 'ansname': ''} | 41 'ansname': ''} |
| 42 defaults_global = {'tasknames': None, | |
| 43 'force_zero_exitcode': True} | |
| 42 patterns = ('inname', 'outname', 'ansname', 'testcaseinname', | 44 patterns = ('inname', 'outname', 'ansname', 'testcaseinname', |
| 43 'testcaseoutname', 'dummyinname', 'dummyoutname') | 45 'testcaseoutname', 'dummyinname', 'dummyoutname') |
| 44 defaults_global = {'tasknames': None, | |
| 45 'force_zero_exitcode': True} | |
| 46 | 46 |
| 47 class Config(object): | 47 class Config(object): |
| 48 __slots__ = 'modules', '__dict__' | 48 __slots__ = 'modules', '__dict__' |
| 49 | 49 |
| 50 def __init__(self, *modules): | 50 def __init__(self, *modules): |
| 57 except AttributeError: | 57 except AttributeError: |
| 58 pass | 58 pass |
| 59 # TODO: provide a message | 59 # TODO: provide a message |
| 60 raise AttributeError(name) | 60 raise AttributeError(name) |
| 61 | 61 |
| 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 | |
| 62 def load_problem(problem_name): | 82 def load_problem(problem_name): |
| 63 dwb = sys.dont_write_bytecode | 83 dwb = sys.dont_write_bytecode |
| 64 sys.dont_write_bytecode = True | 84 sys.dont_write_bytecode = True |
| 65 metafile = files.File('/'.join((problem_name, 'testconf.py')), True, 'configuration') | 85 metafile = files.File('/'.join((problem_name, 'testconf.py')), True, 'configuration') |
| 66 module = None | 86 module = None |
| 70 except zipimport.ZipImportError: | 90 except zipimport.ZipImportError: |
| 71 pass | 91 pass |
| 72 else: | 92 else: |
| 73 del sys.modules['testconf'] | 93 del sys.modules['testconf'] |
| 74 if not module: | 94 if not module: |
| 75 with metafile.open() as f: | 95 try: |
| 76 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) | 96 with metafile.open() as f: |
| 77 del sys.modules['testconf'] | 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'] | |
| 78 if hasattr(module, 'padwithzeroestolength'): | 107 if hasattr(module, 'padwithzeroestolength'): |
| 79 if not hasattr(module, 'padtests'): | 108 if not hasattr(module, 'padtests'): |
| 80 try: | 109 try: |
| 81 module.padtests = module.padwithzeroestolength[0] | 110 module.padtests = module.padwithzeroestolength[0] |
| 82 except TypeError: | 111 except TypeError: |
| 115 except zipimport.ZipImportError: | 144 except zipimport.ZipImportError: |
| 116 pass | 145 pass |
| 117 else: | 146 else: |
| 118 del sys.modules['testconf'] | 147 del sys.modules['testconf'] |
| 119 if not module: | 148 if not module: |
| 120 with metafile.open() as f: | 149 try: |
| 121 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) | 150 with metafile.open() as f: |
| 122 del sys.modules['testconf'] | 151 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) |
| 152 # Handle the case when f is not a true file object but imp requires one | |
| 153 except ValueError: | |
| 154 # FIXME: 2.5 lacks the delete parameter | |
| 155 with tempfile.NamedTemporaryFile(delete=False) as f: | |
| 156 inputdatafname = f.name | |
| 157 metafile.extract(inputdatafname) | |
| 158 with ReadDeleting(inputdatafname) as f: | |
| 159 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE)) | |
| 160 del sys.modules['testconf'] | |
| 123 for name in defaults_global: | 161 for name in defaults_global: |
| 124 setattr(module, name, getattr(module, name, defaults_global[name])) | 162 setattr(module, name, getattr(module, name, defaults_global[name])) |
| 125 global globalconf | 163 global globalconf |
| 126 globalconf = module | 164 globalconf = module |
| 127 sys.dont_write_bytecode = dwb | 165 sys.dont_write_bytecode = dwb |
