use new method to parse git-obs-mapping xml file
authorLin Yang <lin.a.yang@intel.com>
Tue, 18 Sep 2012 09:21:23 +0000 (17:21 +0800)
committerLin Yang <lin.a.yang@intel.com>
Tue, 18 Sep 2012 09:21:23 +0000 (17:21 +0800)
common/mapping.py
job_buildcheck.py
job_policycheck.py
job_submitobs.py

index f2d181d..b32554f 100644 (file)
@@ -99,24 +99,64 @@ class Mapping():
         if not self.mapping_obj:
             raise Exception, "Fatal: parser xml file %s failed"
 
+    def __encode_to_ascii(self, source):
+        """ encode unicode list element to ascii """
+        dest = []
+        for i in source:
+            if type(i) == unicode:
+                dest.append(i.encode('ascii'))
+            else:
+                dest.append(i)
+        return dest
+
+    def __get_mapping(self, obj, branch = None):
+        """ get OBS target project under one object(project or path) """
+
+        mapping = []
+
+        if obj.submission == 'N':
+            # <project name="public/tools/ext4-utils" submission="N"/>
+            return None
+        elif branch:
+            # <project name="public/pkgs/s/shadow-utils">
+            #   <branch name="master" submission="N"/>
+            # or  <branch name="master" OBS_project="Base" OBS_staging_project="Base:build"/>
+            # </project>
+            for brch in obj.branch:
+                # Search the match branch
+                if brch.name == branch:
+                    if brch.submission == 'N':
+                        return None
+                    else:
+                        mapping.append(self.__encode_to_ascii([brch.OBS_project,
+                                                               brch.OBS_staging_project,
+                                                               brch.OBS_package]))
+        else:
+            for brch in obj.branch:
+                if brch.submission == 'N':
+                    continue
+                else:
+                    mapping.append(self.__encode_to_ascii([brch.OBS_project,
+                                                           brch.OBS_staging_project,
+                                                           brch.OBS_package]))
+        return mapping
+
     def get_submit_mapping(self, project, branch = None):
         # Search in project list first
         for prj in self.mapping_obj.project:
-            if prj.name == project:
-                if prj.submission == 'N':
-                    # <project name="public/tools/ext4-utils" submission="N"/>
+            if prj.name == project or prj.name == '/' + project:
+                # <project name="public/pkgs/s/shadow-utils">
+                #   <branch name="master" submission="N"/>
+                # or  <branch name="master" OBS_project="Base" OBS_staging_project="Base:build"/>
+                # </project>
+                mapping = self.__get_mapping(prj, branch)
+                if mapping == None:
+                    # submission is blocked, return empty list
                     return []
-                elif branch and prj.branch.name == branch:
-                    # <project name="public/pkgs/s/shadow-utils">
-                    #   <branch name="master" submission="N"/>
-                    # or  <branch name="master" OBS_project="Base" OBS_staging_project="Base:build"/>
-                    # </project>
-                    if prj.branch.submission == 'N':
-                        return []
-                    else:
-                        return [prj.branch.OBS_project,
-                                prj.branch.OBS_staging_project,
-                                prj.branch.OBS_package]
+                elif mapping:
+                    # retrun if already found matched OBS target project
+                    return mapping
+
         # Search in path list
         prj_path = project
         while True:
@@ -131,35 +171,32 @@ class Mapping():
             if prj_path == '/':
                 for path in self.mapping_obj.default.path:
                     if path.name == '/':
-                        for brch in path.branch:
-                            # Search the match branch
-                            if branch and brch.name == branch:
-                                return [brch.OBS_project,
-                                        brch.OBS_staging_project,
-                                        brch.OBS_package]
+                        mapping = self.__get_mapping(path, branch)
+                        if not mapping:
+                            # does not map to any obs project, return empty list
+                            return []
+                        else:
+                            # retrun if already found matched OBS target project
+                            return mapping
                 # No branch match, return NULL
                 return []
 
             # Search path
             for path in self.mapping_obj.default.path:
                 # path match
-                if os.path.dirname(path.name) == prj_path:
+                if os.path.dirname(path.name+'/') == prj_path:
                     # path pattern match projects blocked
                     #   <path name="/test/" submission="N"/>
-                    if path.submission == 'N':
+                    #   <path name="/apps/">
+                    #      <branch name="master" submission="N"/>
+                    #   </path>
+                    mapping = self.__get_mapping(path, branch)
+                    if mapping == None:
+                        # submission is blocked, return empty list
                         return []
-                    # branch match
-                    if branch and path.branch.name == branch:
-                        if path.branch.submission == 'N':
-                            #   <path name="/apps/">
-                            #      <branch name="master" submission="N"/>
-                            #   </path>
-                            return []
-                        else:
-                            return [path.branch.OBS_project,
-                                    path.branch.OBS_staging_project,
-                                    path.branch.OBS_package]
-                        
+                    elif mapping:
+                        # retrun if already found matched OBS target project
+                        return mapping
                         
     def get_integration_mapping(self, project, branch):
         # Search in project list
index ded5d8b..bc83463 100755 (executable)
@@ -17,6 +17,7 @@ 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 import buildservice
@@ -107,6 +108,14 @@ if __name__ == '__main__':
         if not utils.retry(git.update_git_project, (GIT_CACHE_DIR, MAPPING_PRJ, GIT_URL)):
             end(1)
 
+    # quit directly if project do not map to any OBS project
+    mymapping = mapping.Mapping('%s/%s/git-obs-mapping.xml' % (GIT_CACHE_DIR, MAPPING_PRJ))
+    obstargets = mymapping.get_submit_mapping(GERRIT_PROJECT, GERRIT_BRANCH)
+    print "Project Mapping info: %s" %obstargets
+    if not obstargets:
+        print '\nThis project do not map to any OBS project, exit now'
+        end()
+
     # update local git tree from remote
     try:
         if os.path.exists(os.path.join(GIT_CACHE_DIR, GERRIT_PROJECT)):
@@ -126,13 +135,9 @@ if __name__ == '__main__':
 
     packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging'))
 
-    mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, MAPPING_PRJ), GERRIT_PROJECT, GERRIT_BRANCH)
-
-    print "Project Mapping info: %s" %mapping
-
     return_value = 0
 
-    for target in mapping:
+    for target in obstargets:
         (obs_dest_prj, obs_stg_prj, obs_pkg) = target
 
         # build the package under home project of backend service user
index d3360df..91aaa6e 100755 (executable)
@@ -16,6 +16,7 @@ from common import utils
 from common import git
 from common import obspkg
 from common import gerrit
+from common import mapping
 from common.envparas import *
 import gbp.rpm
 
@@ -66,6 +67,19 @@ if __name__ == '__main__':
         if not utils.retry(git.update_git_project, (GIT_CACHE_DIR, MAPPING_PRJ, GIT_URL)):
             end(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))
+
+    # exit if this project does not map to any obs project
+    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()
+
     # update local git tree from remote
     try:
         if os.path.exists(os.path.join(GIT_CACHE_DIR, GERRIT_PROJECT)):
@@ -101,18 +115,12 @@ if __name__ == '__main__':
     print('submitted to obs: %s' % needsr)
     '''
 
-    mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, MAPPING_PRJ), GERRIT_PROJECT, GERRIT_BRANCH)
-    print 'git-obs-mapping:', mapping
-
-    obstarget = []
-    newpkg = False
+    #newpkg = False
     missedspec = []
     checkspecmsg = ''
-    checkobsmsg = ''
-
     speclist = []
 
-    for target in mapping:
+    for target in obstargets:
         print('obs mapping: ', target)
         (obs_dest_prj, obs_stg_prj, obs_pkg) = target
         if not obs_dest_prj:
@@ -152,8 +160,6 @@ if __name__ == '__main__':
                 checkspecmsg += '\n\n'
             checkspecmsg += msg
 
-    if not mapping:
-        checkobsmsg = '[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
     '''
     elif not obstarget:
         checkobsmsg = '[IMPORTANT NOTICE]: This change will not be submitted to OBS. If want to trigger submission to OBS, please use gbs submit, which will create an annotated tag submit/$(tizen_version)/$(date -u +%F.%H%M%S) on this commit and push to gerrit.'
@@ -166,11 +172,7 @@ if __name__ == '__main__':
     if missedspec:
         checkspecmsg += '\n\nError: Can not find %s under packaging directory!' % ' '.join(missedspec)
 
-    gerrit = gerrit.Gerrit(GERRIT_HOSTNAME, GERRIT_USERNAME, GERRIT_SSHPORT)
-    print('\n-------------------------------\ncheck obs target result:\n%s' % checkobsmsg)
-    if checkobsmsg:
-        gerrit.review(commit = GERRIT_PATCHSET_REVISION, message = checkobsmsg)
     print('\n-------------------------------\ncheck specfile result:\n%s' % checkspecmsg)
     if checkspecmsg:
-        gerrit.review(commit = GERRIT_PATCHSET_REVISION, message = checkspecmsg)
+        mygerrit.review(commit = GERRIT_PATCHSET_REVISION, message = checkspecmsg)
     end()
index a402aef..80160bf 100755 (executable)
@@ -17,7 +17,8 @@ from common import runner
 from common import utils
 from common import git
 from common import obspkg
-from common.envparas import *
+from common import mapping
+from common.envparas import export
 from common import errors
 from common import mysql
 from common import gerrit 
@@ -249,6 +250,12 @@ def main():
             print '\nPulled scm/git-obs-mapping change to local, exit now'
             end('success')
 
+    # quit directly if project do not map to any OBS project
+    mymapping = mapping.Mapping('%s/%s/git-obs-mapping.xml' % (GIT_CACHE_DIR, MAPPING_PRJ))
+    if not mymapping.get_submit_mapping(GERRIT_PROJECT):
+        print '\nThis project do not map to any OBS project, exit now'
+        end('success')
+
     # update local git tree from remote
     try:
         if utils.retry(git.update_git_project, (GIT_CACHE_DIR, GERRIT_PROJECT, GIT_URL)):
@@ -299,9 +306,9 @@ def main():
     commitinfo = mygit.get_commit_info(GERRIT_PATCHSET_REVISION)
 
     # parse git-obs-mapping to get OBS target project, exit if not map to any project
-    mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, MAPPING_PRJ), GERRIT_PROJECT, GERRIT_BRANCH)
-    print 'git-obs-mapping:', mapping
-    if not mapping:
+    obstargets = mymapping.get_submit_mapping(GERRIT_PROJECT, GERRIT_BRANCH)
+    print 'git-obs-mapping:', obstargets
+    if not obstargets:
         end('success')
 
     # post comment and send email if tag format check failed
@@ -320,7 +327,7 @@ def main():
 
     mygit.checkout(tag)
 
-    for target in mapping:
+    for target in obstargets:
         mygit.clean('-fd')
         (obs_dst_prj, obs_stg_prj, obs_pkg) = target
         if not obs_dst_prj: