obspkg: new feature to create branch project
authorJF Ding <jian-feng.ding@intel.com>
Tue, 7 Feb 2012 08:41:14 +0000 (16:41 +0800)
committerJF Ding <jian-feng.ding@intel.com>
Tue, 7 Feb 2012 08:41:14 +0000 (16:41 +0800)
gitbuildsys/buildservice.py
gitbuildsys/errors.py
gitbuildsys/obspkg.py

index 83ef8ae..d896dda 100644 (file)
@@ -245,6 +245,24 @@ class BuildService():
                 raise e
         return new_pkg
 
+    def isNewProject(self, project):
+        """Check whether the specified prject is a new one
+        """
+
+        new_prj = False
+        try:
+            core.meta_exists(metatype = 'prj',
+                        path_args = (core.quote_plus(dst_project)),
+                        create_new = False,
+                        apiurl = self.apiurl)
+        except urllib2.HTTPError, e:
+            if e.code == 404:
+                new_prj = True
+            else:
+                raise e
+
+        return new_prj
+
     def genRequestInfo(self, reqid, show_detail = True):
         """Generate formated diff info for request,
         mainly used by notification mails from BOSS
@@ -1048,7 +1066,7 @@ class BuildService():
 
         core.checkout_package(self.apiurl, prj, pkg, rev, expand_link=True)
 
-    def find_pac(self, wd='.'):
+    def findPac(self, wd='.'):
         """Get the single Package object for specified dir
           the 'wd' should be a working dir for one single pac
         """
@@ -1058,14 +1076,17 @@ class BuildService():
         else:
             return None
 
-    def mk_pac(self, pkg_path):
+    def mkPac(self, prj, pkg):
         """Create empty package for new one under CWD
         """
-        prj = pkg_path.split('/')[0]
-        pkg = pkg_path.split('/')[1]
+
+        import shutil
+        shutil.rmtree(prj, ignore_errors = True)
+
         core.make_dir(self.apiurl, prj, pkg, pathname = '.')
-        os.chdir(pkg_path)
-        core.init_package_dir(self.apiurl, prj, pkg, pkg, files=False)
+
+        shutil.rmtree(pkg_path, ignore_errors = True)
+        core.createPackageDir(pkg_path)
 
     def submit(self, msg, wd='.'):
         if not core.is_package_dir(wd):
@@ -1073,6 +1094,35 @@ class BuildService():
             return
 
         pac = core.findpacs([wd])[0]
-        pac.commit(msg)
+        prj = os.path.normpath(os.path.join(pac.dir, os.pardir))
+        pac_path = os.path.basename(os.path.normpath(pac.absdir))
+        files = {}
+        files[pac_path] = pac.todo
+        core.Project(prj).commit(tuple([pac_path]), msg=msg, files=files)
         core.store_unlink_file(pac.absdir, '_commit_msg')
 
+    def branchPkg(self, src_project, src_package, rev=None, target_project=None, target_package=None):
+        """Create branch package from `src_project/src_package`
+          arguments:
+            rev: revision of src project/package
+            target_project: name of target proj, use default one if None
+            target_package: name of target pkg, use the same as asrc if None
+        """
+
+        if target_project is None:
+            target_project = 'home:%s:branches:%s' \
+                             % (conf.get_apiurl_usr(self.apiurl), src_project)
+
+        if target_package is None:
+            target_package = src_package
+
+        exists, targetprj, targetpkg, srcprj, srcpkg = \
+            branch_pkg(self.apiurl,
+                       src_project,
+                       src_package,
+                       rev=rev,
+                       target_project=target_project,
+                       target_package=target_package,
+                       force=True)
+
+        return (targetprj, targetpkg)
index e61c316..22bd4c3 100644 (file)
@@ -44,3 +44,6 @@ class GitError(CmdError):
 class GitInvalid(GitError):
     def __str__(self):
         return 'Dir %s is not a valid git tree' % str(self.msg)
+
+class ObsError(CmdError):
+    keyword = '<obs>'
index 2970b53..8740c0f 100644 (file)
@@ -72,9 +72,9 @@ class ObsPackage(object):
 
     def _mkpac(self):
         with _Workdir(self._bdir):
-            self._bs.mk_pac(os.path.join(self._prj, self._pkg))
+            self._bs.mkPac(self._prj, self._pkg)
 
-    def _checkout_latest():
+    def _checkout_latest(self):
         """ checkout the 'latest' revision of package with link expanded
         """
 
@@ -95,8 +95,9 @@ class ObsPackage(object):
         """Do the similar work of 'osc addremove',
           remove all deleted files and added all new files
         """
+
         with _Workdir(self._pkgpath):
-            pac = self._bs.find_pac()
+            pac = self._bs.findPac()
             # FIXME, if pac.to_be_added are needed to be considered.
             pac.todo = list(set(pac.filenamelist + pac.filenamelist_unvers))
             for filename in pac.todo:
@@ -117,7 +118,7 @@ class ObsPackage(object):
 
         # add it into local pac
         with _Workdir(self._pkgpath):
-            pac = self._bs.find_pac()
+            pac = self._bs.findPac()
             if pac:
                 pac.addfile(os.path.basename(fpath))
             else:
@@ -126,3 +127,55 @@ class ObsPackage(object):
     def commit(self, msg):
         with _Workdir(self._pkgpath):
             self._bs.submit(msg)
+
+class ObsProject(object):
+    """ Wrapper class of project in OBS
+    """
+
+    def __init__(self, prj, apiurl=None, oscrc=None):
+        """Arguments:
+          prj: name of obs project
+          apiurl: optional, the api url of obs service
+                 if not specified, the one from oscrc will be used
+          oscrc: optional, the path of customized oscrc
+                 if not specified, ~/.oscrc will be used
+        """
+
+        if oscrc:
+            self._oscrc = oscrc
+        else:
+            self._oscrc = os.path.expanduser('~/.oscrc')
+
+        self._bs = buildservice.BuildService(apiurl, oscrc)
+        self._apiurl = self._bs.apiurl
+        self._prj = prj
+
+    def is_new(self):
+        return self._bs.isNewProject(self._prj)
+
+    def create(self):
+        """Create an empty project"""
+        # TODO
+        pass
+
+    def branch(self, src_prj, target_prj=None):
+        """Create a new branch project of `src_prj`
+        """
+
+        if self._bs.isNewProject(src_prj):
+            raise errors.ObsError('project: %s do not exists' % src_prj)
+
+        if not self.is_new():
+            msger.warning('branched project: %s exists' % self._prj)
+            return
+
+        # pick the 1st valid package inside src prj FIXME
+        dumb_pkg = self._bs.getPackageList(src_prj)[0]
+
+        # branch out the new one
+        target_prj, target_pkg = self._bs.branchPkg(src_prj, dumb_pkg,
+                                                    target_project = target_prj,
+                                                    target_package = 'dumb_pkg')
+
+        # remove the dumb pkg
+        self._bs.deletePackage(target_prj, target_pkg)