From a1111b08d99f8920de5ec5134fda126ee7610e32 Mon Sep 17 00:00:00 2001 From: Florian Rau Date: Tue, 26 Sep 2023 12:44:15 +0100 Subject: [PATCH] fetch remote changelog for update notice --- iblrig/gui/wizard.py | 7 ++---- iblrig/version_management.py | 44 +++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/iblrig/gui/wizard.py b/iblrig/gui/wizard.py index a71865a99..182e0c6d7 100644 --- a/iblrig/gui/wizard.py +++ b/iblrig/gui/wizard.py @@ -24,7 +24,7 @@ from iblrig.misc import _get_task_argument_parser from iblrig.base_tasks import BaseSession from iblrig.hardware import Bpod -from iblrig.version_management import check_for_updates +from iblrig.version_management import check_for_updates, get_changelog from iblrig.gui.ui_wizard import Ui_wizard from iblrig.gui.ui_update import Ui_update from pybpodapi import exceptions @@ -508,10 +508,7 @@ class UpdateNotice(QtWidgets.QDialog, Ui_update): def __init__(self, parent=None, *args, **kwargs): super(UpdateNotice, self).__init__(*args, **kwargs) self.setupUi(self) - with open(Path(BASE_DIR).joinpath('CHANGELOG.md')) as f: - changelog = f.read() - self.uiTextBrowserChanges.setMarkdown(changelog) - self.uiTextBrowserChanges.setHtml(self.uiTextBrowserChanges.toHtml()) + self.uiTextBrowserChanges.setMarkdown(get_changelog()) self.uiLabelLogo.setPixmap(QtGui.QPixmap(WIZARD_PNG)) self.setWindowIcon(QtGui.QIcon(WIZARD_PNG)) self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint) diff --git a/iblrig/version_management.py b/iblrig/version_management.py index 807df758b..04074bac3 100644 --- a/iblrig/version_management.py +++ b/iblrig/version_management.py @@ -1,5 +1,6 @@ +from pathlib import Path from typing import Union, Callable, Any - +import requests from packaging import version import re from subprocess import check_output, check_call, SubprocessError, CalledProcessError, STDOUT @@ -131,7 +132,7 @@ def get_detailed_version_string(v_basic: str) -> str: # get details through `git describe` try: - fetch_remote_tags() + get_remote_tags() v_detailed = check_output(["git", "describe", "--dirty", "--broken", "--match", v_sanitized, "--tags", "--long"], cwd=BASE_DIR, text=True, timeout=1, stderr=STDOUT) except (SubprocessError, CalledProcessError): @@ -145,18 +146,45 @@ def get_detailed_version_string(v_basic: str) -> str: return v_detailed +@static_vars(branch=None) +def get_branch() -> Union[str, None]: + if get_branch.branch: + return get_branch.branch + if not IS_GIT: + log.error('This installation of iblrig is not managed through git') + try: + get_branch.branch = check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], + cwd=BASE_DIR, timeout=5, text=True).removesuffix('\n') + return get_branch.branch + except (SubprocessError, CalledProcessError): + return None + + @static_vars(is_fetched_already=False) -def fetch_remote_tags() -> None: - if fetch_remote_tags.is_fetched_already: +def get_remote_tags() -> None: + if get_remote_tags.is_fetched_already: return if not IS_GIT: log.error('This installation of iblrig is not managed through git') try: - branch = check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=BASE_DIR, timeout=5, text=True).removesuffix('\n') - check_call(["git", "fetch", "origin", branch, "-t", "-q"], cwd=BASE_DIR, timeout=5) + check_call(["git", "fetch", "origin", get_branch(), "-t", "-q"], cwd=BASE_DIR, timeout=5) except (SubprocessError, CalledProcessError): return - fetch_remote_tags.is_fetched_already = True + get_remote_tags.is_fetched_already = True + + +@static_vars(changelog=None) +def get_changelog() -> str: + if get_changelog.changelog: + return get_changelog.changelog + try: + changelog = requests.get(f'https://raw.githubusercontent.com/int-brain-lab/iblrig/{get_branch()}/CHANGELOG.md', + allow_redirects=True).text + except requests.RequestException: + with open(Path(BASE_DIR).joinpath('CHANGELOG.md')) as f: + changelog = f.read() + get_changelog.changelog = changelog + return get_changelog.changelog @static_vars(remote_version=None) @@ -187,7 +215,7 @@ def get_remote_version() -> Union[version.Version, None]: try: log.debug('Obtaining remote version from github') - fetch_remote_tags() + get_remote_tags() references = check_output(["git", "ls-remote", "-t", "-q", "--exit-code", "--refs", "origin", "tags", "*"], cwd=BASE_DIR, timeout=5, encoding='UTF-8')