new module obspkg as the wrapper of local obs pkg
authorJF Ding <jian-feng.ding@intel.com>
Mon, 6 Feb 2012 10:05:28 +0000 (18:05 +0800)
committerJF Ding <jian-feng.ding@intel.com>
Mon, 6 Feb 2012 10:05:28 +0000 (18:05 +0800)
From now, all operations around local obs pkg working dir should
be done by this module. And all server only related access should
be thru buildservice module.
And inside obspkg, all obs/osc callings are thru buildservice.

Makefile
gitbuildsys/buildservice.py
gitbuildsys/obspkg.py [new file with mode: 0644]

index 9dbf470..e5f472c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ else
 endif
 
 ifndef PREFIX
-    PREFIX = "/local/usr"
+    PREFIX = "/usr/local"
 endif
 
 all:
index 5341825..ccd1e9a 100644 (file)
@@ -192,7 +192,7 @@ class _ProjectFlags(object):
         print ElementTree.tostring(self.tree)
 
 class BuildService():
-    "Interface to Build Service API"
+    """Interface to Build Service API"""
 
     def __init__(self, apiurl=None, oscrc=None):
         if oscrc:
@@ -1041,3 +1041,35 @@ class BuildService():
         """
 
         return _ProjectFlags(self, project)
+
+    def checkout(self, prj, pkg, rev='latest'):
+        """ checkout the package to current dir with link expanded
+        """
+
+        core.checkout_package(self.apiurl, prj, pkg, rev, expand_link=True)
+
+    def find_pac(self, wd='.'):
+        """Get the single Package object for specified dir
+          the 'wd' should be a working dir for one single pac
+        """
+
+        if core.is_package_dir(wd):
+            return core.findpacs([path])[0]
+        else:
+            return None
+
+    def mk_pac(self, pkg_path):
+        """Create empty package for new one under CWD
+        """
+
+        core.createPackageDir(pkg_path)
+
+    def submit(self, wd='.'):
+        if not core.is_package_dir(wd):
+            # TODO show some error message
+            return
+
+        pac = core.findpacs([wd])[0]
+        pac.commit(msg)
+        core.store_unlink_file(pac.absdir, '_commit_msg')
+
diff --git a/gitbuildsys/obspkg.py b/gitbuildsys/obspkg.py
new file mode 100644 (file)
index 0000000..7eb3bb2
--- /dev/null
@@ -0,0 +1,126 @@
+#!/usr/bin/python -tt
+# vim: ai ts=4 sts=4 et sw=4
+#
+# Copyright (c) 2012 Intel, Inc.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; version 2 of the License
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc., 59
+# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+from __future__ import with_statement
+import os
+import buildservice
+
+class _Workdir(object):
+    def __init__(self, path):
+        self._newdir = path
+        self._cwd = os.getcwd()
+
+    def __enter__(self):
+        os.chdir(self._newdir)
+
+    def __exit__(self, type, value, tb):
+        os.chdir(self._cwd)
+
+class ObsPackage(object):
+    """ Wrapper class of local package dir of OBS
+    """
+
+    def __init__(self, basedir, prj, pkg, apiurl=None, oscrc=None):
+        """Arguments:
+          basedir: the base local dir to store obs packages
+          prj: obs project
+          pkg: obs package
+          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._bdir = os.path.absdir(os.path.expanduser(basedir))
+        self._prj = prj
+        self._pkg = pkg
+        self._pkgpath = os.path.join(self._bdir, prj, pkg)
+
+        if not os.path.exists(self._bdir):
+            os.makedirs(self._bdir)
+
+        if self._bs.isNewPackage(prj, pkg):
+            # to init new package in local dir
+            self._mkpac()
+        else:
+            # to checkout server stuff
+            self._checkout_latest()
+
+    def _mkpac(self):
+        with _Workdir(self._bdir):
+            self._bs.mk_pac(os.path.join(self._prj, self._pkg))
+
+    def _checkout_latest():
+        """ checkout the 'latest' revision of package with link expanded
+        """
+
+        with _Workdir(self._bdir):
+            self._bs.checkout(self._prj, self._pkg)
+
+    def get_workdir(self):
+        return self._pkgpath
+
+    def remove_all(self):
+        """Remove all files under pkg dir
+        """
+
+        with _Workdir(self._pkgpath):
+            runner.quiet('/bin/rm -f *')
+
+    def update_local(self):
+        """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.todo = list(set(pac.filenamelist + pac.filenamelist_unvers + pac.to_be_added))
+            for filename in pac.todo:
+                if os.path.isdir(filename):
+                    continue
+                # ignore foo.rXX, foo.mine for files which are in 'C' state
+                if os.path.splitext(filename)[0] in pac.in_conflict:
+                    continue
+                state = pac.status(filename)
+                if state == '?':
+                    pac.addfile(filename)
+                elif state == '!':
+                    pac.delete_file(filename)
+
+    def add_file(self, fpath):
+        # copy the file in
+        runner.quiet('/bin/cp -f %s %s' % (fpath, self._pkgpath))
+
+        # add it into local pac
+        with _Workdir(self._pkgpath):
+            pac = self._bs.find_pac()
+            if pac:
+                pac.addfile(os.path.basename(fpath))
+            else:
+                msger.warning('Invalid pac working dir, skip')
+
+    def commit(self, msg):
+        with _Workdir(self._pkgpath):
+            self._bs.submit()