gitzilla/utils.py
Devendra Gera 7c8f173732 initialize pybugz via overridable init_bugzilla helper
custom hooks can now provide an external init_bugzilla helper when invoking
the gitzilla.hooks.{post_receive,update} hooks. The init_bugzilla function
is passed the bugzilla URL, the username and the password and is expected
to return an instance of bugz.bugzilla.Bugz (from pybugz).

This may be used, among other things, to extend the range of auth options
available to other than those provided by gitzilla. For example, one could
easy implement HTTP Basic auth.
2010-03-14 03:23:32 +05:30

106 lines
2.4 KiB
Python

"""
utils module for gitzilla
"""
import os
import sys
import subprocess
import bugz.bugzilla
def execute(asCommand, bSplitLines=False, bIgnoreErrors=False):
"""
Utility function to execute a command and return the output.
"""
p = subprocess.Popen(asCommand,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=False,
close_fds=True,
universal_newlines=True,
env=None)
if bSplitLines:
data = p.stdout.readlines()
else:
data = p.stdout.read()
iRetCode = p.wait()
if iRetCode and not bIgnoreErrors:
print >>sys.stderr, 'Failed to execute command: %s\n%s' % (command, data)
sys.exit(-1)
return data
def init_bugzilla(sBZUrl, sBZUser, sBZPasswd):
"""
initializes and returns a bugz.bugzilla.Bugz instance.
This may be overridden in custom hook scripts in order to expand auth
support.
"""
if sBZUrl is None:
raise ValueError("No Bugzilla URL specified")
oBZ = bugz.bugzilla.Bugz(sBZUrl, user=sBZUser, password=sBZPasswd)
return oBZ
def get_changes(sOldRev, sNewRev, sFormatSpec, sSeparator):
"""
returns an array of chronological changes, between sOldRev and sNewRev,
according to the format spec sFormatSpec.
"""
sChangeLog = execute(["git", "whatchanged",
"--format=format:%s%s" % (sSeparator, sFormatSpec),
"%s..%s" % (sOldRev, sNewRev)])
asChangeLogs = sChangeLog.split(sSeparator)
asChangeLogs.reverse()
return asChangeLogs[:-1]
def post_to_bugzilla(iBugId, sComment, sBZUrl, sBZUser, sBZPasswd):
"""
posts the comment to the given bug id.
"""
if sBZUrl is None:
raise ValueError("No Bugzilla URL specified")
oBZ = bugz.bugzilla.Bugz(sBZUrl, user=sBZUser, password=sBZPasswd)
oBZ.modify(iBugId, comment=sComment)
def get_bug_status(oBugz, iBugId):
"""
given the bugz.bugzilla.Bugz instance and the bug id, returns the bug
status.
"""
oBug = oBugz.get(iBugId)
if oBug is None:
return None
return oBug.getroot().find("bug/bug_status").text
def notify_and_exit(sMsg):
"""
notifies the error and exits.
"""
print """
======================================================================
Cannot accept commit.
%s
======================================================================
""" % (sMsg,)
sys.exit(1)