From bf907ac0e1dc32d495a4fdcce5769611b7c56bf9 Mon Sep 17 00:00:00 2001 From: cswimr Date: Sat, 25 Jan 2025 22:46:59 +0000 Subject: [PATCH 1/7] feat(hotreload): init --- hotreload/__init__.py | 5 +++ hotreload/hotreload.py | 70 ++++++++++++++++++++++++++++++++++++++++++ hotreload/info.json | 17 ++++++++++ pyproject.toml | 1 + uv.lock | 2 ++ 5 files changed, 95 insertions(+) create mode 100644 hotreload/__init__.py create mode 100644 hotreload/hotreload.py create mode 100644 hotreload/info.json diff --git a/hotreload/__init__.py b/hotreload/__init__.py new file mode 100644 index 0000000..7140931 --- /dev/null +++ b/hotreload/__init__.py @@ -0,0 +1,5 @@ +from .hotreload import HotReload + + +async def setup(bot): + await bot.add_cog(HotReload(bot)) diff --git a/hotreload/hotreload.py b/hotreload/hotreload.py new file mode 100644 index 0000000..73513f5 --- /dev/null +++ b/hotreload/hotreload.py @@ -0,0 +1,70 @@ +from pathlib import Path + +from red_commons.logging import RedTraceLogger, getLogger +from redbot.core import commands +from redbot.core.bot import Red +from redbot.core.core_commands import CoreLogic +from redbot.core.utils.chat_formatting import bold, humanize_list +from watchdog.events import FileSystemEvent, FileSystemEventHandler +from watchdog.observers import Observer + + +class HotReload(commands.Cog): + """Automatically reload cogs in local cog paths on file change.""" + + __author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"] + __git__ = "https://www.coastalcommits.com/cswimr/SeaCogs" + __version__ = "1.0.0-rc1" + __documentation__ = "https://seacogs.coastalcommits.com/hotreload/" + + def __init__(self, bot: Red) -> None: + super().__init__() + self.bot: Red = bot + self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload") + + def format_help_for_context(self, ctx: commands.Context) -> str: + pre_processed = super().format_help_for_context(ctx) or "" + n = "\n" if "\n\n" not in pre_processed else "" + text = [ + f"{pre_processed}{n}", + f"{bold('Cog Version:')} [{self.__version__}]({self.__git__})", + f"{bold('Author:')} {humanize_list(self.__author__)}", + f"{bold('Documentation:')} {self.__documentation__}", + ] + return "\n".join(text) + + async def get_paths(self) -> tuple[Path]: + """Retrieve user defined paths.""" + cog_manager = self.bot._cog_mgr + cog_paths = await cog_manager.user_defined_paths() + return (Path(path) for path in cog_paths) + + async def start_observer(self) -> None: + """Start the observer to watch for file changes.""" + observer = Observer() + for path in await self.get_paths(): + observer.schedule(HotReloadHandler(self.bot, path), path, recursive=True) + observer.start() + self.logger.info("Started observer.") + + +class HotReloadHandler(FileSystemEventHandler): + """Handler for file changes.""" + + def __init__(self, bot: Red, path: Path) -> None: + self.bot: Red = bot + self.path: Path = path + self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReloadHandler") + + def on_modified(self, event: FileSystemEvent) -> None: + """Handle file modification events.""" + if event.is_directory: + return + self.logger.info(f"File {event.src_path} has been modified.") + # self.bot.loop.create_task(self.reload_cog()) + + async def reload_cog(self, cog_name: str) -> None: + """Reload modified cog.""" + core_logic = CoreLogic(bot=self.bot) + core_logic._reload(pkg_names=(cog_name,)) + self.logger.info(f"Reloaded {cog_name} cog.") diff --git a/hotreload/info.json b/hotreload/info.json new file mode 100644 index 0000000..7d47a1e --- /dev/null +++ b/hotreload/info.json @@ -0,0 +1,17 @@ +{ + "author" : ["cswimr"], + "install_msg" : "Thank you for installing HotReload!", + "name" : "HotReload", + "short" : "Automatically reload cogs in local cog paths on file change.", + "description" : "Automatically reload cogs in local cog paths on file change.", + "end_user_data_statement" : "This cog does not store end user data.", + "hidden": false, + "disabled": false, + "min_bot_version": "3.5.0", + "min_python_version": [3, 10, 0], + "requirements": ["watchdog"], + "tags": [ + "utility", + "development" + ] +} diff --git a/pyproject.toml b/pyproject.toml index 2c40dee..ccf98da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ dependencies = [ "py-dactyl", "pydantic>=2.9.2", "red-discordbot>=3.5.14", + "watchdog>=5.0.3", "websockets>=13.1", ] diff --git a/uv.lock b/uv.lock index 9056ced..a029a28 100644 --- a/uv.lock +++ b/uv.lock @@ -1667,6 +1667,7 @@ dependencies = [ { name = "py-dactyl" }, { name = "pydantic" }, { name = "red-discordbot" }, + { name = "watchdog" }, { name = "websockets" }, ] @@ -1706,6 +1707,7 @@ requires-dist = [ { name = "py-dactyl", git = "https://github.com/cswimr/pydactyl" }, { name = "pydantic", specifier = ">=2.9.2" }, { name = "red-discordbot", specifier = ">=3.5.14" }, + { name = "watchdog", specifier = ">=5.0.3" }, { name = "websockets", specifier = ">=13.1" }, ] -- 2.45.3 From cbd4f6dfcc314a23188ec64fffca72e49d2065b1 Mon Sep 17 00:00:00 2001 From: cswimr Date: Sat, 25 Jan 2025 14:39:24 -0500 Subject: [PATCH 2/7] fix(devcontainer): ensure pip is installed in the devcontainer this is being done because Red-DiscordBot requires pip to be installed to function, but does not declare it as a dependency. this is only a problem when uv is being used to install dependencies in an environment where pip is not present. so, we ensure pip is present! --- .devcontainer/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 19d07ab..c08c396 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -31,4 +31,5 @@ COPY --from=uv --chown=vscode: /uv /uvx /bin/ COPY --from=python --chown=vscode: /usr/local /usr/local RUN ln -s /usr/local/bin/python3.11 /usr/local/bin/python; \ - python --version + python --version; \ + python -m ensurepip -- 2.45.3 From 549519782a32a498775eef43497c250eaefdce98 Mon Sep 17 00:00:00 2001 From: cswimr Date: Sat, 25 Jan 2025 14:43:02 -0500 Subject: [PATCH 3/7] fix(devcontainer): chown workspace directory for some reason, the default `.data` mount is not owned by the `vscode` user (on my system at least), so we `chown` it to ensure that `uv run redbot-setup` can write to the directory. --- .devcontainer/devcontainer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 23d0b75..104770e 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -30,6 +30,6 @@ "PROJECT_DIR": "/workspaces/SeaCogs" }, "mounts": ["source=seacogs-persistent-data,target=/workspaces/SeaCogs/.data,type=volume"], - "postCreateCommand": "uv sync --frozen", + "postCreateCommand": "uv sync --frozen && sudo chown -R vscode:vscode /workspaces/SeaCogs/.data", "remoteUser": "vscode" } -- 2.45.3 From cf7bc48f2f7ea0a5b5940079f3fc2e9197e6bca1 Mon Sep 17 00:00:00 2001 From: cswimr Date: Sat, 25 Jan 2025 15:47:05 -0500 Subject: [PATCH 4/7] chore(vscode): add `-vvv` argument to launch & debug --- .vscode/launch.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1d4d69e..44c2cf3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "debugpy", "request": "launch", "module": "redbot", - "args": ["local"] + "args": ["local", "-vvv"] } ] } -- 2.45.3 From a82d3490e7a0add130cbecbec75660ac420e4cbe Mon Sep 17 00:00:00 2001 From: cswimr Date: Sat, 25 Jan 2025 16:50:51 -0500 Subject: [PATCH 5/7] feat(vscode): add `--dev` argument to redbot args --- .vscode/launch.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 44c2cf3..84a95c2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "debugpy", "request": "launch", "module": "redbot", - "args": ["local", "-vvv"] + "args": ["local", "--dev", "-vvv"] } ] } -- 2.45.3 From ada949b4867aff0c3ad8b91a4df34982e941707b Mon Sep 17 00:00:00 2001 From: cswimr Date: Sat, 25 Jan 2025 22:50:35 +0000 Subject: [PATCH 6/7] feat(devcontainer): initialize redbot instance in postCreateCommand --- .devcontainer/devcontainer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 104770e..06e81b7 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -30,6 +30,6 @@ "PROJECT_DIR": "/workspaces/SeaCogs" }, "mounts": ["source=seacogs-persistent-data,target=/workspaces/SeaCogs/.data,type=volume"], - "postCreateCommand": "uv sync --frozen && sudo chown -R vscode:vscode /workspaces/SeaCogs/.data", + "postCreateCommand": "uv sync --frozen && sudo chown -R vscode:vscode /workspaces/SeaCogs/.data && uv run redbot-setup --no-prompt --instance-name=local --data-path=/workspaces/SeaCogs/.data --backend=json", "remoteUser": "vscode" } -- 2.45.3 From 4d0d4cae2e754f426f23fd6417697d924a17da78 Mon Sep 17 00:00:00 2001 From: cswimr Date: Sat, 25 Jan 2025 23:48:26 +0000 Subject: [PATCH 7/7] feat(hotreload): release 1.0.0 --- hotreload/hotreload.py | 45 +++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/hotreload/hotreload.py b/hotreload/hotreload.py index 73513f5..27a6250 100644 --- a/hotreload/hotreload.py +++ b/hotreload/hotreload.py @@ -1,3 +1,4 @@ +from asyncio import run_coroutine_threadsafe from pathlib import Path from red_commons.logging import RedTraceLogger, getLogger @@ -5,7 +6,7 @@ from redbot.core import commands from redbot.core.bot import Red from redbot.core.core_commands import CoreLogic from redbot.core.utils.chat_formatting import bold, humanize_list -from watchdog.events import FileSystemEvent, FileSystemEventHandler +from watchdog.events import FileSystemEvent, RegexMatchingEventHandler from watchdog.observers import Observer @@ -14,13 +15,28 @@ class HotReload(commands.Cog): __author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"] __git__ = "https://www.coastalcommits.com/cswimr/SeaCogs" - __version__ = "1.0.0-rc1" + __version__ = "1.0.0" __documentation__ = "https://seacogs.coastalcommits.com/hotreload/" def __init__(self, bot: Red) -> None: super().__init__() self.bot: Red = bot self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload") + self.observer = None + watchdog_loggers = [getLogger(name="watchdog.observers.inotify_buffer")] + for watchdog_logger in watchdog_loggers: + watchdog_logger.setLevel("INFO") # SHUT UP!!!! + + def cog_load(self) -> None: + """Start the observer when the cog is loaded.""" + self.bot.loop.create_task(self.start_observer()) + + def cog_unload(self) -> None: + """Stop the observer when the cog is unloaded.""" + if self.observer: + self.observer.stop() + self.observer.join() + self.logger.info("Stopped observer. No longer watching for file changes.") def format_help_for_context(self, ctx: commands.Context) -> str: pre_processed = super().format_help_for_context(ctx) or "" @@ -41,30 +57,35 @@ class HotReload(commands.Cog): async def start_observer(self) -> None: """Start the observer to watch for file changes.""" - observer = Observer() - for path in await self.get_paths(): - observer.schedule(HotReloadHandler(self.bot, path), path, recursive=True) - observer.start() - self.logger.info("Started observer.") + self.observer = Observer() + paths = await self.get_paths() + for path in paths: + self.observer.schedule(event_handler=HotReloadHandler(bot=self.bot, path=path), path=path, recursive=True) + self.observer.start() + self.logger.info("Started observer. Watching for file changes.") -class HotReloadHandler(FileSystemEventHandler): +class HotReloadHandler(RegexMatchingEventHandler): """Handler for file changes.""" def __init__(self, bot: Red, path: Path) -> None: + super().__init__(regexes=[r".*\.py$"]) self.bot: Red = bot self.path: Path = path - self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReloadHandler") + self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload.Observer") def on_modified(self, event: FileSystemEvent) -> None: """Handle file modification events.""" if event.is_directory: return - self.logger.info(f"File {event.src_path} has been modified.") - # self.bot.loop.create_task(self.reload_cog()) + relative_path = Path(event.src_path).relative_to(self.path) + package_name = relative_path.parts[0] + self.logger.info(f"File {'/'.join(relative_path.parts[1:])} in the cog {package_name} has been modified.") + run_coroutine_threadsafe(self.reload_cog(package_name), loop=self.bot.loop) async def reload_cog(self, cog_name: str) -> None: """Reload modified cog.""" core_logic = CoreLogic(bot=self.bot) - core_logic._reload(pkg_names=(cog_name,)) + self.logger.info(f"Reloading {cog_name} cog.") + await core_logic._reload(pkg_names=(cog_name,)) self.logger.info(f"Reloaded {cog_name} cog.") -- 2.45.3