[Core] Add --unload-cogs cli flag. (#5802)

* [Core] Add `--unload-cogs` cli.

* Fixed error + Reformat.

* At @Jack1142's request, the `packages` local variable is no longer a list, but a dictionary with `None` values, to avoid duplication.

* Update redbot/core/bot.py

Co-authored-by: Jakub Kuczys <6032823+jack1142@users.noreply.github.com>

* Update redbot/core/bot.py

Co-authored-by: Jakub Kuczys <6032823+jack1142@users.noreply.github.com>

* Update bot.py

* Update bot.py

Co-authored-by: Jakub Kuczys <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
AAA3A 2022-07-19 17:10:45 +02:00 committed by GitHub
parent 7429b4ff89
commit 6ced7ba945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View File

@ -8,7 +8,7 @@ import sys
import contextlib import contextlib
import weakref import weakref
import functools import functools
from collections import namedtuple from collections import namedtuple, OrderedDict
from datetime import datetime from datetime import datetime
from enum import IntEnum from enum import IntEnum
from importlib.machinery import ModuleSpec from importlib.machinery import ModuleSpec
@ -1109,7 +1109,7 @@ class Red(
await modlog._init(self) await modlog._init(self)
await bank._init() await bank._init()
packages = [] packages = OrderedDict()
last_system_info = await self._config.last_system_info() last_system_info = await self._config.last_system_info()
@ -1135,10 +1135,13 @@ class Red(
python_version_changed = True python_version_changed = True
else: else:
if self._cli_flags.no_cogs is False: if self._cli_flags.no_cogs is False:
packages.extend(await self._config.packages()) packages.update(dict.fromkeys(await self._config.packages()))
if self._cli_flags.load_cogs: if self._cli_flags.load_cogs:
packages.extend(self._cli_flags.load_cogs) packages.update(dict.fromkeys(self._cli_flags.load_cogs))
if self._cli_flags.unload_cogs:
for package in self._cli_flags.unload_cogs:
packages.pop(package, None)
system_changed = False system_changed = False
machine = platform.machine() machine = platform.machine()
@ -1169,11 +1172,9 @@ class Red(
if packages: if packages:
# Load permissions first, for security reasons # Load permissions first, for security reasons
try: try:
packages.remove("permissions") packages.move_to_end("permissions", last=False)
except ValueError: except KeyError:
pass pass
else:
packages.insert(0, "permissions")
to_remove = [] to_remove = []
log.info("Loading packages...") log.info("Loading packages...")
@ -1197,9 +1198,11 @@ class Red(
await self.remove_loaded_package(package) await self.remove_loaded_package(package)
to_remove.append(package) to_remove.append(package)
for package in to_remove: for package in to_remove:
packages.remove(package) del packages[package]
if packages: if packages:
log.info("Loaded packages: " + ", ".join(packages)) log.info("Loaded packages: " + ", ".join(packages))
else:
log.info("No packages were loaded.")
if self.rpc_enabled: if self.rpc_enabled:
await self.rpc.initialize(self.rpc_port) await self.rpc.initialize(self.rpc_port)

View File

@ -185,6 +185,9 @@ def parse_cli_flags(args):
help="Force loading specified cogs from the installed packages. " help="Force loading specified cogs from the installed packages. "
"Can be used with the --no-cogs flag to load these cogs exclusively.", "Can be used with the --no-cogs flag to load these cogs exclusively.",
) )
parser.add_argument(
"--unload-cogs", type=str, nargs="+", help="Force unloading specified cogs."
)
parser.add_argument( parser.add_argument(
"--dry-run", "--dry-run",
action="store_true", action="store_true",