]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
adding cleanup branch functions
authorMathias Klette <mathias.klette@profitbricks.com>
Thu, 15 Sep 2011 15:49:39 +0000 (17:49 +0200)
committerMathias Klette <mathias.klette@profitbricks.com>
Thu, 15 Sep 2011 15:49:39 +0000 (17:49 +0200)
debian_build.py
lib/git_helper.py

index 83b39486314b1f4e1cfe88d7c3243e1c112f9a7a..ee5f0ddcc2829f8ecb4742cfe2b664109e6c2bc6 100755 (executable)
@@ -206,6 +206,8 @@ def remove_git_target_workspace():
         logger.info('deleted %s' %(GIT_TARGET_WORKSPACE))
         return True
 
+
+
 def exit_ok():
     send_email(SMTP_BUILD_SUCCESS)
     sys.exit(0)
@@ -280,6 +282,13 @@ if __name__ == '__main__':
                 os.path.join('origin', GIT_DEBIAN_BRANCH)
         )
 
+    cleanup_files = ('.gbp.conf', 'debian/gbp.conf', '.git/gbp.conf')
+    result = git_helper.git_cleanup_branch(cleanup_files)
+    result = git_helper.git_commit(
+        "Cleanup branch for correct builds.", 
+        cleanup_files
+    )
+
     pb_version_path = os.path.join('debian', 'pb_version')
 
     if not os.path.exists(pb_version_path):
index d3a10e5140e3d1957ec563ee1c7d9cb05e28d451..cb92d1668653cb43b7466115488219c9181ca2e0 100644 (file)
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 
 import os
+import shutil
 import git
 import logging
 import subprocess
@@ -153,4 +154,91 @@ def git_repo_has_branch(name):
             return True
     return False
 
+def git_cleanup_branch(paths):
+    """
+    Delete files in current checkout of branch. Pass in an iterable list 
+    of paths to delete.
+
+    This is sometimes necessary to ensure our build chain is working as 
+    expected. 
+
+    I.e. git-buildpackage tries to find it's configuration in also from 
+    the branch being used. That way our central configuration is ignored
+    and builds fail.
+    """
+    result = dict()
+    for path in paths:
+        try:
+            if not os.path.exists(path):
+                logger.info(
+                    'Cleanup of %s impossible: Not existing'
+                    %(path)
+                )
+                continue
+    
+            if os.path.isdir(path):
+                cmd=shutil.rmtree
+            elif os.path.isfile(path):
+                cmd=os.unlink
+            else:
+                raise Exception(
+                    'Cleanup of %s impossible: Unknown file type'
+                    %(path)
+                )
+                continue
+    
+            logger.debug(
+                'Trying to remove %s'
+                %(path)
+            )
+            cmd(path)
+        except Exception, error:
+            result.setdefault(path, error)
+        else:
+            result.setdefault(path, True)
+    return result
+
+
+def git_commit(message, paths=("-a",)):
+    """
+    Create a new commit with a given message. 
+    
+    You can also pass an interable object with the files to be commited. If
+    ommited, option "-a" is used.
+    """
+    cmd = [
+        GIT, 
+        'commit', 
+        '-m "%s"' %(message), 
+        '%s' %(' '.join(paths))
+    ]
+    cmdobj = subprocess.Popen(
+            cmd,
+            stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE,
+            shell=False,
+            env={'':''},
+            cwd='/',
+            close_fds=True
+    )
+
+    logger.info('commit changes for %s' %(' '.join(paths)))
+    logger.debug(
+            'calling "%s" for commit' %(' '.join(cmd))
+    )
+    ret = cmdobj.wait()
+    if ret:
+        error_str = cmdobj.stderr.read()
+        if not error_str:
+            error_str = cmdobj.stdout.read()
+        if not error_str:
+            error_str = 'No Error Msg found'
+        logger.error(
+                '%s returned with %s. Output was: %s'
+                %(' '.join(cmd), ret, error_str)
+        )
+        return False
+    logger.debug('changes for %s commited' %(' '.join(paths)))
+    return True
+
 # vim: autoindent smartindent tabstop=4 expandtab shiftwidth=4 softtabstop=4 nu enc=utf-8 cinwords=if,elif,else,for,while,try,except,finally,def,class