Pylinted for job_policycheck.py and git-obs-mapping cloning bug fixed
authorLingchaox Xin <lingchaox.xin@intel.com>
Tue, 19 Mar 2013 05:14:28 +0000 (13:14 +0800)
committerLingchaox Xin <lingchaox.xin@intel.com>
Fri, 22 Mar 2013 07:16:51 +0000 (15:16 +0800)
Change-Id: Ia7791f199acd7937197f3d2c6762818ad8ef5b68

job_policycheck.py

index 3a39071..e88f7d4 100755 (executable)
@@ -5,95 +5,90 @@
 """
 
 import os
-import re
 import tempfile
-import glob
 import shutil
+import sys
 
 # internal module
-from common import runner
 from common import utils
+from common.gerritevent import get_gerrit_event
 from common.git import Git, clone_gitproject
-from common import obspkg
 from common import gerrit
 from common import mapping
 from common import rpmlint
-from common.envparas import *
 from common.tempbuildpkg import TempPackageBuild
 import gbp.rpm
 
-envparas = ['JENKINS_HOME',
-            'GERRIT_SSHPORT',
-            'GERRIT_USERNAME',
-            'GERRIT_HOSTNAME',
-            'GERRIT_PROJECT',
-            'GERRIT_BRANCH',
-            'GERRIT_REFSPEC',
-            'GERRIT_PATCHSET_NUMBER',
-            'GERRIT_PATCHSET_REVISION',
-            'GERRIT_CHANGE_NUMBER',
-            'GERRIT_CHANGE_OWNER_EMAIL',
-            'OBS_API_USERNAME',
-            'OBS_API_URL',
-            'OBS_API_USERNAME',
-            'OBS_API_PASSWD',
-            'BUILD_NUMBER',
-            'WORKSPACE',
-            'MAPPING_PRJ',
-            'GIT_CACHE_DIR']
-export(envparas, locals())
-GIT_URL = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT)
-
-def end(rc = 0):
+def end(tmpdir, return_code = 0):
+    """Get return code"""
     shutil.rmtree(tmpdir)
-    exit(rc)
+    return return_code
 
 def check_obs_newpkg(obsprj, pkg):
+    """Check the pkg whether it is a new package in obs"""
     pkglist = obsprj.list()
     if pkg in pkglist:
         return False
     else:
         return True
 
-def check_spec(spec):
+def check_spec(spec, prjdir, missedspec):
+    """Spec file check"""
     if os.path.exists(spec):
         return rpmlint.rpmlint(spec).replace(prjdir+'/', '')
     else:
         missedspec.append(os.path.basename(spec))
         return ''
 
-if __name__ == '__main__':
+def main():
+    """The main body"""
 
     print '---[JOB STARTED]----------------------------------------'
-    tmpdir = tempfile.mkdtemp(prefix=WORKSPACE+'/')
-    prjdir = os.path.join(tmpdir, GERRIT_PROJECT)
-    prjpath, prj = os.path.split(GERRIT_PROJECT)
+
+    tmpdir = tempfile.mkdtemp(prefix=(os.getenv('WORKSPACE') + '/'))
+    events = get_gerrit_event()
+    gerrit_project = events['project']
+    prjdir = os.path.join(tmpdir, gerrit_project)
+    prj = os.path.split(gerrit_project)[1]
+    git_cache_dir = os.getenv('GIT_CACHE_DIR')
+    mapping_prj = os.getenv('MAPPING_PRJ')
+    gerrit_branch = events['branch']
+    gerrit_patchset_revision = events['patchset_revision']
 
     # check whether exist git-obs-mapping.xml in local
-    if not os.path.isfile('%s/%s/git-obs-mapping.xml' % (GIT_CACHE_DIR, MAPPING_PRJ)):
-        print('Update %s/git-obs-mapping.xml to local.' % MAPPING_PRJ)
-        if not utils.retry(git.update_git_project, (GIT_CACHE_DIR, MAPPING_PRJ, GIT_URL)):
-            end(1)
+    if not os.path.isfile('%s/%s/git-obs-mapping.xml' % (git_cache_dir, \
+            mapping_prj)):
+        print('Update %s/git-obs-mapping.xml to local.' % mapping_prj)
+        if not utils.retry(clone_gitproject, (mapping_prj, git_cache_dir)):
+            return end(tmpdir, 1)
 
     # init module instance
-    mygerrit = gerrit.Gerrit(GERRIT_HOSTNAME, GERRIT_USERNAME, GERRIT_SSHPORT)
-    mymapping = mapping.Mapping('%s/%s/git-obs-mapping.xml' % (GIT_CACHE_DIR, MAPPING_PRJ))
+    mygerrit = gerrit.Gerrit(events['hostname'], \
+            events['username'], events['sshport'])
+    mymapping = mapping.Mapping('%s/%s/git-obs-mapping.xml' % (git_cache_dir, \
+            mapping_prj))
 
     # exit if this project does not map to any obs project
-    obstargets = mymapping.get_submit_mapping(GERRIT_PROJECT, GERRIT_BRANCH)
+    obstargets = mymapping.get_submit_mapping(gerrit_project, gerrit_branch)
     print 'git-obs-mapping: ', obstargets
     if not obstargets:
-        checkmappingmsg = '[IMPORTANT NOTICE]: The change for %s branch will not be submitted to OBS according configuration in gerrit scm/git-obs-mapping project. If needed, please modify scm/git-obs-mapping to enable submission to OBS.' % GERRIT_BRANCH
-        print('\n-------------------------------\ncheck obs target result:\n%s' % checkmappingmsg)
-        mygerrit.review(commit = GERRIT_PATCHSET_REVISION, message = checkmappingmsg)
-        end()
+        checkmappingmsg = '[IMPORTANT NOTICE]: The change for %s branch will ' \
+                'not be submitted to OBS according configuration in gerrit ' \
+                'scm/git-obs-mapping project. If needed, please modify ' \
+                'scm/git-obs-mapping to enable submission to OBS.' % \
+                gerrit_branch
+        print('\n-------------------------------\ncheck obs target ' \
+                'result:\n%s' % checkmappingmsg)
+        mygerrit.review(commit = gerrit_patchset_revision, \
+                message = checkmappingmsg)
+        return end(tmpdir)
 
     # clone gerrit project to local dir
-    if not clone_gitproject(GERRIT_PROJECT, prjdir):
-        end(1)
+    if not clone_gitproject(gerrit_project, prjdir):
+        return end(tmpdir, 1)
 
     mygit = Git(prjdir)
-    mygit.fetch('origin', GERRIT_REFSPEC, '-t')
+    mygit.fetch('origin', events['refspec'], '-t')
     mygit.checkout('FETCH_HEAD')
 
     packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging'))
@@ -123,42 +118,69 @@ if __name__ == '__main__':
                 # multiple specs exist under packaging, use default name
                 specfile = '%s/%s/%s.spec' % (prjdir, packagingdir, prj)
                 if not os.path.isfile(specfile):
-                    msg = "[ERROR]: Packaging directory contains multiply spec files, backend service can not decide which spec file to use. Please use OBS_PACKAGE parameter in scm/git-obs-mapping project to specify the target spec file or contact system administrator for more details."
-                    mygerrit.review(commit = GERRIT_PATCHSET_REVISION, message = msg)
+                    msg = "[ERROR]: Packaging directory contains multiply " \
+                            "spec files, backend service can not decide " \
+                            "which spec file to use. Please use OBS_PACKAGE " \
+                            "parameter in scm/git-obs-mapping project to " \
+                            "specify the target spec file or contact system " \
+                            "administrator for more details."
+                    mygerrit.review(commit = gerrit_patchset_revision, \
+                            message = msg)
                     continue
 
         print('specfile %s' % specfile)
 
+        obs_api_username = os.getenv('OBS_API_USERNAME')
         # build the package under home project of backend service user
-        buildcheck_project = "home:%s:%s:buildcheck:%s" %(OBS_API_USERNAME, obs_dest_prj, BUILD_NUMBER)
+        buildcheck_project = "home:%s:%s:buildcheck:%s" % (obs_api_username, \
+                obs_dest_prj, os.getenv('BUILD_NUMBER'))
         try:
             spec = gbp.rpm.parse_spec(specfile)
 
             # submit this change to OBS for build check
             print 'submit this change to OBS for build check...'
-            temp_build = TempPackageBuild(OBS_API_URL, OBS_API_USERNAME, OBS_API_PASSWD, buildcheck_project, spec.name)
-            if 'NoRepo' == temp_build.remote_build(obs_dest_prj, prjdir, os.path.basename(specfile), packagingdir, 'GIT_PROJECT: %s\nCOMMIT_ID: %s\nOWNER_EMAIL: %s' % (GERRIT_PROJECT, GERRIT_PATCHSET_REVISION, GERRIT_CHANGE_OWNER_EMAIL)):
-                msg = "[BUILD CHECK RESULT] Can not test build against to project %s, which currently has no build targets defined. Please contact the build system administrator." %(obs_dest_prj)
-                mygerrit.review(commit = GERRIT_PATCHSET_REVISION, message = msg)
+            temp_build = TempPackageBuild(os.getenv('OBS_API_URL'), \
+                    obs_api_username, os.getenv('OBS_API_PASSWD'), \
+                    buildcheck_project, spec.name)
+            if 'NoRepo' == temp_build.remote_build(obs_dest_prj, prjdir, \
+                    os.path.basename(specfile), packagingdir, \
+                    'GIT_PROJECT: %s\nCOMMIT_ID: %s\nOWNER_EMAIL: %s' % \
+                    (gerrit_project, gerrit_patchset_revision, \
+                    events['change_owner_email'])):
+                msg = "[BUILD CHECK RESULT] Can not test build against to " \
+                        "project %s, which currently has no build targets " \
+                        "defined. Please contact the build system " \
+                        "administrator." % obs_dest_prj
+                mygerrit.review(commit = gerrit_patchset_revision, \
+                        message = msg)
         except Exception, err:
             print '%s' % err
 
         if specfile not in speclist:
             speclist.append(specfile)
-            print('\n---------------------------------\nrpmlint check %s' % specfile)
+            print('\n---------------------------------\nrpmlint check %s' % \
+                    specfile)
             if os.path.exists(os.path.join(prjdir, packagingdir)):
-                msg = check_spec(specfile)
+                msg = check_spec(specfile, prjdir, missedspec)
             else:
-                msg = '[ERROR]: Can not find packaging directory in %s branch! Please create one and move all packaging related files, like spec file and changelog, to this directory.' % GERRIT_BRANCH
+                msg = '[ERROR]: Can not find packaging directory in %s ' \
+                        'branch! Please create one and move all packaging ' \
+                        'related files, like spec file and changelog, ' \
+                        'to this directory.' % gerrit_branch
             print(msg)
             if checkspecmsg:
                 checkspecmsg += '\n\n'
             checkspecmsg += msg
 
     if missedspec:
-        checkspecmsg += '\n\n[ERROR]: Can not find %s under packaging directory!' % ' '.join(missedspec)
+        checkspecmsg += '\n\n[ERROR]: Can not find %s under packaging ' \
+                'directory!' % ' '.join(missedspec)
 
-    print('\n-------------------------------\ncheck specfile result:\n%s' % checkspecmsg)
+    print('\n-------------------------------\ncheck specfile result:\n%s' % \
+            checkspecmsg)
     if checkspecmsg:
-        mygerrit.review(commit = GERRIT_PATCHSET_REVISION, message = checkspecmsg)
-    end()
+        mygerrit.review(commit = gerrit_patchset_revision, \
+                message = checkspecmsg)
+    return end(tmpdir)
+if __name__ == '__main__':
+    sys.exit(main())