Browse Source

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 <erich.cordoba.malibran@intel.com>
tags/v3.0.0.rc0^0
Erich Cordoba 9 months ago
parent
commit
a8acbf01c5
3 changed files with 46 additions and 15 deletions
  1. +44
    -12
      automated-robot-suite/Utils/network.py
  2. +2
    -2
      automated-robot-suite/Utils/watcher.py
  3. +0
    -1
      automated-robot-suite/requirements.txt

+ 44
- 12
automated-robot-suite/Utils/network.py View File

@@ -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():


+ 2
- 2
automated-robot-suite/Utils/watcher.py View File

@@ -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):


+ 0
- 1
automated-robot-suite/requirements.txt View File

@@ -7,7 +7,6 @@ PyYaml
netifaces
bash
elevate
ifparser
git+https://github.com/rlisagor/pynetlinux.git@master#egg=pynetlinux
kmodpy
psutil

Loading…
Cancel
Save