author ffxbld
Tue, 20 May 2014 14:53:03 -0400
changeset 1033 f6d9311d9246cd18990eaebb6ac4b65e1f569f00
parent 97 c2e02e5bbfdb1c7a463cb44d75b06d45070597d3
permissions -rwxr-xr-x
Added FENNEC_30_0b6_RELEASE FENNEC_30_0b6_BUILD2 tag(s) for changeset production-0.8. DONTBUILD CLOSED TREE a=release

# BitKeeper hook script.
# was used as a base for this file, if you find any bugs or
# errors please email me.
# Amar Takhar <>

/path/to/ --repository "$REPOS" --revision "$REV" --branch \
"<branch>" --bbserver localhost --bbport 9989

import commands
import sys
import os
import re
if sys.version_info < (2, 6):
    import sets

# We have hackish "-d" handling here rather than in the Options
# subclass below because a common error will be to not have twisted in
# PYTHONPATH; we want to be able to print that error to the log if
# debug mode is on, so we set it up before the imports.

DEBUG = None

if '-d' in sys.argv:
    i = sys.argv.index('-d')
    DEBUG = sys.argv[i+1]
    del sys.argv[i]
    del sys.argv[i]

    f = open(DEBUG, 'a')
    sys.stderr = f
    sys.stdout = f

from twisted.internet import defer, reactor
from twisted.python import usage
from twisted.spread import pb
from twisted.cred import credentials

class Options(usage.Options):
    optParameters = [
        ['repository', 'r', None,
         "The repository that was changed."],
        ['revision', 'v', None,
         "The revision that we want to examine (default: latest)"],
        ['branch', 'b', None,
         "Name of the branch to insert into the branch field. (REQUIRED)"],
        ['category', 'c', None,
         "Schedular category."],
        ['bbserver', 's', 'localhost',
         "The hostname of the server that buildbot is running on"],
        ['bbport', 'p', 8007,
         "The port that buildbot is listening on"]
    optFlags = [
        ['dryrun', 'n', "Do not actually send changes"],

    def __init__(self):

    def postOptions(self):
        if self['repository'] is None:
            raise usage.error("You must pass --repository")

class ChangeSender:

    def getChanges(self, opts):
        """Generate and stash a list of Change dictionaries, ready to be sent
        to the buildmaster's PBChangeSource."""

        # first we extract information about the files that were changed
        repo = opts['repository']
        print "Repo:", repo
        rev_arg = ''
        if opts['revision']:
            rev_arg = '-r"%s"' % (opts['revision'], )
        changed = commands.getoutput("bk changes -v %s -d':GFILE:\\n' '%s'" % (
            rev_arg, repo)).split('\n')

        # Remove the first line, it's an info message you can't remove (annoying)
        del changed[0]

        change_info = commands.getoutput("bk changes %s -d':USER:\\n$each(:C:){(:C:)\\n}' '%s'" % (
            rev_arg, repo)).split('\n')

        # Remove the first line, it's an info message you can't remove (annoying)
        del change_info[0]

        who = change_info.pop(0)
        branch = opts['branch']
        message = '\n'.join(change_info)
        revision = opts.get('revision')

        changes = {'who': who,
                 'branch': branch,
                 'files': changed,
                 'comments': message,
                 'revision': revision}

        if opts.get('category'):
            changes['category'] = opts.get('category')

        return changes

    def sendChanges(self, opts, changes):
        pbcf = pb.PBClientFactory()
        reactor.connectTCP(opts['bbserver'], int(opts['bbport']), pbcf)
        d = pbcf.login(credentials.UsernamePassword('change', 'changepw'))
        d.addCallback(self.sendAllChanges, changes)
        return d

    def sendAllChanges(self, remote, changes):
        dl = remote.callRemote('addChange', changes)
        return dl

    def run(self):
        opts = Options()
            if not opts['branch']:
                print "You must supply a branch with -b or --branch."

        except usage.error, ue:
            print opts
            print "%s: %s" % (sys.argv[0], ue)

        changes = self.getChanges(opts)
        if opts['dryrun']:
            for k in changes.keys():
                print "[%10s]: %s" % (k, changes[k])
            print "*NOT* sending any changes"

        d = self.sendChanges(opts, changes)

        def quit(*why):
            print "quitting! because", why

        def failed(f):
            print "FAILURE: %s" % f

        d.addCallback(quit, "SUCCESS")
        reactor.callLater(60, quit, "TIMEOUT")

if __name__ == '__main__':
    s = ChangeSender()