#!/usr/bin/env python
# -*- Mode: python -*-

#    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.17.2.2 2004/05/12 04:06:00 giles Exp $

import os
import sys
import re
import time
import string
import gsconf
import anydbm
from popen2 import Popen4

# configuration variables

YESTERDAY = time.strftime("%Y%m%d", time.localtime(time.time() - (24*60*60)))
YESTERDAY_CTIME = time.ctime(time.time() - (24*60*60))

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 read_fnord(file):
    if os.path.exists(file):
        f = open(file)
        keyword = f.readline()
        keyword = keyword.strip()
        f.close()
        return keyword
    else:
        return ''

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

    keyword = read_fnord(gsconf.codedir + 'fnord')
    if keyword:
        msg = 'From: %s\r\nTo: %s\r\nSubject: %s\r\nX-Fnord: %s\r\n\r\n%s' % (frm, to, subject, keyword, text)
    else:
        msg = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s' % (frm, to, subject, text)

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

def die(msg):
    sendmail(gsconf.report_from, gsconf.report_to, "error running regression", msg)
    sys.exit(0)

def update_ghostscript():
    cwd = os.getcwd()
    os.chdir(gsconf.gsroot)
	
    os.unlink("%s/src/gscdef.c" % (gsconf.gsroot,))
    
    if os.system("date >> " + gsconf.cvs_log_stdout) != 0: return (1, None, None)
    if os.system("date >> " + gsconf.cvs_log_stderr) != 0: return (1, None, None)

    starttime = time.time()

    if os.system("cvs update -Pd >> " + gsconf.cvs_log_stdout + " 2>> " +
                 gsconf.cvs_log_stderr) != 0: return (1, None, None)

    endtime = time.time()
    
    change_gsproduct("%s/src/gscdef.c" % (gsconf.gsroot,))
	
    if os.system("make clean > /dev/null 2> /dev/null") != 0:
        return (-1, None, None)
    
    if os.system("make > " + gsconf.make_log_stdout + " 2> " +
                 gsconf.make_log_stderr) != 0: return (2, None, None)

    if os.system("make install > " + gsconf.install_log_stdout + " 2> " + \
                 gsconf.install_log_stderr) != 0: return (3, None, None)

    return (0, starttime, endtime)

def get_file(f):
    file = open(f, 'r')
    lines = file.readlines()
    file.close()
    return string.join(lines, '')

err, starttime, endtime = update_ghostscript()
if err != 0:
    if err == 1:
        msg = "Ghostscript update failed updating from CVS.\n\n"
        msg = msg + "stdout log:\n\n"
        msg = msg + get_file(gsconf.cvs_log_stdout)
        msg = msg + "\nstderr log:\n\n"
        msg = msg + get_file(gsconf.cvs_log_stderr)
        die(msg)
    elif err == 2:
        msg = "Ghostscript update failed during make.\n\n"
        msg = msg + "stdout log:\n\n"
        msg = msg + get_file(gsconf.make_log_stdout)
        msg = msg + "\nstderr log:\n\n"
        msg = msg + get_file(gsconf.make_log_stderr)
        die(msg)
    elif err == 3:
        msg = "Ghostscript update failed during install.\n\n"
        msg = msg + "stdout log:\n\n"
        msg = msg + get_file(gsconf.install_log_stdout)
        msg = msg + "\nstderr log:\n\n"
        msg = msg + get_file(gsconf.install_log_stderr)
        die(msg)
    else:
        die("Unknown error updating Ghostscript")

# start new log files
try:
    log = open(gsconf.log_stdout, "w")
    log.close()
    log = open(gsconf.log_stderr, "w")
    log.close()
except:
    die("The log files were not able to be restarted")

p = os.popen(gsconf.regression_script + " --track 2>&1")
if p == None:
    die("Could not open and run the regression script.")

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(gsconf.diff_script + " " + YESTERDAY + " 2>&1")
if p:
    for line in p.readlines():
        diff = diff + line
    p.close()

# include the main body only on Mondays
if time.gmtime()[6] == 0:
    diff = diff + "\n\nThe complete list of regressions for today:\n"
    msg = diff + msg

# Get updated baselines
baselines = 'The following files had their baselines updated:\n'
p = Popen4(gsconf.get_baselines + " '" + YESTERDAY_CTIME + "'", 1)
if p:
    for line in p.fromchild.readlines():
        baselines = baselines + line
    p.wait()

baselines = baselines + "\n\n"
msg = baselines + msg
  
# Prepend checkout times
times = "CVS checkout for this report started at " + time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime(starttime)) + "\n"
times = times + "CVS checkout ended at " + time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime(endtime)) + "\n\n"

msg = times + msg

retry = 1
while retry:
    try:
        sendmail(gsconf.report_from, gsconf.report_to, 'gs regression report - %s' % (time.strftime('%Y-%m-%d'),), msg)
	time.sleep(5)
	retry = 0
    except:
        pass
