#!/usr/bin/python
#
#  Build slave script.
#

import pysvn, os, time
from optparse import OptionParser

parser = OptionParser()

parser.add_option("-b", "--build-first",
                  action="store_true", dest="build_first", default=False,
                  help="Build on startup")

parser.add_option("-c", "--clean-first",
                  action="store_true", dest="clean_first", default=False,
                  help="Clean on startup")

parser.add_option("-C", "--clean-every-time",
                  action="store_true", dest="clean_every_time", default=False,
                  help="Clean before every build")

parser.add_option("-k", "--keep-rebuilding",
                  action="store_true", dest="keep_rebuilding", default=False,
                  help="Rebuild even if there are no updates to svn")

(options, args) = parser.parse_args()


client = pysvn.Client()

wc_path = r'@CMAKE_SOURCE_DIR@'

def do_build(clean_):
    if clean_:
        clean = 'clean'
    else:
        clean = ''
    if os.name == 'nt':
        cmd = 'nmake /I ' + clean + ' slave-start test slave-finish'
    else:
        cmd = 'make -i ' + clean + ' slave-start test slave-finish'
    print "Starting build:\n>>> ", cmd
    os.system(cmd)
    

if options.build_first:
    do_build(options.clean_first)

while True:
    try:
        svn_entry = client.info(wc_path)

        print "Wc has url %s rev %d.\nChecking for updates." \
            % (svn_entry.url, svn_entry.revision.number)

        ds = client.diff_summarize(url_or_path1=svn_entry.url,
                                   revision1=pysvn.Revision(pysvn.opt_revision_kind.number, 
                                                            svn_entry.revision.number),
                                   url_or_path2=svn_entry.url,
                                   revision2=pysvn.Revision(pysvn.opt_revision_kind.head)
                                   )

        if len(ds):
            print "There are %d changesets:" % len(ds)
            for j in ds:
                print ">>>", j.path
            print "Updating."
            client.update(wc_path)
        if len(ds) or options.keep_rebuilding:
            do_build(options.clean_every_time)
        else:
            print "No updates."
    except Exception, e:
        print e
        print "Error.  Will retry."
    
    print "Sleeping %d seconds" % @BOOST_BUILD_SLAVE_SLEEP_DURATION@
    time.sleep(@BOOST_BUILD_SLAVE_SLEEP_DURATION@)

        

