From 7ef4f8929b22ecfa90279fd90bd8a7219049f7ee Mon Sep 17 00:00:00 2001 From: Renovate Date: Tue, 28 Jan 2025 22:01:18 +0000 Subject: [PATCH 1/5] chore(deps): update ghcr.io/astral-sh/uv docker tag to v0.5.25 --- .devcontainer/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 87d9dbe..950731a 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,4 +1,4 @@ -FROM ghcr.io/astral-sh/uv:0.5.24@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 AS uv +FROM ghcr.io/astral-sh/uv:0.5.25@sha256:a73176b27709bff700a1e3af498981f31a83f27552116f21ae8371445f0be710 AS uv FROM python:3.11-slim@sha256:6ed5bff4d7d377e2a27d9285553b8c21cfccc4f00881de1b24c9bc8d90016e82 AS python FROM code.forgejo.org/forgejo/runner:6.2.0@sha256:936c4fef04f0e2bda86c325b8ef40359aeead4740a48b1d5a0c9a112f1185e45 AS forgejo-runner From 7c2ff7681cffe0a43c94710c0bb4fd5991033475 Mon Sep 17 00:00:00 2001 From: Renovate Date: Wed, 29 Jan 2025 13:39:57 -0500 Subject: [PATCH 2/5] chore(deps): update code.forgejo.org/forgejo/runner docker tag to v6.2.1 (#56) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [code.forgejo.org/forgejo/runner](https://forgejo.org) ([source](https://code.forgejo.org/forgejo/runner)) | stage | patch | `6.2.0` -> `6.2.1` | --- > ⚠️ **Warning** > > Some dependencies could not be looked up. Check the Dependency Dashboard for more information. --- ### Release Notes
forgejo/runner (code.forgejo.org/forgejo/runner) ### [`v6.2.1`](https://code.forgejo.org/forgejo/runner/blob/HEAD/RELEASE-NOTES.md#621) [Compare Source](https://code.forgejo.org/forgejo/runner/compare/v6.2.0...v6.2.1) - LXC [templates are updated if needed](https://code.forgejo.org/forgejo/act/pulls/102).
--- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). Reviewed-on: https://www.coastalcommits.com/cswimr/SeaCogs/pulls/56 Co-authored-by: Renovate Co-committed-by: Renovate --- .devcontainer/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 87d9dbe..2e79139 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,6 +1,6 @@ FROM ghcr.io/astral-sh/uv:0.5.24@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 AS uv FROM python:3.11-slim@sha256:6ed5bff4d7d377e2a27d9285553b8c21cfccc4f00881de1b24c9bc8d90016e82 AS python -FROM code.forgejo.org/forgejo/runner:6.2.0@sha256:936c4fef04f0e2bda86c325b8ef40359aeead4740a48b1d5a0c9a112f1185e45 AS forgejo-runner +FROM code.forgejo.org/forgejo/runner:6.2.1@sha256:fecc96a111a15811a6887ce488e75718089f24599e613e93db8e54fe70b706e8 AS forgejo-runner FROM mcr.microsoft.com/vscode/devcontainers/base:bookworm@sha256:6155a486f236fd5127b76af33086029d64f64cf49dd504accb6e5f949098eb7e LABEL repository="www.coastalcommits.com/cswimr/SeaCogs" From 3eeb2f90a65c97f6cb186f51427d58447d577675 Mon Sep 17 00:00:00 2001 From: cswimr Date: Wed, 29 Jan 2025 23:24:19 +0000 Subject: [PATCH 3/5] feat(hotreload): add pre-compilation this feature will detect syntax errors before reloading a cog and cancel the cog reload if it detects one. DOES NOT detect runtime/logic errors, only syntax errors. --- .docs/hotreload.md | 4 ++++ hotreload/hotreload.py | 48 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/.docs/hotreload.md b/.docs/hotreload.md index e2d4cb3..0144e5d 100644 --- a/.docs/hotreload.md +++ b/.docs/hotreload.md @@ -13,6 +13,10 @@ This is useful for development, as it allows you to make changes to your cogs an ## Commands +### hotreload compile + +Determines if the cog should try to compile a modified Python file before reloading the associated cog. Useful for catching syntax errors. Disabled by default. + ### hotreload notifychannel Set the channel where hotreload will send notifications when a cog is reloaded. diff --git a/hotreload/hotreload.py b/hotreload/hotreload.py index 9357fcd..ee223f2 100644 --- a/hotreload/hotreload.py +++ b/hotreload/hotreload.py @@ -1,5 +1,7 @@ +import py_compile from asyncio import run_coroutine_threadsafe from pathlib import Path +from tempfile import NamedTemporaryFile from typing import TYPE_CHECKING, List, Sequence, Tuple from red_commons.logging import RedTraceLogger, getLogger @@ -19,7 +21,7 @@ class HotReload(commands.Cog): __author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"] __git__ = "https://www.coastalcommits.com/cswimr/SeaCogs" - __version__ = "1.3.3" + __version__ = "1.4.0" __documentation__ = "https://seacogs.coastalcommits.com/hotreload/" def __init__(self, bot: Red) -> None: @@ -28,7 +30,7 @@ class HotReload(commands.Cog): self.config = Config.get_conf(self, identifier=294518358420750336, force_registration=True) self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload") self.observers: List[ObserverType] = [] - self.config.register_global(notify_channel=None) + self.config.register_global(notify_channel=None, compile_before_reload=False) watchdog_loggers = [getLogger(name="watchdog.observers.inotify_buffer")] for watchdog_logger in watchdog_loggers: watchdog_logger.setLevel("INFO") # SHUT UP!!!! @@ -94,6 +96,12 @@ class HotReload(commands.Cog): await self.config.notify_channel.set(channel.id) await ctx.send(f"Notifications will be sent to {channel.mention}.") + @hotreload_group.command(name="compile") + async def hotreload_compile(self, ctx: commands.Context, compile_before_reload: bool) -> None: + """Set whether to compile modified files before reloading.""" + await self.config.compile_before_reload.set(compile_before_reload) + await ctx.send(f"I {'will' if compile_before_reload else 'will not'} compile modified files before hotreloading cogs.") + @hotreload_group.command(name="list") async def hotreload_list(self, ctx: commands.Context) -> None: """List the currently active observers.""" @@ -136,10 +144,19 @@ class HotReloadHandler(RegexMatchingEventHandler): self.logger.info("File %s has been %s%s.", event.src_path, event.event_type, dest) - run_coroutine_threadsafe(self.reload_cogs(cogs_to_reload), loop=self.cog.bot.loop) + run_coroutine_threadsafe( + coro=self.reload_cogs( + cog_names=cogs_to_reload, + paths=[Path(p) for p in (event.src_path, getattr(event, "dest_path", None)) if p], + ), + loop=self.cog.bot.loop, + ) + + async def reload_cogs(self, cog_names: Sequence[str], paths: Sequence[Path]) -> None: + """Reload modified cogs.""" + if not self.compile_modified_files(cog_names, paths): + return - async def reload_cogs(self, cog_names: Sequence[str]) -> None: - """Reload modified cog.""" core_logic = CoreLogic(bot=self.cog.bot) self.logger.info("Reloading cogs: %s", humanize_list(cog_names, style="unit")) await core_logic._reload(pkg_names=cog_names) # noqa: SLF001 # We have to use this private method because there is no public API to reload other cogs @@ -148,3 +165,24 @@ class HotReloadHandler(RegexMatchingEventHandler): channel = self.cog.bot.get_channel(await self.cog.config.notify_channel()) if channel: await channel.send(f"Reloaded cogs: {humanize_list(cog_names, style='unit')}") + + def compile_modified_files(self, cog_names: Sequence[str], paths: Sequence[Path]) -> bool: + """Compile modified files to ensure they are valid Python files.""" + for path in paths: + if not path.exists() or path.suffix != ".py": + self.logger.debug("Path %s does not exist or does not point to a Python file. Skipping compilation step.", path) + continue + + try: + with NamedTemporaryFile() as temp_file: + self.logger.debug("Attempting to compile %s", path) + py_compile.compile(file=path, cfile=temp_file.name, doraise=True) + self.logger.debug("Successfully compiled %s", path) + + except py_compile.PyCompileError as e: + e.__suppress_context__ = True + self.logger.exception("%s failed to compile. Not reloading cogs %s.", path, humanize_list(cog_names, style="unit")) + return False + except OSError: + self.logger.exception("Failed to create tempfile for compilation step. Skipping.") + return True From 89d5108ef263efe619fa961957002726e7003b58 Mon Sep 17 00:00:00 2001 From: cswimr Date: Wed, 29 Jan 2025 23:34:52 +0000 Subject: [PATCH 4/5] chore(repo): update pull request template --- .forgejo/pull_request_template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.forgejo/pull_request_template.md b/.forgejo/pull_request_template.md index efe146e..33f957c 100644 --- a/.forgejo/pull_request_template.md +++ b/.forgejo/pull_request_template.md @@ -2,5 +2,5 @@ -- [ ] By submitting this pull request, I permit cswimr to license my work under +- [ ] By submitting this pull request, I permit [cswimr](https://www.coastalcommits.com/cswimr) to license my work under the [Mozilla Public License Version 2.0](https://www.coastalcommits.com/cswimr/SeaCogs/src/branch/main/LICENSE). From 65e823c3ae38b2beb82fedc185f5f84ba1d768ed Mon Sep 17 00:00:00 2001 From: Renovate Date: Thu, 30 Jan 2025 22:03:38 +0000 Subject: [PATCH 5/5] chore(deps): update ghcr.io/astral-sh/uv docker tag to v0.5.26 --- .devcontainer/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 2e79139..21f8e9d 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,4 +1,4 @@ -FROM ghcr.io/astral-sh/uv:0.5.24@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 AS uv +FROM ghcr.io/astral-sh/uv:0.5.26@sha256:a0c0e6aed043f5138957ea89744536eed81f1db633dc9bb3be2b882116060be2 AS uv FROM python:3.11-slim@sha256:6ed5bff4d7d377e2a27d9285553b8c21cfccc4f00881de1b24c9bc8d90016e82 AS python FROM code.forgejo.org/forgejo/runner:6.2.1@sha256:fecc96a111a15811a6887ce488e75718089f24599e613e93db8e54fe70b706e8 AS forgejo-runner