--- /dev/null
+#!/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
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)
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(