From a8acbf01c5d4ec20431d580a00b55777ed26bc3c Mon Sep 17 00:00:00 2001 From: Erich Cordoba Date: Fri, 1 Nov 2019 15:30:30 -0600 Subject: [PATCH] Remove ifparser module dependency. The ifparser module doesn't work for python 3.6 and higher. As this module is only used to retrieve the network interfaces and their status it is possible to refactor the code to support this feature and remove the dependency. Also, some decode('utf-8') calls were added in the watcher module. Change-Id: I7943d672e9282fbc56fc8d9a1a4b589d1aab5a68 Signed-off-by: Erich Cordoba --- automated-robot-suite/Utils/network.py | 56 ++++++++++++++++++++------ automated-robot-suite/Utils/watcher.py | 4 +- automated-robot-suite/requirements.txt | 1 - 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/automated-robot-suite/Utils/network.py b/automated-robot-suite/Utils/network.py index 777ef8e..c816ae8 100644 --- a/automated-robot-suite/Utils/network.py +++ b/automated-robot-suite/Utils/network.py @@ -1,11 +1,11 @@ """Provides different network functions""" import logging +import re import subprocess from bash import bash from elevate import elevate -from ifparser import Ifcfg from pynetlinux import ifconfig from pynetlinux import brctl @@ -13,6 +13,41 @@ from Libraries.common import update_config_ini LOG = logging.getLogger(__name__) +class NetInterface: + """ Represent the status of a network interface.""" + def __init__(self, name, flags): + self.name = name + self.up = False + self.running = False + self.set_flags(flags) + + def set_flags(self, flags): + if "UP" in flags: + self.up = True + if "RUNNING" in flags: + self.running = True + +class NetIfs: + """ Retrieves the list of network interfaces in the system.""" + def __init__(self): + self.interfaces = [] + output = subprocess.check_output(['ifconfig', '-a']) + # Regex to get the interface line from ifconfig + regex = re.compile(r"(\w+?\-*\w+):\sflags=\d+<(.*)>\s+mtu\s\d+") + for o in output.decode('utf-8').split('\n'): + m = regex.match(o) + if m: + new_interface = NetInterface(m.group(1), m.group(2)) + self.interfaces.append(new_interface) + + def is_up(self, if_name): + idx = self.interfaces.index(if_name) + return self.interfaces[idx].up + + def is_running(self, if_name): + idx = self.interfaces.index(if_name) + return self.interfaces[idx].running + def delete_network_interfaces(): """Delete network interfaces @@ -27,7 +62,7 @@ def delete_network_interfaces(): # becoming in root elevate(graphical=False) - ifdata = Ifcfg(subprocess.check_output(['ifconfig', '-a']).decode('utf-8')) + ifdata = NetIfs() # Destroy NAT network if exist try: @@ -37,23 +72,20 @@ def delete_network_interfaces(): LOG.warning('NAT network not found') for interface in range(1, 5): - current_interface = 'stxbr{}'.format(interface) + net_if = 'stxbr{}'.format(interface) - if current_interface in ifdata.interfaces: - # the network interface exists - net_object = ifdata.get_interface(current_interface) - net_up = net_object.get_values().get('UP') - net_running = net_object.get_values().get('RUNNING') + if net_if in ifdata.interfaces: - if net_up or net_running: + if ifdata.is_up(net_if) or \ + ifdata.is_running(net_if): # the network interface is up or running try: # down and delete the network interface - ifconfig.Interface(current_interface).down() - brctl.Bridge(current_interface).delete() + ifconfig.Interface(net_if).down() + brctl.Bridge(net_if).delete() except IOError: LOG.warning('[Errno 19] No such device: ' - '%s', current_interface) + '%s', net_if) def configure_network_interfaces(): diff --git a/automated-robot-suite/Utils/watcher.py b/automated-robot-suite/Utils/watcher.py index 4aa099f..894d448 100644 --- a/automated-robot-suite/Utils/watcher.py +++ b/automated-robot-suite/Utils/watcher.py @@ -37,11 +37,11 @@ class CustomHandler(PatternMatchingEventHandler): last_line = bash('tail -2 {}'.format(event.src_path)) if 'LAST_CONSOLE_LINE' not in os.environ: - os.environ['LAST_CONSOLE_LINE'] = last_line.stdout + os.environ['LAST_CONSOLE_LINE'] = last_line.stdout.decode('utf-8') print('{}'.format(last_line.stdout)) elif os.environ.get('LAST_CONSOLE_LINE') != last_line.stdout: - os.environ['LAST_CONSOLE_LINE'] = last_line.stdout + os.environ['LAST_CONSOLE_LINE'] = last_line.stdout.decode('utf-8') print('{}'.format(last_line.stdout)) def on_modified(self, event): diff --git a/automated-robot-suite/requirements.txt b/automated-robot-suite/requirements.txt index d9adef6..18d9733 100644 --- a/automated-robot-suite/requirements.txt +++ b/automated-robot-suite/requirements.txt @@ -7,7 +7,6 @@ PyYaml netifaces bash elevate -ifparser git+https://github.com/rlisagor/pynetlinux.git@master#egg=pynetlinux kmodpy psutil