From 3a423c3e409c4e9082b3fe0cf706ec8fc955000c Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 8 Feb 2025 12:12:11 +1100 Subject: [PATCH] [Setup] Enhance version information output (#9049) * Improve output of 'invoke version' - Display backup path - Handle undefined paths without error * Provide colorized output --- src/backend/InvenTree/InvenTree/config.py | 21 +++++++++++----- tasks.py | 29 ++++++++++++++++------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/config.py b/src/backend/InvenTree/InvenTree/config.py index 377efb04ee..325e719a52 100644 --- a/src/backend/InvenTree/InvenTree/config.py +++ b/src/backend/InvenTree/InvenTree/config.py @@ -238,12 +238,15 @@ def get_boolean_setting(env_var=None, config_key=None, default_value=False): return is_true(get_setting(env_var, config_key, default_value)) -def get_media_dir(create=True): +def get_media_dir(create=True, error=True): """Return the absolute path for the 'media' directory (where uploaded files are stored).""" md = get_setting('INVENTREE_MEDIA_ROOT', 'media_root') if not md: - raise FileNotFoundError('INVENTREE_MEDIA_ROOT not specified') + if error: + raise FileNotFoundError('INVENTREE_MEDIA_ROOT not specified') + else: + return None md = Path(md).resolve() @@ -253,12 +256,15 @@ def get_media_dir(create=True): return md -def get_static_dir(create=True): +def get_static_dir(create=True, error=True): """Return the absolute path for the 'static' directory (where static files are stored).""" sd = get_setting('INVENTREE_STATIC_ROOT', 'static_root') if not sd: - raise FileNotFoundError('INVENTREE_STATIC_ROOT not specified') + if error: + raise FileNotFoundError('INVENTREE_STATIC_ROOT not specified') + else: + return None sd = Path(sd).resolve() @@ -268,12 +274,15 @@ def get_static_dir(create=True): return sd -def get_backup_dir(create=True): +def get_backup_dir(create=True, error=True): """Return the absolute path for the backup directory.""" bd = get_setting('INVENTREE_BACKUP_DIR', 'backup_dir') if not bd: - raise FileNotFoundError('INVENTREE_BACKUP_DIR not specified') + if error: + raise FileNotFoundError('INVENTREE_BACKUP_DIR not specified') + else: + return None bd = Path(bd).resolve() diff --git a/tasks.py b/tasks.py index e8f209f21f..575e10e24a 100644 --- a/tasks.py +++ b/tasks.py @@ -49,28 +49,33 @@ def task_exception_handler(t, v, tb): sys.excepthook = task_exception_handler +def wrap_color(text: str, color: str) -> str: + """Wrap text in a color code.""" + return f'\033[{color}m{text}\033[0m' + + def success(*args): """Print a success message to the console.""" msg = ' '.join(map(str, args)) - print(f'\033[92m{msg}\033[0m') + print(wrap_color(msg, '92')) def error(*args): """Print an error message to the console.""" msg = ' '.join(map(str, args)) - print(f'\033[91m{msg}\033[0m') + print(wrap_color(msg, '91')) def warning(*args): """Print a warning message to the console.""" msg = ' '.join(map(str, args)) - print(f'\033[93m{msg}\033[0m') + print(wrap_color(msg, '93')) def info(*args): """Print an informational message to the console.""" msg = ' '.join(map(str, args)) - print(f'\033[94m{msg}\033[0m') + print(wrap_color(msg, '94')) def checkInvokeVersion(): @@ -1284,6 +1289,7 @@ def version(c): """Show the current version of InvenTree.""" import src.backend.InvenTree.InvenTree.version as InvenTreeVersion from src.backend.InvenTree.InvenTree.config import ( + get_backup_dir, get_config_file, get_media_dir, get_static_dir, @@ -1292,6 +1298,10 @@ def version(c): # Gather frontend version information _, node, yarn = node_available(versions=True) + # Special output messages + NOT_SPECIFIED = wrap_color('NOT SPECIFIED', '91') + NA = wrap_color('N/A', '93') + print( f""" InvenTree - inventree.org @@ -1304,16 +1314,17 @@ Environment {sys.prefix} Installation paths: Base {localDir()} Config {get_config_file()} -Media {get_media_dir()} -Static {get_static_dir()} +Media {get_media_dir(error=False) or NOT_SPECIFIED} +Static {get_static_dir(error=False) or NOT_SPECIFIED} +Backup {get_backup_dir(error=False) or NOT_SPECIFIED} Versions: Python {python_version()} Django {InvenTreeVersion.inventreeDjangoVersion()} InvenTree {InvenTreeVersion.inventreeVersion()} API {InvenTreeVersion.inventreeApiVersion()} -Node {node if node else 'N/A'} -Yarn {yarn if yarn else 'N/A'} +Node {node if node else NA} +Yarn {yarn if yarn else NA} Commit hash: {InvenTreeVersion.inventreeCommitHash()} Commit date: {InvenTreeVersion.inventreeCommitDate()}""" @@ -1321,7 +1332,7 @@ Commit date: {InvenTreeVersion.inventreeCommitDate()}""" if len(sys.argv) == 1 and sys.argv[0].startswith('/opt/inventree/env/lib/python'): print( """ -You are probably running the package installer / single-line installer. Please mentioned that in any bug reports! +You are probably running the package installer / single-line installer. Please mention this in any bug reports! Use '--list' for a list of available commands Use '--help' for help on a specific command"""