# HG changeset patch # User RhodeCode Admin # Date 2023-03-07 08:57:26 # Node ID fd6cd0ae8dcabeadacaac19c19d0b71f5978900c # Parent 08819a6d1f373290730880094818f7e95ee551de system-info: optimized code based on linting diff --git a/rhodecode/lib/system_info.py b/rhodecode/lib/system_info.py --- a/rhodecode/lib/system_info.py +++ b/rhodecode/lib/system_info.py @@ -24,6 +24,7 @@ import sys import time import platform import collections +import psutil from functools import wraps import pkg_resources @@ -35,16 +36,8 @@ import configparser log = logging.getLogger(__name__) -psutil = None - -try: - # cygwin cannot have yet psutil support. - import psutil as psutil -except ImportError: - pass - - _NA = 'NOT AVAILABLE' +_NA_FLOAT = 0.0 STATE_OK = 'ok' STATE_ERR = 'error' @@ -74,7 +67,7 @@ def register_sysinfo(func): # HELPERS -def percentage(part, whole): +def percentage(part: (int, float), whole: (int, float)): whole = float(whole) if whole > 0: return round(100 * float(part) / whole, 1) @@ -160,7 +153,7 @@ class SysInfo(object): # SysInfo functions @register_sysinfo def python_info(): - value = dict(version=' '.join(platform._sys_version()), + value = dict(version=f'{platform.python_version()}:{platform.python_implementation()}', executable=sys.executable) return SysInfoRes(value=value) @@ -232,8 +225,6 @@ def uptime(): value = dict(boot_time=0, uptime=0, text='') state = STATE_OK_DEFAULT - if not psutil: - return SysInfoRes(value=value, state=state) boot_time = psutil.boot_time() value['boot_time'] = boot_time @@ -259,8 +250,6 @@ def memory(): total=0, buffers=0, text='') state = STATE_OK_DEFAULT - if not psutil: - return SysInfoRes(value=value, state=state) value.update(dict(psutil.virtual_memory()._asdict())) value['used_real'] = value['total'] - value['available'] @@ -273,7 +262,7 @@ def memory(): format_byte_size_binary(value['total']), value['percent_used'],) - keys = value.keys()[::] + keys = list(value.keys())[::] keys.pop(keys.index('percent')) keys.pop(keys.index('percent_used')) keys.pop(keys.index('text')) @@ -293,21 +282,20 @@ def memory(): @register_sysinfo def machine_load(): - value = {'1_min': _NA, '5_min': _NA, '15_min': _NA, 'text': ''} + value = {'1_min': _NA_FLOAT, '5_min': _NA_FLOAT, '15_min': _NA_FLOAT, 'text': ''} state = STATE_OK_DEFAULT - if not psutil: - return SysInfoRes(value=value, state=state) # load averages if hasattr(psutil.os, 'getloadavg'): value.update(dict( - zip(['1_min', '5_min', '15_min'], psutil.os.getloadavg()))) + zip(['1_min', '5_min', '15_min'], psutil.os.getloadavg()) + )) human_value = value.copy() human_value['text'] = '1min: {}, 5min: {}, 15min: {}'.format( value['1_min'], value['5_min'], value['15_min']) - if state['type'] == STATE_OK and value['15_min'] > 5: + if state['type'] == STATE_OK and value['15_min'] > 5.0: msg = 'Warning: your machine load is very high.' state = {'message': msg, 'type': STATE_WARN} @@ -319,9 +307,6 @@ def cpu(): value = {'cpu': 0, 'cpu_count': 0, 'cpu_usage': []} state = STATE_OK_DEFAULT - if not psutil: - return SysInfoRes(value=value, state=state) - value['cpu'] = psutil.cpu_percent(0.5) value['cpu_usage'] = psutil.cpu_percent(0.5, percpu=True) value['cpu_count'] = psutil.cpu_count() @@ -341,8 +326,6 @@ def storage(): value = dict(percent=0, used=0, total=0, path=path, text='') state = STATE_OK_DEFAULT - if not psutil: - return SysInfoRes(value=value, state=state) try: value.update(dict(psutil.disk_usage(path)._asdict())) @@ -374,10 +357,8 @@ def storage_inodes(): from rhodecode.model.settings import VcsSettingsModel path = VcsSettingsModel().get_repos_location() - value = dict(percent=0, free=0, used=0, total=0, path=path, text='') + value = dict(percent=0.0, free=0, used=0, total=0, path=path, text='') state = STATE_OK_DEFAULT - if not psutil: - return SysInfoRes(value=value, state=state) try: i_stat = os.statvfs(path)