contrib / ciabot / ciabot.pyon commit contrib/thunderbird-patch-inline: do not require bash to run the script (806e0ab)
   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.  If there's nothing unusual about your hosting setup,
  14# you can specify the project name with a -p option and avoid having
  15# to modify this script.  Try it with -n to see the notification mail
  16# dumped to stdout and verify that it looks sane. With -V it dumps its
  17# version and exits.
  18#
  19# In post-commit, run it without arguments (other than possibly a -p
  20# option). It will query for current HEAD and the latest commit ID to
  21# get the information it needs.
  22#
  23# In update, call it with a refname followed by a list of commits:
  24# You want to reverse the order git rev-list emits becxause it lists
  25# from most recent to oldest.
  26#
  27# /path/to/ciabot.py ${refname} $(git rev-list ${oldhead}..${newhead} | tac)
  28#
  29# Note: this script uses mail, not XML-RPC, in order to avoid stalling
  30# until timeout when the CIA XML-RPC server is down.
  31#
  32
  33#
  34# The project as known to CIA. You will either want to change this
  35# or invoke the script with a -p option to set it.
  36#
  37project=None
  38
  39#
  40# You may not need to change these:
  41#
  42import os, sys, commands, socket, urllib
  43
  44# Name of the repository.
  45# You can hardwire this to make the script faster.
  46repo = os.path.basename(os.getcwd())
  47
  48# Fully-qualified domain name of this host.
  49# You can hardwire this to make the script faster.
  50host = socket.getfqdn()
  51
  52# Changeset URL prefix for your repo: when the commit ID is appended
  53# to this, it should point at a CGI that will display the commit
  54# through gitweb or something similar. The defaults will probably
  55# work if you have a typical gitweb/cgit setup.
  56#
  57#urlprefix="http://%(host)s/cgi-bin/gitweb.cgi?p=%(repo)s;a=commit;h="
  58urlprefix="http://%(host)s/cgi-bin/cgit.cgi/%(repo)s/commit/?id="
  59
  60# The service used to turn your gitwebbish URL into a tinyurl so it
  61# will take up less space on the IRC notification line.
  62tinyifier = "http://tinyurl.com/api-create.php?url="
  63
  64# The template used to generate the XML messages to CIA.  You can make
  65# visible changes to the IRC-bot notification lines by hacking this.
  66# The default will produce a notfication line that looks like this:
  67#
  68# ${project}: ${author} ${repo}:${branch} * ${rev} ${files}: ${logmsg} ${url}
  69#
  70# By omitting $files you can collapse the files part to a single slash.
  71xml = '''\
  72<message>
  73  <generator>
  74    <name>CIA Python client for Git</name>
  75    <version>%(gitver)s</version>
  76    <url>%(generator)s</url>
  77  </generator>
  78  <source>
  79    <project>%(project)s</project>
  80    <branch>%(repo)s:%(branch)s</branch>
  81  </source>
  82  <timestamp>%(ts)s</timestamp>
  83  <body>
  84    <commit>
  85      <author>%(author)s</author>
  86      <revision>%(rev)s</revision>
  87      <files>
  88        %(files)s
  89      </files>
  90      <log>%(logmsg)s %(url)s</log>
  91      <url>%(url)s</url>
  92    </commit>
  93  </body>
  94</message>
  95'''
  96
  97#
  98# No user-serviceable parts below this line:
  99#
 100
 101# Addresses for the e-mail. The from address is a dummy, since CIA
 102# will never reply to this mail.
 103fromaddr = "CIABOT-NOREPLY@" + host
 104toaddr = "cia@cia.navi.cx"
 105
 106# Identify the generator script.
 107# Should only change when the script itself gets a new home and maintainer.
 108generator="http://www.catb.org/~esr/ciabot.py"
 109
 110def do(command):
 111    return commands.getstatusoutput(command)[1]
 112
 113def report(refname, merged):
 114    "Generate a commit notification to be reported to CIA"
 115
 116    # Try to tinyfy a reference to a web view for this commit.
 117    try:
 118        url = open(urllib.urlretrieve(tinyifier + urlprefix + merged)[0]).read()
 119    except:
 120        url = urlprefix + merged
 121
 122    branch = os.path.basename(refname)
 123
 124    # Compute a shortnane for the revision
 125    rev = do("git describe '"+ merged +"' 2>/dev/null") or merged[:12]
 126
 127    # Extract the neta-information for the commit
 128    rawcommit = do("git cat-file commit " + merged)
 129    files=do("git diff-tree -r --name-only '"+ merged +"' | sed -e '1d' -e 's-.*-<file>&</file>-'")
 130    inheader = True
 131    headers = {}
 132    logmsg = ""
 133    for line in rawcommit.split("\n"):
 134        if inheader:
 135            if line:
 136                fields = line.split()
 137                headers[fields[0]] = " ".join(fields[1:])
 138            else:
 139                inheader = False
 140        else:
 141            logmsg = line
 142            break
 143    (author, ts) = headers["author"].split(">")
 144
 145    # This discards the part of the authors addrsss after @.
 146    # Might be bnicece to ship the full email address, if not
 147    # for spammers' address harvesters - getting this wrong
 148    # would make the freenode #commits channel into harvester heaven.
 149    author = author.replace("<", "").split("@")[0].split()[-1]
 150
 151    # This ignores the timezone.  Not clear what to do with it...
 152    ts = ts.strip().split()[0]
 153
 154    context = locals()
 155    context.update(globals())
 156
 157    out = xml % context
 158
 159    message = '''\
 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    return message
 169
 170if __name__ == "__main__":
 171    import getopt
 172
 173    try:
 174        (options, arguments) = getopt.getopt(sys.argv[1:], "np:V")
 175    except getopt.GetoptError, msg:
 176        print "ciabot.py: " + str(msg)
 177        raise SystemExit, 1
 178
 179    mailit = True
 180    for (switch, val) in options:
 181        if switch == '-p':
 182            project = val
 183        elif switch == '-n':
 184            mailit = False
 185        elif switch == '-V':
 186            print "ciabot.py: version 3.2"
 187            sys.exit(0)
 188
 189    # Cough and die if user has not specified a project
 190    if not project:
 191        sys.stderr.write("ciabot.py: no project specified, bailing out.\n")
 192        sys.exit(1)
 193
 194    # We'll need the git version number.
 195    gitver = do("git --version").split()[0]
 196
 197    urlprefix = urlprefix % globals()
 198
 199    # The script wants a reference to head followed by the list of
 200    # commit ID to report about.
 201    if len(arguments) == 0:
 202        refname = do("git symbolic-ref HEAD 2>/dev/null")
 203        merges = [do("git rev-parse HEAD")]
 204    else:
 205        refname = arguments[0]
 206        merges = arguments[1:]
 207
 208    if mailit:
 209        import smtplib
 210        server = smtplib.SMTP('localhost')
 211
 212    for merged in merges:
 213        message = report(refname, merged)
 214        if mailit:
 215            server.sendmail(fromaddr, [toaddr], message)
 216        else:
 217            print message
 218
 219    if mailit:
 220        server.quit()
 221
 222#End