Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/test-svn.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 | c1f52b5d80d6 |
children | b500e117080e |
rev | line source |
---|---|
21 | 1 #! /usr/bin/env python |
16 | 2 # Copyright (c) 2009-2010 Chortos-2 <chortos@inbox.lv> |
3 | |
4 from __future__ import division, with_statement | |
5 import optparse, sys, compat | |
21 | 6 |
7 def import_error(e): | |
8 say('Error: your installation of test.py is incomplete;', str(e).lower() + '.', file=sys.stderr) | |
9 sys.exit(3) | |
10 | |
11 from compat import * | |
16 | 12 |
18
a33653cdad57
Still $Rev$ substitution in 2.00
Oleg Oshmyan <chortos@inbox.lv>
parents:
17
diff
changeset
|
13 # $Rev$ |
16 | 14 version = '2.00.0 (SVN r$$REV$$)' |
15 parser = optparse.OptionParser(version='test.py '+version, epilog='Python 2.5 or newer is required, unless you have a custom build of Python.') | |
23 | 16 parser.add_option('-1', dest='legacy', action='store_true', default=False, help='handle configuration files in a way more compatible with test.py 1.x') |
16 | 17 parser.add_option('-u', '--update', dest='update', action='store_true', default=False, help='check for an updated version of test.py') |
18 parser.add_option('-m', '--copy-io', dest='copyonly', action='store_true', default=False, help='create a copy of the input/output files of the last test case for manual testing and exit') | |
19 parser.add_option('-x', '--auto-exit', dest='pause', action='store_false', default=True, help='do not wait for a key to be pressed after finishing testing') | |
21 | 20 parser.add_option('-s', '--save-io', dest='erase', action='store_false', default=True, help='do not delete the copies of input/output files after the last test case; create copies of input files and store output in files even if the solution uses standard I/O; delete the stored input/output files if the solution uses standard I/O and the -c/--cleanup option is specified') |
21 parser.add_option('-t', '--detect-time', dest='autotime', action='store_true', default=False, help='spend a second detecting the most precise time measurement function') | |
22 parser.add_option('--no-time-limits', dest='no_maxtime', action='store_true', default=False, help='disable all time limits') | |
16 | 23 |
24 options, args = parser.parse_args() | |
25 parser.destroy() | |
26 del parser | |
27 | |
28 if options.update: | |
29 try: | |
30 urllib, urlread = compat.import_urllib() | |
31 except ImportError: | |
32 sys.exit('Error: the urllib Python module is missing. Without it, an automatic update is impossible.') | |
33 | |
34 latesttext = urlread('http://chortos.selfip.net/~astiob/test.py/version.txt') | |
35 latest = latesttext.split('.') | |
36 installed = version.split('.') | |
37 update = None | |
38 | |
39 if latest[0] > installed[0]: | |
40 update = 'major' | |
41 elif latest[0] == installed[0]: | |
42 if latest[1] > installed[1]: | |
43 update = 'feature' | |
44 elif latest[1] == installed[1]: | |
45 if latest[2] > installed[2]: | |
46 update = 'bug-fixing' | |
47 elif latest[2] == installed[2]: | |
48 say('You are using the latest publicly available version of test.py.') | |
49 sys.exit() | |
50 | |
51 if not update: | |
52 say('Your copy of test.py is newer than the publicly available version.') | |
53 sys.exit() | |
54 | |
55 say('A ' + update + ' update to test.py is available. Downloading...') | |
56 sys.stdout.flush() | |
57 urllib.urlretrieve('http://chortos.selfip.net/~astiob/test.py/test.py', sys.argv[0]) | |
58 say('Downloaded and installed. Now you are using test.py ' + latesttext + '.') | |
59 sys.exit() | |
60 | |
22 | 61 import config, itertools, os, subprocess, sys, time |
16 | 62 |
21 | 63 if options.autotime: |
22 | 64 # This is really a dirty hack that assumes that sleep() does not spend |
65 # the CPU time of the current process and that if clock() measures | |
66 # wall-clock time, then it is more precise than time() is. Both these | |
67 # assumptions are true on all platforms I have tested this on so far, | |
68 # but I am not aware of any guarantee that they will both be true | |
69 # on every other platform. | |
21 | 70 c = time.clock() |
71 time.sleep(1) | |
72 c = time.clock() - c | |
73 if int(c + .5) == 1: | |
74 clock = time.clock | |
75 else: | |
76 clock = time.time | |
77 elif sys.platform == 'win32': | |
78 clock = time.clock | |
79 else: | |
80 clock = time.time | |
16 | 81 |
82 try: | |
22 | 83 from testcases import pause |
84 except ImportError: | |
85 pause = None | |
86 | |
87 try: | |
21 | 88 globalconf = config.load_global() |
16 | 89 |
21 | 90 # Do this check here so that if we have to warn them, we do it as early as possible |
22 | 91 if options.pause and not pause and not hasattr(globalconf, 'pause'): |
92 # testcases.pause will be sure to import msvcrt if it can | |
93 #try: | |
94 # # If we have getch, we don't need globalconf.pause | |
95 # import msvcrt | |
96 # msvcrt.getch.__call__ | |
97 #except Exception: | |
21 | 98 if os.name == 'posix': |
99 globalconf.pause = 'read -s -n 1' | |
22 | 100 say('Warning: configuration variable pause is not defined; it was devised automatically but the choice might be incorrect, so test.py might exit immediately after the testing is completed.', file=sys.stderr) |
101 sys.stderr.flush() | |
21 | 102 elif os.name == 'nt': |
103 globalconf.pause = 'pause' | |
104 else: | |
105 sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.') | |
16 | 106 |
21 | 107 try: |
108 from problem import * | |
109 except ImportError: | |
110 import_error(sys.exc_info()[1]) | |
16 | 111 |
21 | 112 # Support single-problem configurations |
113 if globalconf.tasknames is None: | |
114 shouldprintnames = False | |
115 globalconf.multiproblem = False | |
116 globalconf.tasknames = os.path.curdir, | |
16 | 117 else: |
21 | 118 globalconf.multiproblem = True |
119 try: | |
120 shouldprintnames = len(globalconf.tasknames) > 1 | |
121 except Exception: | |
122 # Try to retrieve the first two problem names and cache them on success | |
123 globalconf.tasknames = iter(globalconf.tasknames) | |
124 try: | |
125 try: | |
126 first = next(globalconf.tasknames) | |
127 except NameError: | |
128 # Python 2.5 lacks the next() built-in | |
129 first = globalconf.tasknames.next() | |
130 except StopIteration: | |
131 globalconf.tasknames = () | |
132 shouldprintnames = False | |
133 else: | |
134 try: | |
135 try: | |
136 second = next(globalconf.tasknames) | |
137 except NameError: | |
138 second = globalconf.tasknames.next() | |
139 except StopIteration: | |
140 globalconf.tasknames = first, | |
141 shouldprintnames = False | |
142 else: | |
143 globalconf.tasknames = itertools.chain((first, second), globalconf.tasknames) | |
144 shouldprintnames = True | |
16 | 145 |
21 | 146 ntasks = 0 |
147 nfulltasks = 0 | |
148 maxscore = 0 | |
149 realscore = 0 | |
16 | 150 |
21 | 151 for taskname in globalconf.tasknames: |
152 problem = Problem(taskname) | |
153 | |
22 | 154 if ntasks and not options.copyonly: say() |
21 | 155 if shouldprintnames: say(taskname) |
156 | |
157 if options.copyonly: | |
158 problem.copytestdata() | |
159 else: | |
160 real, max = problem.test() | |
161 | |
162 ntasks += 1 | |
22 | 163 nfulltasks += real == max |
21 | 164 realscore += real |
165 maxscore += max | |
166 | |
167 if options.copyonly: | |
168 sys.exit() | |
169 | |
170 if ntasks != 1: | |
171 say() | |
172 say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks)) | |
173 except KeyboardInterrupt: | |
174 sys.exit('Exiting due to a keyboard interrupt.') | |
16 | 175 |
176 if options.pause: | |
21 | 177 say('Press any key to exit...') |
16 | 178 sys.stdout.flush() |
179 | |
22 | 180 #try: |
181 # import msvcrt | |
182 # msvcrt.getch() | |
183 #except Exception: | |
184 if pause: | |
185 pause() | |
186 elif callable(globalconf.pause): | |
187 globalconf.pause() | |
188 else: | |
189 with open(os.devnull, 'w') as devnull: | |
190 subprocess.call(globalconf.pause, stdout=devnull, stderr=subprocess.STDOUT) |