#!/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.11 2002/09/29 22:33:17 ray Exp $

import os
import re
import time
import string
import gsconf
import anydbm

# configuration variables

YESTERDAY = time.strftime("%Y%m%d", time.localtime(time.time() - (24*60*60)))
MAIL_SERVER = 'localhost'
REPORT_EMAIL = 'gs-regression@ghostscript.com'
MAIL_FROM = 'regression@casper.ghostscript.com'
HEAD_DIR = gsconf.gsroot
REGRESSION_SCRIPT = '/home/regression/regression/run_regression 2>&1'
DIFF_SCRIPT = '/home/regression/regression/testdiff %s 2>&1' % (YESTERDAY,)
LOG_STDOUT = gsconf.log_stdout
LOG_STDERR = gsconf.log_stderr
CVS_LOG_STDOUT = "/home/regression/cvs-stdout.log"
CVS_LOG_STDERR = "/home/regression/cvs-stderr.log"
MAKE_LOG_STDOUT = "/home/regression/make-stdout.log"
MAKE_LOG_STDERR = "/home/regression/make-stderr.log"
INSTALL_LOG_STDOUT = "/home/regression/install-stdout.log"
INSTALL_LOG_STDERR = "/home/regression/install-stderr.log"

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 create_dailydb():
    # create a new empty database
    db = anydbm.open(gsconf.dailydb, "n")
    db.close()

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("date >> " + CVS_LOG_STDOUT) != 0: return -1
	if os.system("date >> " + CVS_LOG_STDERR) != 0: return -1
	if os.system("cvs update -Pd >> " + CVS_LOG_STDOUT + " 2>> " + CVS_LOG_STDERR) != 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 > " + MAKE_LOG_STDOUT + " 2> " + MAKE_LOG_STDERR) != 0: return -1
	if os.system("make install > " + INSTALL_LOG_STDOUT + " 2> " + INSTALL_LOG_STDERR) != 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)

# create the daily results database
create_dailydb()

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()

# now do the daily diff
diff = 'The following regression changes happened since yesterday\'s report:\n'

p = os.popen(DIFF_SCRIPT)
if p:
	for line in p.readlines():
		diff = diff + line
	p.close()

diff = diff + "\n\nThe complete list of regressions for today:\n"
msg = diff + msg

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