All pastes #927530 Raw Edit

Miscellany

public python v1 · immutable
·published 2008-03-04 10:53 UTC
rendered paste body
#!/usr/bin/pythonfrom socket import *import sys, os, marshal, ConfigParser, gcmoduleMap = {}def p4CmdList(cmd):    cmd = "p4 -G %s" % cmd    pipe = os.popen(cmd, "rb")    result = []    try:        while True:            entry = marshal.load(pipe)            result.append(entry)    except EOFError:        pass    exitCode = pipe.close()    if exitCode != None:        entry = {}        entry["p4ExitCode"] = exitCode        result.append(entry)    return resultdef p4Cmd(cmd):    list = p4CmdList(cmd)    result = {}    for entry in list:        result.update(entry)    return result;def extractFilesFromCommit(commit):    files = []    fnum = 0    while commit.has_key("depotFile%s" % fnum):        path =  commit["depotFile%s" % fnum]        file = {}        file["path"] = path        file["rev"] = commit["rev%s" % fnum]        file["action"] = commit["action%s" % fnum]        file["type"] = commit["type%s" % fnum]        files.append(file)        fnum = fnum + 1    return filesdef readConfigFile():    parser = ConfigParser.ConfigParser()    parser.read(os.path.expanduser("~/.git-p4-submit-trigger.cfg"))    global moduleMap    moduleMap = {}    for mapping in parser.items("modules"):        moduleMap[mapping[0]] = mapping[1]    for envVar in parser.items("environment"):        os.environ[envVar[0]] = envVar[1]os.environ["PATH"] = "/usr/local/git/bin:/usr/local/bin:/usr/bin" + os.environ.get("PATH")buf = 1024addr = ("", 6666)sock = socket(AF_INET,SOCK_DGRAM)sock.bind(addr)#print "listening for notifications"while 1:    change,addr = sock.recvfrom(buf)    if change.endswith("\n"):        change = change[:-1]    try:        change = int(change)    except ValueError:        print "received invalid notification: %s" % change    #sys.stdout.write("syncing to change %s" % change)    readConfigFile()    commit = p4Cmd("describe %s" % change)    files = extractFilesFromCommit(commit)    modules = set()    for file in files:        for modulePath in moduleMap.keys():            if file["path"].startswith(modulePath):                modules.add(moduleMap[modulePath])    for module in modules:        if os.system("cd %s && /usr/local/bin/git-p4 sync --import-local --silent" % module) != 0:            print("syncing %s failed!" % module)            sys.exit(1)    #sys.stdout.write(" done %s\n" % modules)    gc.collect()