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>
This commit is contained in:
Erich Cordoba 2019-11-01 15:30:30 -06:00
parent 48e311a492
commit a8acbf01c5
3 changed files with 46 additions and 15 deletions

View File

@ -1,11 +1,11 @@
"""Provides different network functions""" """Provides different network functions"""
import logging import logging
import re
import subprocess import subprocess
from bash import bash from bash import bash
from elevate import elevate from elevate import elevate
from ifparser import Ifcfg
from pynetlinux import ifconfig from pynetlinux import ifconfig
from pynetlinux import brctl from pynetlinux import brctl
@ -13,6 +13,41 @@ from Libraries.common import update_config_ini
LOG = logging.getLogger(__name__) 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(): def delete_network_interfaces():
"""Delete network interfaces """Delete network interfaces
@ -27,7 +62,7 @@ def delete_network_interfaces():
# becoming in root # becoming in root
elevate(graphical=False) elevate(graphical=False)
ifdata = Ifcfg(subprocess.check_output(['ifconfig', '-a']).decode('utf-8')) ifdata = NetIfs()
# Destroy NAT network if exist # Destroy NAT network if exist
try: try:
@ -37,23 +72,20 @@ def delete_network_interfaces():
LOG.warning('NAT network not found') LOG.warning('NAT network not found')
for interface in range(1, 5): for interface in range(1, 5):
current_interface = 'stxbr{}'.format(interface) net_if = 'stxbr{}'.format(interface)
if current_interface in ifdata.interfaces: if net_if 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_up or net_running: if ifdata.is_up(net_if) or \
ifdata.is_running(net_if):
# the network interface is up or running # the network interface is up or running
try: try:
# down and delete the network interface # down and delete the network interface
ifconfig.Interface(current_interface).down() ifconfig.Interface(net_if).down()
brctl.Bridge(current_interface).delete() brctl.Bridge(net_if).delete()
except IOError: except IOError:
LOG.warning('[Errno 19] No such device: ' LOG.warning('[Errno 19] No such device: '
'%s', current_interface) '%s', net_if)
def configure_network_interfaces(): def configure_network_interfaces():

View File

@ -37,11 +37,11 @@ class CustomHandler(PatternMatchingEventHandler):
last_line = bash('tail -2 {}'.format(event.src_path)) last_line = bash('tail -2 {}'.format(event.src_path))
if 'LAST_CONSOLE_LINE' not in os.environ: 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)) print('{}'.format(last_line.stdout))
elif os.environ.get('LAST_CONSOLE_LINE') != 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)) print('{}'.format(last_line.stdout))
def on_modified(self, event): def on_modified(self, event):

View File

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