mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
* feat(downloader): Install cog from specific commit in repo (initial commit) - Repo and Installable have commit property now - New class inheriting from Installable - InstalledCog (old one from converters.py removed) - New Repo.checkout() method, which is also async ctx manager ref #2527 * fix(downloader): Keep information about repo's branch in config - This is needed to make sure that repo can go back from detached state in some rare unexpected cases - current branch is determined by `git symbolic-ref` now as this command errors for detached HEAD * feat(downloader): Update repo without cogs, update single cog The most important part of issue #2527 has been added here - `[p]repo update` command added - new conf format - nested dictionary repo_name->cog_name->cog_json installed libraries are now kept in conf too - `InstalledCog` renamed to `InstalledModule` - installed libraries use this class - `Downloader.installed_libraries()` and `Downloader.installed_modules()` added - `Downloader._add_to_installed()` and `Downloader._remove_from_installed()` now accept list of modules, of both cogs and libraries - `[p]cog install` tells about fails of copying cog and installing shared libraries - `[p]cog update` will truly update only chosen cogs (if provided) or cogs that need update - pinned cogs aren't checked - before update, repos are updated - to determine if update is needed `Repo.get_modified_modules()` is used - `[p]cog pin` and `[p]cog unpin` commands for pinning/unpinning cogs added - `Repo.checkout()` allows to choose ctx manager exit's checkout revision - `Repo.install_cog()` returns `InstalledModule` now and raises CopyingError (maybe breaking?) - `Repo.install_libraries()` returns 2-tuple of installed and failed libraries (maybe breaking?) - `RepoManager.get_all_cogs()` added, which returns cogs from all repos - `RepoManager.repos` property added, which contains tuple of `Repo` * test(downloader): Repo.current_branch() throws an exception, when branch can't be determined * style(downloader): rename _add_to_installed to _save_to_installed This method is used for both adding and updating existing modules in Config * refactor(downloader): add ctx.typing() for few commands `[p]cog install` is nested hell, can't wait for moving install logic to separate method * fix(downloader): refactor and fix `set` usage * perf(downloader): update commits for ALL checked modules to omit diffs next time This will also disable running git diff for cogs that have the same commit as the latest one * style(downloader): few style improvements - use of mutable object in method definition - make Repo._get_full_sha1() public method - too long line - don't use len to check if sequence is empty * feat(downloader): add `[p]cog updateallfromrepos` and `[p]cog updatetoversion` commands - moved cog update logic into `Downloader._cog_update_logic()` (lack of better name) - splitted whole cog update process into smaller methods - might still need some improvements - added new methods to `Repo` class: - `is_on_branch()` to check if repo is currently checked out to branch - `is_ancestor()` to check if one commit is ancestor of the other - fix for `Downloader._available_updates()` behaviour broken by commit 5755ab08ba67556b3863e907c6f44d80f4f13d88 * feat(downloader): try to find last commit where module is still present Enhancements: - `Installable` now has `repo` attribute containing repo object or `None` if repo is missing - `Downloader._install_cogs()` and `Downloader._reinstall_libraries()` are able to install modules from different commits of repo - `Repo.checkout()` as ctx manager will now exit to commit which was active before checking out - unification of `rev` and `hash` terms: All function parameters are explicitly called `hash`, if it can only be commit's full sha1 hash or `rev` if it can be anything that names a commit object, see [link](https://git-scm.com/docs/git-rev-parse#_specifying_revisions) - new `Repo.get_last_module_occurence()` method, which gets module's Installable from last commit in which it still occurs * docs(downloader): Add basic description for `InstalledModule` * fix(downloader): cog ignored during updates if its commit was missing After config format update, commit string is empty until update and when such cog was checked and it wasn't available in repo anymore, it was ignored * refactor(downloader): Installing cogs from specific rev will pin them * perf(downloader): Don't checkout when current commit equals target hash - changes to `Repo.checkout()`: - `exit_to_rev` is now keyword only argument - added `force_checkout` to force checkout even if `Repo.commit` value is the same as target hash * refactor(downloader): Repo._run() stderr is redirected to debug log now - added two keyword arguments: - `valid_exit_codes` which specifies valid exit codes, used to determine if stderr should be sent as debug or error level in logging - `debug_only` which specifies if stderr can be sent only as debug level in logging * style(downloader): stop using `set` as arg name in `_load_repos()` * feat(downloader): pass multiple cogs to `[p]cog (un)pin` * refactor(downloader): accept module name instead of instance, fix spelling * style(downloader): few small style changes * fix(downloader): add type annotations + fixes based on them - fix wrong type annotations and add a lot of new ones - add checks for `Installable.repo` being `None` - fix wrong return type in `Downloader._install_requirements` - show repo names correctly when updating all repos - fix error when some requirement fails to install BREAKING CHANGE: - type of `Repo.available_modules` is now consistent (always `tuple`) * tests: use same event loop policy as in Red's code * enhance(downloader): fully handle ambiguous revisions * build(deps): add pytest-mock dependency to tests extra * fix(downloader): minor fixes * feat(downloader): add tool for editing Downloader's test repo This script aims to help update the human-readable version of repo used for git integration tests in ``redbot/tests/downloader_testrepo.export`` by exporting/importing it in/from provided directory. Note ---- Editing `downloader_git_test_repo.export` file manually is strongly discouraged, especially editing any part of commit directives as that causes a change in the commit's hash. Another problem devs could encounter when trying to manually edit that file are editors that will use CRLF instead of LF for new line character(s) and therefore break it. I also used `.gitattributes` to prevent autocrlf from breaking testrepo. Also, if Git ever changes currently used SHA-1 to SHA-256 we will have to update old hashes with new ones. But it's a small drawback, when we can have human-readable version of repo. Known limitations ----------------- ``git fast-export`` exports commits without GPG signs so this script disables it in repo's config. This also means devs shouldn't use ``--gpg-sign`` flag in ``git commit`` within the test repo. * tests(downloader): add git tests and test repo for them Also added Markdown file that is even more clear than export file on what the test repo contains. This is manually created but can be automated on later date. * test(downloader): add more tests related to RepoManager These tests use expected output that is already guaranteed by git tests. * chore(CODEOWNERS): add jack1142 to Downloader's folders I know this doesn't actually give any benefit to people that don't have write permission to the repo but I saw other big fella devs doing this, so I think this might be advisable. * enhance(downloader): allow easy schema updates in future * enhance(downloader): more typing fixes, add comments for clarity * feat(downloader): add python and bot version check to update process follow-up on #2605, this commit fully fixes #1866 * chore(changelog): add towncrier entries * fix(downloader): use `*args` instead of `commands.Greedy` * fix(downloader): hot-reload issue - `InstallableType` now inherits from `IntEnum` There's desync of `InstallableType` class types due to hot-reload and `IntEnum` allows for equality check between different types * enhance(downloader): ensure there's no cog with same name installed should fix #2927 * fix(downloader): last few changes before marking as ready for review
293 lines
9.4 KiB
Python
293 lines
9.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import distutils.dir_util
|
|
import shutil
|
|
from enum import IntEnum
|
|
from pathlib import Path
|
|
from typing import MutableMapping, Any, TYPE_CHECKING, Optional, Dict, Union, Callable, Tuple, cast
|
|
|
|
from .log import log
|
|
from .json_mixins import RepoJSONMixin
|
|
|
|
from redbot.core import __version__, version_info as red_version_info, VersionInfo
|
|
|
|
if TYPE_CHECKING:
|
|
from .repo_manager import RepoManager, Repo
|
|
|
|
|
|
class InstallableType(IntEnum):
|
|
# using IntEnum, because hot-reload breaks its identity
|
|
UNKNOWN = 0
|
|
COG = 1
|
|
SHARED_LIBRARY = 2
|
|
|
|
|
|
class Installable(RepoJSONMixin):
|
|
"""Base class for anything the Downloader cog can install.
|
|
|
|
- Modules
|
|
- Repo Libraries
|
|
- Other stuff?
|
|
|
|
The attributes of this class will mostly come from the installation's
|
|
info.json.
|
|
|
|
Attributes
|
|
----------
|
|
repo_name : `str`
|
|
Name of the repository which this package belongs to.
|
|
repo : Repo, optional
|
|
Repo object of the Installable, if repo is missing this will be `None`
|
|
commit : `str`, optional
|
|
Installable's commit. This is not the same as ``repo.commit``
|
|
author : `tuple` of `str`, optional
|
|
Name(s) of the author(s).
|
|
bot_version : `tuple` of `int`
|
|
The minimum bot version required for this installation. Right now
|
|
this is always :code:`3.0.0`.
|
|
min_python_version : `tuple` of `int`
|
|
The minimum python version required for this cog. This field will not
|
|
apply to repo info.json's.
|
|
hidden : `bool`
|
|
Whether or not this cog will be hidden from the user when they use
|
|
`Downloader`'s commands.
|
|
required_cogs : `dict`
|
|
In the form :code:`{cog_name : repo_url}`, these are cogs which are
|
|
required for this installation.
|
|
requirements : `tuple` of `str`
|
|
Required libraries for this installation.
|
|
tags : `tuple` of `str`
|
|
List of tags to assist in searching.
|
|
type : `int`
|
|
The type of this installation, as specified by
|
|
:class:`InstallationType`.
|
|
|
|
"""
|
|
|
|
def __init__(self, location: Path, repo: Optional[Repo] = None, commit: str = ""):
|
|
"""Base installable initializer.
|
|
|
|
Parameters
|
|
----------
|
|
location : pathlib.Path
|
|
Location (file or folder) to the installable.
|
|
repo : Repo, optional
|
|
Repo object of the Installable, if repo is missing this will be `None`
|
|
commit : str
|
|
Installable's commit. This is not the same as ``repo.commit``
|
|
|
|
"""
|
|
super().__init__(location)
|
|
|
|
self._location = location
|
|
|
|
self.repo = repo
|
|
self.repo_name = self._location.parent.stem
|
|
self.commit = commit
|
|
|
|
self.author: Tuple[str, ...] = ()
|
|
self.min_bot_version = red_version_info
|
|
self.max_bot_version = red_version_info
|
|
self.min_python_version = (3, 5, 1)
|
|
self.hidden = False
|
|
self.disabled = False
|
|
self.required_cogs: Dict[str, str] = {} # Cog name -> repo URL
|
|
self.requirements: Tuple[str, ...] = ()
|
|
self.tags: Tuple[str, ...] = ()
|
|
self.type = InstallableType.UNKNOWN
|
|
|
|
if self._info_file.exists():
|
|
self._process_info_file(self._info_file)
|
|
|
|
if self._info == {}:
|
|
self.type = InstallableType.COG
|
|
|
|
def __eq__(self, other: Any) -> bool:
|
|
# noinspection PyProtectedMember
|
|
return self._location == other._location
|
|
|
|
def __hash__(self) -> int:
|
|
return hash(self._location)
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
"""`str` : The name of this package."""
|
|
return self._location.stem
|
|
|
|
async def copy_to(self, target_dir: Path) -> bool:
|
|
"""
|
|
Copies this cog/shared_lib to the given directory. This
|
|
will overwrite any files in the target directory.
|
|
|
|
:param pathlib.Path target_dir: The installation directory to install to.
|
|
:return: Status of installation
|
|
:rtype: bool
|
|
"""
|
|
copy_func: Callable[..., Any]
|
|
if self._location.is_file():
|
|
copy_func = shutil.copy2
|
|
else:
|
|
# clear copy_tree's cache to make sure missing directories are created (GH-2690)
|
|
distutils.dir_util._path_created = {}
|
|
copy_func = distutils.dir_util.copy_tree
|
|
|
|
# noinspection PyBroadException
|
|
try:
|
|
copy_func(src=str(self._location), dst=str(target_dir / self._location.stem))
|
|
except: # noqa: E722
|
|
log.exception("Error occurred when copying path: {}".format(self._location))
|
|
return False
|
|
return True
|
|
|
|
def _read_info_file(self) -> None:
|
|
super()._read_info_file()
|
|
|
|
if self._info_file.exists():
|
|
self._process_info_file()
|
|
|
|
def _process_info_file(
|
|
self, info_file_path: Optional[Path] = None
|
|
) -> MutableMapping[str, Any]:
|
|
"""
|
|
Processes an information file. Loads dependencies among other
|
|
information into this object.
|
|
|
|
:type info_file_path:
|
|
:param info_file_path: Optional path to information file, defaults to `self.__info_file`
|
|
:return: Raw information dictionary
|
|
"""
|
|
info_file_path = info_file_path or self._info_file
|
|
if info_file_path is None or not info_file_path.is_file():
|
|
raise ValueError("No valid information file path was found.")
|
|
|
|
info: Dict[str, Any] = {}
|
|
with info_file_path.open(encoding="utf-8") as f:
|
|
try:
|
|
info = json.load(f)
|
|
except json.JSONDecodeError:
|
|
info = {}
|
|
log.exception("Invalid JSON information file at path: {}".format(info_file_path))
|
|
else:
|
|
self._info = info
|
|
|
|
try:
|
|
author = tuple(info.get("author", []))
|
|
except ValueError:
|
|
author = ()
|
|
self.author = author
|
|
|
|
try:
|
|
min_bot_version = VersionInfo.from_str(str(info.get("min_bot_version", __version__)))
|
|
except ValueError:
|
|
min_bot_version = self.min_bot_version
|
|
self.min_bot_version = min_bot_version
|
|
|
|
try:
|
|
max_bot_version = VersionInfo.from_str(str(info.get("max_bot_version", __version__)))
|
|
except ValueError:
|
|
max_bot_version = self.max_bot_version
|
|
self.max_bot_version = max_bot_version
|
|
|
|
try:
|
|
min_python_version = tuple(info.get("min_python_version", (3, 5, 1)))
|
|
except ValueError:
|
|
min_python_version = self.min_python_version
|
|
self.min_python_version = min_python_version
|
|
|
|
try:
|
|
hidden = bool(info.get("hidden", False))
|
|
except ValueError:
|
|
hidden = False
|
|
self.hidden = hidden
|
|
|
|
try:
|
|
disabled = bool(info.get("disabled", False))
|
|
except ValueError:
|
|
disabled = False
|
|
self.disabled = disabled
|
|
|
|
self.required_cogs = info.get("required_cogs", {})
|
|
|
|
self.requirements = info.get("requirements", ())
|
|
|
|
try:
|
|
tags = tuple(info.get("tags", ()))
|
|
except ValueError:
|
|
tags = ()
|
|
self.tags = tags
|
|
|
|
installable_type = info.get("type", "")
|
|
if installable_type in ("", "COG"):
|
|
self.type = InstallableType.COG
|
|
elif installable_type == "SHARED_LIBRARY":
|
|
self.type = InstallableType.SHARED_LIBRARY
|
|
self.hidden = True
|
|
else:
|
|
self.type = InstallableType.UNKNOWN
|
|
|
|
return info
|
|
|
|
|
|
class InstalledModule(Installable):
|
|
"""Base class for installed modules,
|
|
this is basically instance of installed `Installable`
|
|
used by Downloader.
|
|
|
|
Attributes
|
|
----------
|
|
pinned : `bool`
|
|
Whether or not this cog is pinned, always `False` if module is not a cog.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
location: Path,
|
|
repo: Optional[Repo] = None,
|
|
commit: str = "",
|
|
pinned: bool = False,
|
|
json_repo_name: str = "",
|
|
):
|
|
super().__init__(location=location, repo=repo, commit=commit)
|
|
self.pinned: bool = pinned if self.type == InstallableType.COG else False
|
|
# this is here so that Downloader could use real repo name instead of "MISSING_REPO"
|
|
self._json_repo_name = json_repo_name
|
|
|
|
def to_json(self) -> Dict[str, Union[str, bool]]:
|
|
module_json: Dict[str, Union[str, bool]] = {
|
|
"repo_name": self.repo_name,
|
|
"module_name": self.name,
|
|
"commit": self.commit,
|
|
}
|
|
if self.type == InstallableType.COG:
|
|
module_json["pinned"] = self.pinned
|
|
return module_json
|
|
|
|
@classmethod
|
|
def from_json(
|
|
cls, data: Dict[str, Union[str, bool]], repo_mgr: RepoManager
|
|
) -> InstalledModule:
|
|
repo_name = cast(str, data["repo_name"])
|
|
cog_name = cast(str, data["module_name"])
|
|
commit = cast(str, data.get("commit", ""))
|
|
pinned = cast(bool, data.get("pinned", False))
|
|
|
|
# TypedDict, where are you :/
|
|
repo = repo_mgr.get_repo(repo_name)
|
|
if repo is not None:
|
|
repo_folder = repo.folder_path
|
|
else:
|
|
repo_folder = repo_mgr.repos_folder / "MISSING_REPO"
|
|
|
|
location = repo_folder / cog_name
|
|
|
|
return cls(
|
|
location=location, repo=repo, commit=commit, pinned=pinned, json_repo_name=repo_name
|
|
)
|
|
|
|
@classmethod
|
|
def from_installable(cls, module: Installable, *, pinned: bool = False) -> InstalledModule:
|
|
return cls(
|
|
location=module._location, repo=module.repo, commit=module.commit, pinned=pinned
|
|
)
|