2024-01-31 12:12:38 -05:00
|
|
|
# _____ _
|
|
|
|
# / ____| (_)
|
|
|
|
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
|
|
|
|
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
|
|
|
|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
|
|
|
|
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
|
|
|
|
2024-01-31 14:15:46 -05:00
|
|
|
import contextlib
|
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
|
|
|
|
2024-03-07 03:38:34 -05:00
|
|
|
from red_commons.logging import getLogger
|
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-02-02 11:22:08 -05:00
|
|
|
from redbot.core import commands
|
|
|
|
from redbot.core.bot import Red
|
2024-03-29 03:29:35 -04:00
|
|
|
from redbot.core.utils.chat_formatting import error, humanize_list, text_to_file
|
2024-01-31 12:12:38 -05:00
|
|
|
|
2024-02-02 11:22:08 -05:00
|
|
|
|
2024-02-28 10:56:46 -05:00
|
|
|
# pylint: disable=protected-access
|
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."""
|
|
|
|
|
2024-03-07 03:38:34 -05:00
|
|
|
__author__ = ["SeaswimmerTheFsh"]
|
2024-03-29 07:17:51 -04:00
|
|
|
__version__ = "1.1.0"
|
|
|
|
__documentation__ = "https://seacogs.coastalcommits.com/backup/"
|
2024-01-31 12:12:38 -05:00
|
|
|
|
|
|
|
def __init__(self, bot: Red):
|
|
|
|
super().__init__()
|
|
|
|
self.bot = bot
|
2024-04-08 05:55:35 -04:00
|
|
|
self.logger = getLogger("red.SeaCogs.Backup")
|
2024-03-07 03:38:34 -05:00
|
|
|
|
|
|
|
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"Cog Version: **{self.__version__}**",
|
|
|
|
f"Author: {humanize_list(self.__author__)}",
|
2024-03-29 07:17:51 -04:00
|
|
|
f"Documentation: {self.__documentation__}",
|
2024-03-07 03:38:34 -05:00
|
|
|
]
|
|
|
|
return "\n".join(text)
|
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."""
|
|
|
|
|
2024-02-02 11:22:08 -05:00
|
|
|
@backup.command(name="export")
|
2024-01-31 12:12:38 -05:00
|
|
|
@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-02-02 11:22:08 -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-02-02 11:27:28 -05:00
|
|
|
all_repos = list(downloader._repo_manager.repos)
|
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-02-02 11:22:08 -05:00
|
|
|
"cogs": [],
|
2024-01-31 12:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2024-02-02 11:22:08 -05:00
|
|
|
# "loaded": cog.name in ctx.bot.extensions.keys(),
|
|
|
|
# this functionality was planned but never implemented due to Red limitations
|
|
|
|
# and the possibility of restoration functionality being added to Core
|
2024-01-31 13:55:34 -05:00
|
|
|
"pinned": cog.pinned,
|
2024-02-02 11:22:08 -05:00
|
|
|
"commit": cog.commit,
|
2024-01-31 13:55:34 -05:00
|
|
|
}
|
|
|
|
repo_dict["cogs"].append(cog_dict)
|
2024-01-31 12:18:55 -05:00
|
|
|
|
|
|
|
export_data.append(repo_dict)
|
|
|
|
|
2024-02-02 11:22:08 -05:00
|
|
|
await ctx.send(
|
|
|
|
file=text_to_file(json.dumps(export_data, indent=4), "backup.json")
|
|
|
|
)
|
2024-01-31 12:35:01 -05:00
|
|
|
|
2024-02-02 11:22:08 -05:00
|
|
|
@backup.command(name="import")
|
2024-01-31 12:35:01 -05:00
|
|
|
@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-04-24 16:52:05 -04:00
|
|
|
export = None
|
|
|
|
if ctx.message.attachments:
|
2024-03-07 13:47:40 -05:00
|
|
|
try:
|
2024-04-24 16:52:05 -04:00
|
|
|
export = json.loads(await ctx.message.attachments[0].read())
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
await ctx.send(error("Invalid JSON in message attachments."))
|
|
|
|
elif ctx.message.reference and hasattr(ctx.message.reference, 'resolved'):
|
|
|
|
if ctx.message.reference.resolved.attachments:
|
|
|
|
try:
|
|
|
|
export = json.loads(await ctx.message.reference.resolved.attachments[0].read())
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
await ctx.send(error("Invalid JSON in referenced message attachments."))
|
|
|
|
if export is None:
|
|
|
|
await ctx.send(error("Please provide a valid JSON export file."))
|
|
|
|
return
|
2024-01-31 12:35:01 -05:00
|
|
|
|
|
|
|
downloader = ctx.bot.get_cog("Downloader")
|
|
|
|
if downloader is None:
|
2024-02-02 11:22:08 -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:35:01 -05:00
|
|
|
return
|
2024-01-31 14:15:46 -05:00
|
|
|
|
2024-04-24 16:52:05 -04:00
|
|
|
all_repos = list(downloader._repo_manager.repos)
|
2024-01-31 14:15:46 -05:00
|
|
|
|
2024-04-24 16:52:05 -04:00
|
|
|
for repo in export:
|
|
|
|
if repo["name"] not in [r.name for r in all_repos]:
|
2024-01-31 14:15:46 -05:00
|
|
|
try:
|
2024-04-24 16:52:05 -04:00
|
|
|
await downloader._repo_manager.add_repo(
|
|
|
|
repo["url"], repo["name"], repo["branch"]
|
2024-02-02 11:22:08 -05:00
|
|
|
)
|
2024-01-31 14:15:46 -05:00
|
|
|
except errors.ExistingGitRepo:
|
2024-04-24 16:52:05 -04:00
|
|
|
pass
|
2024-01-31 14:15:46 -05:00
|
|
|
|
2024-04-24 16:52:05 -04:00
|
|
|
for cog in repo["cogs"]:
|
|
|
|
try:
|
|
|
|
await downloader._cog_install_interface.install_cog(
|
|
|
|
cog["name"], cog["commit"]
|
2024-01-31 14:15:46 -05:00
|
|
|
)
|
2024-04-24 16:52:05 -04:00
|
|
|
except errors.CogNotFoundError:
|
|
|
|
pass
|
|
|
|
except errors.DownloaderError:
|
|
|
|
pass
|