#!/usr/bin/python

import re
import subprocess
import os.path
import sys
from optparse import OptionParser
from distutils.util import strtobool

class Test:
	def __init__(self,name,procs):
		self.name = name
		self.procs = procs

	def printname(self):
		print self.name


	def create_results(self):

		for i in self.procs:
			self.create_resultsprocs(i)


	# Create the result for the given number of procs
	def create_resultsprocs(self,p):
		exelist = ["mpiexec"]
		exelist.append("-n")
		exelist.append(str(p))
		exelist.append(self.name)

		success = False
		subp = subprocess.Popen(exelist, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

		output = open("CorrectResults/" + self.name + ".p" + str(p) + ".out" ,'w')

		for line in subp.stdout.readlines():
			output.write(line)
		retval = subp.wait()
		output.close();
		print retval


	def testprocs(self,p):
		exelist = ["mpiexec"]
		exelist.append("-n")
		exelist.append(str(p))
		exelist.append(self.name)

		success = False
		subp = subprocess.Popen(exelist, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

		output = open(self.name + ".p" + str(p) + ".out" ,'w')

		for line in subp.stdout.readlines():
			output.write(line)
		retval = subp.wait()
		output.close();
		print retval



class TestSuite:
	def __init__(self):

		self.tests = []	
		self.tests.append(Test("testSparse",[1,2,3]));

	def printnames(self):
		for t in self.tests:
			t.printname()


	def create_results(self):
		print "Creating "
		for t in self.tests:
			t.create_results()




suite = TestSuite()



# Parse input parameters
parser = OptionParser()
parser.add_option("-c", "--create", action="store_true",
	dest="create", help="Create the CorrectResults files")
(options, args) = parser.parse_args()
	

if(options.create):
	print "Do you want to rengerate the CorrectResults files [Yes/No]:",
	confirm = None
	try:
		confirm = strtobool(raw_input())
 	except ValueError:
		confirm = False

	if confirm:
		suite.create_results()
	else:
		print "None"
else:
	suite.printnames()

















# Program and number of processes
#program = "mg"
#nprocs = [2,4,8,16]

# Message
#print "Checking:", program
#print "------------------------"

#if not os.path.exists(program):
#	print "Error: Program not found"
#	sys.exit(0)


# Loop for the processes
#for p in nprocs:
	
	# Create the execution line
#	exelist = ["mpiexec"]
#	exelist.append("-n")
#	exelist.append(str(p))
#	exelist.append(program)


	# Execute the program
#	success = False
#	subp = subprocess.Popen(exelist, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#	for line in subp.stdout.readlines():
#		if re.search('VERIFICATION SUCCESSFUL',line):
#			success = True
#	retval = subp.wait()


	# Print the message
#	print "[" + str(p).rjust(2) + "] ",
#	if success:
#		print "Ok"
#	else:
#		print "WRONG !!!"













