From 02d279b6c41078127d656917ed4ea6518b8ffa26 Mon Sep 17 00:00:00 2001 From: Romain J Date: Tue, 10 Sep 2019 23:20:56 +0200 Subject: [PATCH] add(laucnher|update): add bot.version and --update --- README.md | 4 +-- bot.py | 5 ++++ cogs/utils/version.py | 12 ++++++++ launcher.py | 51 +++++++++++++++++++++++++++------ locales/en/LC_MESSAGES/base.po | 4 +++ locales/fr/LC_MESSAGES/base.mo | Bin 917 -> 980 bytes locales/fr/LC_MESSAGES/base.po | 4 +++ requirements.txt | 3 +- 8 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 cogs/utils/version.py diff --git a/README.md b/README.md index ba92b29..878b6cf 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ ## Launcher requirements : - - [ ] Can install the bot - - [ ] Can launch the bot + - [x] Can install the bot + - [x] Can launch the bot - [ ] Can propose updates ## New commands : diff --git a/bot.py b/bot.py index af4cd81..dd130ad 100755 --- a/bot.py +++ b/bot.py @@ -6,11 +6,13 @@ from collections import deque import aiohttp import discord +import git from discord.ext import commands import config from cogs.utils.config import Config from cogs.utils.lang import gettext +from cogs.utils.version import Version description = """ Je suis TuxBot, le bot qui vit de l'OpenSource ! ;) @@ -47,6 +49,8 @@ class TuxBot(commands.AutoShardedBot): self.prefixes = Config('prefixes.json') self.blacklist = Config('blacklist.json') + self.version = Version(10, 0, 0, pre_release='a18', build=git.Repo(search_parent_directories=True).head.object.hexsha) + for extension in l_extensions: if extension not in unload: try: @@ -95,6 +99,7 @@ class TuxBot(commands.AutoShardedBot): self.uptime = datetime.datetime.utcnow() print(gettext('Ready:') + f' {self.user} (ID: {self.user.id})') + print(self.version) presence: dict = dict(status=discord.Status.dnd) if self.config.activity is not None: diff --git a/cogs/utils/version.py b/cogs/utils/version.py new file mode 100644 index 0000000..37f922c --- /dev/null +++ b/cogs/utils/version.py @@ -0,0 +1,12 @@ +class Version: + def __init__(self, major: int, minor: int, patch: int, **kwargs): + self.major: int = major + self.minor: int = minor + self.patch: int = patch + + self.pre_release = kwargs.get('pre_release', '') + self.build = kwargs.get('build', '') + + def __str__(self) -> str: + build = self.build[:10] + return f'v{self.major}.{self.minor}.{self.patch}{self.pre_release}-{build}' diff --git a/launcher.py b/launcher.py index 7149b90..b1278b4 100644 --- a/launcher.py +++ b/launcher.py @@ -3,6 +3,8 @@ import contextlib import logging import socket import sys +import git +import requests import click @@ -42,7 +44,7 @@ def setup_logging(): log.removeHandler(hdlr) -def run_bot(unload): +def run_bot(unload: list = []): loop = asyncio.get_event_loop() log = logging.getLogger() @@ -60,15 +62,46 @@ def run_bot(unload): bot.run() -@click.group(invoke_without_command=True, options_metavar='[options]') -@click.option('-u', '--unload', - multiple=True, type=str, - help=gettext('Launch without loading the module')) -@click.pass_context -def main(ctx, unload): - if ctx.invoked_subcommand is None: +@click.command() +@click.option('-d', '--unload', multiple=True, type=str, help=gettext('Launch without loading the module')) +@click.option('-u', '--update', help=gettext('Search for update'), is_flag=True) +def main(**kwargs): + if kwargs.get('update'): + _update() + + with setup_logging(): + run_bot(kwargs.get('unload')) + + +@click.option('-d', '--update', help=gettext('Search for update'), is_flag=True) +def _update(): + print(gettext('Checking for update...')) + + local = git.Repo(search_parent_directories=True) + current = local.head.object.hexsha + + origin = requests.get('https://git.gnous.eu/api/v1/repos/gnouseu/tuxbot-bot/branches/master') + last = origin.json().get('commit').get('id') + + if current != last: + print(gettext('A new version is available !')) + check = input(gettext('Update ? [Y/n]')).lower().strip() + + while check not in ['', 'y', 'n']: + check = input(gettext('Update ? [Y/n]')) + + if check == 'y': + local.remotes.origin.pull() + with setup_logging(): + run_bot() + else: + with setup_logging(): + run_bot() + else: + print(gettext('Tuxbot is up to date') + '\n') + with setup_logging(): - run_bot(unload) + run_bot() if __name__ == '__main__': diff --git a/locales/en/LC_MESSAGES/base.po b/locales/en/LC_MESSAGES/base.po index ca46dcc..df3cf00 100644 --- a/locales/en/LC_MESSAGES/base.po +++ b/locales/en/LC_MESSAGES/base.po @@ -23,6 +23,10 @@ msgstr "" msgid "Launch without loading the module" msgstr "" +#: launcher.py:73 +msgid "Search for update" +msgstr "" + #: bot.py:52 msgid "Failed to load extension : " msgstr "" diff --git a/locales/fr/LC_MESSAGES/base.mo b/locales/fr/LC_MESSAGES/base.mo index a24b97add354aca21ae6f25a3c0921c6dec55cfb..c0df40f4f79b59fcafdb4cc9083d0c29373a3c23 100644 GIT binary patch delta 233 zcmbQreucgMo)F7a1|Z-BVi_P#0dbJP8X#K^h}QzKG!UNwVrC$I0mK?W{04~GfLN4~ zfk6;R%K&L(Agu?a4S;kKkhTTl$v~_GcBU{C?lUw||#1A`WWB+xXFfIN@}8RP<_ zK@RZ-(z-x814!Ef>1h*ZmI$OK7A0pWq~#YWloq5UmZWaxW|U)+2}(`QNCiry7AfSU X7Axdt76Zw{3ly^QON%CJFy{gQ&Lbop delta 171 zcmcb@K9#-xo)F7a1|Z-7Vi_Qg0dbJP93Wc`i01;aG!X9rVrC$|0mK?WdF|jwOE&8>$}mmNV9o&m)+`dS diff --git a/locales/fr/LC_MESSAGES/base.po b/locales/fr/LC_MESSAGES/base.po index 9bbd996..4b3581f 100644 --- a/locales/fr/LC_MESSAGES/base.po +++ b/locales/fr/LC_MESSAGES/base.po @@ -23,6 +23,10 @@ msgstr "Impossible de lancer PostgreSQL..." msgid "Launch without loading the module" msgstr "Lancer sans charger le module " +#: launcher.py:73 +msgid "Search for update" +msgstr "Rechercher les mises à jour" + #: bot.py:52 msgid "Failed to load extension : " msgstr "Impossible de charger l'extension : " diff --git a/requirements.txt b/requirements.txt index 22eaeb7..eff90f1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ discord.py[voice] jishaku lxml click -asyncpg>=0.12.0 \ No newline at end of file +asyncpg>=0.12.0 +gitpython \ No newline at end of file