Move logic for fetching latest Red version info to internal util (#3904)

* Only Send out of date message to Final builds available on PyPi

* Only Send out of date message to Final builds available on PyPi

* sorted the resulting list so that the newest build is first in the list

* forgot about this one

* well jack is a bitch but we love him.

* simplify logic

* Add the new function to `__all__`

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Draper 2020-06-18 21:21:59 +01:00 committed by GitHub
parent 879d21c528
commit 175fbebd73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 21 deletions

View File

@ -35,6 +35,7 @@ from . import (
config,
)
from .utils import AsyncIter
from .utils._internal_utils import fetch_latest_red_version_info
from .utils.predicates import MessagePredicate
from .utils.chat_formatting import (
box,
@ -315,7 +316,6 @@ class Core(commands.Cog, CoreLogic):
org_repo = "https://github.com/Cog-Creators"
red_repo = org_repo + "/Red-DiscordBot"
red_pypi = "https://pypi.org/project/Red-DiscordBot"
red_pypi_json = "https://pypi.org/pypi/Red-DiscordBot/json"
support_server_url = "https://discord.gg/red"
dpy_repo = "https://github.com/Rapptz/discord.py"
python_url = "https://www.python.org/"
@ -329,14 +329,8 @@ class Core(commands.Cog, CoreLogic):
owner = app_info.owner
custom_info = await self.bot._config.custom_info()
try:
async with aiohttp.ClientSession() as session:
async with session.get(red_pypi_json) as r:
data = await r.json()
except (aiohttp.ClientError, asyncio.TimeoutError):
outdated = None
else:
outdated = VersionInfo.from_str(data["info"]["version"]) > red_version_info
pypi_version, py_version_req = await fetch_latest_red_version_info()
outdated = pypi_version and pypi_version > red_version_info
if embed_links:
dpy_version = "[{}]({})".format(discord.__version__, dpy_repo)
@ -360,7 +354,7 @@ class Core(commands.Cog, CoreLogic):
if outdated in (True, None):
if outdated is True:
outdated_value = _("Yes, {version} is available.").format(
version=data["info"]["version"]
version=str(pypi_version)
)
else:
outdated_value = _("Checking for updates failed.")
@ -403,7 +397,7 @@ class Core(commands.Cog, CoreLogic):
if outdated in (True, None):
if outdated is True:
outdated_value = _("Yes, {version} is available.").format(
version=data["info"]["version"]
version=str(pypi_version)
)
else:
outdated_value = _("Checking for updates failed.")

View File

@ -21,7 +21,12 @@ from .utils import AsyncIter
from .. import __version__ as red_version, version_info as red_version_info, VersionInfo
from . import commands
from .config import get_latest_confs
from .utils._internal_utils import fuzzy_command_search, format_fuzzy_results, expected_version
from .utils._internal_utils import (
fuzzy_command_search,
format_fuzzy_results,
expected_version,
fetch_latest_red_version_info,
)
from .utils.chat_formatting import inline, bordered, format_perms_list, humanize_timedelta
log = logging.getLogger("red")
@ -93,19 +98,17 @@ def init_events(bot, cli_flags):
outdated_red_message = ""
with contextlib.suppress(aiohttp.ClientError, asyncio.TimeoutError):
async with aiohttp.ClientSession() as session:
async with session.get("https://pypi.org/pypi/red-discordbot/json") as r:
data = await r.json()
if VersionInfo.from_str(data["info"]["version"]) > red_version_info:
pypi_version, py_version_req = await fetch_latest_red_version_info()
outdated = pypi_version and pypi_version > red_version_info
if outdated:
INFO.append(
"Outdated version! {} is available "
"but you're using {}".format(data["info"]["version"], red_version)
"but you're using {}".format(pypi_version, red_version)
)
outdated_red_message = _(
"Your Red instance is out of date! {} is the current "
"version, however you are using {}!"
).format(data["info"]["version"], red_version)
requires_python = data["info"]["requires_python"]
).format(pypi_version, red_version)
current_python = platform.python_version()
extra_update = _(
"\n\nWhile the following command should work in most scenarios as it is "
@ -114,7 +117,7 @@ def init_events(bot, cli_flags):
"make sure there is nothing else that "
"needs to be done during the update.**"
).format(docs="https://docs.discord.red/en/stable/update_red.html",)
if expected_version(current_python, requires_python):
if expected_version(current_python, py_version_req):
installed_extras = []
for extra, reqs in red_pkg._dep_map.items():
if extra is None:
@ -151,7 +154,7 @@ def init_events(bot, cli_flags):
"You will need to follow the update instructions in our docs above, "
"if you still need help updating after following the docs go to our "
"#support channel in <https://discord.gg/red>"
).format(py_version=current_python, req_py=requires_python)
).format(py_version=current_python, req_py=py_version_req)
outdated_red_message += extra_update
INFO2 = []

View File

@ -19,11 +19,14 @@ from typing import (
Optional,
Union,
TYPE_CHECKING,
Tuple,
)
import aiohttp
import discord
import pkg_resources
from fuzzywuzzy import fuzz, process
from redbot import VersionInfo
from redbot.core import data_manager
from redbot.core.utils.chat_formatting import box
@ -42,6 +45,7 @@ __all__ = (
"send_to_owners_with_preprocessor",
"send_to_owners_with_prefix_replaced",
"expected_version",
"fetch_latest_red_version_info",
)
@ -298,3 +302,17 @@ async def send_to_owners_with_prefix_replaced(bot: Red, content: str, **kwargs):
def expected_version(current: str, expected: str) -> bool:
# `pkg_resources` needs a regular requirement string, so "x" serves as requirement's name here
return current in pkg_resources.Requirement.parse(f"x{expected}")
async def fetch_latest_red_version_info() -> Tuple[Optional[VersionInfo], Optional[str]]:
try:
async with aiohttp.ClientSession() as session:
async with session.get("https://pypi.org/pypi/Red-DiscordBot/json") as r:
data = await r.json()
except (aiohttp.ClientError, asyncio.TimeoutError):
return None, None
else:
release = VersionInfo.from_str(data["info"]["version"])
required_python = data["info"]["requires_python"]
return release, required_python