Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/test-svn.py @ 28:3d535503161f
hasattr -> try
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Wed, 24 Nov 2010 01:42:39 +0000 |
parents | 5bbb68833868 |
children | f90bd2d1a12b |
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$$)' |
26 | 15 parser = optparse.OptionParser(version='test.py '+version, epilog='Python 2.5 or newer is required.') |
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') |
26 | 17 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
|
18 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 | 19 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') |
20 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 | 21 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') |
22 parser.add_option('-t', '--detect-time', dest='autotime', action='store_true', default=False, help='spend a second detecting the most precise time measurement function') | |
23 parser.add_option('--no-time-limits', dest='no_maxtime', action='store_true', default=False, help='disable all time limits') | |
16 | 24 |
25 options, args = parser.parse_args() | |
26 parser.destroy() | |
27 del parser | |
28 | |
29 if options.update: | |
30 try: | |
31 urllib, urlread = compat.import_urllib() | |
32 except ImportError: | |
33 sys.exit('Error: the urllib Python module is missing. Without it, an automatic update is impossible.') | |
34 | |
35 latesttext = urlread('http://chortos.selfip.net/~astiob/test.py/version.txt') | |
36 latest = latesttext.split('.') | |
37 installed = version.split('.') | |
38 update = None | |
39 | |
40 if latest[0] > installed[0]: | |
41 update = 'major' | |
42 elif latest[0] == installed[0]: | |
43 if latest[1] > installed[1]: | |
44 update = 'feature' | |
45 elif latest[1] == installed[1]: | |
46 if latest[2] > installed[2]: | |
47 update = 'bug-fixing' | |
48 elif latest[2] == installed[2]: | |
49 say('You are using the latest publicly available version of test.py.') | |
50 sys.exit() | |
51 | |
52 if not update: | |
53 say('Your copy of test.py is newer than the publicly available version.') | |
54 sys.exit() | |
55 | |
56 say('A ' + update + ' update to test.py is available. Downloading...') | |
57 sys.stdout.flush() | |
58 urllib.urlretrieve('http://chortos.selfip.net/~astiob/test.py/test.py', sys.argv[0]) | |
59 say('Downloaded and installed. Now you are using test.py ' + latesttext + '.') | |
60 sys.exit() | |
61 | |
22 | 62 import config, itertools, os, subprocess, sys, time |
16 | 63 |
21 | 64 if options.autotime: |
22 | 65 # This is really a dirty hack that assumes that sleep() does not spend |
66 # the CPU time of the current process and that if clock() measures | |
67 # wall-clock time, then it is more precise than time() is. Both these | |
68 # assumptions are true on all platforms I have tested this on so far, | |
69 # but I am not aware of any guarantee that they will both be true | |
70 # on every other platform. | |
21 | 71 c = time.clock() |
72 time.sleep(1) | |
73 c = time.clock() - c | |
74 if int(c + .5) == 1: | |
75 clock = time.clock | |
76 else: | |
77 clock = time.time | |
78 elif sys.platform == 'win32': | |
79 clock = time.clock | |
80 else: | |
81 clock = time.time | |
16 | 82 |
83 try: | |
22 | 84 from testcases import pause |
85 except ImportError: | |
86 pause = None | |
87 | |
88 try: | |
21 | 89 globalconf = config.load_global() |
16 | 90 |
21 | 91 # Do this check here so that if we have to warn them, we do it as early as possible |
22 | 92 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
|
93 if os.name == 'posix': |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
94 globalconf.pause = 'read -s -n 1' |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
95 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
|
96 sys.stderr.flush() |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
97 elif os.name == 'nt': |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
98 globalconf.pause = 'pause' |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
99 else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
100 sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.') |
16 | 101 |
21 | 102 try: |
103 from problem import * | |
104 except ImportError: | |
105 import_error(sys.exc_info()[1]) | |
16 | 106 |
21 | 107 # Support single-problem configurations |
108 if globalconf.tasknames is None: | |
109 shouldprintnames = False | |
110 globalconf.multiproblem = False | |
111 globalconf.tasknames = os.path.curdir, | |
16 | 112 else: |
21 | 113 globalconf.multiproblem = True |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
114 # 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
|
115 shouldprintnames = True |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
116 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
117 # shouldprintnames = len(globalconf.tasknames) > 1 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
118 # except Exception: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
119 # # 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
|
120 # globalconf.tasknames = iter(globalconf.tasknames) |
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 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
123 # first = next(globalconf.tasknames) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
124 # except NameError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
125 # # Python 2.5 lacks the next() built-in |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
126 # first = globalconf.tasknames.next() |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
127 # except StopIteration: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
128 # globalconf.tasknames = () |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
129 # shouldprintnames = False |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
130 # else: |
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 # try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
133 # second = next(globalconf.tasknames) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
134 # except NameError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
135 # second = globalconf.tasknames.next() |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
136 # except StopIteration: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
137 # globalconf.tasknames = first, |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
138 # shouldprintnames = False |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
139 # else: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
140 # globalconf.tasknames = itertools.chain((first, second), globalconf.tasknames) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
141 # shouldprintnames = True |
16 | 142 |
21 | 143 ntasks = 0 |
144 nfulltasks = 0 | |
145 maxscore = 0 | |
146 realscore = 0 | |
16 | 147 |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
148 for taskname in (globalconf.tasknames if not options.problems else options.problems): |
21 | 149 problem = Problem(taskname) |
150 | |
22 | 151 if ntasks and not options.copyonly: say() |
21 | 152 if shouldprintnames: say(taskname) |
153 | |
154 if options.copyonly: | |
155 problem.copytestdata() | |
156 else: | |
157 real, max = problem.test() | |
158 | |
159 ntasks += 1 | |
22 | 160 nfulltasks += real == max |
21 | 161 realscore += real |
162 maxscore += max | |
163 | |
164 if options.copyonly: | |
165 sys.exit() | |
166 | |
167 if ntasks != 1: | |
168 say() | |
169 say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks)) | |
170 except KeyboardInterrupt: | |
171 sys.exit('Exiting due to a keyboard interrupt.') | |
16 | 172 |
173 if options.pause: | |
21 | 174 say('Press any key to exit...') |
16 | 175 sys.stdout.flush() |
176 | |
22 | 177 if pause: |
178 pause() | |
179 elif callable(globalconf.pause): | |
180 globalconf.pause() | |
181 else: | |
182 with open(os.devnull, 'w') as devnull: | |
183 subprocess.call(globalconf.pause, stdout=devnull, stderr=subprocess.STDOUT) |