2024-01-31 12:12:38 -05:00
|
|
|
# _____ _
|
|
|
|
# / ____| (_)
|
|
|
|
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
|
|
|
|
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
|
|
|
|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
|
|
|
|
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
|
|
|
|
2024-01-31 14:15:46 -05:00
|
|
|
import contextlib
|
|
|
|
import logging
|
2024-01-31 12:12:38 -05:00
|
|
|
import json
|
2024-01-31 13:07:10 -05:00
|
|
|
import re
|
2024-01-31 12:12:38 -05:00
|
|
|
|
|
|
|
from redbot.core import commands
|
|
|
|
from redbot.core.bot import Red
|
2024-01-31 14:54:36 -05:00
|
|
|
from redbot.cogs.downloader import errors
|
2024-01-31 14:15:46 -05:00
|
|
|
from redbot.cogs.downloader.converters import InstalledCog
|
2024-01-31 14:54:36 -05:00
|
|
|
from redbot.core.utils.chat_formatting import error, text_to_file
|
2024-01-31 12:12:38 -05:00
|
|
|
|
|
|
|
class Backup(commands.Cog):
|
|
|
|
"""A utility to make reinstalling repositories and cogs after migrating the bot far easier."""
|
|
|
|
|
|
|
|
__author__ = "SeaswimmerTheFsh"
|
|
|
|
__version__ = "1.0.0"
|
|
|
|
|
|
|
|
def __init__(self, bot: Red):
|
|
|
|
super().__init__()
|
|
|
|
self.bot = bot
|
2024-01-31 14:15:46 -05:00
|
|
|
self.logger = logging.getLogger("red.sea.backup")
|
2024-01-31 12:12:38 -05:00
|
|
|
|
|
|
|
@commands.group(autohelp=True)
|
|
|
|
@commands.is_owner()
|
|
|
|
async def backup(self, ctx: commands.Context):
|
|
|
|
"""Backup your installed cogs."""
|
|
|
|
|
|
|
|
@backup.command(name='export')
|
|
|
|
@commands.is_owner()
|
|
|
|
async def backup_export(self, ctx: commands.Context):
|
|
|
|
"""Export your installed repositories and cogs to a file."""
|
|
|
|
downloader = ctx.bot.get_cog("Downloader")
|
|
|
|
if downloader is None:
|
2024-01-31 12:35:01 -05:00
|
|
|
await ctx.send(error(f"You do not have the `Downloader` cog loaded. Please run `{ctx.prefix}load downloader` and try again."))
|
2024-01-31 12:12:38 -05:00
|
|
|
return
|
2024-01-31 12:18:55 -05:00
|
|
|
|
2024-01-31 12:35:01 -05:00
|
|
|
all_repos = list(downloader._repo_manager.repos) # pylint: disable=protected-access
|
2024-01-31 12:18:55 -05:00
|
|
|
|
|
|
|
export_data = []
|
|
|
|
|
|
|
|
for repo in all_repos:
|
|
|
|
repo_dict = {
|
|
|
|
"name": repo.name,
|
|
|
|
"url": repo.url,
|
2024-01-31 13:33:39 -05:00
|
|
|
"branch": repo.branch,
|
2024-01-31 12:18:55 -05:00
|
|
|
"cogs": []
|
|
|
|
}
|
|
|
|
|
|
|
|
cogs = await downloader.installed_cogs()
|
|
|
|
|
|
|
|
for cog in cogs:
|
|
|
|
if cog.repo_name == repo.name:
|
2024-01-31 13:55:34 -05:00
|
|
|
cog_dict = {
|
|
|
|
"name": cog.name,
|
|
|
|
"pinned": cog.pinned,
|
|
|
|
"commit": cog.commit
|
|
|
|
}
|
|
|
|
repo_dict["cogs"].append(cog_dict)
|
2024-01-31 12:18:55 -05:00
|
|
|
|
|
|
|
export_data.append(repo_dict)
|
|
|
|
|
|
|
|
await ctx.send(file=text_to_file(json.dumps(export_data, indent=4), 'backup.json'))
|
2024-01-31 12:35:01 -05:00
|
|
|
|
|
|
|
@backup.command(name='import')
|
|
|
|
@commands.is_owner()
|
2024-01-31 13:17:54 -05:00
|
|
|
async def backup_import(self, ctx: commands.Context):
|
|
|
|
"""Import your installed repositories and cogs from an export file."""
|
2024-01-31 12:55:35 -05:00
|
|
|
try:
|
2024-01-31 13:22:56 -05:00
|
|
|
export = json.loads(await ctx.message.attachments[0].read())
|
2024-01-31 13:21:24 -05:00
|
|
|
except (json.JSONDecodeError, IndexError):
|
2024-01-31 13:19:26 -05:00
|
|
|
await ctx.send(error("Please provide a valid JSON export file."))
|
2024-01-31 12:55:35 -05:00
|
|
|
return
|
2024-01-31 12:35:01 -05:00
|
|
|
|
|
|
|
downloader = ctx.bot.get_cog("Downloader")
|
|
|
|
if downloader is None:
|
|
|
|
await ctx.send(error(f"You do not have the `Downloader` cog loaded. Please run `{ctx.prefix}load downloader` and try again."))
|
|
|
|
return
|
2024-01-31 14:15:46 -05:00
|
|
|
|
2024-01-31 15:00:46 -05:00
|
|
|
repo_s = []
|
|
|
|
uninstall_s = []
|
|
|
|
install_s = []
|
2024-01-31 14:15:46 -05:00
|
|
|
repo_e = []
|
|
|
|
uninstall_e = []
|
|
|
|
install_e = []
|
|
|
|
|
|
|
|
async with ctx.typing():
|
|
|
|
for repo in export:
|
2024-01-31 14:54:36 -05:00
|
|
|
# Most of this code is from the Downloader cog.
|
2024-01-31 14:15:46 -05:00
|
|
|
name = repo['name']
|
|
|
|
branch = repo['branch']
|
|
|
|
url = repo['url']
|
|
|
|
cogs = repo['cogs']
|
|
|
|
|
2024-01-31 14:18:37 -05:00
|
|
|
if 'PyLav/Red-Cogs' in url:
|
2024-01-31 14:15:46 -05:00
|
|
|
repo_e.append("PyLav cogs are not supported.")
|
|
|
|
continue
|
|
|
|
if name.startswith('.') or name.endswith('.'):
|
|
|
|
repo_e.append(f"Invalid repository name: {name}\nRepository names cannot start or end with a dot.")
|
|
|
|
continue
|
|
|
|
if re.match(r"^[a-zA-Z0-9_\-\.]+$", name) is None:
|
|
|
|
repo_e.append(f"Invalid repository name: {name}\nRepository names may only contain letters, numbers, underscores, hyphens, and dots.")
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2024-01-31 14:26:50 -05:00
|
|
|
repository = await downloader._repo_manager.add_repo(url, name, branch) # pylint: disable=protected-access
|
2024-01-31 15:00:46 -05:00
|
|
|
repo_s.append(f"Added repository {name} from {url} on branch {branch}.")
|
2024-01-31 14:15:46 -05:00
|
|
|
|
|
|
|
except errors.ExistingGitRepo:
|
|
|
|
repo_e.append(f"Repository {name} already exists.")
|
2024-01-31 14:30:42 -05:00
|
|
|
repository = downloader._repo_manager.get_repo(name) # pylint: disable=protected-access
|
2024-01-31 14:15:46 -05:00
|
|
|
|
2024-01-31 14:21:54 -05:00
|
|
|
# This is commented out because errors.AuthenticationError is not yet implemented in Red 3.5.5's Downloader cog.
|
|
|
|
# Rather, it is only in the development version and will be added in version 3.5.6 (or whatever the next version is).
|
|
|
|
# except errors.AuthenticationError as err:
|
|
|
|
# repo_e.append(f"Authentication error while adding repository {name}. See logs for more information.")
|
2024-01-31 14:22:46 -05:00
|
|
|
# self.logger.exception(
|
2024-01-31 14:21:54 -05:00
|
|
|
# "Something went wrong whilst cloning %s (to revision %s)",
|
|
|
|
# url,
|
|
|
|
# branch,
|
|
|
|
# exc_info=err,
|
|
|
|
# )
|
|
|
|
# continue
|
2024-01-31 14:15:46 -05:00
|
|
|
|
|
|
|
except errors.CloningError as err:
|
|
|
|
repo_e.append(f"Cloning error while adding repository {name}. See logs for more information.")
|
2024-01-31 14:22:46 -05:00
|
|
|
self.logger.exception(
|
2024-01-31 14:15:46 -05:00
|
|
|
"Something went wrong whilst cloning %s (to revision %s)",
|
|
|
|
url,
|
|
|
|
branch,
|
|
|
|
exc_info=err,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
except OSError:
|
|
|
|
repo_e.append(f"OS error while adding repository {name}. See logs for more information.")
|
2024-01-31 14:22:46 -05:00
|
|
|
self.logger.exception(
|
2024-01-31 14:15:46 -05:00
|
|
|
"Something went wrong trying to add repo %s under name %s",
|
|
|
|
url,
|
|
|
|
name
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
2024-01-31 14:37:51 -05:00
|
|
|
cog_modules = []
|
2024-01-31 14:15:46 -05:00
|
|
|
for cog in cogs:
|
2024-01-31 14:37:51 -05:00
|
|
|
try:
|
2024-01-31 14:38:26 -05:00
|
|
|
cog_module = await InstalledCog.convert(ctx, cog['name'])
|
2024-01-31 14:37:51 -05:00
|
|
|
except commands.BadArgument:
|
|
|
|
uninstall_e.append(f"Failed to uninstall {cog['name']}")
|
|
|
|
continue
|
|
|
|
cog_modules.append(cog_module)
|
|
|
|
|
|
|
|
for cog in set(cog.name for cog in cog_modules):
|
2024-01-31 14:15:46 -05:00
|
|
|
poss_installed_path = (await downloader.cog_install_path()) / cog
|
|
|
|
if poss_installed_path.exists():
|
|
|
|
with contextlib.suppress(commands.ExtensionNotLoaded):
|
|
|
|
await ctx.bot.unload_extension(cog)
|
|
|
|
await ctx.bot.remove_loaded_package(cog)
|
2024-01-31 14:54:36 -05:00
|
|
|
await downloader._delete_cog(poss_installed_path) # pylint: disable=protected-access
|
2024-01-31 15:00:46 -05:00
|
|
|
uninstall_s.append(f"Uninstalled {cog}")
|
2024-01-31 14:15:46 -05:00
|
|
|
else:
|
|
|
|
uninstall_e.append(f"Failed to uninstall {cog}")
|
2024-01-31 14:54:36 -05:00
|
|
|
await downloader._remove_from_installed(cog_modules) # pylint: disable=protected-access
|
2024-01-31 14:15:46 -05:00
|
|
|
|
|
|
|
for cog in cogs:
|
|
|
|
cog_name = cog['name']
|
|
|
|
cog_pinned = cog['pinned']
|
|
|
|
if cog_pinned:
|
|
|
|
commit = cog['commit']
|
|
|
|
else:
|
|
|
|
commit = None
|
|
|
|
|
2024-01-31 14:40:58 -05:00
|
|
|
async with repository.checkout(commit, exit_to_rev=repository.branch):
|
2024-01-31 14:37:51 -05:00
|
|
|
cogs_c, message = await downloader._filter_incorrect_cogs_by_names(repository, [cog_name]) # pylint: disable=protected-access
|
2024-01-31 15:00:46 -05:00
|
|
|
if not cogs_c:
|
2024-01-31 14:15:46 -05:00
|
|
|
install_e.append(message)
|
|
|
|
continue
|
2024-01-31 14:54:36 -05:00
|
|
|
failed_reqs = await downloader._install_requirements(cogs_c) # pylint: disable=protected-access
|
2024-01-31 14:15:46 -05:00
|
|
|
if failed_reqs:
|
|
|
|
install_e.append(f"Failed to install {cog_name} due to missing requirements: {failed_reqs}")
|
|
|
|
continue
|
|
|
|
|
2024-01-31 14:54:36 -05:00
|
|
|
installed_cogs, failed_cogs = await downloader._install_cogs(cogs_c) # pylint: disable=protected-access
|
2024-01-31 14:15:46 -05:00
|
|
|
|
2024-01-31 14:42:29 -05:00
|
|
|
if repository.available_libraries:
|
2024-01-31 14:15:46 -05:00
|
|
|
installed_libs, failed_libs = await repository.install_libraries(target_dir=downloader.SHAREDLIB_PATH, req_target_dir=downloader.LIB_PATH)
|
2024-01-31 14:45:58 -05:00
|
|
|
else:
|
2024-01-31 14:48:07 -05:00
|
|
|
installed_libs = None
|
|
|
|
failed_libs = None
|
2024-01-31 14:15:46 -05:00
|
|
|
|
|
|
|
if cog_pinned:
|
|
|
|
for cog in installed_cogs:
|
|
|
|
cog.pinned = True
|
|
|
|
|
2024-01-31 14:54:36 -05:00
|
|
|
await downloader._save_to_installed(installed_cogs + installed_libs if installed_libs else installed_cogs) # pylint: disable=protected-access
|
2024-01-31 15:00:46 -05:00
|
|
|
if installed_cogs:
|
|
|
|
install_s.append(f"Installed {installed_cogs}")
|
|
|
|
if installed_libs:
|
|
|
|
install_s.append(f"Installed {installed_libs} required for {cog_name}")
|
2024-01-31 14:15:46 -05:00
|
|
|
if failed_cogs:
|
|
|
|
install_e.append(f"Failed to install {failed_cogs}")
|
|
|
|
if failed_libs:
|
|
|
|
install_e.append(f"Failed to install {failed_libs} required for {cog_name}")
|
2024-01-31 15:00:46 -05:00
|
|
|
await ctx.send("Import complete!", file=text_to_file(f"Repositories:\n{repo_s}\n\nRepository Errors:\n{repo_e}\n\nUninstalled Cogs:\n{uninstall_s}\n\nUninstalled Cogs Errors:\n{uninstall_e}\n\nInstalled Cogs:\n{install_s}\n\nInstalled Cogs Errors:{install_e}", 'backup.log'))
|