Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/test-svn.py @ 22:f07b7a431ea6
Further 2.00 work
Testconfs in all supported kinds of archives should now work.
Test runs are now cancelled by pressing Escape rather than Ctrl+C.
Improved time control.
Greatly improved temporary and helper file cleanup.
The pause configuration variable can now be a callable and is now processed using subprocess rather than system().
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Wed, 22 Sep 2010 22:01:56 +0000 |
| parents | ec6f1a132109 |
| children | c1f52b5d80d6 |
| 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 | |
| 22 | 60 import config, itertools, os, subprocess, sys, time |
| 16 | 61 |
| 21 | 62 if options.autotime: |
| 22 | 63 # This is really a dirty hack that assumes that sleep() does not spend |
| 64 # the CPU time of the current process and that if clock() measures | |
| 65 # wall-clock time, then it is more precise than time() is. Both these | |
| 66 # assumptions are true on all platforms I have tested this on so far, | |
| 67 # but I am not aware of any guarantee that they will both be true | |
| 68 # on every other platform. | |
| 21 | 69 c = time.clock() |
| 70 time.sleep(1) | |
| 71 c = time.clock() - c | |
| 72 if int(c + .5) == 1: | |
| 73 clock = time.clock | |
| 74 else: | |
| 75 clock = time.time | |
| 76 elif sys.platform == 'win32': | |
| 77 clock = time.clock | |
| 78 else: | |
| 79 clock = time.time | |
| 16 | 80 |
| 81 try: | |
| 22 | 82 from testcases import pause |
| 83 except ImportError: | |
| 84 pause = None | |
| 85 | |
| 86 try: | |
| 21 | 87 globalconf = config.load_global() |
| 16 | 88 |
| 21 | 89 # Do this check here so that if we have to warn them, we do it as early as possible |
| 22 | 90 if options.pause and not pause and not hasattr(globalconf, 'pause'): |
| 91 # testcases.pause will be sure to import msvcrt if it can | |
| 92 #try: | |
| 93 # # If we have getch, we don't need globalconf.pause | |
| 94 # import msvcrt | |
| 95 # msvcrt.getch.__call__ | |
| 96 #except Exception: | |
| 21 | 97 if os.name == 'posix': |
| 98 globalconf.pause = 'read -s -n 1' | |
| 22 | 99 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) |
| 100 sys.stderr.flush() | |
| 21 | 101 elif os.name == 'nt': |
| 102 globalconf.pause = 'pause' | |
| 103 else: | |
| 104 sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.') | |
| 16 | 105 |
| 21 | 106 try: |
| 107 from problem import * | |
| 108 except ImportError: | |
| 109 import_error(sys.exc_info()[1]) | |
| 16 | 110 |
| 21 | 111 # Support single-problem configurations |
| 112 if globalconf.tasknames is None: | |
| 113 shouldprintnames = False | |
| 114 globalconf.multiproblem = False | |
| 115 globalconf.tasknames = os.path.curdir, | |
| 16 | 116 else: |
| 21 | 117 globalconf.multiproblem = True |
| 118 try: | |
| 119 shouldprintnames = len(globalconf.tasknames) > 1 | |
| 120 except Exception: | |
| 121 # Try to retrieve the first two problem names and cache them on success | |
| 122 globalconf.tasknames = iter(globalconf.tasknames) | |
| 123 try: | |
| 124 try: | |
| 125 first = next(globalconf.tasknames) | |
| 126 except NameError: | |
| 127 # Python 2.5 lacks the next() built-in | |
| 128 first = globalconf.tasknames.next() | |
| 129 except StopIteration: | |
| 130 globalconf.tasknames = () | |
| 131 shouldprintnames = False | |
| 132 else: | |
| 133 try: | |
| 134 try: | |
| 135 second = next(globalconf.tasknames) | |
| 136 except NameError: | |
| 137 second = globalconf.tasknames.next() | |
| 138 except StopIteration: | |
| 139 globalconf.tasknames = first, | |
| 140 shouldprintnames = False | |
| 141 else: | |
| 142 globalconf.tasknames = itertools.chain((first, second), globalconf.tasknames) | |
| 143 shouldprintnames = True | |
| 16 | 144 |
| 21 | 145 ntasks = 0 |
| 146 nfulltasks = 0 | |
| 147 maxscore = 0 | |
| 148 realscore = 0 | |
| 16 | 149 |
| 21 | 150 for taskname in globalconf.tasknames: |
| 151 problem = Problem(taskname) | |
| 152 | |
| 22 | 153 if ntasks and not options.copyonly: say() |
| 21 | 154 if shouldprintnames: say(taskname) |
| 155 | |
| 156 if options.copyonly: | |
| 157 problem.copytestdata() | |
| 158 else: | |
| 159 real, max = problem.test() | |
| 160 | |
| 161 ntasks += 1 | |
| 22 | 162 nfulltasks += real == max |
| 21 | 163 realscore += real |
| 164 maxscore += max | |
| 165 | |
| 166 if options.copyonly: | |
| 167 sys.exit() | |
| 168 | |
| 169 if ntasks != 1: | |
| 170 say() | |
| 171 say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks)) | |
| 172 except KeyboardInterrupt: | |
| 173 sys.exit('Exiting due to a keyboard interrupt.') | |
| 16 | 174 |
| 175 if options.pause: | |
| 21 | 176 say('Press any key to exit...') |
| 16 | 177 sys.stdout.flush() |
| 178 | |
| 22 | 179 #try: |
| 180 # import msvcrt | |
| 181 # msvcrt.getch() | |
| 182 #except Exception: | |
| 183 if pause: | |
| 184 pause() | |
| 185 elif callable(globalconf.pause): | |
| 186 globalconf.pause() | |
| 187 else: | |
| 188 with open(os.devnull, 'w') as devnull: | |
| 189 subprocess.call(globalconf.pause, stdout=devnull, stderr=subprocess.STDOUT) |
