update/sw-patch/cgcs-patch/cgcs_patch/utils.py
Al Bailey 93163a4aaa Minor sw-patch tox cleanup
These changes will not affect the behaviour of sw-patch.

- Remove fm-api from tox dependencies for sw-patch.
  - fm-api is not used by sw-patch

- Cleanup flake8 error:
  H401  docstring should not start with a space

- Cleanup pylint error:
  R1710 inconsistent-return-statements

- Cleanup pylint error:
   W1505 deprecated-method
    The following are the deprecated methods
    - LOG.warn  -> LOG.warning
    - ConfigParser.readfp -> ConfigParser.read_file

- Added missing description for a suppressed pylint error
  - W3101 missing-timeout

- Added two additional tox utilities:
  Neither tool is wired into the default tox, or zuul jobs.

  - tox -e prospector
     prospector calls multiple linters and validators

  - tox -e vulture
      vulture is a tool that detects unused code.

Test Plan:
  - Run tox
  - Build / Install / Unlock AIO-DX
  - Apply a reboot required patch

Story: 2009969
Task: 46265
Signed-off-by: Al Bailey <al.bailey@windriver.com>
Change-Id: I7fcd386d11ec3836059b1036fb334dcae9bedeb1
2022-09-12 16:28:43 +00:00

116 lines
3.1 KiB
Python

"""
Copyright (c) 2016-2022 Wind River Systems, Inc.
SPDX-License-Identifier: Apache-2.0
"""
import logging
from netaddr import IPAddress
import os
import socket
from socket import if_nametoindex as if_nametoindex_func
import cgcs_patch.constants as constants
LOG = logging.getLogger('main_logger')
def if_nametoindex(name):
try:
return if_nametoindex_func(name)
except Exception:
return 0
def gethostbyname(hostname):
"""gethostbyname with IPv6 support """
try:
return socket.getaddrinfo(hostname, None)[0][4][0]
except Exception:
return None
def get_management_version():
"""Determine whether management is IPv4 or IPv6 """
controller_ip_string = gethostbyname(constants.CONTROLLER_FLOATING_HOSTNAME)
if controller_ip_string:
controller_ip_address = IPAddress(controller_ip_string)
return controller_ip_address.version
else:
return constants.ADDRESS_VERSION_IPV4
def get_management_family():
ip_version = get_management_version()
if ip_version == constants.ADDRESS_VERSION_IPV6:
return socket.AF_INET6
else:
return socket.AF_INET
def get_versioned_address_all():
ip_version = get_management_version()
if ip_version == constants.ADDRESS_VERSION_IPV6:
return "::"
else:
return "0.0.0.0"
def ip_to_url(ip_address_string):
"""Add brackets if an IPv6 address """
try:
ip_address = IPAddress(ip_address_string)
if ip_address.version == constants.ADDRESS_VERSION_IPV6:
return "[%s]" % ip_address_string
else:
return ip_address_string
except Exception:
return ip_address_string
def ip_to_versioned_localhost(ip_address_string):
"""Add brackets if an IPv6 address """
ip_address = IPAddress(ip_address_string)
if ip_address.version == constants.ADDRESS_VERSION_IPV6:
return "::1"
else:
return "localhost"
def read_cached_file(filename, cache_info, reload_func=None):
"""Read from a file if it has been modified.
:param cache_info: dictionary to hold opaque cache.
:param reload_func: optional function to be called with data when
file is reloaded due to a modification.
:returns: data from file
"""
mtime = os.path.getmtime(filename)
if not cache_info or mtime != cache_info.get('mtime'):
LOG.debug("Reloading cached file %s", filename)
with open(filename) as fap:
cache_info['data'] = fap.read()
cache_info['mtime'] = mtime
if reload_func:
reload_func(cache_info['data'])
return cache_info['data']
def safe_rstrip(value, chars=None):
"""Removes trailing characters from a string if that does not make it empty
:param value: A string value that will be stripped.
:param chars: Characters to remove.
:return: Stripped value.
"""
if not isinstance(value, str):
LOG.warning("Failed to remove trailing character. Returning original "
"object. Supplied object is not a string: %s", value)
return value
return value.rstrip(chars) or value