From 99eadfc65e8158bdd6be1d09de54a6e4520cb6ca Mon Sep 17 00:00:00 2001 From: Hasan Wan Date: Fri, 27 Jul 2012 15:48:52 +0800 Subject: [PATCH] Implement build check job Signed-off-by: Hasan Wan --- buildcheck.py | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ envparas.py | 4 ++ 2 files changed, 127 insertions(+) create mode 100755 buildcheck.py diff --git a/buildcheck.py b/buildcheck.py new file mode 100755 index 0000000..4dd708d --- /dev/null +++ b/buildcheck.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python +# vim: ai ts=4 sts=4 et sw=4 + +"""This script will do policy check when patch is created in gerrit. +""" + +import os +import re +import tempfile +import glob +import shutil +import time +import runner +import utils +import git +import obspkg +from envparas import * + +import gbp.rpm +import gbp +import buildservice + +mappingprj = 'scm/git-obs-mapping' +gerritcmd = 'ssh -p %s %s@%s gerrit' % (GERRIT_SSHPORT, GERRIT_USERNAME, GERRIT_HOSTNAME) +giturl = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT) + +def end(rc = 0): + shutil.rmtree(tmpdir) + exit(rc) + +def update_git_project(workdir, prj): + result = True + prjdir = os.path.join(workdir, prj) + + with utils.Workdir(workdir): + if os.path.isdir(prjdir): + gitprj = git.Git(prjdir) + if not gitprj.pull(): + shutil.rmtree(prjdir) + if runner.runtool('git clone %s/%s %s' % (giturl, prj, prj))[0] != 0: + result = False + else: + if runner.runtool('git clone %s/%s %s' % (giturl, prj, prj))[0] != 0: + result = False + + if not result: + shutil.rmtree(prjdir) + return result + +if __name__ == '__main__': + + print '---[JOB STARTED]----------------------------------------' + tmpdir = tempfile.mkdtemp(prefix=WORKSPACE+'/') + prjdir = os.path.join(tmpdir, GERRIT_PROJECT) + prjpath, prj = os.path.split(GERRIT_PROJECT) + + if not os.path.isfile('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, mappingprj)): + if not utils.retry(update_git_project, (os.path.join(JENKINS_HOME, 'git'), mappingprj)): + end(1) + + if utils.retry(update_git_project, (os.path.join(JENKINS_HOME, 'git'), GERRIT_PROJECT)): + shutil.copytree(os.path.join(JENKINS_HOME, 'git', GERRIT_PROJECT), prjdir, True) + if not utils.retry(update_git_project, (tmpdir, GERRIT_PROJECT)): + end(1) + + mygit = git.Git(prjdir) + mygit.fetch('%s/%s' % (giturl, GERRIT_PROJECT), GERRIT_REFSPEC, '-t') + mygit.checkout('FETCH_HEAD') + + packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging')) + + mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, mappingprj), GERRIT_PROJECT, GERRIT_BRANCH) + + print "Project Mapping info: ", mapping + + for target in mapping: + (obs_dest_prj, obs_stg_prj, obs_pkg) = target + + buildcheck_project = "home:%s:%s:%s" %(OBS_USERNAME, obs_dest_prj, BUILD_NUMBER) + + if not obs_dest_prj: + continue + print '--------------------' + print "Call gbs command: ", "gbs -v rb -B %s -T %s %s" %(obs_dest_prj, buildcheck_project, prjdir) + os.system("gbs rb -B %s -T %s %s" %(obs_dest_prj, buildcheck_project, prjdir)) + + if obs_pkg: + specfile = '%s/%s/%s.spec' % (prjdir, packagingdir, obs_pkg) + else: + specfile = utils.guess_spec('%s/%s' % (prjdir, packagingdir)) + if not specfile: + specfile = '%s/%s/%s.spec' % (prjdir, packagingdir, prj) + try: + spec = gbp.rpm.parse_spec(specfile) + except GbpError, err: + msger.error('%s' % err) + + bs = buildservice.BuildService(apiurl=OBS_API_URL, oscrc=OBS_OSCRC_PATH) + + # Loop until no building status + loop = True + while loop: + time.sleep(10) + status = bs.get_results(buildcheck_project, spec.name) + for build_repo in status.keys(): + if 'building' in status[build_repo].values(): + loop = True + else: + loop = False + + print '========================================' + print "OBS Package Status" + print status + print '========================================' + + msg='[BUILD CHECK RESULT] This change is built against OBS project %s: \n\n %-15s%-15s%-15s' %(obs_dest_prj, "repository", "arch", "result") + result = [] + for repo in status.keys(): + for arch in status[repo]: + msg = msg + '\n\n %-15s%-15s%-15s' %(repo, arch, status[repo][arch]) + print msg + runner.quiet('%s %s %s,%s --message \'"%s"\'' % (gerritcmd, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, msg)) + end() diff --git a/envparas.py b/envparas.py index df3981c..0ccced3 100644 --- a/envparas.py +++ b/envparas.py @@ -39,6 +39,10 @@ envargs = ['OBS_DEST_PROJECT', 'MYSQL_USERNAME', 'MYSQL_PASSWORD', 'MYSQL_DB_NAME', + 'JENKINS_HOME', + 'JOB_NAME', + 'BUILD_NUMBER', + 'WORKSPACE', 'MAPPING_PROJECT'] __all__ = envargs -- 2.7.4