Pylinted for job_buildcheck_post.py and refactoring
authorLingchaox Xin <lingchaox.xin@intel.com>
Mon, 18 Mar 2013 06:54:29 +0000 (14:54 +0800)
committerLingchaox Xin <lingchaox.xin@intel.com>
Mon, 18 Mar 2013 06:54:29 +0000 (14:54 +0800)
Change-Id: I497638a5dd39e9b4724d87357b5b548087f252cb

job_buildcheck_post.py

index d04edcd..a34882e 100755 (executable)
@@ -10,97 +10,97 @@ import shutil
 import gzip
 
 # internal
-from common import obspkg
-from common import utils
-from common import git
 from common import gerrit
-from common import mapping
-from common import runner
-from common.envparas import export
 
 from common.tempbuildpkg import TempPackageBuild
 from common.send_mail import prepare_mail
 from common.buildtrigger import trigger_info
 
-envparas = ['JENKINS_HOME',
-            'WORKSPACE',
-            'GERRIT_SSHPORT',
-            'GERRIT_USERNAME',
-            'GERRIT_HOSTNAME',
-            'OBS_API_USERNAME',
-            'OBS_API_URL',
-            'OBS_API_USERNAME',
-            'OBS_API_PASSWD',
-            'BUILD_TAG',
-            'TRIGGER_INFO',
-            'NOREPLY_EMAIL_SENDER']
-export(envparas, locals())
-
-GIT_URL = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT)
-ACTIVE_STATUS = ['building', 'scheduled', 'dispatching']
-
-g=gerrit.Gerrit(GERRIT_HOSTNAME, GERRIT_USERNAME, GERRIT_SSHPORT)
-
-def end(rc = 0):
+
+def end(tmpdir, return_code = 0):
+    """Get return code"""
     shutil.rmtree(tmpdir)
-    exit(rc)
+    return return_code
 
-if __name__ == '__main__':
+def main():
+    """The main body"""
 
     print '---[JOB STARTED]----------------------------------------'
-    tmpdir = tempfile.mkdtemp(prefix=WORKSPACE+'/')
 
-    event_dict = trigger_info(TRIGGER_INFO)
+    _gerrit = gerrit.Gerrit(os.getenv('GERRIT_HOSTNAME'), \
+            os.getenv('GERRIT_USERNAME'), os.getenv('GERRIT_SSHPORT'))
+
+    work_space = os.getenv('WORKSPACE')
+
+    tmpdir = tempfile.mkdtemp(prefix=(work_space + '/'))
+
+    active_status = ['building', 'scheduled', 'dispatching']
 
-    if event_dict['type'] == 'OBS_REPO_PUBLISH_STATE' and event_dict['state'] != 'published':
+    event_dict = trigger_info(os.getenv('TRIGGER_INFO'))
+
+    if event_dict['type'] == 'OBS_REPO_PUBLISH_STATE' and \
+            event_dict['state'] != 'published':
         print 'repo is publishing, exit now'
-        end()
+        return end(tmpdir)
 
     project = event_dict['project']
     repo = event_dict['repo']
-    obs_dest_prj = project[len('home:%s:'%OBS_API_USERNAME) : project.index(':buildcheck:')]
+    obs_api_username = os.getenv('OBS_API_USERNAME')
+    obs_dest_prj = project[len('home:%s:' % obs_api_username) : \
+            project.index(':buildcheck:')]
 
-    msg='[BUILD CHECK RESULT] This change is built against OBS project %s: \n\n %-15s%-15s%-15s' %(obs_dest_prj, "repository", "arch", "result")
+    msg = '[BUILD CHECK RESULT] This change is built against OBS project '\
+            '%s: \n\n %-15s%-15s%-15s' % (obs_dest_prj, "repository", \
+            "arch", "result")
 
     try:
-        temp_build = TempPackageBuild(OBS_API_URL, OBS_API_USERNAME, OBS_API_PASSWD, event_dict['project'])
+        temp_build = TempPackageBuild(os.getenv('OBS_API_URL'), \
+                obs_api_username, os.getenv('OBS_API_PASSWD'), \
+                event_dict['project'])
     except:
         print 'Exception occured when init TempPackage, exit now'
-        end()
+        return end(tmpdir)
 
     git_info = temp_build.get_git_info()
     build_results = temp_build.get_build_result()
     print 'build result: ', build_results
     if not build_results:
         print 'Build status is null, exit now'
-        end()
+        return end(tmpdir)
     for build_repo in build_results.keys():
-        for active in ACTIVE_STATUS:
+        for active in active_status:
             if active in build_results[build_repo].values():
                 print 'This package is still building, exit now'
-                end()
+                return end(tmpdir)
 
     for arch in build_results[repo]:
-        msg = msg + '\n\n %-15s%-15s%-15s' %(repo, arch, build_results[repo][arch])
-        if build_results[repo][arch] != 'succeeded' and git_info.has_key('OWNER_EMAIL') and git_info['OWNER_EMAIL']:
+        msg = msg + '\n\n %-15s%-15s%-15s' % (repo, arch, \
+                build_results[repo][arch])
+        if build_results[repo][arch] != 'succeeded' and \
+                git_info.has_key('OWNER_EMAIL') and git_info['OWNER_EMAIL']:
             log = temp_build.get_build_log(repo, arch)
             # write log to a file as attachment
-            buildlog_gz = "%s/buildlog-%s-%s-%s.log.gz" %(WORKSPACE, repo, arch, build_results[repo][arch])
+            buildlog_gz = "%s/buildlog-%s-%s-%s.log.gz" % (work_space, \
+                    repo, arch, build_results[repo][arch])
             log_file = gzip.open(buildlog_gz, "wb")
             log_file.write(log)
             log_file.close()
-            prepare_mail("%s.env" %(BUILD_TAG), '[BUILD CHECK RESULT]: Your change for %s is %s build against %s' %(git_info['GIT_PROJECT'], build_results[repo][arch], obs_dest_prj), msg, NOREPLY_EMAIL_SENDER, '%s' % git_info['OWNER_EMAIL'], attachment = buildlog_gz)
+            prepare_mail("%s.env" % os.getenv('BUILD_TAG'), \
+                    '[BUILD CHECK RESULT]: '\
+                    'Your change for %s is %s build against %s' % (\
+                    git_info['GIT_PROJECT'], build_results[repo][arch], \
+                    obs_dest_prj), msg, os.getenv('NOREPLY_EMAIL_SENDER'), \
+                    '%s' % git_info['OWNER_EMAIL'], attachment = buildlog_gz)
             os.system("rm %s" %(buildlog_gz))
 
-    #if not build_results:
-    #    msg = "[BUILD CHECK RESULT] The project %s is not ready for build, please contact the build systerm administrator" %(buildcheck_project)
-    #    g.review(commit = GERRIT_PATCHSET_REVISION, message = msg)
-    #    end(1)
-
     temp_build.del_itself()
 
     print msg
     if git_info.has_key('COMMIT_ID') and git_info['COMMIT_ID']:
-        g.review(commit = git_info['COMMIT_ID'], message = msg)
+        _gerrit.review(commit = git_info['COMMIT_ID'], message = msg)
 
-    end()
+    return end(tmpdir)
+
+if __name__ == '__main__':
+    import sys
+    sys.exit(main())