From 4557d573bdae37d38b8716057dcd4f8bf9e7541a Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Mon, 22 Mar 2021 17:51:38 +0100 Subject: [PATCH] Update hacking and fix pep8 violations Using version 2.0.0 which was the last to support Python 2. Change-Id: Ie7028516f41445791125b2f670a309c40150058b --- glean/cmd.py | 26 +++++++++++++------------- glean/install.py | 7 ++++--- glean/tests/test_glean.py | 3 +-- glean/utils.py | 11 ++++++----- test-requirements.txt | 2 +- tox.ini | 4 +++- 6 files changed, 28 insertions(+), 25 deletions(-) diff --git a/glean/cmd.py b/glean/cmd.py index 65291a7..280d78c 100644 --- a/glean/cmd.py +++ b/glean/cmd.py @@ -26,10 +26,10 @@ import subprocess import sys import time +from glean._vendor import distro from glean import install from glean import systemlock from glean import utils -from glean._vendor import distro try: import configparser @@ -357,9 +357,10 @@ def _write_networkd_interface(name, interfaces, args, files_struct=dict()): ) # define network if needed (basically always) if ((interface['type'] in ['ipv4_dhcp', 'ipv6_slaac', - 'ipv6_dhcpv6_stateful', 'manual', 'ipv4', 'ipv6']) or - ('vlan_id' in interface) or - ('bond_mode' in interface)): + 'ipv6_dhcpv6_stateful', 'manual', + 'ipv4', 'ipv6']) + or ('vlan_id' in interface) + or ('bond_mode' in interface)): if '[Network]' not in files_struct[network_file]: files_struct[network_file]['[Network]'] = list() if 'services' in interface: @@ -383,8 +384,8 @@ def _write_networkd_interface(name, interfaces, args, files_struct=dict()): else: files_struct[network_file]['[Network]'].append('DHCP=ipv6') # slaac can start dhcp6 if the associated RA option is sent to server - if (interface['type'] == 'ipv6_slaac' or - interface['type'] == 'ipv6_dhcpv6-stateless'): + if (interface['type'] == 'ipv6_slaac' + or interface['type'] == 'ipv6_dhcpv6-stateless'): # we are accepting slaac now, remove the disabling of slaac if 'IPv6AcceptRA=no' in files_struct[network_file]['[Network]']: files_struct[network_file]['[Network]'].remove( @@ -666,11 +667,10 @@ mac_{name}="{hwaddr}\"\n""".format( ) routes = list() for route in interface.get('routes', ()): - if (route['network'] == '0.0.0.0' and - route['netmask'] == '0.0.0.0'): + if (route['network'] == '0.0.0.0' + and route['netmask'] == '0.0.0.0'): # add default route if it exists routes.append('default via {gw}'.format( - name=name, gw=route['gateway'] )) else: @@ -903,10 +903,10 @@ def write_debian_interfaces(interfaces, sys_interfaces, args): utils.ipv6_netmask_length(interface['netmask'])) for route in interface.get('routes', ()): - if ((route['network'] == '0.0.0.0' and - route['netmask'] == '0.0.0.0') or - (route['network'] == '::' and - route['netmask'] == '::')): + if ((route['network'] == '0.0.0.0' + and route['netmask'] == '0.0.0.0') + or (route['network'] == '::' + and route['netmask'] == '::')): result += " gateway {0}\n".format(route['gateway']) else: if interface['type'] == 'ipv4': diff --git a/glean/install.py b/glean/install.py index f3e3837..080a018 100755 --- a/glean/install.py +++ b/glean/install.py @@ -119,9 +119,9 @@ def main(): # may be wrong as install is generally executed in a chroot # with diskimage-builder. - if (os.path.exists('/usr/lib/systemd/system') and - (os.path.exists('/usr/bin/systemctl') or - os.path.exists('/bin/systemctl'))): + if (os.path.exists('/usr/lib/systemd/system') + and (os.path.exists('/usr/bin/systemctl') + or os.path.exists('/bin/systemctl'))): log.info("Installing systemd services") log.info("glean.sh in %s" % p) @@ -180,5 +180,6 @@ def main(): replacements={'EXTRA_ARGS': extra_args}) os.system('update-rc.d glean defaults') + if __name__ == '__main__': main() diff --git a/glean/tests/test_glean.py b/glean/tests/test_glean.py index eca8ba8..63440ac 100644 --- a/glean/tests/test_glean.py +++ b/glean/tests/test_glean.py @@ -224,8 +224,7 @@ class TestGlean(base.BaseTestCase): with open(output_path) as f: for line in f: if line == '%NM_CONTROLLED%\n': - line = 'NM_CONTROLLED=%s\n' % \ - ("yes" if use_nm else "no") + line = 'NM_CONTROLLED=%s\n' % ("yes" if use_nm else "no") lines.append(line) write_dest = None write_content = None diff --git a/glean/utils.py b/glean/utils.py index eee8b6f..2a8f3d7 100644 --- a/glean/utils.py +++ b/glean/utils.py @@ -26,16 +26,17 @@ def ipv6_netmask_length(netmask): if not w or int(w, 16) == 0: break count += bitCount.index(int(w, 16)) - except: + except Exception: raise SyntaxError('Bad Netmask') return count -# code to convert netmask ip to cidr number -# https://stackoverflow.com/a/43885814 def ipv4_netmask_length(netmask): - ''' + """Convert netmask IP to CIDR number. + + From https://stackoverflow.com/a/43885814 + :param netmask: netmask ip addr (eg: 255.255.255.0) :return: equivalent cidr number to given netmask ip (eg: 24) - ''' + """ return sum([bin(int(x)).count('1') for x in netmask.split('.')]) diff --git a/test-requirements.txt b/test-requirements.txt index 5de0765..92fe7e5 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,7 +2,7 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -hacking>=0.9.2,<0.10 +hacking>=2.0.0,<3.0.0 coverage>=3.6 mock>=1.0;python_version=='2.7' diff --git a/tox.ini b/tox.ini index 304cdce..a82e078 100644 --- a/tox.ini +++ b/tox.ini @@ -33,8 +33,10 @@ commands = python setup.py build_sphinx # Hacking ignored becaues its rules are too zealous and cause more # grief than value. # E123, E125 skipped as they are invalid PEP-8. +# W503 contradicts W504 which seems more preferred per +# https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator show-source = True -ignore = E123,E125,H +ignore = E123,E125,W503,H builtins = _ exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build