impove the function of linkedbuild
authorHasan Wan <hasan.wan@intel.com>
Sun, 6 Jan 2013 13:05:42 +0000 (21:05 +0800)
committerLin Yang <lin.a.yang@intel.com>
Thu, 28 Feb 2013 07:44:18 +0000 (15:44 +0800)
Change-Id: Idcc35e2760c2a4204baf045925435bd435c9cb33
Signed-off-by: Hasan Wan <hasan.wan@intel.com>
common/tempbuildpkg.py
repomaker/__init__.py
testprojects/prerelease.py

index 5259814..453e718 100644 (file)
@@ -5,6 +5,10 @@ import base64
 import runner
 import buildservice
 
+from urllib import quote_plus
+from osc import core
+from xml.etree import cElementTree as ET
+
 def encode_passwd(passwd):
     '''encode passwd by bz2 and base64'''
     return base64.b64encode(passwd.encode('bz2'))
@@ -84,8 +88,25 @@ class BuildService2(buildservice.BuildService):
 
         self.apiurl = apiurl
         tmpf = Temp(prefix='.oscrc', content=oscrc)
-        oscrcpath = tmpf.path
-        buildservice.BuildService(apiurl, oscrc=oscrcpath)
+        self.oscrcpath = tmpf.path
+        buildservice.BuildService(apiurl, oscrc=self.oscrcpath)
+
+    def update_meta(self, project, metaconf):
+        """
+        update meta configuration for project
+        """
+        meta_url = core.make_meta_url('prj',
+                                      path_args=quote_plus(project))
+
+        core.http_PUT(meta_url, file=metaconf)
+
+    def get_meta(self, project):
+        """
+        update meta configuration for project
+        """
+        meta_url = core.make_meta_url('prj',
+                                      path_args=quote_plus(project))
+        return core.http_GET(meta_url).readlines()
 
 class TempPackageBuild:
 
@@ -178,21 +199,6 @@ class TempPackageBuild:
     def del_itself(self):
         self.bs.deleteProject(self.project)
 
-PRJ_TEMPLATE = """<project name="%(target)s">
-  <title></title>
-  <description>%(desp)s</description>
-  <person role="maintainer" userid="%(user)s"/>
-  <person role="bugowner" userid="%(user)s"/>
-  <link project="%(src)s"/>
-  <build>
-    <%(build)s/>
-  </build>
-  <repository name="standard" linkedbuild="localdep">
-    <path repository="standard" project="%(src)s"/>
-    <arch>i586</arch>
-  </repository>
-</project>"""
-
 class TempBuildService():
 
     def __init__(self, project, apiurl, apiuser, apipasswd):
@@ -205,7 +211,6 @@ class TempBuildService():
         return self.bs.getRepoState(self.project)
 
     def cleanup(self):
-        os.system("osc -H -d -v rdelete --recursive %s -m 'deleted'"  %(self.project))
         return self.bs.deleteProject(self.project)
 
     def is_published(self):
@@ -251,34 +256,48 @@ class TempBuildService():
         if not found:
             print "did not find any image configurations"
 
-    def disable_build(self, user):
-
-        targetmeta = """<project name="%(target)s">
-  <title></title>
-  <description></description>
-  <person role="maintainer" userid="%(user)s"/>
-  <person role="bugowner" userid="%(user)s"/>
-  <build>
-    <%(build)s/>
-  </build>
-</project>"""% {'target': self.project, 'user': user, 'build': 'disable'}
-        print targetmeta
+
+    def disable_build(self):
+        """ disable the project"""
+        origin_meta = self.bs.get_meta(self.project)
+
+        root = ET.fromstringlist(origin_meta)
+        build = root.find('build')
+
+        if build:
+            build.clear()
+            build.append(ET.Element('disable'))
+        else:
+            build = ET.Element('build')
+            build.append(ET.Element('disable'))
+            root.append(build)
+
         fileh, filename = tempfile.mkstemp(prefix="osc_metafile.",
                                            suffix=".xml", text=True)
-        os.write(fileh, targetmeta)
+        os.write(fileh, ET.tostring(root))
+
+        self.bs.update_meta(self.project, filename)
+
         os.close(fileh)
-        os.system("osc -d -v meta prj %s -F %s" %(self.project, filename))
-        sys.stdout.flush()
         os.unlink(filename)
-        
-    def link_project(self, src, user, desp = ''):
-        targetmeta = PRJ_TEMPLATE % {'target': self.project, 'src': src,
-                                     'user': user, 'build': 'enable',
-                                     'desp': desp}
+
+    def linkbuild_localdep(self, desp=''):
+        """ Link the project build against repo with dependency (localdep)"""
+        origin_meta = self.bs.get_meta(self.project)
+
+        root = ET.fromstringlist(origin_meta)
+        for repo_setting in root.findall('repository'):
+            repo_setting.set('linkedbuild', 'localdep')
+
+        if desp:
+            description = root.find('description')
+            description.text = desp
 
         fileh, filename = tempfile.mkstemp(prefix="osc_metafile.",
                                            suffix=".xml", text=True)
-        os.write(fileh, targetmeta)
+        os.write(fileh, ET.tostring(root))
+
+        self.bs.update_meta(self.project, filename)
+
         os.close(fileh)
-        os.system("osc meta prj %s -F %s" %(self.project, filename))
         os.unlink(filename)
index 35f308e..a6b25ce 100644 (file)
@@ -762,10 +762,14 @@ class ReviewRepo(RepoManager):
 
     def get_gitinfo(self):
         ret_dict = {}
+        meta = self.bs.get_meta(self.project)
 
-        stdin, stdout = os.popen2("osc meta prj %s" %self.project)
+        try:
+            root = ET.fromstringlist(meta)
+        except Exception, e:
+            print e
+            return {}
 
-        root = ET.parse(stdout)
         if root:
             try:
                 for line in root.find('description').text.split('\n'):
index 7e4d3ab..b2b1144 100644 (file)
@@ -59,12 +59,12 @@ class PreRelease:
 
         print "Temporary review project created on OBS: %s" %(self.obs_project)
 
-        project_info = "OBS_REQ_PRJ: %s\n\GIT_PROJECT: %s\n\GIT_TAG: %s\n" %(
+        project_info = "OBS_REQ_PRJ: %s\nGIT_PROJECT: %s\nGIT_TAG: %s\n" %(
             self.target_project,
             self.git_project,
             self.git_tag)
 
-        bs.link_project(self.target_project, self.user, desp = project_info)
+        bs.linkbuild_localdep(desp = project_info)
 
         time.sleep(2)