From 7a15e5b2bf314093d6ce36ae56832a793712113a Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 3 Feb 2020 23:00:53 +0000 Subject: store timestamp and current git commit when loading a module --- src/EventManager.py | 8 ++++++++ src/ModuleManager.py | 11 ++++++++--- src/utils/__init__.py | 19 +++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/EventManager.py b/src/EventManager.py index 02a0e08f..f10ebcd4 100644 --- a/src/EventManager.py +++ b/src/EventManager.py @@ -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() diff --git a/src/ModuleManager.py b/src/ModuleManager.py index 93545c59..dd378456 100644 --- a/src/ModuleManager.py +++ b/src/ModuleManager.py @@ -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: diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 303c70c5..7180766b 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -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 -- cgit v1.3.1-10-gc9f91