#!/usr/bin/env python2.1

#    Copyright (C) 2001 Artifex Software Inc.
# 
# This software is provided AS-IS with no warranty, either express or
# implied.
# 
# This software is distributed under license and may not be copied,
# modified or distributed except as expressly authorized under the terms
# of the license contained in the file LICENSE in this distribution.
# 
# For more information about licensing, please refer to
# http://www.ghostscript.com/licensing/. For information on
# commercial licensing, go to http://www.artifex.com/licensing/ or
# contact Artifex Software, Inc., 101 Lucas Valley Road #110,
# San Rafael, CA  94903, U.S.A., +1(415)492-9861.

# $Id: run_nightly,v 1.3 2002/02/21 22:44:45 giles Exp $

import os
import re
import time
import string

# configuration variables

MAIL_SERVER = 'localhost'
REPORT_EMAIL = 'gs-regression@ghostscript.com'
MAIL_FROM = 'regression@casper.ghostscript.com'
HEAD_DIR = '/path/to/cvs'
REGRESSION_SCRIPT = 'run_regression 2>&1'


def sendmail(frm, to, subject, text):
	import smtplib

	msg = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s' % (frm, to, subject, text)

	server = smtplib.SMTP(MAIL_SERVER)
	server.sendmail(frm, to, msg)
	server.quit()

def update_ghostscript():
	cwd = os.getcwd()
	os.chdir(HEAD_DIR)
	
	if os.system("cvs update -Pd > /dev/null 2> /dev/null") != 0: return -1
	if os.system("make clean > /dev/null 2> /dev/null") != 0: return -1
	if os.system("make > /dev/null 2> /dev/null") != 0: return -1
	if os.system("make install > /dev/null 2> /dev/null") != 0: return -1

	return 0


if update_ghostscript() != 0:
	sendmail(MAIL_FROM, REPORT_EMAIL, 'error running regression', 'There was an error updating and building ghostscript from CVS.  Please investigate.')
	exit(0)

p = os.popen(REGRESSION_SCRIPT)
if p == None:
	sendmail(MAIL_FROM, REPORT_EMAIL, 'error running regression', 'There was an error updating and building ghostscript from CVS.  Please investigate.')
	exit(0)

pattern = 'ok$'
msg = ''
for line in p.readlines():
	if re.search(pattern, line):
		continue
	msg = msg + line
	
p.close()

sendmail(MAIL_FROM, REPORT_EMAIL, 'gs regression report - %s' % (time.strftime('%Y-%m-%d'),), msg)
