#!/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.5 2002/05/30 21:43:10 jack Exp $

import os
import re
import time
import string
import gsconf

# configuration variables

MAIL_SERVER = 'localhost'
REPORT_EMAIL = 'gs-regression@ghostscript.com'
MAIL_FROM = 'regression@casper.ghostscript.com'
HEAD_DIR = gsconf.gsroot
REGRESSION_SCRIPT = 'run_regression 2>&1'
LOG_STDOUT = gsconf.log_stdout
LOG_STDERR = gsconf.log_stderr

def change_gsproduct(file):
    tmpfile = "%s.tmp" % (file,)

    startre = re.compile("^#ifndef\ GS_PRODUCT$")
    changere = re.compile("^.*?\"[A-Za-z -]+\".*?$")
    endre = re.compile("^$")

    old = open(file, "r")
    new = open(tmpfile, "w")

    state = 0
    for line in old.readlines():
        if state == 0:
            m = startre.search(line)
            if m:
                state = 1
                
            new.write(line)
        elif state == 1:
            m = changere.search(line)
            if m:
                state = 2
                new.write("\t\"AFPL Ghostscript\"\n")
            else:
                new.write(line)
        elif state == 2:
            m = endre.search(line)
            if m:
                state = 0

            new.write(line)


    old.close()
    new.close()

    os.unlink(file)
    os.rename(tmpfile, file)

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)
	
	os.unlink("%s/src/gscdef.c" % (HEAD_DIR,))
	if os.system("cvs update -Pd > /dev/null 2> /dev/null") != 0: return -1
	change_gsproduct("%s/src/gscdef.c" % (HEAD_DIR,))
	
	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)

# start new log files
try:
    log = open(LOG_STDOUT, "w")
    log.close()
    log = open(LOG_STDERR, "w")
    log.close()
except:
    sendmail(MAIL_FROM, REPORT_EMAIL, 'error running regression', 'The log files were not able to be restartd.')
    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)
