From: Eric S. Raymond Date: Tue, 2 Oct 2012 19:06:17 +0000 (-0400) Subject: Refactoring step. X-Git-Tag: 1.5~24 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1c04ae9b08472b2b8fb8fcf96aa220a4d58f6294;p=irker.git Refactoring step. --- diff --git a/irkerhook.py b/irkerhook.py index 26855c4..cd5e070 100755 --- a/irkerhook.py +++ b/irkerhook.py @@ -337,7 +337,47 @@ def hg_hook(ui, repo, _hooktype, node=None, _url=None, **_kwds): # [hooks] # incoming.irker = python:/path/to/irkerhook.py:hg_hook extractor = HgExtractor([(ui, repo, node)]) - generate_and_send(extractor) + ship(extractor) + +def ship(commit, debug): + metadata = extractor.commit_factory(commit) + # Message reduction. The assumption here is that IRC can't handle + # lines more than 510 characters long. If we exceed that length, we + # try knocking out the file list, on the theory that for notification + # purposes the commit text is more important. If it's still too long + # there's nothing much can be done other than ship it expecting the IRC + # server to truncate. + privmsg = str(metadata) + if len(privmsg) > 510: + metadata.files = "" + privmsg = str(metadata) + + # Anti-spamming guard. + channel_list = extractor.channels.split(",") + if extractor.maxchannels != 0: + channel_list = channel_list[:extractor.maxchannels] + + # Ready to ship. + message = json.dumps({"to":channel_list, "privmsg":privmsg}) + if debug: + print message + else: + try: + if extractor.tcp: + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((extractor.server or default_server, IRKER_PORT)) + sock.sendall(message + "\n") + finally: + sock.close() + else: + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.sendto(message + "\n", (extractor.server or default_server, IRKER_PORT)) + finally: + sock.close() + except socket.error, e: + sys.stderr.write("%s\n" % e) if __name__ == "__main__": notify = True @@ -376,44 +416,6 @@ if __name__ == "__main__": commits = [extractor.head()] for commit in commits: - metadata = extractor.commit_factory(commit) - - # Message reduction. The assumption here is that IRC can't handle - # lines more than 510 characters long. If we exceed that length, we - # try knocking out the file list, on the theory that for notification - # purposes the commit text is more important. If it's still too long - # there's nothing much can be done other than ship it expecting the IRC - # server to truncate. - privmsg = str(metadata) - if len(privmsg) > 510: - metadata.files = "" - privmsg = str(metadata) - - # Anti-spamming guard. - channel_list = extractor.channels.split(",") - if extractor.maxchannels != 0: - channel_list = channel_list[:extractor.maxchannels] - - # Ready to ship. - message = json.dumps({"to":channel_list, "privmsg":privmsg}) - if not notify: - print message - else: - try: - if extractor.tcp: - try: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((extractor.server or default_server, IRKER_PORT)) - sock.sendall(message + "\n") - finally: - sock.close() - else: - try: - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.sendto(message + "\n", (extractor.server or default_server, IRKER_PORT)) - finally: - sock.close() - except socket.error, e: - sys.stderr.write("%s\n" % e) + ship(commit, not notify) #End