From 40cff058644a6c7e7a6e0de056fe134802fffaba Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 6 Jan 2025 10:06:38 +1100 Subject: [PATCH] Tasks cleanup (#8838) * Pretty print and typo fix * Improved output * Improve logging --- src/backend/InvenTree/InvenTree/settings.py | 8 ++-- tasks.py | 42 ++++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/settings.py b/src/backend/InvenTree/InvenTree/settings.py index 9109520e7b3a..60d2f6481bfd 100644 --- a/src/backend/InvenTree/InvenTree/settings.py +++ b/src/backend/InvenTree/InvenTree/settings.py @@ -219,11 +219,9 @@ # Append directory for sample plugin static content (if in debug mode) if PLUGINS_ENABLED: - print('Adding plugin sample static content') + logger.info('Adding plugin sample static content') STATICFILES_DIRS.append(BASE_DIR.joinpath('plugin', 'samples', 'static')) - print('-', STATICFILES_DIRS[-1]) - # Database backup options # Ref: https://django-dbbackup.readthedocs.io/en/master/configuration.html DBBACKUP_SEND_EMAIL = False @@ -1037,7 +1035,9 @@ validator = URLValidator() validator(SITE_URL) except Exception: - print(f"Invalid SITE_URL value: '{SITE_URL}'. InvenTree server cannot start.") + msg = f"Invalid SITE_URL value: '{SITE_URL}'. InvenTree server cannot start." + logger.error(msg) + print(msg) sys.exit(-1) else: diff --git a/tasks.py b/tasks.py index d787e4b1fc7e..326104d682b4 100644 --- a/tasks.py +++ b/tasks.py @@ -80,8 +80,8 @@ def checkPythonVersion(): valid = False if not valid: - print(f'The installed python version ({version}) is not supported!') - print(f'InvenTree requires Python {REQ_MAJOR}.{REQ_MINOR} or above') + error(f'The installed python version ({version}) is not supported!') + error(f'InvenTree requires Python {REQ_MAJOR}.{REQ_MINOR} or above') sys.exit(1) @@ -254,7 +254,7 @@ def check(cmd): # Print a warning if node is available but yarn is not if node_version and not yarn_passes: - print( + warning( 'Node is available but yarn is not. Install yarn if you wish to build the frontend.' ) @@ -262,7 +262,7 @@ def check(cmd): return ret(yarn_passes and node_version, node_version, yarn_version) -def check_file_existance(filename: Path, overwrite: bool = False): +def check_file_existence(filename: Path, overwrite: bool = False): """Checks if a file exists and asks the user if it should be overwritten. Args: @@ -276,7 +276,7 @@ def check_file_existance(filename: Path, overwrite: bool = False): response = str(response).strip().lower() if response not in ['y', 'yes']: - print('Cancelled export operation') + error('Cancelled export operation') sys.exit(1) @@ -522,7 +522,7 @@ def restore( base_cmd += f' -I {path}' if ignore_database: - print('Skipping database archive...') + info('Skipping database archive...') else: info('Restoring InvenTree database') cmd = f'dbrestore {base_cmd}' @@ -533,7 +533,7 @@ def restore( manage(c, cmd) if ignore_media: - print('Skipping media restore...') + info('Skipping media restore...') else: info('Restoring InvenTree media files') cmd = f'mediarestore {base_cmd}' @@ -615,7 +615,11 @@ def update( # - INVENTREE_DOCKER is set (by the docker image eg.) and not overridden by `--frontend` flag # - `--no-frontend` flag is set if (os.environ.get('INVENTREE_DOCKER', False) and not frontend) or no_frontend: - print('Skipping frontend update!') + if no_frontend: + info('Skipping frontend update (no_frontend flag set)') + else: + info('Skipping frontend update (INVENTREE_DOCKER flag set)') + frontend = False no_frontend = True else: @@ -678,7 +682,7 @@ def export_records( info(f"Exporting database records to file '{target}'") - check_file_existance(target, overwrite) + check_file_existence(target, overwrite) tmpfile = f'{target}.tmp' @@ -723,7 +727,7 @@ def export_records( f_out.write(json.dumps(data_out, indent=2)) if not retain_temp: - print('Removing temporary files') + info('Removing temporary files') os.remove(tmpfile) success('Data export completed') @@ -1122,7 +1126,7 @@ def schema( ): """Export current API schema.""" filename = Path(filename).resolve() - check_file_existance(filename, overwrite) + check_file_existence(filename, overwrite) info(f"Exporting schema file to '{filename}'") @@ -1154,7 +1158,7 @@ def schema( def export_settings_definitions(c, filename='inventree_settings.json', overwrite=False): """Export settings definition to a JSON file.""" filename = Path(filename).resolve() - check_file_existance(filename, overwrite) + check_file_existence(filename, overwrite) info(f"Exporting settings definition to '{filename}'...") manage(c, f'export_settings_definitions {filename}', pty=True) @@ -1382,10 +1386,10 @@ def check_already_current(tag=None, sha=None): current_content = current.read_text().strip() ref_value = tag or sha if current_content == ref_value: - print(f'Frontend {ref} is already `{ref_value}`') + info(f'Frontend {ref} is already `{ref_value}`') return True else: - print( + info( f'Frontend {ref} is not expected `{ref_value}` but `{current_content}` - new version will be downloaded' ) return False @@ -1418,7 +1422,7 @@ def check_already_current(tag=None, sha=None): and content['INVENTREE_PKG_INSTALLER'] == 'PKG' ): ref = content.get('INVENTREE_COMMIT_SHA') - print( + info( f'[INFO] Running in package environment, got commit "{ref}" from VERSION file' ) else: @@ -1459,7 +1463,8 @@ def check_already_current(tag=None, sha=None): if not (qc_run := find_resource(workflow_runs['workflow_runs'], 'name', 'QC')): error(f'ERROR: Cannot find any workflow runs for current SHA {ref}') return - print( + + info( f'Found workflow {qc_run["name"]} (run {qc_run["run_number"]}-{qc_run["run_attempt"]})' ) @@ -1472,9 +1477,10 @@ def check_already_current(tag=None, sha=None): artifacts['artifacts'], 'name', 'frontend-build' ) ): - print('[ERROR] Cannot find frontend-build.zip attachment for current sha') + error('[ERROR] Cannot find frontend-build.zip attachment for current sha') return - print( + + info( f'Found artifact {frontend_artifact["name"]} with id {frontend_artifact["id"]} ({frontend_artifact["size_in_bytes"] / 1e6:.2f}MB).' )