view 2.00/test-svn.py @ 20:5bfa23cd638d

More ZIP archive fixes One typo and one Windows incompatibility fixed.
author Oleg Oshmyan <chortos@inbox.lv>
date Mon, 14 Jun 2010 21:02:06 +0000
parents a33653cdad57
children ec6f1a132109
line wrap: on
line source

#!/usr/bin/python
# Copyright (c) 2009-2010 Chortos-2 <chortos@inbox.lv>

from __future__ import division, with_statement
import optparse, sys, compat
from compat import say

# $Rev$
version = '2.00.0 (SVN r$$REV$$)'
parser = optparse.OptionParser(version='test.py '+version, epilog='Python 2.5 or newer is required, unless you have a custom build of Python.')
parser.add_option('-u', '--update', dest='update', action='store_true', default=False, help='check for an updated version of test.py')
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')
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')

options, args = parser.parse_args()
parser.destroy()
del parser

if options.update:
	try:
		urllib, urlread = compat.import_urllib()
	except ImportError:
		sys.exit('Error: the urllib Python module is missing. Without it, an automatic update is impossible.')
	
	latesttext = urlread('http://chortos.selfip.net/~astiob/test.py/version.txt')
	latest = latesttext.split('.')
	installed = version.split('.')
	update = None
	
	if latest[0] > installed[0]:
		update = 'major'
	elif latest[0] == installed[0]:
		if latest[1] > installed[1]:
			update = 'feature'
		elif latest[1] == installed[1]:
			if latest[2] > installed[2]:
				update = 'bug-fixing'
			elif latest[2] == installed[2]:
				say('You are using the latest publicly available version of test.py.')
				sys.exit()
	
	if not update:
		say('Your copy of test.py is newer than the publicly available version.')
		sys.exit()
	
	say('A ' + update + ' update to test.py is available. Downloading...')
	sys.stdout.flush()
	urllib.urlretrieve('http://chortos.selfip.net/~astiob/test.py/test.py', sys.argv[0])
	say('Downloaded and installed. Now you are using test.py ' + latesttext + '.')
	sys.exit()

def import_error(e):
	say('Your installation of test.py is incomplete:', str(e).lower() + '.', file=sys.stderr)
	sys.exit(3)

import os, config

# Do this check here so that if we have to warn them, we do it as early as possible
if options.pause and not hasattr(config, 'pause'):
	try:
		# If we have getch, we don't need config.pause
		import msvcrt
		msvcrt.getch.__call__
	except Exception:
		if os.name == 'posix':
			config.pause = 'read -s -n 1'
			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.')
			sys.stdout.flush()
		elif os.name == 'nt':
			config.pause = 'pause'
		else:
			sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.')

try:
	from problem import *
except ImportError as e:
	import_error(e)

# Support single-problem configurations
try:
	shouldprintnames = len(config.tasknames) > 1
except Exception:
	shouldprintnames = True

ntasks = 0
nfulltasks = 0
maxscore = 0
realscore = 0

for taskname in config.tasknames:
	problem = Problem(taskname)
	
	if ntasks: say()
	if shouldprintnames: say(taskname)
	
	if options.copyonly:
		problem.copytestdata()
	else:
		real, max = problem.test()
	
	ntasks += 1
	nfulltasks += (real == max)
	realscore += real
	maxscore += max

if options.copyonly:
	sys.exit()

if ntasks != 1:
	say()
	say('Grand grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks))

if options.pause:
	say('Press any key to exit...', end='')
	sys.stdout.flush()
	
	try:
		import msvcrt
		msvcrt.getch()
	except Exception:
		os.system(config.pause + ' >' + os.devnull)