From 146dc7c21f8307070bd76556b56d480757a278f3 Mon Sep 17 00:00:00 2001 From: Robin Wittler Date: Wed, 3 Aug 2011 13:30:03 +0200 Subject: [PATCH] add git_helper and implement it into testgit_build --- lib/git_helper.py | 153 ++++++++++++++++++++++++++++++++++++++++++++++ testgit_build.py | 76 +++-------------------- 2 files changed, 163 insertions(+), 66 deletions(-) create mode 100644 lib/git_helper.py diff --git a/lib/git_helper.py b/lib/git_helper.py new file mode 100644 index 0000000..31bdc86 --- /dev/null +++ b/lib/git_helper.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import git +import logging +import subprocess + +GIT = '/usr/bin/git' + +logger = logging.getLogger(__file__) + +class Error(Exception): + ''' + The git_helper base Exception + ''' + pass + +class BranchExistError(Error): + def __init__(self, bname): + self.bname = bname + + def __str__(self): + return 'Branch %s allready exists' %(self.bname) + +class BranchNotExistError(BranchExistError): + def __init__(self, bname): + self.bname = bname + + def __str__(self): + return 'Branch %s not exists' %(self.bname) + +def git_clone_remote_repository(url, destination): + if os.path.exists(destination): + logger.debug('%s allready exists' %(destination)) + if os.path.isdir(destination): + shutil.rmtree(destination) + else: + os.unlink(destination) + logger.debug('%s deleted' %(destination)) + + cmd = [GIT, 'clone', '%s' %(url), '%s' %(destination)] + cmdobj = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=False, + env={'':''}, + cwd='/', + close_fds=True + ) + + logger.info('begin to clone git repo from %s' %(url)) + logger.debug( + 'calling "%s" for cloning git repo' %(' '.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('repository %s checked out into %s' %(url, destination)) + return True + +def git_new_branch_from(branch_name, from_branch): + if git_repo_has_branch(branch_name): + raise BranchExistError(branch_name) + cmd = [GIT, 'checkout', '-b', branch_name, from_branch] + + cmdobj = subprocess.Popen( + cmd, + shell=False, + close_fds=True, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE, + env={'':''}, + cwd=os.getcwd() + ) + + logger.info( + 'checking out local branch %s from (remote) branch %s' + %(branch_name, from_branch) + ) + 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.info( + 'local branch %s successfully checked out.' %(branch_name) + ) + return True + +def git_new_debian_branch_from(from_branch): + return git_new_branch_from('debian', from_branch) + +def git_checkout_branch(branch_name): + if not git_repo_has_branch(branch_name): + raise BranchNotExistError(branch_name) + cmd = [GIT, 'checkout', branch_name] + + cmdobj = subprocess.Popen( + cmd, + shell=False, + close_fds=True, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE, + env={'':''}, + cwd=os.getcwd() + ) + + logger.info( + 'checking out local branch %s' + %(branch_name) + ) + 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.info( + 'local branch %s successfully checked out.' %(branch_name) + ) + return True + +def git_repo_has_branch(name): + r = git.repo.Repo() + for branch in r.branches: + if branch.name == name: + return True + return False + +# 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 diff --git a/testgit_build.py b/testgit_build.py index df92a41..135902d 100755 --- a/testgit_build.py +++ b/testgit_build.py @@ -81,71 +81,6 @@ def send_email(result): smtp.sendmail(SMTP_FROM, GIT_COMMITTER_EMAIL, msg) smtp.quit() -def git_clone_remote_repository(url, destination): - if os.path.exists(destination): - logger.debug('%s allready exists' %(destination)) - if os.path.isdir(destination): - shutil.rmtree(destination) - else: - os.unlink(destination) - logger.debug('%s deleted' %(destination)) - - cmd = [GIT, 'clone', '%s' %(url), '%s' %(destination)] - cmdobj = subprocess.Popen( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=False, - env={'':''}, - cwd=CWD, - close_fds=True - ) - - logger.info('begin to clone git repo from %s' %(url)) - logger.debug( - 'calling »%s« for cloning git repo' %(' '.join(cmd)) - ) - ret = cmdobj.wait() - - if ret: - logger.error('%s returned with %s' %(' '.join(cmd), ret)) - logger.error('Error was: %s' %(cmdobj.stderr.readlines())) - return False - logger.debug('repository %s checked out into %s' %(url, destination)) - return True - -def git_checkout_debian_branch(): - cmd = [GIT, 'checkout', '-b', GIT_DEBIAN_BRANCH, GIT_DEBIAN_REMOTE_BRANCH] - - cmdobj = subprocess.Popen( - cmd, - shell=False, - close_fds=True, - stderr=subprocess.PIPE, - stdout=subprocess.PIPE, - env={'':''}, - cwd=GIT_TARGET_DIR - ) - - logger.info( - 'checking out local branch %s from remote branch %s' - %(GIT_BRANCH_NAME, GIT_REMOTE_BRANCH_NAME) - ) - - logger.debug( - 'calling "%s" for checkout' %(' '.join(cmd)) - ) - - ret = cmdobj.wait() - if ret: - logger.error('%s returned with %s' %(' '.join(cmd), ret)) - logger.error('Error was: %s' %(cmdobj.stderr.readlines())) - return False - logger.info( - 'local branch %s successfully checked out.' %(GIT_DEBIAN_BRANCH) - ) - return True - def read_file(path): try: fh = open(path, 'r', 1) @@ -175,13 +110,22 @@ def exit_error(): sys.exit(1) if __name__ == '__main__': - if git_clone_remote_repository(GIT_REPO_PATH, GIT_TARGET_DIR): + if git_helper.git_clone_remote_repository(GIT_REPO_PATH, GIT_TARGET_DIR): logger.info('git clone was successfull') else: logger.info('git clone was not successfull') exit_error() atexit.register(remove_git_target_workspace) os.chdir(GIT_TARGET_DIR) + repo = git.repo.Repo() + if not GIT_COMMITTER_EMAIL: + # if the GIT_COMMIT_EMAIL is not set, + # get the email addi from the commit + for commit in repo.commits(): + if commit.id == GIT_NEW_ID: + GIT_COMMITTER_EMAIL = commit.committer.email + else: + raise Exception('No git_committer_email found') logger.info('Directory Listing: %s' %(os.listdir(GIT_TARGET_DIR))) #ftp = FTP( -- 2.39.5