From: Mathias Klette Date: Thu, 15 Sep 2011 15:49:39 +0000 (+0200) Subject: adding cleanup branch functions X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=c699db844661255e8d465cdb6cc55576d2c66d6f;p=profitbricks%2Fjenkins-build-scripts.git adding cleanup branch functions --- diff --git a/debian_build.py b/debian_build.py index 83b3948..ee5f0dd 100755 --- a/debian_build.py +++ b/debian_build.py @@ -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): diff --git a/lib/git_helper.py b/lib/git_helper.py index d3a10e5..cb92d16 100644 --- a/lib/git_helper.py +++ b/lib/git_helper.py @@ -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