Add outputs when execute git commands and pylinted
authorLingchaox Xin <lingchaox.xin@intel.com>
Mon, 11 Mar 2013 09:42:30 +0000 (17:42 +0800)
committerLingchaox Xin <lingchaox.xin@intel.com>
Mon, 11 Mar 2013 10:15:37 +0000 (18:15 +0800)
Change-Id: I0023123a00044d19ebdf7f0198fa9f586490d737

common/git.py

index d1d39a2..6bacdb7 100644 (file)
 # with this program; if not, write to the Free Software Foundation, Inc., 59
 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
+"""
+Git module inherited from GitRepository and self customed
+"""
+
 import os
-import re
 import shutil
 
-# internal modules
-import runner
-import errors
-from utils import retry
+from common import runner
+from common import errors
+from common.utils import retry
 
-# gbp import
 from gbp.git.repository import GitRepository
-from gbp.git.commit import GitCommit
 import gbp.log as log
 
+
 class Git(GitRepository):
+    """The Git class wrappered from GitRepository"""
     def __init__(self, path):
         GitRepository.__init__(self, path)
         log.setup('auto', log.DEBUG)
 
+    def _git_getoutput(self, command, args=[], extra_env=None, cwd=None):
+        outs, ret = GitRepository._git_getoutput(self, command, args,
+                extra_env, cwd)
+        print '%s outputs: %s' % (command, outs)
+        return outs, ret
+
     def get_tag(self, tag_name):
-        outs, ret = self._git_getoutput('show',
-                             ['-s', '--pretty=format:###', tag_name])
+        """Get tag info though tag name"""
+        outs = self._git_getoutput('show', [
+            '-s', '--pretty=format:###', tag_name])[0]
         tag = {}
         msg = ''
         msgstub = False
@@ -45,7 +54,8 @@ class Git(GitRepository):
             if not line:
                 continue
             if msgstub:
-                if line.startswith('###') or line.startswith('-----BEGIN PGP SIGNATURE-----'):
+                if line.startswith('###') or line.startswith(
+                        '-----BEGIN PGP SIGNATURE-----'):
                     tag['message'] = msg[:-1]
                     break
                 else:
@@ -67,7 +77,7 @@ class Git(GitRepository):
     def fetch(self, *args):
         """Download objects and refs from another repository
         """
-        outs, ret = self._git_getoutput('fetch', list(args))
+        ret = self._git_getoutput('fetch', list(args))[1]
         return not ret
 
     def describe(self, *args):
@@ -83,16 +93,17 @@ class Git(GitRepository):
     def pull(self, *args):
         """Fetch from and merge with another repository or a local branch
         """
-        outs, ret = self._git_getoutput('pull', list(args))
+        ret = self._git_getoutput('pull', list(args))[1]
         return not ret
 
     def clean(self, *args):
+        """Clean objects which are not tracked by git"""
         self._git_getoutput('clean', list(args))
 
     def get_base_commit(self, commit):
-        out = self._git_getoutput('log',
-                             ['--pretty=format:%H',
-                              '-n2', commit])[0]
+        """Get base commit of the given commit"""
+        out = self._git_getoutput('log', [
+            '--pretty=format:%H', '-n2', commit])[0]
         return out[1].strip()
 
     def format_patch(self, *args):
@@ -103,14 +114,14 @@ class Git(GitRepository):
         outs, ret = self._git_getoutput('branch', ['-a', '--contains', cmitid])
         if not ret:
             branches = []
-            for br in outs:
-                if br.find('remotes/origin/HEAD ->') != -1:
+            for branch in outs:
+                if branch.find('remotes/origin/HEAD ->') != -1:
                     continue
-                br = br.strip().lstrip('* ')
-                if br.startswith('remotes/origin/'):
-                    br = br[len('remotes/origin/'):]
-                if br not in branches:
-                    branches.append(br)
+                branch = branch.strip().lstrip('* ')
+                if branch.startswith('remotes/origin/'):
+                    branch = branch[len('remotes/origin/'):]
+                if branch not in branches:
+                    branches.append(branch)
             print 'git branch contains %s' % branches
             return branches
         else:
@@ -122,10 +133,11 @@ class Git(GitRepository):
         options += list(args)
         outs, ret = self._git_getoutput('rev-parse', options)
         if not ret:
-            return outs[0][:-1] # remove '\n'
+            return outs[0][:-1]  # remove '\n'
         else:
             return None
 
+
 def _update_gitproject(localdir):
     """Fetch latest code to local dir"""
 
@@ -138,11 +150,12 @@ def _update_gitproject(localdir):
         return False
 
     if not (localgit.pull('--all') and localgit.fetch('--tags')):
-        shutil.rmtree(prjdir)
+        shutil.rmtree(localdir)
         return False
 
     return True
 
+
 def _clone_gitproject(gerritprj, localdir):
     """Clone gerrit project from remote to local dir"""
 
@@ -159,10 +172,9 @@ def _clone_gitproject(gerritprj, localdir):
     try:
         if os.path.isdir(os.path.join(os.getenv('GIT_CACHE_DIR'), gerritprj)):
             # use local cache repo as reference to clone
-            gitcmd = 'git clone %s/%s --reference %s %s' % (giturl,
-                                                            gerritprj,
-                                                            os.path.join(os.getenv('GIT_CACHE_DIR'), gerritprj),
-                                                            localdir)
+            cache_dir = os.path.join(os.getenv('GIT_CACHE_DIR'), gerritprj)
+            gitcmd = 'git clone %s/%s --reference %s %s' % (giturl, gerritprj,
+                    cache_dir, localdir)
         else:
             gitcmd = 'git clone %s/%s %s' % (giturl, gerritprj, localdir)
 
@@ -179,6 +191,7 @@ def _clone_gitproject(gerritprj, localdir):
 
     return result
 
+
 def clone_gitproject(gerritprj, localdir):
     """Clone gerrit project from remote to local dir"""
     return retry(_clone_gitproject, (gerritprj, localdir))