misc: fix python sonarcloud BLOCKER level issues 53/26353/3
authorPaul Vinciguerra <pvinci@vinciconsulting.com>
Fri, 3 Apr 2020 16:18:40 +0000 (12:18 -0400)
committerNeale Ranns <nranns@cisco.com>
Mon, 6 Apr 2020 11:30:05 +0000 (11:30 +0000)
  Fix of the top 11 python issues flagged as BLOCKER.

Ticket: VPP-1856
Type: fix

Change-Id: Icf4691e62f4a69d6ee196b6d6e2ab52d961b5c76
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
doxygen/siphon/process.py
extras/vpp_config/vpplib/AutoConfig.py
extras/vpp_config/vpplib/VppGrubUtil.py
src/plugins/srv6-mobile/extra/runner.py
src/plugins/vrrp/test/test_vrrp.py
src/tools/vppapigen/vppapigen.py
test/template_ipsec.py
test/test_ip6.py
test/vpp_pg_interface.py

index 57e323e..ce70be5 100644 (file)
@@ -82,6 +82,7 @@ class Siphon(object):
         self._tplenv = jinja2.Environment(
             loader=loader,
             trim_blocks=True,
+            autoescape=True,
             keep_trailing_newline=True)
 
         # Convenience, get a reference to the internal escape and
index c477710..8c052a0 100644 (file)
@@ -122,7 +122,7 @@ class AutoConfig(object):
                     answer = input("Please enter the netmask [n.n.n.n]: ")
                     plen = ip_address(answer).netmask_bits()
                 return '{}/{}'.format(ipaddr, plen)
-            except None:
+            except ValueError:
                 print("Please enter a valid IPv4 address.")
 
     @staticmethod
index d199f1e..f17efd8 100644 (file)
@@ -164,63 +164,63 @@ class VppGrubUtil(object):
         """
 
         vpp_cmdline = self.create_cmdline(isolated_cpus)
-        if vpp_cmdline == '':
-            return vpp_cmdline
-
-        # Update grub
-        # Save the original file
-        rootdir = node['rootdir']
-        grubcmdline = node['cpu']['grubcmdline']
-        ofilename = rootdir + node['cpu']['grub_config_file'] + '.orig'
-        filename = rootdir + node['cpu']['grub_config_file']
-
-        # Write the output file
-        # Does a copy of the original file exist, if not create one
-        (ret, stdout, stderr) = VPPUtil.exec_command('ls {}'.format(ofilename))
-        if ret != 0:
-            if stdout.strip('\n') != ofilename:
-                cmd = 'sudo cp {} {}'.format(filename, ofilename)
-                (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
-                if ret != 0:
-                    raise RuntimeError('{} failed on node {} {}'.
-                                       format(cmd, self._node['host'], stderr))
-
-        # Get the contents of the current grub config file
-        cmd = 'cat {}'.format(filename)
-        (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
-        if ret != 0:
-            raise RuntimeError('{} failed on node {} {}'.format(
-                cmd,
-                self._node['host'],
-                stderr))
-
-        # Write the new contents
-        # Get the Default Linux command line, ignoring commented lines
-        content = ""
-        lines = stdout.split('\n')
-        for line in lines:
-            if line == '':
-                content += line + '\n'
-                continue
-            if line[0] == '#':
-                content += line + '\n'
-                continue
-
-            ldefault = re.findall(r'{}=.+'.format(grubcmdline), line)
-            if ldefault:
-                content += vpp_cmdline + '\n'
-            else:
-                content += line + '\n'
-
-        content = content.replace(r"`", r"\`")
-        content = content.rstrip('\n')
-        cmd = "sudo cat > {0} << EOF\n{1}\n".format(filename, content)
-        (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
-        if ret != 0:
-            raise RuntimeError('{} failed on node {} {}'.format(
-                cmd,
-                self._node['host'],
-                stderr))
+        if len(vpp_cmdline):
+            # Update grub
+            # Save the original file
+            rootdir = node['rootdir']
+            grubcmdline = node['cpu']['grubcmdline']
+            ofilename = rootdir + node['cpu']['grub_config_file'] + '.orig'
+            filename = rootdir + node['cpu']['grub_config_file']
+
+            # Write the output file
+            # Does a copy of the original file exist, if not create one
+            (ret, stdout, stderr) = VPPUtil.exec_command(
+                'ls {}'.format(ofilename))
+            if ret != 0:
+                if stdout.strip('\n') != ofilename:
+                    cmd = 'sudo cp {} {}'.format(filename, ofilename)
+                    (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
+                    if ret != 0:
+                        raise RuntimeError('{} failed on node {} {}'.
+                                           format(cmd, self._node['host'],
+                                                  stderr))
+
+            # Get the contents of the current grub config file
+            cmd = 'cat {}'.format(filename)
+            (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
+            if ret != 0:
+                raise RuntimeError('{} failed on node {} {}'.format(
+                    cmd,
+                    self._node['host'],
+                    stderr))
+
+            # Write the new contents
+            # Get the Default Linux command line, ignoring commented lines
+            content = ""
+            lines = stdout.split('\n')
+            for line in lines:
+                if line == '':
+                    content += line + '\n'
+                    continue
+                if line[0] == '#':
+                    content += line + '\n'
+                    continue
+
+                ldefault = re.findall(r'{}=.+'.format(grubcmdline), line)
+                if ldefault:
+                    content += vpp_cmdline + '\n'
+                else:
+                    content += line + '\n'
+
+            content = content.replace(r"`", r"\`")
+            content = content.rstrip('\n')
+            cmd = "sudo cat > {0} << EOF\n{1}\n".format(filename, content)
+            (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
+            if ret != 0:
+                raise RuntimeError('{} failed on node {} {}'.format(
+                    cmd,
+                    self._node['host'],
+                    stderr))
 
         return vpp_cmdline
 
index c438fb1..b18fdc3 100755 (executable)
@@ -248,6 +248,7 @@ class Containers(object):
 
     def build(self, path, vpp_path):
         env = Environment(loader=FileSystemLoader(path),
+                          autoescape=True,
                           trim_blocks=True)
 
         self.tmp_render(join(vpp_path, "Dockerfile"),
@@ -264,6 +265,7 @@ class Containers(object):
 
     def release(self, path, vpp_path):
         env = Environment(loader=FileSystemLoader(path),
+                          autoescape=True,
                           trim_blocks=True)
 
         self.tmp_render(join(vpp_path, "Dockerfile"),
index cd2aeb1..3a85d2e 100644 (file)
@@ -8,12 +8,13 @@
 
 import unittest
 import time
-from socket import inet_pton, inet_ntop, AF_INET6
+import socket
+from socket import inet_pton, inet_ntop
 
 from vpp_object import VppObject
 from vpp_papi import VppEnum
 
-from scapy.packet import Raw
+from scapy.packet import raw
 from scapy.layers.l2 import Ether, ARP
 from scapy.layers.inet import IP, ICMP, icmptypes
 from scapy.layers.inet6 import IPv6, ipv6nh, IPv6ExtHdrHopByHop, \
@@ -833,8 +834,8 @@ class TestVRRP6(VppTestCase):
 
         self.assertEqual(pkt[IPv6].dst, "ff02::1")
         # convert addrs to packed format since string versions could differ
-        src_addr = inet_pton(AF_INET6, pkt[IPv6].src)
-        vr_ll_addr = inet_pton(AF_INET6, vr.interface().local_ip6_ll)
+        src_addr = inet_pton(socket.AF_INET6, pkt[IPv6].src)
+        vr_ll_addr = inet_pton(socket.AF_INET6, vr.interface().local_ip6_ll)
         self.assertEqual(src_addr, vr_ll_addr)
 
         self.assertTrue(pkt[ICMPv6ND_NA].tgt in vr.virtual_ips())
index f3013aa..b17ad6d 100755 (executable)
@@ -648,7 +648,7 @@ class VPPAPIParser(object):
         elif len(p) == 4:
             p[0] = Field(p[1], p[2])
         else:
-            self._parse_error('ERROR')
+            self._parse_error('ERROR', self._token_coord(p, 1))
         self.fields.append(p[2])
 
     def p_declaration_array_vla(self, p):
index 5a700e8..1caed0d 100644 (file)
@@ -5,7 +5,7 @@ import struct
 from scapy.layers.inet import IP, ICMP, TCP, UDP
 from scapy.layers.ipsec import SecurityAssociation, ESP
 from scapy.layers.l2 import Ether
-from scapy.packet import Raw
+from scapy.packet import raw, Raw
 from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, IPv6ExtHdrHopByHop, \
     IPv6ExtHdrFragment, IPv6ExtHdrDestOpt
 
index 1f00ed3..63d3e41 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 
+import socket
 from socket import inet_pton, inet_ntop
 import unittest
 
index b7f1881..32c0eae 100755 (executable)
@@ -1,7 +1,8 @@
 import os
-import time
+import socket
 from socket import inet_pton, inet_ntop
 import struct
+import time
 from traceback import format_exc, format_stack
 
 import scapy.compat