Fix status where change object's comments field is None
[buildbot.git] / contrib / viewcvspoll.py
blobf7dfb16190cd651b1df9088b59b4e0b4a5e83291
1 #! /usr/bin/python
3 """Based on the fakechanges.py contrib script"""
5 import sys
6 import commands
7 import random
8 import os.path
9 import time
10 import MySQLdb
12 from twisted.spread import pb
13 from twisted.cred import credentials
14 from twisted.internet import reactor, task
15 from twisted.python import log
18 class ViewCvsPoller:
20 def __init__(self):
22 def _load_rc():
23 import user
24 ret = {}
25 for line in open(os.path.join(
26 user.home, ".cvsblamerc")).readlines():
27 if line.find("=") != -1:
28 key, val = line.split("=")
29 ret[key.strip()] = val.strip()
30 return ret
31 # maybe add your own keys here db=xxx, user=xxx, passwd=xxx
32 self.cvsdb = MySQLdb.connect("cvs", **_load_rc())
33 #self.last_checkin = "2005-05-11" # for testing
34 self.last_checkin = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
36 def get_changes(self):
37 changes = []
39 def empty_change():
40 return {'who': None, 'files': [], 'comments': None}
41 change = empty_change()
43 cursor = self.cvsdb.cursor()
44 cursor.execute("""SELECT whoid, descid, fileid, dirid, branchid, \
45 ci_when FROM checkins WHERE ci_when>='%s'""" % self.last_checkin)
46 last_checkin = None
47 for whoid, descid, fileid, dirid, branchid, ci_when in \
48 cursor.fetchall():
49 if branchid != 1: # only head
50 continue
51 cursor.execute("""SELECT who from people where id=%s""" % whoid)
52 who = cursor.fetchone()[0]
53 cursor.execute("""SELECT description from descs where id=%s""" % (
54 descid))
55 desc = cursor.fetchone()[0]
56 cursor.execute("""SELECT file from files where id=%s""" % fileid)
57 filename = cursor.fetchone()[0]
58 cursor.execute("""SELECT dir from dirs where id=%s""" % dirid)
59 dirname = cursor.fetchone()[0]
60 if who == change["who"] and desc == change["comments"]:
61 change["files"].append("%s/%s" % (dirname, filename))
62 elif change["who"]:
63 changes.append(change)
64 change = empty_change()
65 else:
66 change["who"] = who
67 change["files"].append("%s/%s" % (dirname, filename))
68 change["comments"] = desc
69 if last_checkin == None or ci_when > last_checkin:
70 last_checkin = ci_when
71 if last_checkin:
72 self.last_checkin = last_checkin
73 return changes
76 poller = ViewCvsPoller()
79 def error(*args):
80 log.err()
81 reactor.stop()
84 def poll_changes(remote):
85 print "GET CHANGES SINCE", poller.last_checkin,
86 changes = poller.get_changes()
87 for change in changes:
88 print change["who"], "\n *", "\n * ".join(change["files"])
89 remote.callRemote('addChange', change).addErrback(error)
90 print
91 reactor.callLater(60, poll_changes, remote)
94 factory = pb.PBClientFactory()
95 reactor.connectTCP("localhost", 9999, factory)
96 deferred = factory.login(credentials.UsernamePassword("change", "changepw"))
97 deferred.addCallback(poll_changes).addErrback(error)
99 reactor.run()