comparison 2.00/config.py @ 21:ec6f1a132109

A pretty usable version Test groups and testconfs in non-ZIP archives or ZIP archives with comments are not yet supported.
author Oleg Oshmyan <chortos@inbox.lv>
date Fri, 06 Aug 2010 15:39:29 +0000
parents f2279b7602d3
children f07b7a431ea6
comparison
equal deleted inserted replaced
20:5bfa23cd638d 21:ec6f1a132109
1 #!/usr/bin/python 1 #! /usr/bin/env python
2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> 2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv>
3 3
4 import os 4 from __future__ import division, with_statement
5 tasknames = (os.path.curdir,)
6 5
7 def load_problem(name): 6 try:
8 return object() 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 import imp, os, sys
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': ''}
42 patterns = ('inname', 'outname', 'ansname', 'testcaseinname',
43 'testcaseoutname', 'dummyinname', 'dummyoutname')
44 defaults_global = {'tasknames': None,
45 'force_zero_exitcode': True}
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)
61
62 def load_problem(problem_name):
63 dwb = sys.dont_write_bytecode
64 sys.dont_write_bytecode = True
65 metafile = files.File('/'.join((problem_name, 'testconf.py')), True, 'configuration')
66 module = None
67 if zipimport and isinstance(metafile.archive, files.ZipArchive):
68 try:
69 module = zipimport.zipimporter(os.path.dirname(metafile.full_real_path)).load_module('testconf')
70 except zipimport.ZipImportError:
71 pass
72 else:
73 del sys.modules['testconf']
74 if not module:
75 with metafile.open() as f:
76 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE))
77 del sys.modules['testconf']
78 if hasattr(module, 'padwithzeroestolength'):
79 if not hasattr(module, 'padtests'):
80 try:
81 module.padtests = module.padwithzeroestolength[0]
82 except TypeError:
83 module.padtests = module.padwithzeroestolength
84 if not hasattr(module, 'paddummies'):
85 try:
86 module.paddummies = module.padwithzeroestolength[1]
87 except TypeError:
88 module.paddummies = module.padwithzeroestolength
89 for name in defaults_problem:
90 if not hasattr(globalconf, name):
91 setattr(module, name, getattr(module, name, defaults_problem[name]))
92 for name in patterns:
93 if hasattr(module, name):
94 setattr(module, name, getattr(module, name).replace('%', problem_name))
95 if not hasattr(module, 'path'):
96 if hasattr(module, 'name'):
97 module.path = module.name
98 elif sys.platform != 'win32':
99 module.path = os.path.join(os.path.curdir, problem_name)
100 else:
101 module.path = problem_name
102 if options.no_maxtime:
103 module.maxtime = 0
104 sys.dont_write_bytecode = dwb
105 return Config(module, globalconf)
106
107 def load_global():
108 dwb = sys.dont_write_bytecode
109 sys.dont_write_bytecode = True
110 metafile = files.File('testconf.py', True, 'configuration')
111 module = None
112 if zipimport and isinstance(metafile.archive, files.ZipArchive):
113 try:
114 module = zipimport.zipimporter(os.path.dirname(metafile.full_real_path)).load_module('testconf')
115 except zipimport.ZipImportError:
116 pass
117 else:
118 del sys.modules['testconf']
119 if not module:
120 with metafile.open() as f:
121 module = imp.load_module('testconf', f, metafile.full_real_path, ('.py', 'r', imp.PY_SOURCE))
122 del sys.modules['testconf']
123 for name in defaults_global:
124 setattr(module, name, getattr(module, name, defaults_global[name]))
125 global globalconf
126 globalconf = module
127 sys.dont_write_bytecode = dwb
128 return module