comparison upreckon-vcs @ 50:4ea7133ac25c

Converted 2.00 into the default branch
author Oleg Oshmyan <chortos@inbox.lv>
date Sun, 19 Dec 2010 23:25:13 +0200
parents 2.00/upreckon-vcs@5afefe51dcdc
children 94946bc0a3a8
comparison
equal deleted inserted replaced
47:06f1683c8db9 50:4ea7133ac25c
1 #! /usr/bin/env python
2 # Copyright (c) 2009-2010 Chortos-2 <chortos@inbox.lv>
3
4 from __future__ import division, with_statement
5 import optparse, sys, compat
6
7 def import_error(e):
8 say('Error: your installation of Upreckon is incomplete;', str(e).lower() + '.', file=sys.stderr)
9 sys.exit(3)
10
11 from compat import *
12
13 version = '2.00.0 ($$REV$$)'
14 parser = optparse.OptionParser(version='Upreckon '+version, epilog='Python 2.5 or newer is required.')
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')
16 parser.add_option('-u', '--update', dest='update', action='store_true', default=False, help='update the installed Upreckon to the latest publicly available version')
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)')
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')
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')
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 Upreckon.')
49 sys.exit()
50
51 if not update:
52 say('Your copy of Upreckon is newer than the publicly available version.')
53 sys.exit()
54
55 say('A ' + update + ' update to Upreckon is available. Downloading...')
56 sys.stdout.flush()
57 # FIXME: need to update all files!
58 urllib.urlretrieve('http://chortos.selfip.net/~astiob/test.py/test.py', sys.argv[0])
59 say('Downloaded and installed. Now you are using Upreckon ' + latesttext + '.')
60 sys.exit()
61
62 import config, itertools, os, subprocess, sys, time
63
64 if options.autotime:
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.
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
82
83 try:
84 from testcases import pause
85 except ImportError:
86 pause = None
87
88 try:
89 globalconf = config.load_global()
90
91 # Do this check here so that if we have to warn them, we do it as early as possible
92 if options.pause and not pause and not hasattr(globalconf, 'pause'):
93 if os.name == 'posix':
94 globalconf.pause = 'read -s -n 1'
95 say('Warning: configuration variable pause is not defined; it was devised automatically but the choice might be incorrect, so Upreckon might exit immediately after the testing is completed.', file=sys.stderr)
96 sys.stderr.flush()
97 elif os.name == 'nt':
98 globalconf.pause = 'pause'
99 else:
100 sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.')
101
102 try:
103 from problem import *
104 except ImportError:
105 import_error(sys.exc_info()[1])
106
107 # Support single-problem configurations
108 if globalconf.tasknames is None:
109 shouldprintnames = False
110 globalconf.multiproblem = False
111 globalconf.tasknames = os.path.curdir,
112 else:
113 globalconf.multiproblem = True
114 shouldprintnames = True
115
116 ntasks = 0
117 nfulltasks = 0
118 maxscore = 0
119 realscore = 0
120
121 for taskname in (globalconf.tasknames if not options.problems else options.problems):
122 problem = Problem(taskname)
123
124 if ntasks and not options.copyonly: say()
125 if shouldprintnames: say(taskname)
126
127 if options.copyonly:
128 problem.copytestdata()
129 else:
130 real, max = problem.test()
131
132 ntasks += 1
133 nfulltasks += real == max
134 realscore += real
135 maxscore += max
136
137 if options.copyonly:
138 sys.exit()
139
140 if ntasks != 1:
141 say()
142 say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks))
143 except KeyboardInterrupt:
144 sys.exit('Exiting due to a keyboard interrupt.')
145
146 if options.pause:
147 say('Press any key to exit...')
148 sys.stdout.flush()
149
150 if pause:
151 pause()
152 elif callable(globalconf.pause):
153 globalconf.pause()
154 else:
155 with open(os.devnull, 'w') as devnull:
156 subprocess.call(globalconf.pause, stdout=devnull, stderr=subprocess.STDOUT)