From a469d8ad9b88b17930ee8eb985f03eae07965607 Mon Sep 17 00:00:00 2001 From: Bernardo Decco Date: Mon, 27 Sep 2021 10:33:25 -0300 Subject: [PATCH] Re-enable important py3k checks for monitoring kube-memory Re-enabling some of the disabled tox warnings present on the pylint.rc file Re-enabling: W1619: old-division W1633: round-builtin Test Plan: Sanity test run on AIO-SX: PASS: test_system_health_pre_session[pods] PASS: test_system_health_pre_session[alarms] PASS: test_system_health_pre_session[system_apps] PASS: test_wr_analytics[deploy_and_remove] PASS: test_horizon_host_inventory_display PASS: test_lock_unlock_host[controller] PASS: test_pod_to_pod_connection PASS: test_pod_to_service_connection PASS: test_host_to_service_connection Story: 2006796 Task: 43444 Signed-off-by: Bernardo Decco Change-Id: I00dc37bbd8f60f475f85e4f0463b7c066a719f1f --- .../kube-memory/kube_memory/kube_memory.py | 19 ++++++++++++++----- kube-memory/kube-memory/pylint.rc | 4 +--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/kube-memory/kube-memory/kube_memory/kube_memory.py b/kube-memory/kube-memory/kube_memory/kube_memory.py index 53d72d9..5f0fd26 100644 --- a/kube-memory/kube-memory/kube_memory/kube_memory.py +++ b/kube-memory/kube-memory/kube_memory/kube_memory.py @@ -18,6 +18,7 @@ Usage: kube-memory [-h] [--debug] import argparse import json import logging +import math import os import re import subprocess @@ -51,6 +52,14 @@ GREP_CMD = ["grep", "-rs", "total_rss"] LOG = logging.getLogger(__name__) +def py2_round(number, decimal=0): + # This function will keep the behavior of py2 round method + param = 10 ** decimal + if number > 0: + return float(math.floor((number * param) + 0.5)) / param # pylint: disable=W1619 + return float(math.ceil((number * param) - 0.5)) / param # pylint: disable=W1619 + + def mem_to_mebibytes(n_bytes): """Convert a string that represents memory in bytes into mebibytes(MiB) @@ -58,8 +67,8 @@ def mem_to_mebibytes(n_bytes): e.g., '1829108992' is converted to 1744.374. """ try: - mebibytes = (float(n_bytes) / BYTES_IN_MEBIBYTE) - return str(round(mebibytes, DECIMAL_DIGITS)) + mebibytes = (float(n_bytes) / BYTES_IN_MEBIBYTE) # pylint: disable=W1619 + return str(py2_round(mebibytes, DECIMAL_DIGITS)) except (ValueError, TypeError): return "-" @@ -434,13 +443,13 @@ def gather_info_and_display(): mem_info['MemAvailable'])) * KBYTE total_mebib = float(anon_mebib + avail_mebib) - anon_percent = round(100 * anon_mebib / total_mebib, DECIMAL_DIGITS) + anon_percent = py2_round(100 * anon_mebib / total_mebib, DECIMAL_DIGITS) # pylint: disable=W1619 reserved_mebib = get_platform_reserved_memory() # Calculate platform memory in terms of percent reserved if reserved_mebib > 0.0: - platform_memory_percent = round( - 100 * platform_mebib / reserved_mebib, DECIMAL_DIGITS) + platform_memory_percent = py2_round( + 100 * platform_mebib / reserved_mebib, DECIMAL_DIGITS) # pylint: disable=W1619 pt_platf = prettytable.PrettyTable( ['Reserved', diff --git a/kube-memory/kube-memory/pylint.rc b/kube-memory/kube-memory/pylint.rc index 827151a..d664e46 100644 --- a/kube-memory/kube-memory/pylint.rc +++ b/kube-memory/kube-memory/pylint.rc @@ -135,9 +135,7 @@ enable=E1603,E1609,E1610,E1602,E1606,E1608,E1607,E1605,E1604,E1601,E1611,W1652, # C0111: Missing %s docstring # Disable Python3 checkers: # W1618: no-absolute-import -# W1619: old-division -# W1633: round-builtin -disable= R0914, R0915, W0703, C0325, C0111, C0330, W1618, W1619, W1633 +disable= R0914, R0915, W0703, C0325, C0111, C0330, W1618 [REPORTS]