TAG buildbot-0.7.3
[buildbot.git] / contrib / hg_buildbot.py
blob0ab99fc56ba3443d8651df4a4f2d4d9ca3027b32
1 #! /usr/bin/python
3 # This is a script which delivers Change events from Mercurial to the
4 # buildmaster each time a changeset is pushed into a repository. Add it to
5 # the 'incoming' commit hook on your canonical "central" repository, by
6 # putting something like the following in the .hg/hgrc file of that
7 # repository:
9 # [hooks]
10 # incoming.buildbot = /PATH/TO/hg_buildbot.py BUILDMASTER:PORT
12 # Note that both Buildbot and Mercurial must be installed on the repository
13 # machine.
15 import os, sys, commands
16 from StringIO import StringIO
17 from buildbot.scripts import runner
19 MASTER = sys.argv[1]
21 CHANGESET_ID = os.environ["HG_NODE"]
23 # TODO: consider doing 'import mercurial.hg' and extract this information
24 # using the native python
25 out = commands.getoutput("hg -v log -r %s" % CHANGESET_ID)
26 # TODO: or maybe use --template instead of trying hard to parse everything
27 #out = commands.getoutput("hg --template SOMETHING log -r %s" % CHANGESET_ID)
29 s = StringIO(out)
30 while True:
31 line = s.readline()
32 if not line:
33 break
34 if line.startswith("user:"):
35 user = line[line.find(":")+1:].strip()
36 elif line.startswith("files:"):
37 files = line[line.find(":")+1:].strip().split()
38 elif line.startswith("description:"):
39 comments = "".join(s.readlines())
40 if comments[-1] == "\n":
41 # this removes the additional newline that hg emits
42 comments = comments[:-1]
43 break
45 change = {
46 'master': MASTER,
47 # note: this is more likely to be a full email address, which would make
48 # the left-hand "Changes" column kind of wide. The buildmaster should
49 # probably be improved to display an abbreviation of the username.
50 'username': user,
51 'revision': CHANGESET_ID,
52 'comments': comments,
53 'files': files,
56 runner.sendchange(c, True)