Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/test-svn.py @ 35:23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Fri, 03 Dec 2010 01:19:55 +0000 |
parents | f90bd2d1a12b |
children | ddb3e1098727 |
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 |
33
f90bd2d1a12b
Converted revision reporting from SVN to hg.
Oleg Oshmyan <chortos@inbox.lv>
parents:
26
diff
changeset
|
13 version = '2.00.0 ($$REV$$)' |
26 | 14 parser = optparse.OptionParser(version='test.py '+version, epilog='Python 2.5 or newer is required.') |
23 | 15 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') |
26 | 16 parser.add_option('-u', '--update', dest='update', action='store_true', default=False, help='update the installed test.py to the latest publicly available version') |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
17 parser.add_option('-p', '--problem', dest='problems', metavar='PROBLEM', action='append', help='test only the PROBLEM (this option can be specified more than once with different problem names, all of which will be tested)') |
16 | 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'): |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
92 if os.name == 'posix': |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
93 globalconf.pause = 'read -s -n 1' |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
94 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) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
95 sys.stderr.flush() |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
96 elif os.name == 'nt': |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
97 globalconf.pause = 'pause' |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
98 else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
99 sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.') |
16 | 100 |
21 | 101 try: |
102 from problem import * | |
103 except ImportError: | |
104 import_error(sys.exc_info()[1]) | |
16 | 105 |
21 | 106 # Support single-problem configurations |
107 if globalconf.tasknames is None: | |
108 shouldprintnames = False | |
109 globalconf.multiproblem = False | |
110 globalconf.tasknames = os.path.curdir, | |
16 | 111 else: |
21 | 112 globalconf.multiproblem = True |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
113 # TODO: erase the commented part? if it has a tasknames variable, it is by definition multi-problem |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
114 shouldprintnames = True |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
115 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
116 # shouldprintnames = len(globalconf.tasknames) > 1 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
117 # except Exception: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
118 # # Try to retrieve the first two problem names and cache them on success |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
119 # globalconf.tasknames = iter(globalconf.tasknames) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
120 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
121 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
122 # first = next(globalconf.tasknames) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
123 # except NameError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
124 # # Python 2.5 lacks the next() built-in |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
125 # first = globalconf.tasknames.next() |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
126 # except StopIteration: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
127 # globalconf.tasknames = () |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
128 # shouldprintnames = False |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
129 # else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
130 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
131 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
132 # second = next(globalconf.tasknames) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
133 # except NameError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
134 # second = globalconf.tasknames.next() |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
135 # except StopIteration: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
136 # globalconf.tasknames = first, |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
137 # shouldprintnames = False |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
138 # else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
139 # globalconf.tasknames = itertools.chain((first, second), globalconf.tasknames) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
140 # shouldprintnames = True |
16 | 141 |
21 | 142 ntasks = 0 |
143 nfulltasks = 0 | |
144 maxscore = 0 | |
145 realscore = 0 | |
16 | 146 |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
147 for taskname in (globalconf.tasknames if not options.problems else options.problems): |
21 | 148 problem = Problem(taskname) |
149 | |
22 | 150 if ntasks and not options.copyonly: say() |
21 | 151 if shouldprintnames: say(taskname) |
152 | |
153 if options.copyonly: | |
154 problem.copytestdata() | |
155 else: | |
156 real, max = problem.test() | |
157 | |
158 ntasks += 1 | |
22 | 159 nfulltasks += real == max |
21 | 160 realscore += real |
161 maxscore += max | |
162 | |
163 if options.copyonly: | |
164 sys.exit() | |
165 | |
166 if ntasks != 1: | |
167 say() | |
168 say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks)) | |
169 except KeyboardInterrupt: | |
170 sys.exit('Exiting due to a keyboard interrupt.') | |
16 | 171 |
172 if options.pause: | |
21 | 173 say('Press any key to exit...') |
16 | 174 sys.stdout.flush() |
175 | |
22 | 176 if pause: |
177 pause() | |
178 elif callable(globalconf.pause): | |
179 globalconf.pause() | |
180 else: | |
181 with open(os.devnull, 'w') as devnull: | |
182 subprocess.call(globalconf.pause, stdout=devnull, stderr=subprocess.STDOUT) |