16
|
1 #!/usr/bin/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 from compat import say
|
|
7
|
|
8 # $Rev: 15 $
|
|
9 version = '2.00.0 (SVN r$$REV$$)'
|
|
10 parser = optparse.OptionParser(version='test.py '+version, epilog='Python 2.5 or newer is required, unless you have a custom build of Python.')
|
|
11 parser.add_option('-u', '--update', dest='update', action='store_true', default=False, help='check for an updated version of test.py')
|
|
12 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')
|
|
13 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')
|
|
14
|
|
15 options, args = parser.parse_args()
|
|
16 parser.destroy()
|
|
17 del parser
|
|
18
|
|
19 if options.update:
|
|
20 try:
|
|
21 urllib, urlread = compat.import_urllib()
|
|
22 except ImportError:
|
|
23 sys.exit('Error: the urllib Python module is missing. Without it, an automatic update is impossible.')
|
|
24
|
|
25 latesttext = urlread('http://chortos.selfip.net/~astiob/test.py/version.txt')
|
|
26 latest = latesttext.split('.')
|
|
27 installed = version.split('.')
|
|
28 update = None
|
|
29
|
|
30 if latest[0] > installed[0]:
|
|
31 update = 'major'
|
|
32 elif latest[0] == installed[0]:
|
|
33 if latest[1] > installed[1]:
|
|
34 update = 'feature'
|
|
35 elif latest[1] == installed[1]:
|
|
36 if latest[2] > installed[2]:
|
|
37 update = 'bug-fixing'
|
|
38 elif latest[2] == installed[2]:
|
|
39 say('You are using the latest publicly available version of test.py.')
|
|
40 sys.exit()
|
|
41
|
|
42 if not update:
|
|
43 say('Your copy of test.py is newer than the publicly available version.')
|
|
44 sys.exit()
|
|
45
|
|
46 say('A ' + update + ' update to test.py is available. Downloading...')
|
|
47 sys.stdout.flush()
|
|
48 urllib.urlretrieve('http://chortos.selfip.net/~astiob/test.py/test.py', sys.argv[0])
|
|
49 say('Downloaded and installed. Now you are using test.py ' + latesttext + '.')
|
|
50 sys.exit()
|
|
51
|
|
52 def import_error(e):
|
|
53 say('Your installation of test.py is incomplete:', str(e).lower() + '.', file=sys.stderr)
|
|
54 sys.exit(3)
|
|
55
|
|
56 import os, config
|
|
57
|
|
58 # Do this check here so that if we have to warn them, we do it as early as possible
|
|
59 if options.pause and not hasattr(config, 'pause'):
|
|
60 try:
|
|
61 # If we have getch, we don't need config.pause
|
|
62 import msvcrt
|
|
63 msvcrt.getch.__call__
|
|
64 except Exception:
|
|
65 if os.name == 'posix':
|
|
66 config.pause = 'read -s -n 1'
|
|
67 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.')
|
|
68 sys.stdout.flush()
|
|
69 elif os.name == 'nt':
|
|
70 config.pause = 'pause'
|
|
71 else:
|
|
72 sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.')
|
|
73
|
|
74 try:
|
|
75 from problem import *
|
|
76 except ImportError as e:
|
|
77 import_error(e)
|
|
78
|
|
79 # Support single-problem configurations
|
|
80 try:
|
|
81 shouldprintnames = len(config.tasknames) > 1
|
|
82 except Exception:
|
|
83 shouldprintnames = True
|
|
84
|
|
85 ntasks = 0
|
|
86 nfulltasks = 0
|
|
87 maxscore = 0
|
|
88 realscore = 0
|
|
89
|
|
90 for taskname in config.tasknames:
|
|
91 problem = Problem(taskname)
|
|
92
|
|
93 if ntasks: say()
|
|
94 if shouldprintnames: say(taskname)
|
|
95
|
|
96 if options.copyonly:
|
|
97 problem.copytestdata()
|
|
98 else:
|
|
99 real, max = problem.test()
|
|
100
|
|
101 ntasks += 1
|
|
102 nfulltasks += (real == max)
|
|
103 realscore += real
|
|
104 maxscore += max
|
|
105
|
|
106 if options.copyonly:
|
|
107 sys.exit()
|
|
108
|
|
109 if ntasks != 1:
|
|
110 say()
|
|
111 say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks))
|
|
112
|
|
113 if options.pause:
|
|
114 say('Press any key to exit...', end='')
|
|
115 sys.stdout.flush()
|
|
116
|
|
117 try:
|
|
118 import msvcrt
|
|
119 msvcrt.getch()
|
|
120 except Exception:
|
|
121 os.system(config.pause + ' >' + os.devnull) |