[Zope-CVS] CVS: Packages/TestScripts - backgroundtester.py:1.1

Jens Vagelpohl jens@zope.com
Thu, 24 Jan 2002 08:34:59 -0500


Update of /cvs-repository/Packages/TestScripts
In directory cvs.zope.org:/tmp/cvs-serv29722

Added Files:
	backgroundtester.py 
Log Message:
A unittesting script in python. More docs to follow.

It is called from crontab and the user must cd to the directory in which the script resides first. write perms are needed for the respective user in that directory.



=== Added File Packages/TestScripts/backgroundtester.py ===
#!/usr/local/bin/python2.1
#
#   Backgroundtester : Build and run tests
#

CVSROOT = ':pserver:anonymous@cvs.zope.org:/cvs-repository'
RECIPIENTS = 'jens@zope.com'
SMTPHOST = 'smtp.zope.com'
PYTHON = '/usr/local/bin/python2.1'
LOGFILENAME = 'testresults'
LOG_BUFFER = []

ZOPES = { 'HEAD' : 'Trunk'
        , 'Zope-2_5-branch' : '2.5 Branch'
        }

PACKAGES = { 'CMF' : ( 'CMF', 'CMF' )
           , 'ZEO' : ( 'ZEO', 'ZEO' )
           }

TEST_DIVIDER = """\n\n***************************************************
***************************************************
**
** Testing Zope %s (%s)
**
***************************************************
***************************************************\n
"""

import os, time, smtplib

def _get_time( format='date'):
    """ Get a nice time representation """
    if format == 'date':
        t_str = time.strftime( '%Y/%m/%d %H:%M:%S' )
    else:
        t_str = time.strftime( '%H:%M:%S' )

    return t_str


def _cleanup( home_dir ):
    """ Remove crud """
    t_str = _get_time( format='time' )
    LOG_BUFFER.append( '  - Cleaning out old files (%s)' % t_str )
    if os.path.isfile( LOGFILENAME ):
        os.remove( LOGFILENAME )

    for cvs_package, subdir in PACKAGES.values():
        subdir_path = os.path.join( home_dir, subdir )

        if os.path.isdir( subdir_path ):
            os.system( 'rm -rf %s 2>&1' % subdir_path )

        if os.path.isfile( subdir_path ):
            os.remove( subdir_path )

    for zope_dir in ZOPES.keys():
        zopedir_path = os.path.join( home_dir, zope_dir )

        if os.path.isdir( zopedir_path ):
            os.system( 'rm -rf %s 2>&1' % zopedir_path )

        if os.path.isfile( zopedir_path ):
            os.remove( zopedir_path )


def _checkout( home_dir ):
    """ Do all checkouts """
    cvspass_line = '%s A' % CVSROOT
    cvspass_path = os.path.join( os.environ['HOME'], '.cvspass' )
    if not os.path.isfile( cvspass_path ):
        cvspass_file = open( cvspass_path, 'w' )
        cvspass_file.write( cvspass_line + '\n' )
        cvspass_file.close()
    else:
        cvspass_file = open( cvspass_path, 'r' )
        cvspass_content = cvspass_file.read()
        cvspass_file.close()

        if cvspass_content.find( CVSROOT ) == -1:
            cvspass_file = open( cvspass_path, 'w' )
            cvspass_file.write( cvspass_line + '\n' )
            cvspass_file.close()


    for tag, label in ZOPES.items():
        t_str = _get_time( format='time' )
        LOG_BUFFER.append( '  - Checking out Zope %s (%s)' % ( label, t_str ) )
        subdir_path = os.path.join( home_dir, tag )

        os.system( 'cvs -Q co -d %s -r%s Zope >/dev/null' % ( tag, tag ) )
    
    for cvs_package, subdir in PACKAGES.values():
        t_str = _get_time( format='time' )
        LOG_BUFFER.append( '  - Checking out %s (%s)' % ( cvs_package, t_str ) )
        subdir_path = os.path.join( home_dir, subdir )

        os.system( 'cvs -Q co -d %s %s >/dev/null' % ( subdir, cvs_package ) )

def _wire( home_dir ):
    """ Wire things up correctly """
    zeo_loc = os.path.join( home_dir, PACKAGES.get( 'ZEO' )[1], 'ZEO' )
    cmf_loc = os.path.join( home_dir, PACKAGES.get( 'CMF' )[1] )
    cmf_packages = ( 'CMFCalendar', 'CMFCore', 'CMFCollector'
                   , 'CMFDefault', 'CMFTopic', 'CMFTracker'
                   , 'CMFWiki', 'DCWorkflow'
                   )

    t_str = _get_time( format='time' )
    LOG_BUFFER.append( '  - Linking ZEO (%s)' % t_str )
    for subdir in ZOPES.keys():
        os.chdir( os.path.join( home_dir, subdir, 'lib', 'python' ) )
        os.system( 'ln -s %s 2>&1' % zeo_loc )

    t_str = _get_time( format='time' )
    LOG_BUFFER.append( '  - Linking CMF Packages (%s)' % t_str )
    for subdir in ZOPES.keys():
        p_path = os.path.join( home_dir, subdir, 'lib', 'python', 'Products' )
        os.chdir( p_path )

        for cmf_package in cmf_packages:
            os.system( 'ln -s %s 2>&1' % os.path.join( cmf_loc, cmf_package ) )

def _build( home_dir ):
    """ Compile all those items that need compiling """
    for subdir, label in ZOPES.items():
        zope_loc = os.path.join( home_dir, subdir )
        t_str = _get_time( format='time' )
        LOG_BUFFER.append( '  - Building Zope %s (%s)' % ( label, t_str ) )
        os.chdir( zope_loc )
        os.system( '%s wo_pcgi.py >/dev/null' % PYTHON )


def _test( home_dir ):
    """ Run the actual tests """
    log_file = open( os.path.join( home_dir, LOGFILENAME ), 'w' )
    log_file.write( '\n'.join( LOG_BUFFER ) )
    log_file.close()

    for subdir, label in ZOPES.items():
        zope_loc = os.path.join( home_dir, subdir )

        log_file = open( os.path.join( home_dir, LOGFILENAME ), 'a' )
        t_str = _get_time( format='time' )
        log_file.write( TEST_DIVIDER % ( label, t_str ) )
        log_file.close()
    
        os.chdir( zope_loc )
        os.system( '%s utilities/testrunner.py -v0 -a >>%s/%s 2>&1' % (
                    PYTHON, home_dir, LOGFILENAME ) )


def _mail( home_dir ):
    """ Mail out the result """
    res_file = open( os.path.join( home_dir, LOGFILENAME ), 'r' )
    msg = res_file.read()
    res_file.close()

    m_to = RECIPIENTS.split( ',' )
    m_from = '"OS X Unit Tests" <jens@zope.com>'
    m_subj = 'OS X Results from %s' % _get_time()
    hdr = 'From: %s\012To: %s\012Subject: %s\012' % ( m_from
                                                    , RECIPIENTS
                                                    , m_subj
                                                    )
    mail_server = smtplib.SMTP( SMTPHOST )
    try:
        mail_server.sendmail( m_from, m_to, hdr + msg )
        mail_server.quit()
    except:
        pass


def main():
    """ Run the script """
    pkgs = ', '.join( PACKAGES.keys() )
    msg = '*** Zope unit tests with packages %s (%s) ***\n\n'
    LOG_BUFFER.append( msg % ( pkgs, _get_time() ) )
    HOME = os.getcwd()
    os.environ['CVSROOT'] = CVSROOT

    _cleanup( HOME )
    _checkout( HOME )
    _wire( HOME )
    _build( HOME )
    _test( HOME )
    _mail( HOME )


if __name__ == '__main__':
    main()