Compare commits

..

42 commits

Author SHA1 Message Date
71b900fa08
Merge pull request 'Backup' (#16) from backup into main
All checks were successful
Actions / Lint Code (Pylint) (push) Successful in 16s
Actions / Build Documentation (MkDocs) (push) Successful in 11s
Reviewed-on: https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs/pulls/16
2024-01-31 20:58:07 +00:00
053b5b7472
fix(backup): do not uninstall yourself
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 15:56:28 -05:00
49907e7803
fix(backup): fixed not installing cogs from repositories that already exist
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 14s
2024-01-31 15:52:44 -05:00
22237a21b1
fix(backup): doing some cleanup
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 15:48:23 -05:00
4e70145d38
feat(backup): exports will now check if a cog is loaded
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 14s
2024-01-31 15:12:34 -05:00
f57b2c5196
fix(backup): fixed some logging stuff
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 15:05:25 -05:00
7766b94a9e
fix(backup): added a missing newline 2024-01-31 15:02:06 -05:00
a00bfc3f93
feat(backup): added more verbose logging
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 15:00:46 -05:00
756265503c
fix(backup): pylint fixes
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 14:54:36 -05:00
9b930fc05b
fix(backup): fixed the final logging step
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 17s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 18s
2024-01-31 14:48:07 -05:00
f33ebd29e9
fix(backup): fixed some other errors
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 14s
2024-01-31 14:45:58 -05:00
57c129c9ce
fix(backup): fixed another attributeerror
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 15s
2024-01-31 14:42:29 -05:00
acf89e090f
fix(backup): fixed an AttributeError
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 18s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 14s
2024-01-31 14:40:58 -05:00
8327270e61
fix(backup): awaited a coroutine
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 14:38:26 -05:00
760059f8f0
fix(backup): fixed some stuff
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 14:37:51 -05:00
f4f174ce81
fix(backup): fixed a few errors
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 14:30:42 -05:00
417ee9bde4
fix(backup): swapped around some kwargs for add_repo (L109)
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 14:26:50 -05:00
6dfeb97180
fix(backup): logger, not log
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 13s
2024-01-31 14:22:46 -05:00
fdb5d323eb
fix(backup): commented out error handling for an error that doesn't exist yet
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 14:21:54 -05:00
add1e3ef1d
fix(backup): contains isn't a thing
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 14:18:37 -05:00
bbecff3ed0
fix(backup): forced incompatibility with pylav as intended
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 14:16:18 -05:00
fa6fcfb75f
feat(backup): finished the importer, minus the loading functionality
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 14:15:46 -05:00
d7b5c5f239
feat(backup): export more information on each individual cog
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 17s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:55:34 -05:00
3662ce3b23
fix(backup): export branch as well
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:33:39 -05:00
7fdb106fad
misc(backup): condensed some code more 2024-01-31 13:22:56 -05:00
ac761d9284
misc(backup): condensed some code
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:21:24 -05:00
037ef84dcc
fix(backup): added error message for if you don't provide an attachment
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:19:26 -05:00
c07c7317f4
fix(backup): remove support for message content based json loading
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:17:54 -05:00
c2aa674bff Revert "fix(backup): properly support codeblocks (maybe?)"
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
This reverts commit 168f62d43f.
2024-01-31 13:11:17 -05:00
168f62d43f
fix(backup): properly support codeblocks (maybe?)
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:10:31 -05:00
5eb762d9fe
fix(backup): fixed extract_json_from_codeblock
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:07:48 -05:00
2dd8663e78
fix(backup): support codeblocks
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:07:10 -05:00
702de21448
fix(backup): added a kwarg to backup_import
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 13:03:02 -05:00
52f8439383
fix(backup): lol.
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 14s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 12:56:51 -05:00
d0375c3bdb
fix(backup): loads the export to see if it's actually valid json or not
All checks were successful
Actions / Lint Code (Pylint) (pull_request) Successful in 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 12s
2024-01-31 12:55:35 -05:00
26b085231b
fix(backup): fixed list out of index error
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 14s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 12:38:47 -05:00
045d51bc23
fix(backup): only try to access message attachments if the message has any
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 15s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 10s
2024-01-31 12:37:27 -05:00
35ffec9c3f
fix(backup): pylint fixes
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 14s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 12:35:01 -05:00
932c9f61ed
fix(docs): fixed incorrect headers
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 16s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 11s
2024-01-31 12:34:26 -05:00
6fbfa96cf2
feat(docs): added Backup documentation
Some checks failed
Actions / Lint Code (Pylint) (pull_request) Failing after 17s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 13s
2024-01-31 12:31:37 -05:00
7fa6cd5922
feat(backup): changed export format 2024-01-31 12:18:55 -05:00
abc9927dea
feat(backup): added the cog 2024-01-31 12:12:38 -05:00
5 changed files with 270 additions and 0 deletions

25
.docs/backup.md Normal file
View file

@ -0,0 +1,25 @@
# Backup
Backup allows you to export a JSON list of all of your installed repositories and cogs, then reimport them and automatically reinstall/reload the cogs.
## Installation
```bash
[p]repo add seacogs https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs
[p]cog install seacogs backup
[p]cog load backup
```
## Commands
### backup export
- Usage: `[p]backup export`
Exports a JSON list of all of your added repositories, and their installed cogs.
### backup import
- Usage: `[p]backup import`
Reinstalls repositories and cogs from a valid export. Requires the JSON export to be attached to the invoking message as an attachment. Ignores itself and PyLav cogs, due to possible conflicts with Docker images.

5
backup/__init__.py Normal file
View file

@ -0,0 +1,5 @@
from .backup import Backup
async def setup(bot):
await bot.add_cog(Backup(bot))

224
backup/backup.py Normal file
View file

@ -0,0 +1,224 @@
# _____ _
# / ____| (_)
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
import contextlib
import logging
import json
import re
from redbot.core import commands
from redbot.core.bot import Red
from redbot.cogs.downloader import errors
from redbot.cogs.downloader.converters import InstalledCog
from redbot.core.utils.chat_formatting import error, text_to_file
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
self.logger = logging.getLogger("red.sea.backup")
@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:
await ctx.send(error(f"You do not have the `Downloader` cog loaded. Please run `{ctx.prefix}load downloader` and try again."))
return
all_repos = list(downloader._repo_manager.repos) # pylint: disable=protected-access
export_data = []
for repo in all_repos:
repo_dict = {
"name": repo.name,
"url": repo.url,
"branch": repo.branch,
"cogs": []
}
cogs = await downloader.installed_cogs()
for cog in cogs:
if cog.repo_name == repo.name:
cog_dict = {
"name": cog.name,
# "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
"pinned": cog.pinned,
"commit": cog.commit
}
repo_dict["cogs"].append(cog_dict)
export_data.append(repo_dict)
await ctx.send(file=text_to_file(json.dumps(export_data, indent=4), 'backup.json'))
@backup.command(name='import')
@commands.is_owner()
async def backup_import(self, ctx: commands.Context):
"""Import your installed repositories and cogs from an export file."""
try:
export = json.loads(await ctx.message.attachments[0].read())
except (json.JSONDecodeError, IndexError):
await ctx.send(error("Please provide a valid JSON export file."))
return
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
repo_s = []
uninstall_s = []
install_s = []
repo_e = []
uninstall_e = []
install_e = []
async with ctx.typing():
for repo in export:
# Most of this code is from the Downloader cog.
name = repo['name']
branch = repo['branch']
url = repo['url']
cogs = repo['cogs']
if 'PyLav/Red-Cogs' in url:
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:
repository = await downloader._repo_manager.add_repo(url, name, branch) # pylint: disable=protected-access
repo_s.append(f"Added repository {name} from {url} on branch {branch}.")
except errors.ExistingGitRepo:
repo_e.append(f"Repository {name} already exists.")
repository = downloader._repo_manager.get_repo(name) # pylint: disable=protected-access
# 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.")
# self.logger.exception(
# "Something went wrong whilst cloning %s (to revision %s)",
# url,
# branch,
# exc_info=err,
# )
# continue
except errors.CloningError as err:
repo_e.append(f"Cloning error while adding repository {name}. See logs for more information.")
self.logger.exception(
"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.")
self.logger.exception(
"Something went wrong trying to add repo %s under name %s",
url,
name
)
continue
cog_modules = []
for cog in cogs:
# If you're forking this cog, make sure to change these strings!
if cog['name'] == "backup" and 'SeaswimmerTheFsh/SeaCogs' in url:
continue
try:
cog_module = await InstalledCog.convert(ctx, cog['name'])
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):
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)
await downloader._delete_cog(poss_installed_path) # pylint: disable=protected-access
uninstall_s.append(f"Uninstalled {cog}")
else:
uninstall_e.append(f"Failed to uninstall {cog}")
await downloader._remove_from_installed(cog_modules) # pylint: disable=protected-access
for cog in cogs:
cog_name = cog['name']
cog_pinned = cog['pinned']
if cog_pinned:
commit = cog['commit']
else:
commit = None
# If you're forking this cog, make sure to change these strings!
if cog_name == 'backup' and 'SeaswimmerTheFsh/SeaCogs' in url:
continue
async with repository.checkout(commit, exit_to_rev=repository.branch):
cogs_c, message = await downloader._filter_incorrect_cogs_by_names(repository, [cog_name]) # pylint: disable=protected-access
if not cogs_c:
install_e.append(message)
continue
failed_reqs = await downloader._install_requirements(cogs_c) # pylint: disable=protected-access
if failed_reqs:
install_e.append(f"Failed to install {cog_name} due to missing requirements: {failed_reqs}")
continue
installed_cogs, failed_cogs = await downloader._install_cogs(cogs_c) # pylint: disable=protected-access
if repository.available_libraries:
installed_libs, failed_libs = await repository.install_libraries(target_dir=downloader.SHAREDLIB_PATH, req_target_dir=downloader.LIB_PATH)
else:
installed_libs = None
failed_libs = None
if cog_pinned:
for cog in installed_cogs:
cog.pinned = True
await downloader._save_to_installed(installed_cogs + installed_libs if installed_libs else installed_cogs) # pylint: disable=protected-access
if installed_cogs:
installed_cog_name = installed_cogs[0].name
install_s.append(f"Installed {installed_cog_name}")
if installed_libs:
for lib in installed_libs:
install_s.append(f"Installed {lib.name} required for {cog_name}")
if failed_cogs:
failed_cog_name = failed_cogs[0].name
install_e.append(f"Failed to install {failed_cog_name}")
if failed_libs:
for lib in failed_libs:
install_e.append(f"Failed to install {lib.name} required for {cog_name}")
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:\n{install_e}", 'backup.log'))

14
backup/info.json Normal file
View file

@ -0,0 +1,14 @@
{
"author" : ["SeaswimmerTheFsh"],
"install_msg" : "Thank you for installing Backup!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).",
"name" : "Backup",
"short" : "A utility to make reinstalling repositories and cogs after migrating the bot far easier.",
"description" : "A utility to make reinstalling repositories and cogs after migrating the bot far easier.",
"end_user_data_statement" : "This cog does not store user information.",
"hidden": false,
"disabled": false,
"min_bot_version": "3.5.5",
"max_bot_version": "3.5.5",
"min_python_version": [3, 10, 0],
"tags": []
}

View file

@ -16,6 +16,7 @@ nav:
- Moderation Commands: aurora/moderation-commands.md
- Case Commands: aurora/case-commands.md
- Configuration: aurora/configuration.md
- Backup: backup.md
- Nerdify: nerdify.md
plugins:
@ -100,4 +101,5 @@ theme:
watch:
- ./aurora
- ./backup
- ./nerdify