Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/test-svn.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 | a33653cdad57 |
| children | f07b7a431ea6 |
| 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.') | |
| 16 parser.add_option('-u', '--update', dest='update', action='store_true', default=False, help='check for an updated version of test.py') | |
| 17 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') | |
| 18 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 | 19 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') |
| 20 parser.add_option('-t', '--detect-time', dest='autotime', action='store_true', default=False, help='spend a second detecting the most precise time measurement function') | |
| 21 parser.add_option('--no-time-limits', dest='no_maxtime', action='store_true', default=False, help='disable all time limits') | |
| 16 | 22 |
| 23 options, args = parser.parse_args() | |
| 24 parser.destroy() | |
| 25 del parser | |
| 26 | |
| 27 if options.update: | |
| 28 try: | |
| 29 urllib, urlread = compat.import_urllib() | |
| 30 except ImportError: | |
| 31 sys.exit('Error: the urllib Python module is missing. Without it, an automatic update is impossible.') | |
| 32 | |
| 33 latesttext = urlread('http://chortos.selfip.net/~astiob/test.py/version.txt') | |
| 34 latest = latesttext.split('.') | |
| 35 installed = version.split('.') | |
| 36 update = None | |
| 37 | |
| 38 if latest[0] > installed[0]: | |
| 39 update = 'major' | |
| 40 elif latest[0] == installed[0]: | |
| 41 if latest[1] > installed[1]: | |
| 42 update = 'feature' | |
| 43 elif latest[1] == installed[1]: | |
| 44 if latest[2] > installed[2]: | |
| 45 update = 'bug-fixing' | |
| 46 elif latest[2] == installed[2]: | |
| 47 say('You are using the latest publicly available version of test.py.') | |
| 48 sys.exit() | |
| 49 | |
| 50 if not update: | |
| 51 say('Your copy of test.py is newer than the publicly available version.') | |
| 52 sys.exit() | |
| 53 | |
| 54 say('A ' + update + ' update to test.py is available. Downloading...') | |
| 55 sys.stdout.flush() | |
| 56 urllib.urlretrieve('http://chortos.selfip.net/~astiob/test.py/test.py', sys.argv[0]) | |
| 57 say('Downloaded and installed. Now you are using test.py ' + latesttext + '.') | |
| 58 sys.exit() | |
| 59 | |
| 21 | 60 import config, itertools, os, sys, time |
| 16 | 61 |
| 21 | 62 if options.autotime: |
| 63 c = time.clock() | |
| 64 time.sleep(1) | |
| 65 c = time.clock() - c | |
| 66 if int(c + .5) == 1: | |
| 67 clock = time.clock | |
| 68 else: | |
| 69 clock = time.time | |
| 70 elif sys.platform == 'win32': | |
| 71 clock = time.clock | |
| 72 else: | |
| 73 clock = time.time | |
| 16 | 74 |
| 75 try: | |
| 21 | 76 globalconf = config.load_global() |
| 16 | 77 |
| 21 | 78 # Do this check here so that if we have to warn them, we do it as early as possible |
| 79 if options.pause and not hasattr(globalconf, 'pause'): | |
| 80 try: | |
| 81 # If we have getch, we don't need config.pause | |
| 82 import msvcrt | |
| 83 msvcrt.getch.__call__ | |
| 84 except Exception: | |
| 85 if os.name == 'posix': | |
| 86 globalconf.pause = 'read -s -n 1' | |
| 87 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.') | |
| 88 sys.stdout.flush() | |
| 89 elif os.name == 'nt': | |
| 90 globalconf.pause = 'pause' | |
| 91 else: | |
| 92 sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.') | |
| 16 | 93 |
| 21 | 94 try: |
| 95 from problem import * | |
| 96 except ImportError: | |
| 97 import_error(sys.exc_info()[1]) | |
| 16 | 98 |
| 21 | 99 # Support single-problem configurations |
| 100 if globalconf.tasknames is None: | |
| 101 shouldprintnames = False | |
| 102 globalconf.multiproblem = False | |
| 103 globalconf.tasknames = os.path.curdir, | |
| 16 | 104 else: |
| 21 | 105 globalconf.multiproblem = True |
| 106 try: | |
| 107 shouldprintnames = len(globalconf.tasknames) > 1 | |
| 108 except Exception: | |
| 109 # Try to retrieve the first two problem names and cache them on success | |
| 110 globalconf.tasknames = iter(globalconf.tasknames) | |
| 111 try: | |
| 112 try: | |
| 113 first = next(globalconf.tasknames) | |
| 114 except NameError: | |
| 115 # Python 2.5 lacks the next() built-in | |
| 116 first = globalconf.tasknames.next() | |
| 117 except StopIteration: | |
| 118 globalconf.tasknames = () | |
| 119 shouldprintnames = False | |
| 120 else: | |
| 121 try: | |
| 122 try: | |
| 123 second = next(globalconf.tasknames) | |
| 124 except NameError: | |
| 125 second = globalconf.tasknames.next() | |
| 126 except StopIteration: | |
| 127 globalconf.tasknames = first, | |
| 128 shouldprintnames = False | |
| 129 else: | |
| 130 globalconf.tasknames = itertools.chain((first, second), globalconf.tasknames) | |
| 131 shouldprintnames = True | |
| 16 | 132 |
| 21 | 133 ntasks = 0 |
| 134 nfulltasks = 0 | |
| 135 maxscore = 0 | |
| 136 realscore = 0 | |
| 16 | 137 |
| 21 | 138 for taskname in globalconf.tasknames: |
| 139 problem = Problem(taskname) | |
| 140 | |
| 141 if ntasks: say() | |
| 142 if shouldprintnames: say(taskname) | |
| 143 | |
| 144 if options.copyonly: | |
| 145 problem.copytestdata() | |
| 146 else: | |
| 147 real, max = problem.test() | |
| 148 | |
| 149 ntasks += 1 | |
| 150 nfulltasks += (real == max) | |
| 151 realscore += real | |
| 152 maxscore += max | |
| 153 | |
| 154 if options.copyonly: | |
| 155 sys.exit() | |
| 156 | |
| 157 if ntasks != 1: | |
| 158 say() | |
| 159 say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks)) | |
| 160 except KeyboardInterrupt: | |
| 161 sys.exit('Exiting due to a keyboard interrupt.') | |
| 16 | 162 |
| 163 if options.pause: | |
| 21 | 164 say('Press any key to exit...') |
| 16 | 165 sys.stdout.flush() |
| 166 | |
| 167 try: | |
| 168 import msvcrt | |
| 169 msvcrt.getch() | |
| 170 except Exception: | |
| 21 | 171 os.system(globalconf.pause + ' >' + os.devnull) |
