From bae290d6058e8cf1b50780596d50041f226442ff Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:17:21 +0100 Subject: [PATCH 1/8] check git version and safe for runtime --- InvenTree/plugin/apps.py | 4 ++++ InvenTree/plugin/helpers.py | 20 ++++++++++++++++++++ InvenTree/plugin/registry.py | 1 + 3 files changed, 25 insertions(+) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index dd75e3c8fb..4de6542bf1 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -10,6 +10,7 @@ from maintenance_mode.core import set_maintenance_mode from InvenTree.ready import isImportingData from plugin import registry +from plugin.helpers import check_git_version logger = logging.getLogger('inventree') @@ -34,3 +35,6 @@ class PluginAppConfig(AppConfig): # drop out of maintenance # makes sure we did not have an error in reloading and maintenance is still active set_maintenance_mode(False) + + # check git version + registry.git_is_modern = check_git_version() diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 2271c01d98..5afc8ea5fc 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -108,6 +108,26 @@ def get_git_log(path): output = 7 * [''] # pragma: no cover return {'hash': output[0], 'author': output[1], 'mail': output[2], 'date': output[3], 'message': output[4], 'verified': output[5], 'key': output[6]} +def check_git_version(): + """returns if the current git version supports modern features""" + + # get version string + try: + output = str(subprocess.check_output(['git', '--version'], cwd=os.path.dirname(settings.BASE_DIR)), 'utf-8') + except subprocess.CalledProcessError: # pragma: no cover + return False + + # process version string + try: + version = output[12:-1].split(".") + if len(version) > 1 and version[0] == '2': + if len(version) > 2 and int(version[1]) >= 22: + return True + except ValueError: + pass + + return False + class GitStatus: """ diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index aec38cc623..40a4a4e2d9 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -52,6 +52,7 @@ class PluginsRegistry: # flags self.is_loading = False self.apps_loading = True # Marks if apps were reloaded yet + self.git_is_modern = True # Is a modern version of git available # integration specific self.installed_apps = [] # Holds all added plugin_paths From 8550915528e518c002392059c792fde3eb2c2a99 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:23:25 +0100 Subject: [PATCH 2/8] opimise coverage --- InvenTree/plugin/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 5afc8ea5fc..838dfeb275 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -123,7 +123,7 @@ def check_git_version(): if len(version) > 1 and version[0] == '2': if len(version) > 2 and int(version[1]) >= 22: return True - except ValueError: + except ValueError: # pragma: no cover pass return False From 1591597ef9a92bfb7f72809e3466666bcb118bf8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:23:46 +0100 Subject: [PATCH 3/8] check if modern git is used --- InvenTree/plugin/helpers.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 838dfeb275..2f0f80ab83 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -94,15 +94,18 @@ def get_git_log(path): """ Get dict with info of the last commit to file named in path """ - path = path.replace(os.path.dirname(settings.BASE_DIR), '')[1:] - command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path] + from plugin import registry + output = None - try: - output = str(subprocess.check_output(command, cwd=os.path.dirname(settings.BASE_DIR)), 'utf-8')[1:-1] - if output: - output = output.split('\n') - except subprocess.CalledProcessError: # pragma: no cover - pass + if registry.git_is_modern: + path = path.replace(os.path.dirname(settings.BASE_DIR), '')[1:] + command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path] + try: + output = str(subprocess.check_output(command, cwd=os.path.dirname(settings.BASE_DIR)), 'utf-8')[1:-1] + if output: + output = output.split('\n') + except subprocess.CalledProcessError: # pragma: no cover + pass if not output: output = 7 * [''] # pragma: no cover From 084f9d544460ac59f09071e22e1ea5c5afbc7c89 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:23:55 +0100 Subject: [PATCH 4/8] make more readable --- InvenTree/plugin/helpers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 2f0f80ab83..14c5184d01 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -109,6 +109,7 @@ def get_git_log(path): if not output: output = 7 * [''] # pragma: no cover + return {'hash': output[0], 'author': output[1], 'mail': output[2], 'date': output[3], 'message': output[4], 'verified': output[5], 'key': output[6]} def check_git_version(): From ca8df60d005beb6ff453ee0c91fdc151d41964a0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:28:03 +0100 Subject: [PATCH 5/8] PEP fixes --- InvenTree/plugin/helpers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 14c5184d01..1a5089aefe 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -97,7 +97,7 @@ def get_git_log(path): from plugin import registry output = None - if registry.git_is_modern: + if registry.git_is_modern: path = path.replace(os.path.dirname(settings.BASE_DIR), '')[1:] command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path] try: @@ -112,9 +112,10 @@ def get_git_log(path): return {'hash': output[0], 'author': output[1], 'mail': output[2], 'date': output[3], 'message': output[4], 'verified': output[5], 'key': output[6]} + def check_git_version(): """returns if the current git version supports modern features""" - + # get version string try: output = str(subprocess.check_output(['git', '--version'], cwd=os.path.dirname(settings.BASE_DIR)), 'utf-8') From f3f010c4bed0a71adb465fd41d1289a762428458 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:34:02 +0100 Subject: [PATCH 6/8] write error if too old git --- InvenTree/plugin/apps.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index 4de6542bf1..d46721962b 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -10,7 +10,7 @@ from maintenance_mode.core import set_maintenance_mode from InvenTree.ready import isImportingData from plugin import registry -from plugin.helpers import check_git_version +from plugin.helpers import check_git_version, log_error logger = logging.getLogger('inventree') @@ -38,3 +38,5 @@ class PluginAppConfig(AppConfig): # check git version registry.git_is_modern = check_git_version() + if not registry.git_is_modern: + log_error('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.', 'load') From f80f9609231a4542be1cc8429983e61f9ae29724 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:34:28 +0100 Subject: [PATCH 7/8] translate error message --- InvenTree/plugin/apps.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index d46721962b..a10618e2be 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -5,6 +5,7 @@ import logging from django.apps import AppConfig from django.conf import settings +from django.utils.translation import ugettext_lazy as _ from maintenance_mode.core import set_maintenance_mode @@ -39,4 +40,4 @@ class PluginAppConfig(AppConfig): # check git version registry.git_is_modern = check_git_version() if not registry.git_is_modern: - log_error('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.', 'load') + log_error(_('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.'), 'load') From ba263187a5ad3a8b3291a537f8b0247662b58951 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 00:35:36 +0100 Subject: [PATCH 8/8] optimise coverage --- InvenTree/plugin/apps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index a10618e2be..75063c6b3c 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -39,5 +39,5 @@ class PluginAppConfig(AppConfig): # check git version registry.git_is_modern = check_git_version() - if not registry.git_is_modern: + if not registry.git_is_modern: # pragma: no cover # simulating old git seems not worth it for coverage log_error(_('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.'), 'load')