store timestamp and current git commit when loading a module

This commit is contained in:
jesopo 2020-02-03 23:00:53 +00:00
parent e65fecd1b2
commit 7a15e5b2bf
4 changed files with 35 additions and 19 deletions

View file

@ -4,23 +4,11 @@ from src import IRCBot, ModuleManager, utils
class Module(ModuleManager.BaseModule):
@utils.hook("received.command.version")
def version(self, event):
commit_hash = None
git_dir = os.path.join(self.bot.directory, ".git")
head_filepath = os.path.join(git_dir, "HEAD")
if os.path.isfile(head_filepath):
ref = None
with open(head_filepath, "r") as head_file:
ref = head_file.readline().split(" ", 1)[1].strip()
branch = ref.rsplit("/", 1)[1]
ref_filepath = os.path.join(git_dir, ref)
if os.path.isfile(ref_filepath):
with open(ref_filepath, "r") as ref_file:
commit_hash = ref_file.readline().strip()
commit_hash = utils.git_commit(self.bot.directory)
out = "Version: BitBot %s" % IRCBot.VERSION
if not commit_hash == None:
out = "%s (%s@%s)" % (out, branch, commit_hash[:8])
out = "%s (%s@%s)" % (out, branch, commit_hash)
event["stdout"].write(out)
@utils.hook("received.command.source")

View file

@ -37,6 +37,7 @@ class EventHook(object):
self.priority = priority
self.docstring = utils.parse.docstring(func.__doc__ or "")
self.call_count = 0
self._kwargs: typing.Dict[str, typing.Any] = {}
self._multi_kwargs: typing.Dict[str, typing.List[typing.Any]] = {}
for key, value in kwargs:
@ -48,6 +49,7 @@ class EventHook(object):
self._kwargs[key] = value
def call(self, event: Event) -> typing.Any:
self.call_count += 1
return self.function(event)
def get_kwargs(self, key: str) -> typing.List[typing.Any]:
@ -122,6 +124,9 @@ class Events(object):
def purge_context(self, context: str):
self._root._purge_context(context)
def all_hooks(self):
return self._root.all_hooks()
class EventRoot(object):
def __init__(self, log: Logging.Log):
self.log = log
@ -232,3 +237,6 @@ class EventRoot(object):
if path_str in self._hooks:
return self._hooks[path_str][:]
return []
def all_hooks(self):
return self._hooks.copy()

View file

@ -1,5 +1,5 @@
import dataclasses, enum, gc, glob, importlib, importlib.util, io, inspect, os
import sys, typing, uuid
import dataclasses, datetime, enum, gc, glob, importlib, importlib.util, io
import inspect, os, sys, typing, uuid
from src import Config, EventManager, Exports, IRCBot, Logging, Timers, utils
class ModuleException(Exception):
@ -95,6 +95,10 @@ class LoadedModule(object):
context: str
import_name: str
is_core: bool
commit: typing.Optional[str] = None
loaded_at: datetime.datetime = dataclasses.field(
default_factory=lambda: utils.datetime.utcnow())
class ModuleManager(object):
def __init__(self,
@ -268,8 +272,9 @@ class ModuleManager(object):
for key, value in magic.get_exports():
context_exports.add(key, value)
current_commit = utils.git_commit(bot.directory)
return LoadedModule(definition.name, module_title, module_object,
context, import_name, definition.is_core)
context, import_name, definition.is_core, commit=current_commit)
def load_module(self, bot: "IRCBot.Bot", definition: ModuleDefinition
) -> LoadedModule:

View file

@ -1,5 +1,5 @@
import contextlib, enum, ipaddress, multiprocessing, queue, signal, threading
import typing
import contextlib, enum, ipaddress, multiprocessing, os.path, queue, signal
import threading, typing
from . import cli, consts, datetime, decorators, irc, http, parse, security
from .decorators import export, hook, kwarg, spec
@ -126,3 +126,18 @@ def deadline_process(func: typing.Callable[[], DeadlineProcessReturnType],
return out
else:
raise out # type: ignore
def git_commit(bot_directory: str) -> typing.Optional[str]:
git_dir = os.path.join(bot_directory, ".git")
head_filepath = os.path.join(git_dir, "HEAD")
if os.path.isfile(head_filepath):
ref = None
with open(head_filepath, "r") as head_file:
ref = head_file.readline().split(" ", 1)[1].strip()
branch = ref.rsplit("/", 1)[1]
ref_filepath = os.path.join(git_dir, ref)
if os.path.isfile(ref_filepath):
with open(ref_filepath, "r") as ref_file:
return ref_file.readline().strip()[:8]
return None