view upreckon/upreckon-vcs @ 246:1bc89faac941 2.04

Fixed: match='re' could produce duplicate test identifiers files.Files.regexp(pattern) now makes sure to return only one metafile for each matching virtual path, namely, the one that would be returned for that virtual path by files.Files.from_virtual_path.
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 03 Oct 2013 01:19:09 +0300
parents 478d4217fe03
children bbd735702dc4
line wrap: on
line source

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

from __future__ import division, with_statement
import optparse

from upreckon import compat
from upreckon.compat import *

parser = optparse.OptionParser(version='Upreckon 2.04.0 ($$REV$$)', epilog='Python 2.6 or newer is required.')
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')
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)')
parser.add_option('--list-problems', action='store_true', default=False, help='just list all problem names')
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')
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')
parser.add_option('-k', '--skim', action='store_true', default=False, help='skip test groups as soon as one test case is failed')
parser.add_option('--no-time-limits', dest='no_maxtime', action='store_true', default=False, help='disable all time limits')

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

from upreckon import config
import os, subprocess, sys

if options.legacy:
	compat.pseudobuiltins += 'xrange',

if options.list_problems:
	options.pause = False

from upreckon import testcases

try:
	from upreckon.testcases import pause
except ImportError:
	pause = None

try:
	globalconf = config.load_global()
	
	# Do this check here so that if we have to warn them, we do it as early as possible
	if options.pause and not pause and not hasattr(globalconf, 'pause'):
		if os.name == 'posix':
			globalconf.pause = 'read -s -n 1'
			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)
			sys.stderr.flush()
		elif os.name == 'nt':
			globalconf.pause = 'pause'
		else:
			sys.exit('Error: configuration variable pause is not defined and cannot be devised automatically.')
	
	from upreckon.problem import *
	
	# Support single-problem configurations
	if globalconf.problems is None:
		shouldprintnames = False
		globalconf.multiproblem = False
		globalconf.problems = os.path.curdir,
	else:
		globalconf.multiproblem = True
		shouldprintnames = True
	
	if options.list_problems:
		for taskname in globalconf.problems:
			say(taskname)
		sys.exit()
	
	ntasks = 0
	nfulltasks = 0
	maxscore = 0
	realscore = 0
	
	for taskname in options.problems or globalconf.problems:
		problem = Problem(taskname)
		
		if ntasks: say()
		if shouldprintnames: say(taskname)
		
		real, max = problem.test()
		
		ntasks += 1
		nfulltasks += real == max
		realscore += real
		maxscore += max
	
	if ntasks != 1:
		say()
		say('Grand total: %g/%g weighted points; %d/%d problems solved fully' % (realscore, maxscore, nfulltasks, ntasks))
except KeyboardInterrupt:
	say('Exiting due to a keyboard interrupt.', end='', file=sys.stderr)
	sys.stderr.flush()
	try:
		import signal
		signal.signal(signal.SIGINT, signal.SIG_DFL)
		os.kill(os.getpid(), signal.SIGINT)
	except Exception:
		pass
	# Do this even if we got no exceptions, just in case
	say(file=sys.stderr)
	sys.exit(1)

try:
	if options.pause:
		say('Press any key to exit...')
		sys.stdout.flush()
		
		if pause:
			pause()
		elif callable(globalconf.pause):
			globalconf.pause()
		else:
			with open(os.devnull, 'w') as devnull:
				subprocess.call(globalconf.pause, shell=True, stdout=devnull, stderr=subprocess.STDOUT)
except KeyboardInterrupt:
	pass