contrib / ciabot / ciabot.pyon commit Merge branch 'jk/receive-pack-deadlocks-with-early-failure' into maint (bd8e338)
   1#!/usr/bin/env python
   2# Copyright (c) 2010 Eric S. Raymond <esr@thyrsus.com>
   3# Distributed under BSD terms.
   4#
   5# This script contains porcelain and porcelain byproducts.
   6# It's Python because the Python standard libraries avoid portability/security
   7# issues raised by callouts in the ancestral Perl and sh scripts.  It should
   8# be compatible back to Python 2.1.5
   9#
  10# usage: ciabot.py [-V] [-n] [-p projectname]  [refname [commits...]]
  11#
  12# This script is meant to be run either in a post-commit hook or in an
  13# update hook. Try it with -n to see the notification mail dumped to
  14# stdout and verify that it looks sane. With -V it dumps its version
  15# and exits.
  16#
  17# In post-commit, run it without arguments. It will query for
  18# current HEAD and the latest commit ID to get the information it
  19# needs.
  20#
  21# In update, call it with a refname followed by a list of commits:
  22# You want to reverse the order git rev-list emits because it lists
  23# from most recent to oldest.
  24#
  25# /path/to/ciabot.py ${refname} $(git rev-list ${oldhead}..${newhead} | tac)
  26#
  27# Configuration variables affecting this script:
  28#
  29# ciabot.project = name of the project
  30# ciabot.repo = name of the project repo for gitweb/cgit purposes
  31# ciabot.xmlrpc  = if true (default), ship notifications via XML-RPC
  32# ciabot.revformat = format in which the revision is shown
  33#
  34# ciabot.project defaults to the directory name of the repository toplevel.
  35# ciabot.repo defaults to ciabot.project lowercased.
  36#
  37# This means that in the normal case you need not do any configuration at all,
  38# but setting the project name will speed it up slightly.
  39#
  40# The revformat variable may have the following values
  41# raw -> full hex ID of commit
  42# short -> first 12 chars of hex ID
  43# describe = -> describe relative to last tag, falling back to short
  44# The default is 'describe'.
  45#
  46# Note: the CIA project now says only XML-RPC is reliable, so
  47# we default to that.
  48#
  49
  50import sys
  51if sys.hexversion < 0x02000000:
  52        # The limiter is the xml.sax module
  53        sys.stderr.write("ciabot.py: requires Python 2.0.0 or later.\n")
  54        sys.exit(1)
  55
  56import os, commands, socket, urllib
  57from xml.sax.saxutils import escape
  58
  59# Changeset URL prefix for your repo: when the commit ID is appended
  60# to this, it should point at a CGI that will display the commit
  61# through gitweb or something similar. The defaults will probably
  62# work if you have a typical gitweb/cgit setup.
  63#
  64#urlprefix="http://%(host)s/cgi-bin/gitweb.cgi?p=%(repo)s;a=commit;h="
  65urlprefix="http://%(host)s/cgi-bin/cgit.cgi/%(repo)s/commit/?id="
  66
  67# The service used to turn your gitwebbish URL into a tinyurl so it
  68# will take up less space on the IRC notification line.
  69tinyifier = "http://tinyurl.com/api-create.php?url="
  70
  71# The template used to generate the XML messages to CIA.  You can make
  72# visible changes to the IRC-bot notification lines by hacking this.
  73# The default will produce a notfication line that looks like this:
  74#
  75# ${project}: ${author} ${repo}:${branch} * ${rev} ${files}: ${logmsg} ${url}
  76#
  77# By omitting $files you can collapse the files part to a single slash.
  78xml = '''\
  79<message>
  80  <generator>
  81    <name>CIA Python client for Git</name>
  82    <version>%(version)s</version>
  83    <url>%(generator)s</url>
  84  </generator>
  85  <source>
  86    <project>%(project)s</project>
  87    <branch>%(repo)s:%(branch)s</branch>
  88  </source>
  89  <timestamp>%(ts)s</timestamp>
  90  <body>
  91    <commit>
  92      <author>%(author)s</author>
  93      <revision>%(rev)s</revision>
  94      <files>
  95        %(files)s
  96      </files>
  97      <log>%(logmsg)s %(url)s</log>
  98      <url>%(url)s</url>
  99    </commit>
 100  </body>
 101</message>
 102'''
 103
 104#
 105# No user-serviceable parts below this line:
 106#
 107
 108# Where to ship e-mail notifications.
 109toaddr = "cia@cia.vc"
 110
 111# Identify the generator script.
 112# Should only change when the script itself gets a new home and maintainer.
 113generator = "http://www.catb.org/~esr/ciabot.py"
 114version = "3.6"
 115
 116def do(command):
 117    return commands.getstatusoutput(command)[1]
 118
 119def report(refname, merged, xmlrpc=True):
 120    "Generate a commit notification to be reported to CIA"
 121
 122    # Try to tinyfy a reference to a web view for this commit.
 123    try:
 124        url = open(urllib.urlretrieve(tinyifier + urlprefix + merged)[0]).read()
 125    except:
 126        url = urlprefix + merged
 127
 128    branch = os.path.basename(refname)
 129
 130    # Compute a description for the revision
 131    if revformat == 'raw':
 132        rev = merged
 133    elif revformat == 'short':
 134        rev = ''
 135    else: # revformat == 'describe'
 136        rev = do("git describe %s 2>/dev/null" % merged)
 137    if not rev:
 138        rev = merged[:12]
 139
 140    # Extract the meta-information for the commit
 141    files=do("git diff-tree -r --name-only '"+ merged +"' | sed -e '1d' -e 's-.*-<file>&</file>-'")
 142    metainfo = do("git log -1 '--pretty=format:%an <%ae>%n%at%n%s' " + merged)
 143    (author, ts, logmsg) = metainfo.split("\n")
 144    logmsg = escape(logmsg)
 145
 146    # This discards the part of the author's address after @.
 147    # Might be be nice to ship the full email address, if not
 148    # for spammers' address harvesters - getting this wrong
 149    # would make the freenode #commits channel into harvester heaven.
 150    author = escape(author.replace("<", "").split("@")[0].split()[-1])
 151
 152    # This ignores the timezone.  Not clear what to do with it...
 153    ts = ts.strip().split()[0]
 154
 155    context = locals()
 156    context.update(globals())
 157
 158    out = xml % context
 159    mail = '''\
 160Message-ID: <%(merged)s.%(author)s@%(project)s>
 161From: %(fromaddr)s
 162To: %(toaddr)s
 163Content-type: text/xml
 164Subject: DeliverXML
 165
 166%(out)s''' % locals()
 167
 168    if xmlrpc:
 169        return out
 170    else:
 171        return mail
 172
 173if __name__ == "__main__":
 174    import getopt
 175
 176    # Get all config variables
 177    revformat = do("git config --get ciabot.revformat")
 178    project = do("git config --get ciabot.project")
 179    repo = do("git config --get ciabot.repo")
 180    xmlrpc = do("git config --get ciabot.xmlrpc")
 181    xmlrpc = not (xmlrpc and xmlrpc == "false")
 182
 183    host = socket.getfqdn()
 184    fromaddr = "CIABOT-NOREPLY@" + host
 185
 186    try:
 187        (options, arguments) = getopt.getopt(sys.argv[1:], "np:xV")
 188    except getopt.GetoptError, msg:
 189        print "ciabot.py: " + str(msg)
 190        raise SystemExit, 1
 191
 192    notify = True
 193    for (switch, val) in options:
 194        if switch == '-p':
 195            project = val
 196        elif switch == '-n':
 197            notify = False
 198        elif switch == '-x':
 199            xmlrpc = True
 200        elif switch == '-V':
 201            print "ciabot.py: version", version
 202            sys.exit(0)
 203
 204    # The project variable defaults to the name of the repository toplevel.
 205    if not project:
 206        here = os.getcwd()
 207        while True:
 208            if os.path.exists(os.path.join(here, ".git")):
 209                project = os.path.basename(here)
 210                break
 211            elif here == '/':
 212                sys.stderr.write("ciabot.py: no .git below root!\n")
 213                sys.exit(1)
 214            here = os.path.dirname(here)
 215
 216    if not repo:
 217        repo = project.lower()
 218
 219    urlprefix = urlprefix % globals()
 220
 221    # The script wants a reference to head followed by the list of
 222    # commit ID to report about.
 223    if len(arguments) == 0:
 224        refname = do("git symbolic-ref HEAD 2>/dev/null")
 225        merges = [do("git rev-parse HEAD")]
 226    else:
 227        refname = arguments[0]
 228        merges = arguments[1:]
 229
 230    if notify:
 231        if xmlrpc:
 232            import xmlrpclib
 233            server = xmlrpclib.Server('http://cia.vc/RPC2');
 234        else:
 235            import smtplib
 236            server = smtplib.SMTP('localhost')
 237
 238    for merged in merges:
 239        message = report(refname, merged, xmlrpc)
 240        if not notify:
 241            print message
 242        elif xmlrpc:
 243            try:
 244                # RPC server is flaky, this can fail due to timeout.
 245                server.hub.deliver(message)
 246            except socket.error, e:
 247                sys.stderr.write("%s\n" % e)
 248        else:
 249            server.sendmail(fromaddr, [toaddr], message)
 250
 251    if notify:
 252        if not xmlrpc:
 253            server.quit()
 254
 255#End