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 for name in patterns:
|
|
122 if hasattr(module, name):
|
|
123 setattr(module, name, getattr(module, name).replace('%', problem_name))
|
|
124 if not hasattr(module, 'path'):
|
|
125 if hasattr(module, 'name'):
|
|
126 module.path = module.name
|
|
127 elif sys.platform != 'win32':
|
|
128 module.path = os.path.join(os.path.curdir, problem_name)
|
|
129 else:
|
|
130 module.path = problem_name
|
|
131 if options.no_maxtime:
|
|
132 module.maxtime = 0
|
|
133 sys.dont_write_bytecode = dwb
|
|
134 return Config(module, globalconf)
|
|
135
|
|
136 def load_global():
|
|
137 dwb = sys.dont_write_bytecode
|
|
138 sys.dont_write_bytecode = True
|
|
139 metafile = files.File('testconf.py', True, 'configuration')
|
|
140 module = None
|
|
141 if zipimport and isinstance(metafile.archive, files.ZipArchive):
|
|
142 try:
|
|
143 module = zipimport.zipimporter(os.path.dirname(metafile.full_real_path)).load_module('testconf')
|
|
144 except zipimport.ZipImportError:
|
|
145 pass
|
|
146 else:
|
|
147 del sys.modules['testconf']
|
|
148 if not module:
|
22
|
149 try:
|
|
150 with metafile.open() as f:
|
|
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']
|
21
|
161 for name in defaults_global:
|
|
162 setattr(module, name, getattr(module, name, defaults_global[name]))
|
|
163 global globalconf
|
|
164 globalconf = module
|
|
165 sys.dont_write_bytecode = dwb
|
|
166 return module |