🏡 index : ~doyle/dave.git

author Jordan Doyle <jordan@doyle.wf> 2017-07-30 7:25:31.0 +01:00:00
committer Jordan Doyle <jordan@doyle.wf> 2017-07-30 7:25:31.0 +01:00:00
commit
f39a69cb80acffad16653b5960cf2738f552a640 [patch]
tree
a631169effe9d9085f8700b9f38cae74493e1e6e
parent
d954b1a8572c10bd7ab5682d1bcdd95e2ec8e767
download
f39a69cb80acffad16653b5960cf2738f552a640.tar.gz

Pull from git every 2 minutes since changes to our modules take effect immediately



Diff

 dave/dave.py | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/dave/dave.py b/dave/dave.py
index 60ac383..9ced8c8 100644
--- a/dave/dave.py
+++ a/dave/dave.py
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
import sys
from datetime import datetime

from humanize import naturaltime
from twisted.internet import reactor, protocol, ssl
from twisted.words.protocols import irc
from twisted.python import log
@@ -7,14 +10,18 @@
import pkgutil
import dave.modules as modules
import re
import subprocess
import dave.config as config
from twisted.internet import reactor
from twisted.internet import reactor, task
from twisted.internet.threads import deferToThread


class Dave(irc.IRCClient):
    nickname = config.config["irc"]["nick"]

    def __init__(self):
        Dave.instance = self

    def connectionMade(self):
        irc.IRCClient.connectionMade(self)
        log.msg("Connected to server at {} with name {}".format(
@@ -105,12 +112,42 @@
        else:
            self.msg(source, "{}: {}".format(sender, msg))

def autopull():
    log.msg("Pulling latest code from git.")
    output = subprocess.check_output(["git", "pull"])

    if not "Already up-to-date" in str(output):
        # check latest commit message
        args = ["git", "log", "-1", "--pretty=format:{}".format(",".join([
            "%h", "%s", "%at", "%an", "%ae"
        ]))]
        output = subprocess.check_output(args).split(b",")
        log.msg("Pulled latest commit.")

        msg = "{} ({}) authored by {} ({}) {}".format(
            str(output[1], 'utf-8'),
            str(output[0], 'utf-8'),
            str(output[3], 'utf-8'),
            str(output[4], 'utf-8'),
            naturaltime(datetime.utcnow().timestamp() -
                        float(output[2]))
        )

        log.msg("Updated, {}".format(msg))

        if hasattr(Dave, "instance"):
            for channel in config.config["irc"]["channels"]:
                Dave.instance.msg(channel, msg)
    else:
        log.msg("Already up to date.")

def main():
    log.startLogging(sys.stdout)

    # pull from github every 2 minutes
    task.LoopingCall(lambda: deferToThread(autopull)).start(120.0)

    factory = protocol.ReconnectingClientFactory()
    factory.protocol = Dave
    factory = protocol.ReconnectingClientFactory.forProtocol(Dave)
    reactor.connectSSL(config.config["irc"]["host"], config.config["irc"]["port"],
                       factory, ssl.ClientContextFactory())
    reactor.run()