Update git_obs_map function 39/60439/1
authorDonghoon Shin <dhs.shin@samsung.com>
Fri, 26 Feb 2016 07:45:39 +0000 (16:45 +0900)
committerDonghoon Shin <dhs.shin@samsung.com>
Fri, 26 Feb 2016 07:45:39 +0000 (16:45 +0900)
Change-Id: Id772dbc2629ec5479e92f952387c4740c2ffcd41

common/mapping.py
job_submit.py
job_submitobs.py

index 31905a6..6aa81ee 100644 (file)
@@ -20,6 +20,7 @@
 
 import os
 from common.utils import xml_to_obj
+from common.git import clone_gitproject
 from xml.sax import SAXException
 
 class MappingError(Exception):
@@ -115,3 +116,105 @@ class Mapping(object):
                         #   </path>
                         return self.__get_mapping(path, branch) or []
         return []
+
+
+class MappingV2(object):
+    """A class to handle mapping xml file """
+    def __init__(self, mapping_file):
+        """ convert xml to git obj """
+        if not os.path.isfile(mapping_file):
+            raise MappingError("Fatal: %s is not a regular file" \
+                               % mapping_file)
+
+        with open(mapping_file) as filei:
+            try:
+                self.mapping_obj = xml_to_obj(''.join(filei.readlines()))
+            except SAXException, err:
+                raise MappingError("Fatal: parsing of xml file %s failed: %s" \
+                                   % (mapping_file, str(err)))
+    @staticmethod
+    def __encode_to_ascii(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_submit_mapping(self, project, branch=None):
+        """Get submit mapping though project or branch"""
+        mapping = []
+
+        if self.mapping_obj.configure:
+            # if configure.enable is false then return []
+            if self.mapping_obj.configure.enable == 'false':
+                return mapping
+
+        if self.mapping_obj.branch:
+            for brch in self.mapping_obj.branch:
+                for prj in brch.project:
+                    if (prj.name == project or prj.name == '/' + project) and brch.name == branch:
+                        mapping.append(self.__encode_to_ascii([brch.OBS_project, brch.OBS_staging_project, prj.OBS_package]))
+            return mapping
+
+        return mapping
+
+
+def git_obs_map(gerrit_prj, gerrit_branch=None, gitcache=None):
+    """
+    Find an OBS project[s correspondent to Gerrit project and branch
+    by parsing git-obs-mapping.xml.
+    """
+
+    def remove_overlaps(orig_list):
+        """docstring for make_unique"""
+        result = []
+        [result.append(obj) for obj in orig_list if obj not in result]
+        return result
+
+    def get_xml_file_list(path):
+        file_list = []
+        for root, dirs, files in os.walk(path):
+            for file in files:
+                if file.endswith('.xml'):
+                    file_list.append(os.path.join(path, file))
+        return file_list
+
+    if gitcache:
+        git_cache = gitcache
+    else:
+        git_cache = os.getenv("GIT_CACHE_DIR")
+
+    mapping_prj = os.getenv("MAPPING_PRJ")
+
+    git_obs_mapping_path = os.path.join(git_cache, mapping_prj)
+    mapping_path_v1 = '{0}/git-obs-mapping.xml'.format(git_obs_mapping_path)
+
+    if not os.path.isfile(mapping_path_v1):
+        print 'Cloning %s' % mapping_prj
+        if not clone_gitproject(mapping_prj, \
+                os.path.join(git_cache, mapping_prj)):
+            raise MappingError('Error cloning %s' % mapping_prj)
+
+
+    # get mappings v1
+    mymapping = Mapping(mapping_path_v1)
+    obs_prjs = mymapping.get_submit_mapping(gerrit_prj, gerrit_branch)
+
+    # get v2 mapping files list
+    mapping_path_v2 = '{0}/profiles/'.format(git_obs_mapping_path)
+    mapping_v2_file_lists = get_xml_file_list(mapping_path_v2)
+
+    # get mappings v2
+    for file in mapping_v2_file_lists:
+        mymapping_v2 = MappingV2(file)
+        obs_prjs.extend(mymapping_v2.get_submit_mapping(gerrit_prj,
+                                                        gerrit_branch))
+
+    # remove overlapped items
+    obs_prjs = remove_overlaps(obs_prjs)
+
+    print 'Found git-obs-mapping: %s -> %s' % (gerrit_prj, obs_prjs)
+    return obs_prjs
index d5ece2c..0a4468a 100755 (executable)
@@ -32,7 +32,7 @@ from xml.sax.saxutils import escape
 from osc import core
 from gitbuildsys.errors import ObsError
 
-from common import mapping
+from common.mapping import git_obs_map
 from common.git import clone_gitproject
 from common.upload_service import upload_obs_service, UploadError
 from common.gerrit import is_ref_deleted
@@ -47,28 +47,6 @@ class LocalError(Exception):
     pass
 
 
-def git_obs_map(gerrit_prj, gerrit_branch):
-    """
-    Find an OBS project[s correspondent to Gerrit project and branch
-    by parsing git-obs-mapping.xml.
-    """
-    git_cache = os.getenv("GIT_CACHE_DIR")
-    mapping_prj = os.getenv("MAPPING_PRJ")
-    mapping_path = '%s/git-obs-mapping.xml' % \
-                       os.path.join(git_cache, mapping_prj)
-
-    if not os.path.isfile(mapping_path):
-        print 'Cloning %s' % mapping_prj
-        if not clone_gitproject(mapping_prj, \
-                os.path.join(git_cache, mapping_prj)):
-            raise LocalError('Error cloning %s' % mapping_prj)
-
-    # get mapping
-    mymapping = mapping.Mapping(mapping_path)
-    obs_prjs = mymapping.get_submit_mapping(gerrit_prj, gerrit_branch)
-    print 'Found git-obs-mapping: %s -> %s' % (gerrit_prj, obs_prjs)
-    return obs_prjs
-
 def change_release_name(build, project, git_tag):
     """
     Change release name from project config in OBS
index 7e7a133..615c2c2 100755 (executable)
@@ -35,7 +35,7 @@ from common import utils
 from common.upload_service import upload_obs_service, UploadError
 from common.git import Git, clone_gitproject
 from common import obspkg
-from common import mapping
+from common.mapping import git_obs_map
 from common.gerrit import Gerrit, get_gerrit_event, GerritError, is_ref_deleted
 from common.send_mail import prepare_mail
 from common.buildservice import BuildService
@@ -308,8 +308,6 @@ def main():
 
     # prepare related global variables
     workspace = os.getenv('WORKSPACE')
-    gitcache = os.getenv('GIT_CACHE_DIR')
-    mapping_prj = os.getenv('MAPPING_PRJ')
     apiurl = os.getenv('OBS_API_URL')
     apiuser = os.getenv('OBS_API_USERNAME')
     apipasswd = os.getenv('OBS_API_PASSWD')
@@ -362,17 +360,8 @@ def main():
         print '\nREFNAME "%s" is deleted, exit now' % event['refname']
         return 0
 
-    obs_mapping = '%s/%s/git-obs-mapping.xml' % (gitcache, mapping_prj)
-    # check whether git-obs-mapping.xml exist in local
-    if not os.path.isfile(obs_mapping):
-        print 'Update %s/git-obs-mapping.xml to local.' % mapping_prj
-        if not clone_gitproject(mapping_prj, \
-                os.path.join(gitcache, mapping_prj)):
-            return 1
-
     # quit directly if project do not map to any OBS project
-    mymapping = mapping.Mapping(obs_mapping)
-    if not mymapping.get_submit_mapping(event['project']):
+    if not git_obs_map(event['project']):
         print '\nThis project do not map to any OBS project, exit now'
         return 0
 
@@ -392,7 +381,7 @@ def main():
     commitinfo = mygit.get_commit_info(event['patchset_revision'])
 
     # parse git-obs-mapping to get OBS target project
-    obstargets = mymapping.get_submit_mapping(event['project'], event['branch'])
+    obstargets = git_obs_map(event['project'], event['branch'])
     print 'git-obs-mapping:', event['project'], event['branch'], obstargets
 
     packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging'))