From bd8d6c7b89dffe7f95d48c71a8092fd77d40b32d Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sun, 15 Nov 2020 11:05:54 -0800 Subject: [PATCH] Convert to Python 3 (#1128) Conform syntax to support Python 3 using 2to3 tool and manual assessment --- README.md | 17 ++- acl_loader/main.py | 54 ++++---- clear/main.py | 12 +- config/aaa.py | 9 -- config/config_mgmt.py | 15 ++- config/console.py | 3 - config/feature.py | 4 +- config/kube.py | 5 +- config/main.py | 170 +++++++++++++------------- config/mlnx.py | 2 +- config/nat.py | 38 +++--- connect/main.py | 9 +- consutil/lib.py | 9 +- consutil/main.py | 4 +- crm/main.py | 2 +- debug/main.py | 4 +- fdbutil/filter_fdb_entries.py | 10 +- fwutil/lib.py | 32 ++--- fwutil/log.py | 1 - fwutil/main.py | 8 +- pcieutil/main.py | 4 +- pddf_fanutil/main.py | 8 +- pddf_ledutil/main.py | 2 +- pddf_psuutil/main.py | 8 +- pddf_thermalutil/main.py | 6 +- pfc/main.py | 2 +- pfcwd/main.py | 16 +-- psuutil/main.py | 4 +- scripts/aclshow | 12 +- scripts/boot_part | 2 +- scripts/configlet | 26 ++-- scripts/db_migrator.py | 8 +- scripts/decode-syseeprom | 2 +- scripts/dropconfig | 12 +- scripts/dropstat | 30 ++--- scripts/dump_nat_entries.py | 4 +- scripts/ecnconfig | 9 +- scripts/fanshow | 3 +- scripts/fast-reboot-dump.py | 4 +- scripts/fdbclear | 2 +- scripts/fdbshow | 4 +- scripts/gearboxutil | 2 +- scripts/intfstat | 14 +-- scripts/intfutil | 19 ++- scripts/lldpshow | 20 ++- scripts/mellanox_buffer_migrator.py | 14 +-- scripts/mmuconfig | 6 +- scripts/natclear | 2 +- scripts/natconfig | 11 +- scripts/natshow | 2 +- scripts/nbrshow | 7 +- scripts/neighbor_advertiser | 4 +- scripts/pcmping | 26 ++-- scripts/pfcstat | 22 ++-- scripts/port2alias | 4 +- scripts/portconfig | 6 +- scripts/portstat | 20 +-- scripts/psushow | 4 +- scripts/queuestat | 18 +-- scripts/route_check.py | 2 +- scripts/sfpshow | 2 +- scripts/sonic-kdump-config | 4 +- scripts/sonic_sku_create.py | 23 ++-- scripts/tempershow | 4 +- scripts/update_json.py | 2 +- scripts/watermarkcfg | 4 +- scripts/watermarkstat | 8 +- setup.py | 6 +- sfputil/main.py | 2 +- show/feature.py | 8 +- show/fgnhg.py | 12 +- show/interfaces/__init__.py | 26 ++-- show/main.py | 88 ++++++------- show/mlnx.py | 4 +- show/system_health.py | 4 +- show/vlan.py | 27 ++-- sonic_installer/bootloader/aboot.py | 2 +- sonic_installer/bootloader/grub.py | 2 +- sonic_installer/bootloader/onie.py | 2 +- sonic_installer/bootloader/uboot.py | 6 +- sonic_installer/common.py | 4 +- sonic_installer/main.py | 28 ++--- ssdutil/main.py | 2 +- tests/aclshow_test.py | 12 +- tests/bgp_commands_test.py | 2 +- tests/config_mgmt_test.py | 150 ++++++++++++----------- tests/conftest.py | 15 ++- tests/console_test.py | 8 +- tests/counterpoll_test.py | 3 +- tests/crm_test.py | 15 +-- tests/drops_group_test.py | 30 +++-- tests/fgnhg_test.py | 14 +-- tests/filter_fdb_entries_test.py | 3 +- tests/gearbox_test.py | 6 +- tests/intfstat_test.py | 3 +- tests/intfutil_test.py | 22 ++-- tests/{kube.py => kube_test.py} | 4 +- tests/mock_tables/dbconnector.py | 16 +-- tests/mock_tables/mock_multi_asic.py | 3 +- tests/mock_tables/mock_single_asic.py | 3 +- tests/multi_asic_intfutil_test.py | 2 +- tests/neighbor_advertiser_test.py | 56 +++++---- tests/pfcstat_test.py | 2 +- tests/pfcwd_test.py | 2 +- tests/portstat_test.py | 2 +- tests/sflow_test.py | 2 +- tests/sfp_test.py | 3 +- tests/show_breakout_test.py | 3 +- tests/show_platform_test.py | 2 +- tests/sku_create_test.py | 6 +- tests/system_health_test.py | 25 ++-- tests/utils.py | 2 +- tests/vlan_test.py | 12 +- tests/watermarkstat_test.py | 4 +- undebug/main.py | 4 +- utilities_common/bgp_util.py | 38 +++--- utilities_common/cli.py | 36 +++--- utilities_common/util_base.py | 3 - watchdogutil/main.py | 2 +- 119 files changed, 762 insertions(+), 802 deletions(-) rename tests/{kube.py => kube_test.py} (97%) diff --git a/README.md b/README.md index 868a309cd87d..1c8a33fc7eb0 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,12 @@ Currently, this list of dependencies is as follows: - libyang_1.0.73_amd64.deb - libyang-cpp_1.0.73_amd64.deb -- python2-yang_1.0.73_amd64.deb - python3-yang_1.0.73_amd64.deb -- redis_dump_load-1.1-py2-none-any.whl -- swsssdk-2.0.1-py2-none-any.whl -- sonic_py_common-1.0-py2-none-any.whl -- sonic_config_engine-1.0-py2-none-any.whl -- sonic_yang_mgmt-1.0-py2-none-any.whl +- redis_dump_load-1.1-py3-none-any.whl +- swsssdk-2.0.1-py3-none-any.whl +- sonic_py_common-1.0-py3-none-any.whl +- sonic_config_engine-1.0-py3-none-any.whl +- sonic_yang_mgmt-1.0-py3-none-any.whl - sonic_yang_models-1.0-py3-none-any.whl @@ -43,7 +42,7 @@ A convenient alternative is to let the SONiC build system configure a build envi 2. Build the sonic-utilities Python wheel package inside the Buster slave container, and tell the build system to keep the container alive when finished ``` - make NOJESSIE=1 NOSTRETCH=1 KEEP_SLAVE_ON=yes target/python-wheels/sonic_utilities-1.2-py2-none-any.whl + make NOJESSIE=1 NOSTRETCH=1 KEEP_SLAVE_ON=yes target/python-wheels/sonic_utilities-1.2-py3-none-any.whl ``` 3. When the build finishes, your prompt will change to indicate you are inside the slave container. Change into the `src/sonic-utilities/` directory @@ -56,13 +55,13 @@ A convenient alternative is to let the SONiC build system configure a build envi #### To build ``` -python2 setup.py bdist_wheel +python3 setup.py bdist_wheel ``` #### To run unit tests ``` -python2 setup.py test +python3 setup.py test ``` diff --git a/acl_loader/main.py b/acl_loader/main.py index ef8a516fc647..cb89156a374a 100644 --- a/acl_loader/main.py +++ b/acl_loader/main.py @@ -1,7 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import click -import ipaddr +import ipaddress import json import syslog @@ -30,7 +30,7 @@ def error(msg): def deep_update(dst, src): - for key, value in src.iteritems(): + for key, value in src.items(): if isinstance(value, dict): node = dst.setdefault(key, {}) deep_update(node, value) @@ -182,7 +182,7 @@ def read_policers_info(self): # For multi-npu platforms we will read from any one of front asic namespace # config db as the information should be same across all config db if self.per_npu_configdb: - namespace_configdb = (self.per_npu_configdb.values())[0] + namespace_configdb = (list(self.per_npu_configdb.values()))[0] self.policers_db_info = namespace_configdb.get_table(self.POLICER) else: self.policers_db_info = self.configdb.get_table(self.POLICER) @@ -199,11 +199,11 @@ def read_sessions_info(self): # For multi-npu platforms we will read from any one of front asic namespace # config db as the information should be same across all config db if self.per_npu_configdb: - namespace_configdb = (self.per_npu_configdb.values())[0] + namespace_configdb = (list(self.per_npu_configdb.values()))[0] self.sessions_db_info = namespace_configdb.get_table(self.CFG_MIRROR_SESSION_TABLE) else: self.sessions_db_info = self.configdb.get_table(self.CFG_MIRROR_SESSION_TABLE) - for key in self.sessions_db_info.keys(): + for key in list(self.sessions_db_info.keys()): if self.per_npu_statedb: # For multi-npu platforms we will read from all front asic name space # statedb as the monitor port will be differnt for each asic @@ -211,7 +211,7 @@ def read_sessions_info(self): # We will store them as dict of 'asic' : value self.sessions_db_info[key]["status"] = {} self.sessions_db_info[key]["monitor_port"] = {} - for namespace_key, namespace_statedb in self.per_npu_statedb.iteritems(): + for namespace_key, namespace_statedb in self.per_npu_statedb.items(): state_db_info = namespace_statedb.get_all(self.statedb.STATE_DB, "{}|{}".format(self.STATE_MIRROR_SESSION_TABLE, key)) self.sessions_db_info[key]["status"][namespace_key] = state_db_info.get("status", "inactive") if state_db_info else "error" self.sessions_db_info[key]["monitor_port"][namespace_key] = state_db_info.get("monitor_port", "") if state_db_info else "" @@ -367,7 +367,7 @@ def validate_actions(self, table_name, action_props): # For multi-npu we will read using anyone statedb connector for front asic namespace. # Same information should be there in all state DB's # as it is static information about switch capability - namespace_statedb = (self.per_npu_statedb.values())[0] + namespace_statedb = (list(self.per_npu_statedb.values()))[0] capability = namespace_statedb.get_all(self.statedb.STATE_DB, "{}|switch".format(self.SWITCH_CAPABILITY_TABLE)) else: capability = self.statedb.get_all(self.statedb.STATE_DB, "{}|switch".format(self.SWITCH_CAPABILITY_TABLE)) @@ -417,7 +417,7 @@ def convert_ip(self, table_name, rule_idx, rule): # FIXME: 0 is a valid protocol number, but openconfig seems to use it as a default value, # so there isn't currently a good way to check if the user defined proto=0 or not. if rule.ip.config.protocol: - if self.ip_protocol_map.has_key(rule.ip.config.protocol): + if rule.ip.config.protocol in self.ip_protocol_map: rule_props["IP_PROTOCOL"] = self.ip_protocol_map[rule.ip.config.protocol] else: try: @@ -430,14 +430,14 @@ def convert_ip(self, table_name, rule_idx, rule): if rule.ip.config.source_ip_address: source_ip_address = rule.ip.config.source_ip_address.encode("ascii") - if ipaddr.IPNetwork(source_ip_address).version == 4: + if ipaddress.ip_network(source_ip_address).version == 4: rule_props["SRC_IP"] = source_ip_address else: rule_props["SRC_IPV6"] = source_ip_address if rule.ip.config.destination_ip_address: destination_ip_address = rule.ip.config.destination_ip_address.encode("ascii") - if ipaddr.IPNetwork(destination_ip_address).version == 4: + if ipaddress.ip_network(destination_ip_address).version == 4: rule_props["DST_IP"] = destination_ip_address else: rule_props["DST_IPV6"] = destination_ip_address @@ -579,17 +579,17 @@ def full_update(self): be removed and new rules in that table will be installed. :return: """ - for key in self.rules_db_info.keys(): + for key in list(self.rules_db_info.keys()): if self.current_table is None or self.current_table == key[0]: self.configdb.mod_entry(self.ACL_RULE, key, None) # Program for per front asic namespace also if present - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.mod_entry(self.ACL_RULE, key, None) self.configdb.mod_config({self.ACL_RULE: self.rules_info}) # Program for per front asic namespace also if present - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.mod_config({self.ACL_RULE: self.rules_info}) def incremental_update(self): @@ -605,10 +605,10 @@ def incremental_update(self): # update on dataplane ACLs, and only perform an incremental update on # control plane ACLs. - new_rules = set(self.rules_info.iterkeys()) + new_rules = set(self.rules_info.keys()) new_dataplane_rules = set() new_controlplane_rules = set() - current_rules = set(self.rules_db_info.iterkeys()) + current_rules = set(self.rules_db_info.keys()) current_dataplane_rules = set() current_controlplane_rules = set() @@ -630,7 +630,7 @@ def incremental_update(self): for key in current_dataplane_rules: self.configdb.mod_entry(self.ACL_RULE, key, None) # Program for per-asic namespace also if present - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.mod_entry(self.ACL_RULE, key, None) @@ -638,7 +638,7 @@ def incremental_update(self): for key in new_dataplane_rules: self.configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key]) # Program for per-asic namespace corresponding to front asic also if present. - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key]) added_controlplane_rules = new_controlplane_rules.difference(current_controlplane_rules) @@ -649,14 +649,14 @@ def incremental_update(self): self.configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key]) # Program for per-asic namespace corresponding to front asic also if present. # For control plane ACL it's not needed but to keep all db in sync program everywhere - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key]) for key in removed_controlplane_rules: self.configdb.mod_entry(self.ACL_RULE, key, None) # Program for per-asic namespace corresponding to front asic also if present. # For control plane ACL it's not needed but to keep all db in sync program everywhere - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.mod_entry(self.ACL_RULE, key, None) for key in existing_controlplane_rules: @@ -664,7 +664,7 @@ def incremental_update(self): self.configdb.set_entry(self.ACL_RULE, key, self.rules_info[key]) # Program for per-asic namespace corresponding to front asic also if present. # For control plane ACL it's not needed but to keep all db in sync program everywhere - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.set_entry(self.ACL_RULE, key, self.rules_info[key]) def delete(self, table=None, rule=None): @@ -673,12 +673,12 @@ def delete(self, table=None, rule=None): :param rule: :return: """ - for key in self.rules_db_info.iterkeys(): + for key in self.rules_db_info.keys(): if not table or table == key[0]: if not rule or rule == key[1]: self.configdb.set_entry(self.ACL_RULE, key, None) # Program for per-asic namespace corresponding to front asic also if present. - for namespace_configdb in self.per_npu_configdb.values(): + for namespace_configdb in list(self.per_npu_configdb.values()): namespace_configdb.set_entry(self.ACL_RULE, key, None) def show_table(self, table_name): @@ -690,7 +690,7 @@ def show_table(self, table_name): header = ("Name", "Type", "Binding", "Description", "Stage") data = [] - for key, val in self.get_tables_db_info().iteritems(): + for key, val in self.get_tables_db_info().items(): if table_name and key != table_name: continue @@ -728,7 +728,7 @@ def show_session(self, session_name): erspan_data = [] span_data = [] - for key, val in self.get_sessions_db_info().iteritems(): + for key, val in self.get_sessions_db_info().items(): if session_name and key != session_name: continue @@ -756,7 +756,7 @@ def show_policer(self, policer_name): header = ("Name", "Type", "Mode", "CIR", "CBS") data = [] - for key, val in self.get_policers_db_info().iteritems(): + for key, val in self.get_policers_db_info().items(): if policer_name and key != policer_name: continue @@ -807,7 +807,7 @@ def pop_matches(val): return matches raw_data = [] - for (tname, rid), val in self.get_rules_db_info().iteritems(): + for (tname, rid), val in self.get_rules_db_info().items(): if table_name and table_name != tname: continue diff --git a/clear/main.py b/clear/main.py index a1dcc4d2374a..a0561ba89203 100755 --- a/clear/main.py +++ b/clear/main.py @@ -1,13 +1,8 @@ -import click +import configparser import os import subprocess -try: - # noinspection PyPep8Naming - import ConfigParser as configparser -except ImportError: - # noinspection PyUnresolvedReferences - import configparser +import click # This is from the aliases example: @@ -82,6 +77,7 @@ def get_routing_stack(): proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, + text=True, stderr=subprocess.STDOUT) stdout = proc.communicate()[0] proc.wait() @@ -99,7 +95,7 @@ def get_routing_stack(): def run_command(command, pager=False, return_output=False): # Provide option for caller function to Process the output. - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) if return_output: return proc.communicate() elif pager: diff --git a/config/aaa.py b/config/aaa.py index fdb85c3f27e8..7102227dd5ef 100644 --- a/config/aaa.py +++ b/config/aaa.py @@ -1,8 +1,4 @@ -#!/usr/bin/env python -u -# -*- coding: utf-8 -*- - import click - from swsssdk import ConfigDBConnector import utilities_common.cli as clicommon @@ -193,8 +189,3 @@ def delete(address): config_db.connect() config_db.set_entry('TACPLUS_SERVER', address, None) tacacs.add_command(delete) - - -if __name__ == "__main__": - aaa() - diff --git a/config/config_mgmt.py b/config/config_mgmt.py index 6bca25a90911..d291a33ea0a9 100644 --- a/config/config_mgmt.py +++ b/config/config_mgmt.py @@ -77,7 +77,7 @@ def __init__(self, source="configDB", debug=False, allowTablesWithoutYang=True): except Exception as e: self.sysLog(doPrint=True, logLevel=syslog.LOG_ERR, msg=str(e)) - raise(Exception('ConfigMgmt Class creation failed')) + raise Exception('ConfigMgmt Class creation failed') return @@ -168,7 +168,7 @@ def readConfigDBJson(self, source=CONFIG_DB_JSON_FILE): self.configdbJsonIn = readJsonFile(source) #self.sysLog(msg=type(self.configdbJsonIn)) if not self.configdbJsonIn: - raise(Exception("Can not load config from config DB json file")) + raise Exception("Can not load config from config DB json file") self.sysLog(msg='Reading Input {}'.format(self.configdbJsonIn)) return @@ -247,7 +247,7 @@ def __init__(self, source="configDB", debug=False, allowTablesWithoutYang=True): except Exception as e: self.sysLog(doPrint=True, logLevel=syslog.LOG_ERR, msg=str(e)) - raise(Exception('ConfigMgmtDPB Class creation failed')) + raise Exception('ConfigMgmtDPB Class creation failed') return @@ -331,8 +331,7 @@ def _verifyAsicDB(self, db, ports, portMap, timeout): if waitTime + 1 == timeout: self.sysLog(syslog.LOG_CRIT, "!!! Critical Failure, Ports \ are not Deleted from ASIC DB, Bail Out !!!", doPrint=True) - raise(Exception("Ports are present in ASIC DB after {} secs".\ - format(timeout))) + raise Exception("Ports are present in ASIC DB after {} secs".format(timeout)) except Exception as e: self.sysLog(doPrint=True, logLevel=syslog.LOG_ERR, msg=str(e)) @@ -470,7 +469,7 @@ def _addPorts(self, portJson=dict(), loadDefConfig=True): (configToLoad, ret) (tuple)[dict, bool] ''' configToLoad = None - ports = portJson['PORT'].keys() + ports = list(portJson['PORT'].keys()) try: self.sysLog(doPrint=True, msg='Start Port Addition') self.sysLog(msg="addPorts Args portjson: {} loadDefConfig: {}".\ @@ -546,7 +545,7 @@ def _mergeItems(it1, it2): pass return - for it in D1.keys(): + for it in list(D1.keys()): # D2 has the key if D2.get(it): _mergeItems(D1[it], D2[it]) @@ -578,7 +577,7 @@ def _searchKeysInConfig(self, In, Out, skeys): ''' found = False if isinstance(In, dict): - for key in In.keys(): + for key in list(In.keys()): for skey in skeys: # pattern is very specific to current primary keys in # config DB, may need to be updated later. diff --git a/config/console.py b/config/console.py index 9b3142c03f9b..0a3cfbe5c8d2 100644 --- a/config/console.py +++ b/config/console.py @@ -1,7 +1,4 @@ -#!/usr/bin/env python - import click - import utilities_common.cli as clicommon # diff --git a/config/feature.py b/config/feature.py index b2271b279c47..3e19c45c455a 100644 --- a/config/feature.py +++ b/config/feature.py @@ -1,6 +1,6 @@ -import click import sys +import click from utilities_common.cli import AbbreviationGroup, pass_db # @@ -42,7 +42,7 @@ def feature_autorestart(db, name, autorestart): click.echo("Unable to retrieve feature table from Config DB.") sys.exit(1) - if not feature_table.has_key(name): + if name not in feature_table: click.echo("Unable to retrieve feature '{}'".format(name)) sys.exit(1) diff --git a/config/kube.py b/config/kube.py index 5726d7881d5f..08cca628d817 100644 --- a/config/kube.py +++ b/config/kube.py @@ -2,18 +2,17 @@ import os import shutil import tempfile +from urllib.parse import urlparse import click import netaddr import requests import urllib3 +import utilities_common.cli as clicommon import yaml -from urlparse import urlparse - from sonic_py_common import device_info from swsssdk import ConfigDBConnector from utilities_common.db import Db -import utilities_common.cli as clicommon from .utils import log diff --git a/config/main.py b/config/main.py index c3ddaf9db9f4..e803b74a238f 100755 --- a/config/main.py +++ b/config/main.py @@ -24,15 +24,15 @@ from .utils import log -import aaa -import console -import feature -import kube -import mlnx -import nat -import vlan -from config_mgmt import ConfigMgmtDPB -import chassis_modules +from . import aaa +from . import chassis_modules +from . import console +from . import feature +from . import kube +from . import mlnx +from . import nat +from . import vlan +from .config_mgmt import ConfigMgmtDPB CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?']) @@ -85,7 +85,7 @@ def _get_breakout_options(ctx, args, incomplete): else: breakout_file_input = readJsonFile(breakout_cfg_file) if interface_name in breakout_file_input[INTF_KEY]: - breakout_mode_list = [v["breakout_modes"] for i ,v in breakout_file_input[INTF_KEY].items() if i == interface_name][0] + breakout_mode_list = [v["breakout_modes"] for i, v in breakout_file_input[INTF_KEY].items() if i == interface_name][0] breakout_mode_options = [] for i in breakout_mode_list.split(','): breakout_mode_options.append(i) @@ -94,7 +94,7 @@ def _get_breakout_options(ctx, args, incomplete): def shutdown_interfaces(ctx, del_intf_dict): """ shut down all the interfaces before deletion """ - for intf in del_intf_dict.keys(): + for intf in list(del_intf_dict.keys()): config_db = ctx.obj['config_db'] if clicommon.get_interface_naming_mode() == "alias": interface_name = interface_alias_to_name(config_db, intf) @@ -111,7 +111,7 @@ def shutdown_interfaces(ctx, del_intf_dict): click.echo("port_dict is None!") return False - if intf in port_dict.keys(): + if intf in port_dict: config_db.mod_entry("PORT", intf, {"admin_status": "down"}) else: click.secho("[ERROR] Could not get the correct interface name, exiting", fg='red') @@ -141,7 +141,7 @@ def _validate_interface_mode(ctx, breakout_cfg_file, interface_name, target_brko return False # Check whether the user-selected interface is part of 'port' table in config db. - if interface_name not in port_dict.keys(): + if interface_name not in port_dict: click.secho("[ERROR] {} is not in port_dict".format(interface_name)) return False click.echo("\nRunning Breakout Mode : {} \nTarget Breakout Mode : {}".format(cur_brkout_mode, target_brkout_mode)) @@ -258,7 +258,7 @@ def _get_device_type(): """ command = "{} -m -v DEVICE_METADATA.localhost.type".format(SONIC_CFGGEN_PATH) - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) device_type, err = proc.communicate() if err: click.echo("Could not get the device type from minigraph, setting device type to Unknown") @@ -295,7 +295,7 @@ def interface_alias_to_name(config_db, interface_alias): if not port_dict: click.echo("port_dict is None!") raise click.Abort() - for port_name in port_dict.keys(): + for port_name in list(port_dict.keys()): if interface_alias == port_dict[port_name]['alias']: return port_name if sub_intf_sep_idx == -1 else port_name + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id @@ -326,15 +326,15 @@ def interface_name_is_valid(config_db, interface_name): if not port_dict: click.echo("port_dict is None!") raise click.Abort() - for port_name in port_dict.keys(): + for port_name in list(port_dict.keys()): if interface_name == port_name: return True if port_channel_dict: - for port_channel_name in port_channel_dict.keys(): + for port_channel_name in list(port_channel_dict.keys()): if interface_name == port_channel_name: return True if sub_port_intf_dict: - for sub_port_intf_name in sub_port_intf_dict.keys(): + for sub_port_intf_name in list(sub_port_intf_dict.keys()): if interface_name == sub_port_intf_name: return True return False @@ -357,7 +357,7 @@ def interface_name_to_alias(config_db, interface_name): if not port_dict: click.echo("port_dict is None!") raise click.Abort() - for port_name in port_dict.keys(): + for port_name in list(port_dict.keys()): if interface_name == port_name: return port_dict[port_name]['alias'] @@ -410,7 +410,7 @@ def get_port_namespace(port): if clicommon.get_interface_naming_mode() == "alias": port_dict = config_db.get_table(table_name) if port_dict: - for port_name in port_dict.keys(): + for port_name in list(port_dict.keys()): if port == port_dict[port_name]['alias']: return namespace else: @@ -427,8 +427,8 @@ def del_interface_bind_to_vrf(config_db, vrf_name): for table_name in tables: interface_dict = config_db.get_table(table_name) if interface_dict: - for interface_name in interface_dict.keys(): - if interface_dict[interface_name].has_key('vrf_name') and vrf_name == interface_dict[interface_name]['vrf_name']: + for interface_name in list(interface_dict.keys()): + if 'vrf_name' in interface_dict[interface_name] and vrf_name == interface_dict[interface_name]['vrf_name']: interface_dependent = interface_ipaddr_dependent_on_interface(config_db, interface_name) for interface_del in interface_dependent: config_db.set_entry(table_name, interface_del, None) @@ -459,7 +459,7 @@ def set_interface_naming_mode(mode): click.echo("port_dict is None!") raise click.Abort() - for port_name in port_dict.keys(): + for port_name in list(port_dict.keys()): try: if port_dict[port_name]['alias']: pass @@ -502,7 +502,7 @@ def _get_all_neighbor_ipaddresses(config_db): """ addrs = [] bgp_sessions = config_db.get_table('BGP_NEIGHBOR') - for addr, session in bgp_sessions.iteritems(): + for addr, session in bgp_sessions.items(): addrs.append(addr) return addrs @@ -512,8 +512,8 @@ def _get_neighbor_ipaddress_list_by_hostname(config_db, hostname): """ addrs = [] bgp_sessions = config_db.get_table('BGP_NEIGHBOR') - for addr, session in bgp_sessions.iteritems(): - if session.has_key('name') and session['name'] == hostname: + for addr, session in bgp_sessions.items(): + if 'name' in session and session['name'] == hostname: addrs.append(addr) return addrs @@ -639,7 +639,7 @@ def _get_disabled_services_list(config_db): feature_table = config_db.get_table('FEATURE') if feature_table is not None: - for feature_name in feature_table.keys(): + for feature_name in list(feature_table.keys()): if not feature_name: log.log_warning("Feature is None") continue @@ -750,8 +750,8 @@ def _restart_services(config_db): def interface_is_in_vlan(vlan_member_table, interface_name): - """ Check if an interface is in a vlan """ - for _,intf in vlan_member_table.keys(): + """ Check if an interface is in a vlan """ + for _, intf in list(vlan_member_table.keys()): if intf == interface_name: return True @@ -759,7 +759,7 @@ def interface_is_in_vlan(vlan_member_table, interface_name): def interface_is_in_portchannel(portchannel_member_table, interface_name): """ Check if an interface is part of portchannel """ - for _,intf in portchannel_member_table.keys(): + for _, intf in list(portchannel_member_table.keys()): if intf == interface_name: return True @@ -767,7 +767,7 @@ def interface_is_in_portchannel(portchannel_member_table, interface_name): def interface_has_mirror_config(mirror_table, interface_name): """ Check if port is already configured with mirror config """ - for _,v in mirror_table.items(): + for _, v in mirror_table.items(): if 'src_port' in v and v['src_port'] == interface_name: return True if 'dst_port' in v and v['dst_port'] == interface_name: @@ -874,12 +874,12 @@ def config(ctx): # Add groups from other modules config.add_command(aaa.aaa) config.add_command(aaa.tacacs) +config.add_command(chassis_modules.chassis_modules) config.add_command(console.console) config.add_command(feature.feature) config.add_command(kube.kubernetes) config.add_command(nat.nat) config.add_command(vlan.vlan) -config.add_command(chassis_modules.chassis_modules) @config.command() @click.option('-y', '--yes', is_flag=True, callback=_abort_if_false, @@ -1029,7 +1029,7 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart): if load_sysinfo: command = "{} -j {} -v DEVICE_METADATA.localhost.hwsku".format(SONIC_CFGGEN_PATH, filename) - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) cfg_hwsku, err = proc.communicate() if err: click.echo("Could not get the HWSKU from config file, exiting") @@ -1128,8 +1128,8 @@ def load_mgmt_config(filename): config_data = parse_device_desc_xml(filename) hostname = config_data['DEVICE_METADATA']['localhost']['hostname'] _change_hostname(hostname) - mgmt_conf = netaddr.IPNetwork(config_data['MGMT_INTERFACE'].keys()[0][1]) - gw_addr = config_data['MGMT_INTERFACE'].values()[0]['gwaddr'] + mgmt_conf = netaddr.IPNetwork(list(config_data['MGMT_INTERFACE'].keys())[0][1]) + gw_addr = list(config_data['MGMT_INTERFACE'].values())[0]['gwaddr'] command = "ifconfig eth0 {} netmask {}".format(str(mgmt_conf.ip), str(mgmt_conf.netmask)) clicommon.run_command(command, display_cmd=True) command = "ip route add default via {} dev eth0 table default".format(gw_addr) @@ -1719,7 +1719,7 @@ def warm_restart_enable(ctx, module): @click.pass_context def warm_restart_neighsyncd_timer(ctx, seconds): db = ctx.obj['db'] - if seconds not in range(1,9999): + if seconds not in range(1, 9999): ctx.fail("neighsyncd warm restart timer must be in range 1-9999") db.mod_entry('WARM_RESTART', 'swss', {'neighsyncd_timer': seconds}) @@ -1728,7 +1728,7 @@ def warm_restart_neighsyncd_timer(ctx, seconds): @click.pass_context def warm_restart_bgp_timer(ctx, seconds): db = ctx.obj['db'] - if seconds not in range(1,3600): + if seconds not in range(1, 3600): ctx.fail("bgp warm restart timer must be in range 1-3600") db.mod_entry('WARM_RESTART', 'bgp', {'bgp_timer': seconds}) @@ -1737,7 +1737,7 @@ def warm_restart_bgp_timer(ctx, seconds): @click.pass_context def warm_restart_teamsyncd_timer(ctx, seconds): db = ctx.obj['db'] - if seconds not in range(1,3600): + if seconds not in range(1, 3600): ctx.fail("teamsyncd warm restart timer must be in range 1-3600") db.mod_entry('WARM_RESTART', 'teamd', {'teamsyncd_timer': seconds}) @@ -1772,7 +1772,7 @@ def vrf_add_management_vrf(config_db): if entry and entry['mgmtVrfEnabled'] == 'true' : click.echo("ManagementVRF is already Enabled.") return None - config_db.mod_entry('MGMT_VRF_CONFIG',"vrf_global",{"mgmtVrfEnabled": "true"}) + config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "true"}) mvrf_restart_services() def vrf_delete_management_vrf(config_db): @@ -1782,7 +1782,7 @@ def vrf_delete_management_vrf(config_db): if not entry or entry['mgmtVrfEnabled'] == 'false' : click.echo("ManagementVRF is already Disabled.") return None - config_db.mod_entry('MGMT_VRF_CONFIG',"vrf_global",{"mgmtVrfEnabled": "false"}) + config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "false"}) mvrf_restart_services() @config.group(cls=clicommon.AbbreviationGroup) @@ -1856,11 +1856,11 @@ def modify_snmptrap_server(ctx, ver, serverip, port, vrf, comm): config_db = ctx.obj['db'] if ver == "1": #By default, v1TrapDest value in snmp.yml is "NotConfigured". Modify it. - config_db.mod_entry('SNMP_TRAP_CONFIG',"v1TrapDest",{"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) + config_db.mod_entry('SNMP_TRAP_CONFIG', "v1TrapDest", {"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) elif ver == "2": - config_db.mod_entry('SNMP_TRAP_CONFIG',"v2TrapDest",{"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) + config_db.mod_entry('SNMP_TRAP_CONFIG', "v2TrapDest", {"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) else: - config_db.mod_entry('SNMP_TRAP_CONFIG',"v3TrapDest",{"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) + config_db.mod_entry('SNMP_TRAP_CONFIG', "v3TrapDest", {"DestIp": serverip, "DestPort": port, "vrf": vrf, "Community": comm}) cmd="systemctl restart snmp" os.system (cmd) @@ -1873,11 +1873,11 @@ def delete_snmptrap_server(ctx, ver): config_db = ctx.obj['db'] if ver == "1": - config_db.mod_entry('SNMP_TRAP_CONFIG',"v1TrapDest",None) + config_db.mod_entry('SNMP_TRAP_CONFIG', "v1TrapDest", None) elif ver == "2": - config_db.mod_entry('SNMP_TRAP_CONFIG',"v2TrapDest",None) + config_db.mod_entry('SNMP_TRAP_CONFIG', "v2TrapDest", None) else: - config_db.mod_entry('SNMP_TRAP_CONFIG',"v3TrapDest",None) + config_db.mod_entry('SNMP_TRAP_CONFIG', "v3TrapDest", None) cmd="systemctl restart snmp" os.system (cmd) @@ -2124,17 +2124,17 @@ def startup(ctx, interface_name): log.log_info("'interface startup {}' executing...".format(interface_name)) port_dict = config_db.get_table('PORT') - for port_name in port_dict.keys(): + for port_name in list(port_dict.keys()): if port_name in intf_fs: config_db.mod_entry("PORT", port_name, {"admin_status": "up"}) portchannel_list = config_db.get_table("PORTCHANNEL") - for po_name in portchannel_list.keys(): + for po_name in list(portchannel_list.keys()): if po_name in intf_fs: config_db.mod_entry("PORTCHANNEL", po_name, {"admin_status": "up"}) subport_list = config_db.get_table("VLAN_SUB_INTERFACE") - for sp_name in subport_list.keys(): + for sp_name in list(subport_list.keys()): if sp_name in intf_fs: config_db.mod_entry("VLAN_SUB_INTERFACE", sp_name, {"admin_status": "up"}) @@ -2164,17 +2164,17 @@ def shutdown(ctx, interface_name): ctx.fail("Interface name is invalid. Please enter a valid interface name!!") port_dict = config_db.get_table('PORT') - for port_name in port_dict.keys(): + for port_name in list(port_dict.keys()): if port_name in intf_fs: config_db.mod_entry("PORT", port_name, {"admin_status": "down"}) portchannel_list = config_db.get_table("PORTCHANNEL") - for po_name in portchannel_list.keys(): + for po_name in list(portchannel_list.keys()): if po_name in intf_fs: config_db.mod_entry("PORTCHANNEL", po_name, {"admin_status": "down"}) subport_list = config_db.get_table("VLAN_SUB_INTERFACE") - for sp_name in subport_list.keys(): + for sp_name in list(subport_list.keys()): if sp_name in intf_fs: config_db.mod_entry("VLAN_SUB_INTERFACE", sp_name, {"admin_status": "down"}) @@ -2272,20 +2272,21 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load remains unchanged to limit the traffic impact """ click.secho("\nAfter running Logic to limit the impact", fg="cyan", underline=True) - matched_item = [intf for intf, speed in del_intf_dict.items() if intf in add_intf_dict.keys() and speed == add_intf_dict[intf]] + matched_items = [intf for intf in del_intf_dict if intf in add_intf_dict and del_intf_dict[intf] == add_intf_dict[intf]] # Remove the interface which remains unchanged from both del_intf_dict and add_intf_dict - map(del_intf_dict.pop, matched_item) - map(add_intf_dict.pop, matched_item) + for item in matched_items: + del_intf_dict.pop(item) + add_intf_dict.pop(item) click.secho("\nFinal list of ports to be deleted : \n {} \nFinal list of ports to be added : \n {}".format(json.dumps(del_intf_dict, indent=4), json.dumps(add_intf_dict, indent=4), fg='green', blink=True)) - if len(add_intf_dict.keys()) == 0: - click.secho("[ERROR] add_intf_dict is None! No interfaces are there to be added", fg='red') + if not add_intf_dict: + click.secho("[ERROR] add_intf_dict is None or empty! No interfaces are there to be added", fg='red') raise click.Abort() port_dict = {} for intf in add_intf_dict: - if intf in add_ports.keys(): + if intf in add_ports: port_dict[intf] = add_ports[intf] # writing JSON object @@ -2298,7 +2299,7 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load cm = load_ConfigMgmt(verbose) """ Delete all ports if forced else print dependencies using ConfigMgmt API """ - final_delPorts = [intf for intf in del_intf_dict.keys()] + final_delPorts = [intf for intf in list(del_intf_dict.keys())] """ Warn user if tables without yang models exist and have final_delPorts """ breakout_warnUser_extraTables(cm, final_delPorts, confirm=True) @@ -2306,24 +2307,21 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load portJson = dict(); portJson['PORT'] = port_dict # breakout_Ports will abort operation on failure, So no need to check return - breakout_Ports(cm, delPorts=final_delPorts, portJson=portJson, force=force_remove_dependencies, \ - loadDefConfig=load_predefined_config, verbose=verbose) + breakout_Ports(cm, delPorts=final_delPorts, portJson=portJson, force=force_remove_dependencies, + loadDefConfig=load_predefined_config, verbose=verbose) # Set Current Breakout mode in config DB brkout_cfg_keys = config_db.get_keys('BREAKOUT_CFG') if interface_name.decode("utf-8") not in brkout_cfg_keys: - click.secho("[ERROR] {} is not present in 'BREAKOUT_CFG' Table!".\ - format(interface_name), fg='red') + click.secho("[ERROR] {} is not present in 'BREAKOUT_CFG' Table!".format(interface_name), fg='red') raise click.Abort() - config_db.set_entry("BREAKOUT_CFG", interface_name,\ - {'brkout_mode': target_brkout_mode}) - click.secho("Breakout process got successfully completed.".\ - format(interface_name), fg="cyan", underline=True) + config_db.set_entry("BREAKOUT_CFG", interface_name, {'brkout_mode': target_brkout_mode}) + click.secho("Breakout process got successfully completed." + .format(interface_name), fg="cyan", underline=True) click.echo("Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`.") except Exception as e: - click.secho("Failed to break out Port. Error: {}".format(str(e)), \ - fg='magenta') + click.secho("Failed to break out Port. Error: {}".format(str(e)), fg='magenta') sys.exit(0) def _get_all_mgmtinterface_keys(): @@ -2331,7 +2329,7 @@ def _get_all_mgmtinterface_keys(): """ config_db = ConfigDBConnector() config_db.connect() - return config_db.get_table('MGMT_INTERFACE').keys() + return list(config_db.get_table('MGMT_INTERFACE').keys()) def mgmt_ip_restart_services(): """Restart the required services when mgmt inteface IP address is changed""" @@ -2431,7 +2429,7 @@ def add(ctx, interface_name, ip_addr, gw): ctx.fail("'interface_name' is None!") try: - net = ipaddress.ip_network(unicode(ip_addr), strict=False) + net = ipaddress.ip_network(ip_addr, strict=False) if '/' not in ip_addr: ip_addr = str(net) @@ -2492,7 +2490,7 @@ def remove(ctx, interface_name, ip_addr): ctx.fail("'interface_name' is None!") try: - net = ipaddress.ip_network(unicode(ip_addr), strict=False) + net = ipaddress.ip_network(ip_addr, strict=False) if '/' not in ip_addr: ip_addr = str(net) @@ -2708,7 +2706,7 @@ def route(ctx): """route-related configuration tasks""" pass -@route.command('add',context_settings={"ignore_unknown_options":True}) +@route.command('add', context_settings={"ignore_unknown_options":True}) @click.argument('command_str', metavar='prefix [vrf ] nexthop <[vrf ] >|>', nargs=-1, type=click.Path()) @click.pass_context def add_route(ctx, command_str): @@ -2719,7 +2717,7 @@ def add_route(ctx, command_str): ctx.fail("argument is incomplete, prefix not found!") if "nexthop" not in command_str: ctx.fail("argument is incomplete, nexthop not found!") - for i in range(0,len(command_str)): + for i in range(0, len(command_str)): if "nexthop" == command_str[i]: prefix_str = command_str[:i] nexthop_str = command_str[i:] @@ -2760,7 +2758,7 @@ def add_route(ctx, command_str): cmd += '"' clicommon.run_command(cmd) -@route.command('del',context_settings={"ignore_unknown_options":True}) +@route.command('del', context_settings={"ignore_unknown_options":True}) @click.argument('command_str', metavar='prefix [vrf ] nexthop <[vrf ] >|>', nargs=-1, type=click.Path()) @click.pass_context def del_route(ctx, command_str): @@ -2771,7 +2769,7 @@ def del_route(ctx, command_str): ctx.fail("argument is incomplete, prefix not found!") if "nexthop" not in command_str: ctx.fail("argument is incomplete, nexthop not found!") - for i in range(0,len(command_str)): + for i in range(0, len(command_str)): if "nexthop" == command_str[i]: prefix_str = command_str[:i] nexthop_str = command_str[i:] @@ -3226,7 +3224,7 @@ def add_loopback(ctx, loopback_name): ctx.fail("{} is invalid, name should have prefix '{}' and suffix '{}' " .format(loopback_name, CFG_LOOPBACK_PREFIX, CFG_LOOPBACK_NO)) - lo_intfs = [k for k,v in config_db.get_table('LOOPBACK_INTERFACE').iteritems() if type(k) != tuple] + lo_intfs = [k for k, v in config_db.get_table('LOOPBACK_INTERFACE').items() if type(k) != tuple] if loopback_name in lo_intfs: ctx.fail("{} already exists".format(loopback_name)) @@ -3242,7 +3240,7 @@ def del_loopback(ctx, loopback_name): .format(loopback_name, CFG_LOOPBACK_PREFIX, CFG_LOOPBACK_NO)) lo_config_db = config_db.get_table('LOOPBACK_INTERFACE') - lo_intfs = [k for k,v in lo_config_db.iteritems() if type(k) != tuple] + lo_intfs = [k for k, v in lo_config_db.items() if type(k) != tuple] if loopback_name not in lo_intfs: ctx.fail("{} does not exists".format(loopback_name)) @@ -3420,7 +3418,7 @@ def enable(ctx): config_db.mod_entry('SFLOW', 'global', sflow_tbl['global']) try: - proc = subprocess.Popen("systemctl is-active sflow", shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen("systemctl is-active sflow", shell=True, text=True, stdout=subprocess.PIPE) (out, err) = proc.communicate() except SystemExit as e: ctx.fail("Unable to check sflow status {}".format(e)) @@ -3495,7 +3493,7 @@ def enable(ctx, ifname): intf_dict = config_db.get_table('SFLOW_SESSION') - if intf_dict and ifname in intf_dict.keys(): + if intf_dict and ifname in intf_dict: intf_dict[ifname]['admin_state'] = 'up' config_db.mod_entry('SFLOW_SESSION', ifname, intf_dict[ifname]) else: @@ -3515,7 +3513,7 @@ def disable(ctx, ifname): intf_dict = config_db.get_table('SFLOW_SESSION') - if intf_dict and ifname in intf_dict.keys(): + if intf_dict and ifname in intf_dict: intf_dict[ifname]['admin_state'] = 'down' config_db.mod_entry('SFLOW_SESSION', ifname, intf_dict[ifname]) else: @@ -3540,7 +3538,7 @@ def sample_rate(ctx, ifname, rate): sess_dict = config_db.get_table('SFLOW_SESSION') - if sess_dict and ifname in sess_dict.keys(): + if sess_dict and ifname in sess_dict: sess_dict[ifname]['sample_rate'] = rate config_db.mod_entry('SFLOW_SESSION', ifname, sess_dict[ifname]) else: @@ -3596,7 +3594,7 @@ def add(ctx, name, ipaddr, port, vrf): config_db = ctx.obj['db'] collector_tbl = config_db.get_table('SFLOW_COLLECTOR') - if (collector_tbl and name not in collector_tbl.keys() and len(collector_tbl) == 2): + if (collector_tbl and name not in collector_tbl and len(collector_tbl) == 2): click.echo("Only 2 collectors can be configured, please delete one") return @@ -3616,7 +3614,7 @@ def del_collector(ctx, name): config_db = ctx.obj['db'] collector_tbl = config_db.get_table('SFLOW_COLLECTOR') - if name not in collector_tbl.keys(): + if name not in collector_tbl: click.echo("Collector: {} not configured".format(name)) return @@ -3649,7 +3647,7 @@ def add(ctx, ifname): if not sflow_tbl: sflow_tbl = {'global': {'admin_state': 'down'}} - if 'agent_id' in sflow_tbl['global'].keys(): + if 'agent_id' in sflow_tbl['global']: click.echo("Agent already configured. Please delete it first.") return @@ -3669,7 +3667,7 @@ def delete(ctx): if not sflow_tbl: sflow_tbl = {'global': {'admin_state': 'down'}} - if 'agent_id' not in sflow_tbl['global'].keys(): + if 'agent_id' not in sflow_tbl['global']: click.echo("sFlow agent not configured.") return diff --git a/config/mlnx.py b/config/mlnx.py index 8c97d3b38236..c8c9cfb0c238 100644 --- a/config/mlnx.py +++ b/config/mlnx.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # diff --git a/config/nat.py b/config/nat.py index e6a67f33d283..652baec906fa 100644 --- a/config/nat.py +++ b/config/nat.py @@ -1,10 +1,10 @@ -#!/usr/bin/env python +import ipaddress import click -import ipaddress from swsssdk import ConfigDBConnector from swsscommon.swsscommon import SonicV2Connector + def is_valid_ipv4_address(address): """Check if the given ipv4 address is valid""" invalid_list = ['0.0.0.0','255.255.255.255'] @@ -24,7 +24,7 @@ def is_valid_port_address(address): except ValueError: return False - if port_address not in xrange(1, 65535): + if port_address not in range(1, 65535): return False return True @@ -77,7 +77,7 @@ def isIpOverlappingWithAnyStaticEntry(ipAddress, table): else: continue elif table == 'STATIC_NAT': - if isinstance(key, unicode) is True: + if isinstance(key, str) is True: global_ip = key else: continue @@ -107,15 +107,15 @@ def isOverlappingWithAnyDynamicEntry(ipAddress): if not nat_pool_dict: return False - for values in nat_pool_dict.values(): + for values in list(nat_pool_dict.values()): global_ip = values["nat_ip"] ipAddr = global_ip.split('-') if (len(ipAddr) == 1): - startIp = int(ipaddress.IPv4Address(unicode(ipAddr[0]))) - endIp = int(ipaddress.IPv4Address(unicode(ipAddr[0]))) + startIp = int(ipaddress.IPv4Address(ipAddr[0])) + endIp = int(ipaddress.IPv4Address(ipAddr[0])) else: - startIp = int(ipaddress.IPv4Address(unicode(ipAddr[0]))) - endIp = int(ipaddress.IPv4Address(unicode(ipAddr[1]))) + startIp = int(ipaddress.IPv4Address(ipAddr[0])) + endIp = int(ipaddress.IPv4Address(ipAddr[1])) if ((ip >= startIp) and (ip <= endIp)): return True @@ -604,7 +604,7 @@ def remove_static_all(ctx): for table_name in tables: table_dict = config_db.get_table(table_name) if table_dict: - for table_key_name in table_dict.keys(): + for table_key_name in list(table_dict.keys()): config_db.set_entry(table_name, table_key_name, None) # @@ -689,12 +689,12 @@ def add_pool(ctx, pool_name, global_ip_range, global_port_range): if entryFound == False: static_dict = config_db.get_table('STATIC_NAT') if static_dict: - for staticKey,staticValues in static_dict.items(): + for staticKey, staticValues in static_dict.items(): global_ip = "---" local_ip = "---" nat_type = "dnat" - if isinstance(staticKey, unicode) is True: + if isinstance(staticKey, str) is True: global_ip = staticKey else: continue @@ -707,7 +707,7 @@ def add_pool(ctx, pool_name, global_ip_range, global_port_range): if nat_type == "snat": global_ip = local_ip - ipAddress = int(ipaddress.IPv4Address(unicode(global_ip))) + ipAddress = int(ipaddress.IPv4Address(global_ip)) if (ipAddress >= ipLowLimit and ipAddress <= ipHighLimit): ctx.fail("Given Ip address entry is overlapping with existing Static NAT entry !!") @@ -828,7 +828,7 @@ def remove_pools(ctx): binding_dict = config_db.get_table(binding_table_name) pool_dict = config_db.get_table(pool_table_name) if pool_dict: - for pool_key_name in pool_dict.keys(): + for pool_key_name in list(pool_dict.keys()): entryFound = False for binding_name, binding_values in binding_dict.items(): if binding_values['nat_pool'] == pool_key_name: @@ -880,7 +880,7 @@ def remove_bindings(ctx): binding_table_name = 'NAT_BINDINGS' binding_dict = config_db.get_table(binding_table_name) if binding_dict: - for binding_key_name in binding_dict.keys(): + for binding_key_name in list(binding_dict.keys()): config_db.set_entry(binding_table_name, binding_key_name, None) # @@ -961,8 +961,8 @@ def remove_interfaces(ctx): for table_name in tables: table_dict = config_db.get_table(table_name) if table_dict: - for table_key_name in table_dict.keys(): - if isinstance(table_key_name, unicode) is False: + for table_key_name in list(table_dict.keys()): + if isinstance(table_key_name, str) is False: continue config_db.set_entry(table_name, table_key_name, nat_config) @@ -1075,7 +1075,3 @@ def udp_timeout(ctx): seconds = 300 config_db.mod_entry("NAT_GLOBAL", "Values", {"nat_udp_timeout": seconds}) - -if __name__ == "__main__": - nat() - diff --git a/connect/main.py b/connect/main.py index 939db4b0c890..38cd136d7882 100755 --- a/connect/main.py +++ b/connect/main.py @@ -1,13 +1,8 @@ -import click +import configparser import os import pexpect -try: - # noinspection PyPep8Naming - import ConfigParser as configparser -except ImportError: - # noinspection PyUnresolvedReferences - import configparser +import click # This is from the aliases example: diff --git a/consutil/lib.py b/consutil/lib.py index 0f5a697bd3f7..4ba64770145b 100644 --- a/consutil/lib.py +++ b/consutil/lib.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # # lib.py # @@ -276,7 +275,7 @@ def list_console_ttys(): cmd = "ls " + SysInfoProvider.DEVICE_PREFIX + "*" output, _ = SysInfoProvider.run_command(cmd, abort=False) ttys = output.split('\n') - ttys = list(filter(lambda dev: re.match(SysInfoProvider.DEVICE_PREFIX + r"\d+", dev) != None, ttys)) + ttys = list([dev for dev in ttys if re.match(SysInfoProvider.DEVICE_PREFIX + r"\d+", dev) != None]) return ttys @staticmethod @@ -292,8 +291,8 @@ def get_active_console_process_info(pid): cmd = 'ps -p {} -o pid,lstart,cmd | grep -E "(mini|pico)com"'.format(pid) output = SysInfoProvider.run_command(cmd) processes = SysInfoProvider._parse_processes_info(output) - if len(processes.keys()) == 1: - return (processes.keys()[0],) + list(processes.values())[0] + if len(list(processes.keys())) == 1: + return (list(processes.keys())[0],) + list(processes.values())[0] else: return None @@ -324,7 +323,7 @@ def _parse_processes_info(output): @staticmethod def run_command(cmd, abort=True): """runs command, exit if stderr is written to and abort argument is ture, returns stdout, stderr otherwise""" - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True) output = proc.stdout.read() error = proc.stderr.read() if abort and error != "": diff --git a/consutil/main.py b/consutil/main.py index 8775b1d32ffe..535ec413c278 100644 --- a/consutil/main.py +++ b/consutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -12,7 +12,7 @@ import utilities_common.cli as clicommon from tabulate import tabulate - from lib import * + from .lib import * except ImportError as e: raise ImportError("%s - required module not found" % str(e)) diff --git a/crm/main.py b/crm/main.py index f3b59b32886f..37e0b8eb12c5 100644 --- a/crm/main.py +++ b/crm/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import click import swsssdk diff --git a/debug/main.py b/debug/main.py index b91d22e78af9..8c502c96ad08 100755 --- a/debug/main.py +++ b/debug/main.py @@ -3,7 +3,7 @@ def run_command(command, pager=False): click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green')) - p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + p = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) output = p.stdout.read() if pager: click.echo_via_pager(output) @@ -22,7 +22,7 @@ def cli(): pass -p = subprocess.check_output(["sudo vtysh -c 'show version'"], shell=True) +p = subprocess.check_output(["sudo vtysh -c 'show version'"], shell=True, text=True) if 'FRRouting' in p: # # 'bgp' group for FRR ### diff --git a/fdbutil/filter_fdb_entries.py b/fdbutil/filter_fdb_entries.py index 736827d34ea4..2c65d8688e27 100755 --- a/fdbutil/filter_fdb_entries.py +++ b/fdbutil/filter_fdb_entries.py @@ -27,14 +27,14 @@ def get_vlan_cidr_map(filename): config_db_entries = json.load(fp) vlan_cidr = defaultdict() - if "VLAN_INTERFACE" in config_db_entries.keys() and "VLAN" in config_db_entries.keys(): - for vlan_key in config_db_entries["VLAN_INTERFACE"].keys(): + if "VLAN_INTERFACE" in config_db_entries and "VLAN" in config_db_entries: + for vlan_key in list(config_db_entries["VLAN_INTERFACE"].keys()): if '|' not in vlan_key: continue vlan, cidr = tuple(vlan_key.split('|')) if vlan in config_db_entries["VLAN"]: if vlan not in vlan_cidr: - vlan_cidr[vlan] = {4: ip_address("0.0.0.0".decode()), 6: ip_address("::".decode())} + vlan_cidr[vlan] = {4: ip_address("0.0.0.0"), 6: ip_address("::")} vlan_cidr[vlan][ip_interface(cidr).version] = ip_interface(cidr).network return vlan_cidr @@ -64,9 +64,9 @@ def get_arp_entries_map(arp_filename, config_db_filename): if "NEIGH_TABLE" not in key: continue table, vlan, ip = tuple(key.split(':')) - if "NEIGH_TABLE" in table and vlan in vlan_cidr.keys() \ + if "NEIGH_TABLE" in table and vlan in vlan_cidr \ and ip_address(ip) in ip_network(vlan_cidr[vlan][ip_interface(ip).version]) \ - and "neigh" in config.keys(): + and "neigh" in config: arp_map[config["neigh"].replace(':', '-').upper()] = "" return arp_map diff --git a/fwutil/lib.py b/fwutil/lib.py index 71f4d1ae8526..d54b1fa8ad1b 100755 --- a/fwutil/lib.py +++ b/fwutil/lib.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # # lib.py # @@ -11,16 +10,16 @@ import socket import subprocess import time - import urllib from collections import OrderedDict + from urllib.parse import urlparse + from urllib.request import urlopen, urlretrieve import click - from log import LogHelper from sonic_py_common import device_info from tabulate import tabulate - from urlparse import urlparse from . import Platform + from .log import LogHelper except ImportError as e: raise ImportError("Required module not found: {}".format(str(e))) @@ -96,7 +95,7 @@ def __validate(self): # Check URL existence try: - urlfile = urllib.urlopen(self.__url) + urlfile = urlopen(self.__url) response_code = urlfile.getcode() except IOError: raise RuntimeError("Did not receive a response from remote machine") @@ -124,7 +123,7 @@ def retrieve(self): socket.setdefaulttimeout(self.DOWNLOAD_TIMEOUT) try: - filename, headers = urllib.urlretrieve( + filename, headers = urlretrieve( self.__url, self.DOWNLOAD_PATH_TEMPLATE.format(basename), self.__reporthook @@ -222,13 +221,13 @@ def __init__(self): def get_current_image(self): cmd = "sonic-installer list | grep 'Current: ' | cut -f2 -d' '" - output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True) + output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True, text=True) return output.rstrip(NEWLINE) def get_next_image(self): cmd = "sonic-installer list | grep 'Next: ' | cut -f2 -d' '" - output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True) + output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True, text=True) return output.rstrip(NEWLINE) @@ -304,7 +303,7 @@ def __get_platform_components_path(self, root_path): ) def __is_str(self, obj): - return isinstance(obj, unicode) or isinstance(obj, str) + return isinstance(obj, str) def __is_dict(self, obj): return isinstance(obj, dict) @@ -418,15 +417,20 @@ def __parse_module_section(self, module): self.__module_component_map[key] = OrderedDict() self.__parse_component_section(key, value[self.COMPONENT_KEY], True) + # TODO: This function should not be necessary once we no longer support Python 2 def __deunicodify_hook(self, pairs): new_pairs = [ ] for key, value in pairs: - if isinstance(key, unicode): + try: key = key.encode(self.UTF8_ENCODING) + except Exception: + pass - if isinstance(value, unicode): + try: value = value.encode(self.UTF8_ENCODING) + except Exception: + pass new_pairs.append((key, value)) @@ -504,7 +508,7 @@ def __diff_keys(self, keys1, keys2): return set(keys1) ^ set(keys2) def __validate_component_map(self, section, pdp_map, pcp_map): - diff_keys = self.__diff_keys(pdp_map.keys(), pcp_map.keys()) + diff_keys = self.__diff_keys(list(pdp_map.keys()), list(pcp_map.keys())) if diff_keys: raise RuntimeError( @@ -514,8 +518,8 @@ def __validate_component_map(self, section, pdp_map, pcp_map): ) ) - for key in pdp_map.keys(): - diff_keys = self.__diff_keys(pdp_map[key].keys(), pcp_map[key].keys()) + for key in list(pdp_map.keys()): + diff_keys = self.__diff_keys(list(pdp_map[key].keys()), list(pcp_map[key].keys())) if diff_keys: raise RuntimeError( diff --git a/fwutil/log.py b/fwutil/log.py index 0a37eb301344..f9dc03345325 100755 --- a/fwutil/log.py +++ b/fwutil/log.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # # log.py # diff --git a/fwutil/main.py b/fwutil/main.py index 703a5fa9f392..4e4b1276425a 100755 --- a/fwutil/main.py +++ b/fwutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -9,9 +9,9 @@ import os import click - from lib import PlatformDataProvider, ComponentStatusProvider, ComponentUpdateProvider - from lib import URL, SquashFs - from log import LogHelper + from .lib import PlatformDataProvider, ComponentStatusProvider, ComponentUpdateProvider + from .lib import URL, SquashFs + from .log import LogHelper except ImportError as e: raise ImportError("Required module not found: {}".format(str(e))) diff --git a/pcieutil/main.py b/pcieutil/main.py index ca1dd0fb9604..30174a925104 100644 --- a/pcieutil/main.py +++ b/pcieutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -31,7 +31,7 @@ def print_result(name, result): string = "PCI Device: {} ".format(name) length = 105-len(string) sys.stdout.write(string) - for i in xrange(int(length)): + for i in range(int(length)): sys.stdout.write("-") click.echo(' [%s]' % result) diff --git a/pddf_fanutil/main.py b/pddf_fanutil/main.py index 1aa90b942451..3620be42f910 100644 --- a/pddf_fanutil/main.py +++ b/pddf_fanutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -149,7 +149,7 @@ def numfans(): @click.option('-i', '--index', default=-1, type=int, help="the index of FAN") def status(index): """Display FAN status""" - supported_fan = range(1, _wrapper_get_num_fans()+1) + supported_fan = list(range(1, _wrapper_get_num_fans()+1)) fan_ids = [] if (index < 0): fan_ids = supported_fan @@ -182,7 +182,7 @@ def status(index): @click.option('-i', '--index', default=-1, type=int, help="the index of FAN") def direction(index): """Display FAN airflow direction""" - supported_fan = range(1, _wrapper_get_num_fans() + 1) + supported_fan = list(range(1, _wrapper_get_num_fans() + 1)) fan_ids = [] if (index < 0): fan_ids = supported_fan @@ -209,7 +209,7 @@ def direction(index): @click.option('-i', '--index', default=-1, type=int, help="the index of FAN") def getspeed(index): """Display FAN speed in RPM""" - supported_fan = range(1, _wrapper_get_num_fans() + 1) + supported_fan = list(range(1, _wrapper_get_num_fans() + 1)) fan_ids = [] if (index < 0): fan_ids = supported_fan diff --git a/pddf_ledutil/main.py b/pddf_ledutil/main.py index 6ff08ffa9758..c7bfb71314d0 100644 --- a/pddf_ledutil/main.py +++ b/pddf_ledutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # diff --git a/pddf_psuutil/main.py b/pddf_psuutil/main.py index f916883828be..4662eb3534b5 100644 --- a/pddf_psuutil/main.py +++ b/pddf_psuutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -185,7 +185,7 @@ def numpsus(): @click.option('-i', '--index', default=-1, type=int, help="the index of PSU") def status(index): """Display PSU status""" - supported_psu = range(1, _wrapper_get_num_psus() + 1) + supported_psu = list(range(1, _wrapper_get_num_psus() + 1)) psu_ids = [] if (index < 0): psu_ids = supported_psu @@ -218,7 +218,7 @@ def status(index): @click.option('-i', '--index', default=-1, type=int, help="the index of PSU") def mfrinfo(index): """Display PSU manufacturer info""" - supported_psu = range(1, _wrapper_get_num_psus() + 1) + supported_psu = list(range(1, _wrapper_get_num_psus() + 1)) psu_ids = [] if (index < 0): psu_ids = supported_psu @@ -251,7 +251,7 @@ def mfrinfo(index): @click.option('-i', '--index', default=-1, type=int, help="the index of PSU") def seninfo(index): """Display PSU sensor info""" - supported_psu = range(1, _wrapper_get_num_psus() + 1) + supported_psu = list(range(1, _wrapper_get_num_psus() + 1)) psu_ids = [] if (index < 0): psu_ids = supported_psu diff --git a/pddf_thermalutil/main.py b/pddf_thermalutil/main.py index ee327696350e..976e5b4f2f8c 100644 --- a/pddf_thermalutil/main.py +++ b/pddf_thermalutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -98,7 +98,7 @@ def numthermals(): @click.option('-i', '--index', default=-1, type=int, help="the index of Temp Sensor") def gettemp(index): """Display Temperature values of thermal sensors""" - supported_thermal = range(1, _wrapper_get_num_thermals()+ 1) + supported_thermal = list(range(1, _wrapper_get_num_thermals()+ 1)) thermal_ids = [] if (index < 0): thermal_ids = supported_thermal @@ -159,7 +159,7 @@ def debug(): def dump_sysfs(): """Dump all Temp Sensor related SysFS paths""" if platform_chassis is not None: - supported_thermal = range(1, _wrapper_get_num_thermals()+ 1) + supported_thermal = list(range(1, _wrapper_get_num_thermals()+ 1)) for index in supported_thermal: status = platform_chassis.get_thermal(index-1).dump_sysfs() else: diff --git a/pfc/main.py b/pfc/main.py index 856c16ce9552..ffcc42a97a70 100644 --- a/pfc/main.py +++ b/pfc/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import click import swsssdk diff --git a/pfcwd/main.py b/pfcwd/main.py index 1076fa5e1b71..c608c05beb65 100644 --- a/pfcwd/main.py +++ b/pfcwd/main.py @@ -45,8 +45,8 @@ ('RESTORATION TIME', 'restoration_time', 'infinite') ] -STATS_HEADER = ('QUEUE', 'STATUS',) + zip(*STATS_DESCRIPTION)[0] -CONFIG_HEADER = ('PORT',) + zip(*CONFIG_DESCRIPTION)[0] +STATS_HEADER = ('QUEUE', 'STATUS',) + list(zip(*STATS_DESCRIPTION))[0] +CONFIG_HEADER = ('PORT',) + list(zip(*CONFIG_DESCRIPTION))[0] CONFIG_DB_PFC_WD_TABLE_NAME = 'PFC_WD' @@ -59,7 +59,7 @@ def cli(): def get_all_queues(db, namespace=None, display=constants.DISPLAY_ALL): queue_names = db.get_all(db.COUNTERS_DB, 'COUNTERS_QUEUE_NAME_MAP') - queues = queue_names.keys() if queue_names else {} + queues = list(queue_names.keys()) if queue_names else {} if display == constants.DISPLAY_ALL: return natsorted(queues) # filter the backend ports @@ -77,7 +77,7 @@ def get_all_ports(db, namespace=None, display=constants.DISPLAY_ALL): for i in all_port_names: if i.startswith('Ethernet'): port_names[i] = all_port_names[i] - display_ports = port_names.keys() + display_ports = list(port_names.keys()) if display == constants.DISPLAY_EXTERNAL: display_ports = get_external_ports(display_ports, namespace) return natsorted(display_ports) @@ -86,14 +86,14 @@ def get_all_ports(db, namespace=None, display=constants.DISPLAY_ALL): def get_server_facing_ports(db): candidates = db.get_table('DEVICE_NEIGHBOR') server_facing_ports = [] - for port in candidates.keys(): + for port in list(candidates.keys()): neighbor = db.get_entry( 'DEVICE_NEIGHBOR_METADATA', candidates[port]['name'] ) if neighbor and neighbor['type'].lower() == 'server': server_facing_ports.append(port) if not server_facing_ports: - server_facing_ports = [p[1] for p in db.get_table('VLAN_MEMBER').keys()] + server_facing_ports = [p[1] for p in list(db.get_table('VLAN_MEMBER').keys())] return server_facing_ports @@ -350,13 +350,13 @@ def start_default(self): # Get active ports from Config DB active_ports = natsorted( - self.config_db.get_table('DEVICE_NEIGHBOR').keys() + list(self.config_db.get_table('DEVICE_NEIGHBOR').keys()) ) if not enable or enable.lower() != "enable": return - port_num = len(self.config_db.get_table('PORT').keys()) + port_num = len(list(self.config_db.get_table('PORT').keys())) # Paramter values positively correlate to the number of ports. multiply = max(1, (port_num-1)/DEFAULT_PORT_NUM+1) diff --git a/psuutil/main.py b/psuutil/main.py index efea5509628e..9e03b4ffd956 100644 --- a/psuutil/main.py +++ b/psuutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -96,7 +96,7 @@ def numpsus(): @click.option('-i', '--index', default=-1, type=int, help="the index of PSU") def status(index): """Display PSU status""" - supported_psu = range(1, platform_psuutil.get_num_psus() + 1) + supported_psu = list(range(1, platform_psuutil.get_num_psus() + 1)) psu_ids = [] if (index < 0): psu_ids = supported_psu diff --git a/scripts/aclshow b/scripts/aclshow index 7e4c42a4359a..86cca06044e7 100755 --- a/scripts/aclshow +++ b/scripts/aclshow @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ using aclshow to display SONiC switch acl rules and counters @@ -17,8 +17,6 @@ optional arguments: -t TABLES, --tables TABLES action by specific tables list: Table_1,Table_2 """ -from __future__ import print_function - import argparse import json import os @@ -94,7 +92,7 @@ class AclStat(object): """ def lowercase_keys(dictionary): - return dict((k.lower(), v) for k,v in dictionary.iteritems()) if dictionary else None + return dict((k.lower(), v) for k, v in dictionary.items()) if dictionary else None def fetch_acl_tables(): """ @@ -189,19 +187,19 @@ class AclStat(object): clear counters -- write current counters to file in /tmp """ def remap_keys(dict): - return [{'key':k, 'value': v} for k, v in dict.iteritems()] + return [{'key': k, 'value': v} for k, v in dict.items()] - with open(COUNTER_POSITION, 'wb') as fp: + with open(COUNTER_POSITION, 'w') as fp: json.dump(remap_keys(self.acl_counters), fp) def main(): parser = argparse.ArgumentParser(description='Display SONiC switch Acl Rules and Counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-a', '--all', action='store_true', help='Show all ACL counters') parser.add_argument('-c', '--clear', action='store_true', help='Clear ACL counters statistics') parser.add_argument('-r', '--rules', type=str, help='action by specific rules list: Rule1_Name,Rule2_Name', default=None) parser.add_argument('-t', '--tables', type=str, help='action by specific tables list: Table1_Name,Table2_Name', default=None) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') parser.add_argument('-vv', '--verbose', action='store_true', help='Verbose output', default=False) args = parser.parse_args() diff --git a/scripts/boot_part b/scripts/boot_part index e9e51a936203..f41950e03435 100755 --- a/scripts/boot_part +++ b/scripts/boot_part @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ## Script to list partition information and set the boot partition import re diff --git a/scripts/configlet b/scripts/configlet index 9837a0362e5c..d2d2e27b4d1e 100755 --- a/scripts/configlet +++ b/scripts/configlet @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ JSON based configlet update A tool to update CONFIG-DB with JSON diffs that can update/delete redis-DB. @@ -75,10 +75,10 @@ A sample for update: """ -from __future__ import print_function import argparse import json import time + from swsssdk import ConfigDBConnector test_only = False @@ -141,21 +141,21 @@ def do_update(t, k, lst): if test_only == False: db_update(t, k, lst) else: - print ("TEST update") - print ("table: " + t) - print ("key: " + str(k)) - print (lst) - print ("---------------------") + print("TEST update") + print("table: " + t) + print("key: " + str(k)) + print(lst) + print("---------------------") def do_delete(t, k, lst): if test_only == False: db_delete(t, k, lst) else: - print ("TEST delete") - print ("table: " + t) - print ("key: " + str(k)) - print (lst) - print ("---------------------") + print("TEST delete") + print("table: " + t) + print("key: " + str(k)) + print(lst) + print("---------------------") def do_operate(op_upd, t, k, lst): @@ -194,7 +194,7 @@ def main(): do_act = test_only | parse_only | do_update | do_delete if not do_act: - print ("Expect an action update/delete or for debug parse/test\n") + print("Expect an action update/delete or for debug parse/test\n") parser.print_help() exit(-1) diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index 326eed41f7fc..fc2d993d993b 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import argparse import json @@ -64,7 +64,7 @@ def migrate_pfc_wd_table(self): Migrate all data entries from table PFC_WD_TABLE to PFC_WD ''' data = self.configDB.get_table('PFC_WD_TABLE') - for key in data.keys(): + for key in list(data.keys()): self.configDB.set_entry('PFC_WD', key, data[key]) self.configDB.delete_table('PFC_WD_TABLE') @@ -92,14 +92,14 @@ def migrate_interface_table(self): } for table in if_tables: data = self.configDB.get_table(table) - for key in data.keys(): + for key in list(data.keys()): if not self.is_ip_prefix_in_key(key): if_db.append(key) continue for table in if_tables: data = self.configDB.get_table(table) - for key in data.keys(): + for key in list(data.keys()): if not self.is_ip_prefix_in_key(key) or key[0] in if_db: continue log.log_info('Migrating interface table for ' + key[0]) diff --git a/scripts/decode-syseeprom b/scripts/decode-syseeprom index 2dc892f2aa9b..581edd84477d 100755 --- a/scripts/decode-syseeprom +++ b/scripts/decode-syseeprom @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ############################################################################# # diff --git a/scripts/dropconfig b/scripts/dropconfig index 950706df4443..066aadc23226 100755 --- a/scripts/dropconfig +++ b/scripts/dropconfig @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -91,14 +91,14 @@ class DropConfig(object): return table = [] - for counter, capabilities in device_caps.iteritems(): + for counter, capabilities in device_caps.items(): table.append((counter, capabilities.get('count', 'N/A'))) print(tabulate(table, drop_counter_capability_header, tablefmt='simple', stralign='left')) - for counter, capabilities in device_caps.iteritems(): + for counter, capabilities in device_caps.items(): supported_reasons = deserialize_reason_list(capabilities.get('reasons', '')) if supported_reasons and int(capabilities.get('count', 0)) > 0: print('\n{}'.format(counter)) @@ -252,7 +252,7 @@ class DropConfig(object): config_table = self.config_db.get_table(DEBUG_COUNTER_CONFIG_TABLE) config = [] - for counter_name, counter_attributes in sorted(config_table.iteritems()): + for counter_name, counter_attributes in sorted(config_table.items()): if group and counter_attributes.get('group', '') != group: continue @@ -328,13 +328,15 @@ def deserialize_reason_list(list_str): def main(): parser = argparse.ArgumentParser(description='Manage drop counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: dropconfig """) + # Version + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') + # Actions parser.add_argument('-c', '--command', type=str, help='Desired action to perform') diff --git a/scripts/dropstat b/scripts/dropstat index 808913805dae..bddd3fa0b237 100755 --- a/scripts/dropstat +++ b/scripts/dropstat @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -11,16 +11,16 @@ # - Refactor calls to COUNTERS_DB to reduce redundancy # - Cache DB queries to reduce # of expensive queries +import _pickle as pickle import argparse -import swsssdk import os -import sys -import cPickle as pickle import socket +import sys -from tabulate import tabulate +import swsssdk from collections import OrderedDict from natsort import natsorted +from tabulate import tabulate # mock the redis for unit test purposes # try: @@ -112,9 +112,9 @@ class DropStat(object): try: pickle.dump(self.get_counts_table(self.gather_counters(std_port_rx_counters + std_port_tx_counters, DEBUG_COUNTER_PORT_STAT_MAP), COUNTERS_PORT_NAME_MAP), - open(self.port_drop_stats_file, 'w+')) + open(self.port_drop_stats_file, 'wb+')) pickle.dump(self.get_counts(self.gather_counters([], DEBUG_COUNTER_SWITCH_STAT_MAP), self.get_switch_id()), - open(self.switch_drop_stats_file, 'w+')) + open(self.switch_drop_stats_file, 'wb+')) except IOError as e: print(e) sys.exit(e.errno) @@ -129,7 +129,7 @@ class DropStat(object): # Grab the latest clear checkpoint, if it exists if os.path.isfile(self.port_drop_stats_file): - port_drop_ckpt = pickle.load(open(self.port_drop_stats_file, 'r')) + port_drop_ckpt = pickle.load(open(self.port_drop_stats_file, 'rb')) counters = self.gather_counters(std_port_rx_counters + std_port_tx_counters, DEBUG_COUNTER_PORT_STAT_MAP, group, counter_type) headers = std_port_description_header + self.gather_headers(counters, DEBUG_COUNTER_PORT_STAT_MAP) @@ -138,7 +138,7 @@ class DropStat(object): return table = [] - for key, value in self.get_counts_table(counters, COUNTERS_PORT_NAME_MAP).iteritems(): + for key, value in self.get_counts_table(counters, COUNTERS_PORT_NAME_MAP).items(): row = [key, self.get_port_state(key)] for counter in counters: row.append(value.get(counter, 0) - port_drop_ckpt.get(key, {}).get(counter, 0)) @@ -156,7 +156,7 @@ class DropStat(object): # Grab the latest clear checkpoint, if it exists if os.path.isfile(self.switch_drop_stats_file): - switch_drop_ckpt = pickle.load(open(self.switch_drop_stats_file, 'r')) + switch_drop_ckpt = pickle.load(open(self.switch_drop_stats_file, 'rb')) counters = self.gather_counters([], DEBUG_COUNTER_SWITCH_STAT_MAP, group, counter_type) headers = std_switch_description_header + self.gather_headers(counters, DEBUG_COUNTER_SWITCH_STAT_MAP) @@ -198,7 +198,7 @@ class DropStat(object): counter_names = self.get_reverse_stat_lookup(object_stat_map) for counter in counters: - if counter in std_port_headers_map.iterkeys(): + if counter in std_port_headers_map: headers.append(std_port_headers_map[counter]) else: headers.append(self.get_alias(counter_names[counter])) @@ -270,7 +270,7 @@ class DropStat(object): if not self.reverse_stat_lookup.get(object_stat_map, None): stats_map = self.get_stat_lookup(object_stat_map) if stats_map: - self.reverse_stat_lookup[object_stat_map] = {v: k for k, v in stats_map.iteritems()} + self.reverse_stat_lookup[object_stat_map] = {v: k for k, v in stats_map.items()} else: self.reverse_stat_lookup[object_stat_map] = None @@ -288,7 +288,7 @@ class DropStat(object): if not counters: return configured_counters - return [ctr for ctr in counters.itervalues()] + return list(counters.values()) def get_counter_name(self, object_stat_map, counter_stat): """ @@ -382,13 +382,15 @@ class DropStat(object): def main(): parser = argparse.ArgumentParser(description='Display drop counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: dropstat """) + # Version + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') + # Actions parser.add_argument('-c', '--command', type=str, help='Desired action to perform') diff --git a/scripts/dump_nat_entries.py b/scripts/dump_nat_entries.py index 09899b83368d..3ab99f245a84 100644 --- a/scripts/dump_nat_entries.py +++ b/scripts/dump_nat_entries.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """" Description: dump_nat_entries.py -- dump conntrack nat entries from kernel into a file @@ -9,7 +9,7 @@ def main(): ctdumpcmd = 'conntrack -L -j > /host/warmboot/nat/nat_entries.dump' - p = subprocess.Popen(ctdumpcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = subprocess.Popen(ctdumpcmd, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (output, err) = p.communicate() rc = p.wait() diff --git a/scripts/ecnconfig b/scripts/ecnconfig index 4102f9d5865c..edb2b754943d 100755 --- a/scripts/ecnconfig +++ b/scripts/ecnconfig @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ ecnconfig is the utility to @@ -47,11 +47,10 @@ $ecnconfig -q 3 ECN status: queue 3: on """ -from __future__ import print_function - +import argparse import os import sys -import argparse + import swsssdk from tabulate import tabulate @@ -232,7 +231,6 @@ def main(): parser = argparse.ArgumentParser(description='Show and change:\n' '1) ECN WRED configuration\n' '2) ECN on/off status on queues', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-l', '--list', action='store_true', help='show ECN WRED configuration') @@ -246,6 +244,7 @@ def main(): parser.add_argument('-gdrop', '--green-drop-prob', type=str, help='set max drop/mark probability for packets marked \'green\'', default=None) parser.add_argument('-ydrop', '--yellow-drop-prob', type=str, help='set max drop/mark probability for packets marked \'yellow\'', default=None) parser.add_argument('-rdrop', '--red-drop-prob', type=str, help='set max drop/mark probability for packets marked \'red\'', default=None) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') parser.add_argument('-vv', '--verbose', action='store_true', help='Verbose output', default=False) parser.add_argument('command', nargs='?', choices=['on', 'off'], type=str, help='turn on/off ecn', default=None) diff --git a/scripts/fanshow b/scripts/fanshow index e8d41c751710..856f8d663c59 100755 --- a/scripts/fanshow +++ b/scripts/fanshow @@ -1,9 +1,8 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to show fan status. """ -from __future__ import print_function import os import sys diff --git a/scripts/fast-reboot-dump.py b/scripts/fast-reboot-dump.py index 3881e9f5f491..65403f145646 100644 --- a/scripts/fast-reboot-dump.py +++ b/scripts/fast-reboot-dump.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import swsssdk import json @@ -239,7 +239,7 @@ def send_garp_nd(neighbor_entries, map_mac_ip_per_vlan): send_ndp(sockets[src_if], src_mac_addrs[src_if], src_ip_addrs[vlan_name], dst_mac, dst_ip) # close the raw sockets - for s in sockets.values(): + for s in list(sockets.values()): s.close() return diff --git a/scripts/fdbclear b/scripts/fdbclear index 18fa2864841d..b7eec2d555cb 100644 --- a/scripts/fdbclear +++ b/scripts/fdbclear @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to clear MAC/FDB entries learnt in Hardware diff --git a/scripts/fdbshow b/scripts/fdbshow index b9f38a71fe6d..9122740e830e 100755 --- a/scripts/fdbshow +++ b/scripts/fdbshow @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to show MAC/FDB entries learnt in Hardware @@ -42,7 +42,7 @@ class FdbShow(object): def __init__(self): super(FdbShow,self).__init__() - self.db = SonicV2Connector(host="127.0.0.1", decode_responses=True) + self.db = SonicV2Connector(host="127.0.0.1") self.if_name_map, \ self.if_oid_map = port_util.get_interface_oid_map(self.db) self.if_br_oid_map = port_util.get_bridge_port_map(self.db) diff --git a/scripts/gearboxutil b/scripts/gearboxutil index f6027ea0227a..22d69853bb9d 100755 --- a/scripts/gearboxutil +++ b/scripts/gearboxutil @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import swsssdk import sys diff --git a/scripts/intfstat b/scripts/intfstat index f38f67e55a0b..f36cf949a862 100755 --- a/scripts/intfstat +++ b/scripts/intfstat @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -6,8 +6,8 @@ # ##################################################################### +import _pickle as pickle import argparse -import cPickle as pickle import datetime import sys import os @@ -132,7 +132,7 @@ class Intfstat(object): """ table = [] - for key, data in cnstat_dict.iteritems(): + for key, data in cnstat_dict.items(): if key == 'time': continue @@ -152,7 +152,7 @@ class Intfstat(object): table = [] - for key, cntr in cnstat_new_dict.iteritems(): + for key, cntr in cnstat_new_dict.items(): if key == 'time': time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time') time_gap = time_gap.total_seconds() @@ -224,7 +224,6 @@ class Intfstat(object): def main(): parser = argparse.ArgumentParser(description='Display the interfaces state and counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Port state: (U)-Up (D)-Down (X)-Disabled @@ -246,6 +245,7 @@ def main(): parser.add_argument('-t', '--tag', type=str, help='Save stats with name TAG', default=None) parser.add_argument('-i', '--interface', type=str, help='Show stats for a single interface', required=False) parser.add_argument('-p', '--period', type=int, help='Display stats over a specified period (in seconds).', default=0) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() save_fresh_stats = args.clear @@ -304,7 +304,7 @@ def main(): if save_fresh_stats: try: - pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'w')) + pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'wb')) except IOError as e: sys.exit(e.errno) else: @@ -314,7 +314,7 @@ def main(): if wait_time_in_seconds == 0: if os.path.isfile(cnstat_fqn_file): try: - cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) if interface_name: intfstat.cnstat_single_interface(interface_name, cnstat_dict, cnstat_cached_dict) diff --git a/scripts/intfutil b/scripts/intfutil index eff145b9eb17..c26c352263d5 100755 --- a/scripts/intfutil +++ b/scripts/intfutil @@ -1,10 +1,9 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import argparse import os import re import sys -import types from natsort import natsorted from tabulate import tabulate @@ -51,7 +50,7 @@ SUB_PORT = "subport" def get_frontpanel_port_list(config_db): ports_dict = config_db.get_table('PORT') front_panel_ports_list = [] - for port in ports_dict.iterkeys(): + for port in ports_dict.keys(): front_panel_ports_list.append(port) return front_panel_ports_list @@ -60,7 +59,7 @@ def get_sub_port_intf_list(config_db): sub_intf_dict = config_db.get_table('VLAN_SUB_INTERFACE') sub_intf_list = [] for sub_intf in sub_intf_dict.keys(): - if isinstance(sub_intf, basestring): + if isinstance(sub_intf, str): sub_intf_list.append(sub_intf) return sub_intf_list @@ -155,7 +154,7 @@ def merge_dicts(x,y): # then we need to put back any missing x[key] values for key in xkeys: # if this key is a dictionary, recurse - if type(x[key]) is types.DictType and y.has_key(key): + if isinstance(x[key], dict) and key in y: merged[key] = merge(x[key],y[key]) return merged @@ -242,7 +241,7 @@ def po_speed_dict(po_int_dict, appl_db): """ if po_int_dict: po_list = [] - for key, value in po_int_dict.iteritems(): + for key, value in po_int_dict.items(): agg_speed_list = [] po_list.append(key) if len(value) == 1: @@ -354,10 +353,10 @@ class IntfStatus(object): def display_intf_status(self): self.get_intf_status() sorted_table = natsorted(self.table) - print tabulate(sorted_table, + print(tabulate(sorted_table, header_stat if not self.sub_intf_only else header_stat_sub_intf, tablefmt="simple", - stralign='right') + stralign='right')) def generate_intf_status(self): """ @@ -393,7 +392,7 @@ class IntfStatus(object): state_db_port_optics_get(self.db, key, PORT_OPTICS_TYPE), appl_db_port_status_get(self.db, key, PORT_PFC_ASYM_STATUS))) - for po, value in self.portchannel_speed_dict.iteritems(): + for po, value in self.portchannel_speed_dict.items(): if po: if self.multi_asic.skip_display(constants.PORT_CHANNEL_OBJ, po): continue @@ -467,7 +466,7 @@ class IntfDescription(object): # Sorting and tabulating the result table. sorted_table = natsorted(self.table) - print tabulate(sorted_table, header_desc, tablefmt="simple", stralign='right') + print(tabulate(sorted_table, header_desc, tablefmt="simple", stralign='right')) def generate_intf_description(self): """ diff --git a/scripts/lldpshow b/scripts/lldpshow index ba826a3f57e4..1530a56f8b68 100755 --- a/scripts/lldpshow +++ b/scripts/lldpshow @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to list LLDP neighbors in a summary view instead of default detailed view @@ -19,8 +19,6 @@ Total entries displayed: 33 """ -from __future__ import print_function - import argparse import re import subprocess @@ -81,7 +79,7 @@ class Lldpshow(object): # and in table format we will get xml output lldp_cmd = 'sudo docker exec -i lldp{} lldpctl '.format(self.lldp_instance[lldp_instace_num]) + ( '-f xml' if not lldp_detail_info else lldp_interface_list) - p = subprocess.Popen(lldp_cmd, stdout=subprocess.PIPE, shell=True) + p = subprocess.Popen(lldp_cmd, stdout=subprocess.PIPE, shell=True, text=True) (output, err) = p.communicate() ## Wait for end of command. Get return returncode ## returncode = p.wait() @@ -159,25 +157,24 @@ class Lldpshow(object): lldp_output = '' for lldp_detail_output in self.lldpraw: lldp_output += lldp_detail_output - print (lldp_output) + print(lldp_output) elif self.lldpraw: lldpstatus = [] - print ('Capability codes: (R) Router, (B) Bridge, (O) Other') + print('Capability codes: (R) Router, (B) Bridge, (O) Other') header = ['LocalPort', 'RemoteDevice', 'RemotePortID', 'Capability', 'RemotePortDescr'] sortedsum = self.sort_sum(self.lldpsum) for key in sortedsum: lldpstatus.append([key, self.lldpsum[key]['r_name'], self.lldpsum[key]['r_portid'], self.lldpsum[key]['capability'], self.lldpsum[key]['r_portname']]) - print (tabulate(lldpstatus, header)) - print ('-'.rjust(50, '-')) - print ('Total entries displayed: ', len(self.lldpsum)) + print(tabulate(lldpstatus, header)) + print('-'.rjust(50, '-')) + print('Total entries displayed: ', len(self.lldpsum)) elif self.err is not None: - print ('Error:', self.err) + print('Error:', self.err) def main(): parser = argparse.ArgumentParser(description='Display the LLDP neighbors', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: @@ -189,6 +186,7 @@ def main(): parser.add_argument('-d', '--detail', action='store_true', help='LLDP neighbors detail information', default=False) parser.add_argument('-p', '--port', type=str, help='LLDP neighbors detail information for given port', default=None) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() lldp_detail_info = args.detail diff --git a/scripts/mellanox_buffer_migrator.py b/scripts/mellanox_buffer_migrator.py index 40b96d4704d0..90abf0d821fa 100644 --- a/scripts/mellanox_buffer_migrator.py +++ b/scripts/mellanox_buffer_migrator.py @@ -255,7 +255,7 @@ def mlnx_migrate_buffer_pool_size(self, old_version, new_version): # Get current buffer pool configuration, only migrate configuration which # with default values, if it's not default, leave it as is. - name_list_of_pools_in_db = buffer_pool_conf_in_db.keys() + name_list_of_pools_in_db = list(buffer_pool_conf_in_db.keys()) # Buffer pool numbers is different with default, don't need migrate if len(name_list_of_pools_in_db) != len(old_default_buffer_pools): @@ -304,7 +304,7 @@ def mlnx_migrate_buffer_profile(self, old_version, new_version): This is to migrate BUFFER_PROFILE configuration """ device_data = self.configDB.get_table('DEVICE_METADATA') - if 'localhost' in device_data.keys(): + if 'localhost' in device_data: platform = device_data['localhost']['platform'] else: log.log_error("Trying to get DEVICE_METADATA from DB but doesn't exist, skip migration") @@ -333,8 +333,8 @@ def mlnx_migrate_buffer_profile(self, old_version, new_version): new_lossless_profiles = self.mlnx_default_buffer_parameters(new_version, "spc1_headroom") if default_lossless_profiles and new_lossless_profiles: - for name, profile in buffer_profile_conf.iteritems(): - if name in default_lossless_profiles.keys(): + for name, profile in buffer_profile_conf.items(): + if name in default_lossless_profiles: default_profile = default_lossless_profiles.get(name) new_profile = new_lossless_profiles.get(name) if not default_profile or not new_profile: @@ -353,14 +353,14 @@ def mlnx_migrate_buffer_profile(self, old_version, new_version): log.log_notice("No buffer profile in {}, don't need to migrate non-lossless profiles".format(new_version)) return True - for name, profile in buffer_profile_old_configure.iteritems(): - if name in buffer_profile_conf.keys() and profile == buffer_profile_conf[name]: + for name, profile in buffer_profile_old_configure.items(): + if name in buffer_profile_conf and profile == buffer_profile_conf[name]: continue # return if any default profile isn't in cofiguration log.log_notice("Default profile {} isn't in database or doesn't match default value".format(name)) return True - for name, profile in buffer_profile_new_configure.iteritems(): + for name, profile in buffer_profile_new_configure.items(): log.log_info("Successfully migrate profile {}".format(name)) self.configDB.set_entry('BUFFER_PROFILE', name, profile) diff --git a/scripts/mmuconfig b/scripts/mmuconfig index ed9d2e52d305..e77a46d9ded8 100644 --- a/scripts/mmuconfig +++ b/scripts/mmuconfig @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ mmuconfig is the utility to show and change mmu configuration @@ -15,8 +15,6 @@ optional arguments: """ -from __future__ import print_function - import os import sys import argparse @@ -86,12 +84,12 @@ class MmuConfig(object): def main(): parser = argparse.ArgumentParser(description='Show and change: mmu configuration', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-l', '--list', action='store_true', help='show mmu configuration') parser.add_argument('-p', '--profile', type=str, help='specify buffer profile name', default=None) parser.add_argument('-a', '--alpha', type=str, help='set n for dyanmic threshold alpha 2^(n)', default=None) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') parser.add_argument('-vv', '--verbose', action='store_true', help='verbose output', default=False) args = parser.parse_args() diff --git a/scripts/natclear b/scripts/natclear index 37af71c8db5f..2a46aaab18e5 100644 --- a/scripts/natclear +++ b/scripts/natclear @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to clear nat dynamic entries from Hardware and also to clear the nat statistics diff --git a/scripts/natconfig b/scripts/natconfig index 7fd0726f541a..3a1540e13b8d 100644 --- a/scripts/natconfig +++ b/scripts/natconfig @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to show nat configuration @@ -44,6 +44,7 @@ import sys from tabulate import tabulate from swsssdk import ConfigDBConnector + class NatConfig(object): def __init__(self): @@ -70,7 +71,7 @@ class NatConfig(object): nat_type = "dnat" twice_nat_id = "---" - if isinstance(key, unicode) is True: + if isinstance(key, str) is True: global_ip = key else: continue @@ -145,7 +146,7 @@ class NatConfig(object): for key,values in nat_pool_dict.items(): global_port = "---" - if isinstance(key, unicode) is True: + if isinstance(key, str) is True: pool_name = key else: continue @@ -176,7 +177,7 @@ class NatConfig(object): nat_type = "snat" twice_nat_id = "---" - if isinstance(key, unicode) is True: + if isinstance(key, str) is True: binding_name = key else: continue @@ -214,7 +215,7 @@ class NatConfig(object): for key,values in interface_zone_dict.items(): zone = "0" - if isinstance(key, unicode) is False: + if isinstance(key, str) is False: continue if "nat_zone" in values: diff --git a/scripts/natshow b/scripts/natshow index 0599c759aee6..a61d4948cdc1 100644 --- a/scripts/natshow +++ b/scripts/natshow @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to show nat entries and nat statistics in a summary view diff --git a/scripts/nbrshow b/scripts/nbrshow index f210f99d9005..8791803298be 100644 --- a/scripts/nbrshow +++ b/scripts/nbrshow @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to show Ipv4/Ipv6 neighbor entries @@ -37,6 +37,7 @@ from swsssdk import port_util from swsscommon.swsscommon import SonicV2Connector from tabulate import tabulate + """ Base class for v4 and v6 neighbor. """ @@ -49,7 +50,7 @@ class NbrBase(object): def __init__(self, cmd): super(NbrBase, self).__init__() - self.db = SonicV2Connector(host="127.0.0.1", decode_responses=True) + self.db = SonicV2Connector(host="127.0.0.1") self.if_name_map, self.if_oid_map = port_util.get_interface_oid_map(self.db) self.if_br_oid_map = port_util.get_bridge_port_map(self.db) self.fetch_fdb_data() @@ -130,7 +131,7 @@ class NbrBase(object): vlan = '-' if 'Vlan' in ent[2]: vlanid = int(re.search(r'\d+', ent[2]).group()) - mac = unicode(ent[1].upper()) + mac = ent[1].upper() fdb_ent = next((fdb for fdb in self.bridge_mac_list[:] if fdb[0] == vlanid and fdb[1] == mac), None) vlan = vlanid diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index b61edd3a7456..070d448b4834 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ######################################## # @@ -102,7 +102,7 @@ def is_dip_in_device_vlan(ferret_dip): ferret_dip = IPAddress(ferret_dip) - for vlan_interface in vlan_interface_query.iterkeys(): + for vlan_interface in vlan_interface_query.keys(): if not is_ip_prefix_in_key(vlan_interface): log.log_info('{} does not have a subnet, skipping...'.format(vlan_interface)) continue diff --git a/scripts/pcmping b/scripts/pcmping index 06fd61bfd1b4..b5b36d35210d 100755 --- a/scripts/pcmping +++ b/scripts/pcmping @@ -1,18 +1,20 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Portchannel member ping: get portchannel member packet loss state. """ -from scapy.all import * import argparse +import ipaddress import random -import socket import select -import time -import ipaddr as ipaddress +import socket import sys +import time + import swsssdk +from scapy.all import * + RCV_SIZE = 20480 RCV_TIMEOUT = 10000 @@ -50,13 +52,13 @@ def find_probe_packet(interface, dst_out, dst_in, sockets, exp_socket, max_iter) for x in range(0, max_iter): print("---------- Attempting to create probe packet that goes through %s, iteration: %d ---------" % (interface, x)) ip_src = socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff))) - ip_src =ipaddress.IPv4Address(unicode(ip_src,'utf-8')) - while ip_src == ipaddress.IPv4Address(unicode(dst_in,'utf-8')) or \ + ip_src = ipaddress.IPv4Address(ip_src) + while ip_src == ipaddress.IPv4Address(dst_in) or \ ip_src.is_multicast or ip_src.is_private or ip_src.is_reserved: ip_src = socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff))) - ip_src = ipaddress.IPv4Address(unicode(ip_src,'utf-8')) + ip_src = ipaddress.IPv4Address(ip_src) inner_pkt = IP(src=ip_src, dst=dst_in, tos=tos, ttl=64, id=1, ihl=None,proto=4)/ICMP() - inner_pkt = inner_pkt/("".join([chr(x%256) for x in xrange(PROBE_PACKET_LEN - len(inner_pkt))])) + inner_pkt = inner_pkt/("".join([chr(x%256) for x in range(PROBE_PACKET_LEN - len(inner_pkt))])) pkt = IP(src=src_out, dst=dst_out, tos=tos)/inner_pkt exp_pkt = inner_pkt exp_pkt['IP'].ttl = 63 @@ -80,7 +82,7 @@ def get_portchannel(interface): configdb = swsssdk.ConfigDBConnector() configdb.connect() portchannels = configdb.get_table("PORTCHANNEL") - for key, value in portchannels.iteritems(): + for key, value in portchannels.items(): if interface in value['members']: return key, value sys.stderr.write("Interface %s is not a portchannel member. Please Check!\n" % interface) @@ -104,7 +106,7 @@ def get_portchannel_ipv4(portchannel_name): continue pc, ip = key ip = ip.split("/")[0] - if pc == portchannel_name and ipaddress.IPAddress(ip).version == 4: + if pc == portchannel_name and ipaddress.ip_address(ip).version == 4: return ip sys.stderr.write("Portchannel %s doesn't have IPV4 address. Please Check!\n" % portchannel) sys.exit(1) @@ -131,7 +133,6 @@ def print_statistics(interface, total_count, recv_count): def main(): parser = argparse.ArgumentParser(description='Check portchannel member link state', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Example: @@ -145,6 +146,7 @@ def main(): parser.add_argument('-ip','--decapip', type=str, required=True, help='Neighbor decap ip (usually same as neighbor loopback ip)') parser.add_argument('-c', '--count', type=int, help='Number of decap packets to be sent.', default=0) parser.add_argument('-t', '--trial', type=int, help='Maximum attempt times to create probe packet.\nPlease increase the value as the number of portchannel members increases', default=20) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() interface = args.interface diff --git a/scripts/pfcstat b/scripts/pfcstat index 2979ba74b605..f3256444bef9 100755 --- a/scripts/pfcstat +++ b/scripts/pfcstat @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -6,11 +6,11 @@ # ##################################################################### -import sys +import _pickle as pickle import argparse -import cPickle as pickle import datetime import os.path +import sys from collections import namedtuple, OrderedDict from copy import deepcopy @@ -89,7 +89,7 @@ class Pfcstat(object): bucket_dict = counter_bucket_rx_dict else: bucket_dict = counter_bucket_tx_dict - for counter_name, pos in bucket_dict.iteritems(): + for counter_name, pos in bucket_dict.items(): full_table_id = COUNTER_TABLE_PREFIX + table_id counter_data = self.db.get( self.db.COUNTERS_DB, full_table_id, counter_name @@ -137,7 +137,7 @@ class Pfcstat(object): """ table = [] - for key, data in cnstat_dict.iteritems(): + for key, data in cnstat_dict.items(): if key == 'time': continue table.append((key, @@ -157,7 +157,7 @@ class Pfcstat(object): """ table = [] - for key, cntr in cnstat_new_dict.iteritems(): + for key, cntr in cnstat_new_dict.items(): if key == 'time': continue old_cntr = None @@ -188,7 +188,6 @@ class Pfcstat(object): def main(): parser = argparse.ArgumentParser(description='Display the pfc counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: @@ -211,6 +210,7 @@ Examples: parser.add_argument('-n', '--namespace', default=None, help='Display interfaces for specific namespace' ) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() save_fresh_stats = args.clear @@ -261,8 +261,8 @@ Examples: if save_fresh_stats: try: - pickle.dump(cnstat_dict_rx, open(cnstat_fqn_file_rx, 'w')) - pickle.dump(cnstat_dict_tx, open(cnstat_fqn_file_tx, 'w')) + pickle.dump(cnstat_dict_rx, open(cnstat_fqn_file_rx, 'wb')) + pickle.dump(cnstat_dict_tx, open(cnstat_fqn_file_tx, 'wb')) except IOError as e: print(e.errno, e) sys.exit(e.errno) @@ -276,7 +276,7 @@ Examples: """ if os.path.isfile(cnstat_fqn_file_rx): try: - cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_rx, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_rx, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) pfcstat.cnstat_diff_print(cnstat_dict_rx, cnstat_cached_dict, True) except IOError as e: @@ -291,7 +291,7 @@ Examples: """ if os.path.isfile(cnstat_fqn_file_tx): try: - cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_tx, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_tx, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) pfcstat.cnstat_diff_print(cnstat_dict_tx, cnstat_cached_dict, False) except IOError as e: diff --git a/scripts/port2alias b/scripts/port2alias index 426971479c71..2063f50befe2 100755 --- a/scripts/port2alias +++ b/scripts/port2alias @@ -1,7 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import sys -from cStringIO import StringIO +from io import StringIO from portconfig import get_port_config from sonic_py_common import device_info diff --git a/scripts/portconfig b/scripts/portconfig index 5f700a1cf343..8f17bf690d39 100755 --- a/scripts/portconfig +++ b/scripts/portconfig @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ portconfig is the utility to show and change ECN configuration @@ -17,8 +17,6 @@ optional arguments: -m --mtu port mtu in bytes -n --namesapce Namespace name """ -from __future__ import print_function - import sys import argparse import swsssdk @@ -70,13 +68,13 @@ class portconfig(object): def main(): parser = argparse.ArgumentParser(description='Set SONiC port parameters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-p', '--port', type=str, help='port name (e.g. Ethernet0)', required=True, default=None) parser.add_argument('-l', '--list', action='store_true', help='list port parametars', default=False) parser.add_argument('-s', '--speed', type=int, help='port speed value in Mbit', default=None) parser.add_argument('-f', '--fec', type=str, help='port fec mode value in (none, rs, fc)', default=None) parser.add_argument('-m', '--mtu', type=int, help='port mtu value in bytes', default=None) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') parser.add_argument('-vv', '--verbose', action='store_true', help='Verbose output', default=False) parser.add_argument('-n', '--namespace', metavar='namespace details', type = str, required = False, help = 'The asic namespace whose DB instance we need to connect', default=None) diff --git a/scripts/portstat b/scripts/portstat index 71f6d2d8f5a9..b05fdbdf8b43 100755 --- a/scripts/portstat +++ b/scripts/portstat @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -6,8 +6,8 @@ # ##################################################################### +import _pickle as pickle import argparse -import cPickle as pickle import datetime import os.path import sys @@ -111,7 +111,7 @@ class Portstat(object): Get the counters from specific table. """ fields = ["0","0","0","0","0","0","0","0","0","0"] - for counter_name, pos in counter_bucket_dict.iteritems(): + for counter_name, pos in counter_bucket_dict.items(): full_table_id = COUNTER_TABLE_PREFIX + table_id counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name) if counter_data is None: @@ -177,7 +177,7 @@ class Portstat(object): table = [] header = None - for key, data in cnstat_dict.iteritems(): + for key, data in cnstat_dict.items(): if key == 'time': continue if intf_list and key not in intf_list: @@ -221,7 +221,7 @@ class Portstat(object): table = [] header = None - for key, cntr in cnstat_new_dict.iteritems(): + for key, cntr in cnstat_new_dict.items(): if key == 'time': time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time') time_gap = time_gap.total_seconds() @@ -326,7 +326,6 @@ class Portstat(object): def main(): parser = argparse.ArgumentParser(description='Display the ports state and counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Port state: (U)-Up (D)-Down (X)-Disabled @@ -354,7 +353,8 @@ Examples: parser.add_argument('-p', '--period', type=int, help='Display stats over a specified period (in seconds).', default=0) parser.add_argument('-i', '--interface', type=str, help='Display stats for interface lists.', default=None) parser.add_argument('-s','--show', default=constants.DISPLAY_EXTERNAL, help='Display all interfaces or only external interfaces') - parser.add_argument('-n','--namespace', default=None, help='Display interfaces for specific namespace') + parser.add_argument('-n','--namespace', default=None, help='Display interfaces for specific namespace') + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() save_fresh_stats = args.clear @@ -430,7 +430,7 @@ Examples: if save_fresh_stats: try: - pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'w')) + pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'wb')) except IOError as e: sys.exit(e.errno) else: @@ -441,7 +441,7 @@ Examples: cnstat_cached_dict = OrderedDict() if os.path.isfile(cnstat_fqn_file): try: - cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) portstat.cnstat_diff_print(cnstat_dict, cnstat_cached_dict, intf_list, use_json, print_all, errors_only, rates_only) except IOError as e: @@ -455,7 +455,7 @@ Examples: else: #wait for the specified time and then gather the new stats and output the difference. time.sleep(wait_time_in_seconds) - print "The rates are calculated within %s seconds period" % wait_time_in_seconds + print("The rates are calculated within %s seconds period" % wait_time_in_seconds) cnstat_new_dict = portstat.get_cnstat_dict() portstat.cnstat_diff_print(cnstat_new_dict, cnstat_dict, intf_list, use_json, print_all, errors_only, rates_only) diff --git a/scripts/psushow b/scripts/psushow index b3af3faa8334..e46bad3697e8 100755 --- a/scripts/psushow +++ b/scripts/psushow @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import argparse import sys @@ -61,7 +61,6 @@ def psu_status_show(index): def main(): parser = argparse.ArgumentParser(description='Display the psu status information', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: @@ -70,6 +69,7 @@ Examples: parser.add_argument('-s', '--status', action='store_true', help='show the status information') parser.add_argument('-i', '--index', type=int, default=-1, help='the index of psu') + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() status_show = args.status diff --git a/scripts/queuestat b/scripts/queuestat index ca8a5ad47b5b..078e4a099e8b 100755 --- a/scripts/queuestat +++ b/scripts/queuestat @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -6,8 +6,8 @@ # ##################################################################### +import _pickle as pickle import argparse -import cPickle as pickle import datetime import os.path import swsssdk @@ -118,7 +118,7 @@ class Queuestat(object): fields[0] = get_queue_index(table_id) fields[1] = get_queue_type(table_id) - for counter_name, pos in counter_bucket_dict.iteritems(): + for counter_name, pos in counter_bucket_dict.items(): full_table_id = COUNTER_TABLE_PREFIX + table_id counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name) if counter_data is None: @@ -143,7 +143,7 @@ class Queuestat(object): """ table = [] - for key, data in cnstat_dict.iteritems(): + for key, data in cnstat_dict.items(): if key == 'time': continue table.append((port, data.queuetype + str(data.queueindex), @@ -159,7 +159,7 @@ class Queuestat(object): """ table = [] - for key, cntr in cnstat_new_dict.iteritems(): + for key, cntr in cnstat_new_dict.items(): if key == 'time': continue old_cntr = None @@ -188,7 +188,7 @@ class Queuestat(object): cnstat_fqn_file_name = cnstat_fqn_file + port if os.path.isfile(cnstat_fqn_file_name): try: - cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'rb')) print(port + " Last cached time was " + str(cnstat_cached_dict.get('time'))) self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict) except IOError as e: @@ -206,7 +206,7 @@ class Queuestat(object): cnstat_fqn_file_name = cnstat_fqn_file + port if os.path.isfile(cnstat_fqn_file_name): try: - cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'r')) + cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'rb')) print("Last cached time was " + str(cnstat_cached_dict.get('time'))) self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict) except IOError as e: @@ -226,7 +226,7 @@ class Queuestat(object): for port in natsorted(self.counter_port_name_map): cnstat_dict = self.get_cnstat(self.port_queues_map[port]) try: - pickle.dump(cnstat_dict, open(cnstat_fqn_file + port, 'w')) + pickle.dump(cnstat_dict, open(cnstat_fqn_file + port, 'wb')) except IOError as e: print(e.errno, e) sys.exit(e.errno) @@ -238,7 +238,6 @@ def main(): global cnstat_fqn_file parser = argparse.ArgumentParser(description='Display the queue state and counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: @@ -251,6 +250,7 @@ Examples: parser.add_argument('-p', '--port', type=str, help='Show the queue conters for just one port', default=None) parser.add_argument('-c', '--clear', action='store_true', help='Clear previous stats and save new ones') parser.add_argument('-d', '--delete', action='store_true', help='Delete saved stats') + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() save_fresh_stats = args.clear diff --git a/scripts/route_check.py b/scripts/route_check.py index dddbc619e75d..0a17f9ba21bb 100755 --- a/scripts/route_check.py +++ b/scripts/route_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os diff --git a/scripts/sfpshow b/scripts/sfpshow index 43f243ceb94a..5b6ee830337e 100755 --- a/scripts/sfpshow +++ b/scripts/sfpshow @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to show sfp eeprom and presence status. diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index 5ef5a136ce50..6c3e8763f027 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ''' Copyright 2019 Broadcom. The term "Broadcom" refers to Broadcom Inc. @@ -63,7 +63,7 @@ def run_command(cmd, use_shell=False): shcmd = shlex.split(cmd) else: shcmd = cmd - proc = subprocess.Popen(shcmd, shell=use_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, close_fds=True) + proc = subprocess.Popen(shcmd, shell=use_shell, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, close_fds=True) output_stdout, output_stderr = proc.communicate() list_stdout = [] for l in output_stdout.splitlines(): diff --git a/scripts/sonic_sku_create.py b/scripts/sonic_sku_create.py index d0256c72916e..52c99c8c37dd 100755 --- a/scripts/sonic_sku_create.py +++ b/scripts/sonic_sku_create.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ usage: sonic_sku_create.py [-h] [-v] [-f FILE] [-m [MINIGRAPH_FILE]] [-b BASE] @@ -22,8 +22,6 @@ """ -from __future__ import print_function - import argparse import json import os @@ -39,6 +37,7 @@ from lxml import etree as ET from lxml.etree import QName + minigraph_ns = "Microsoft.Search.Autopilot.Evolution" minigraph_ns1 = "http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution" INTERFACE_KEY = "Ethernet" @@ -255,7 +254,7 @@ def json_file_parser(self, json_file): f_out = open(new_file, 'w') header_str = "#name lanes alias index speed\n" f_out.write(header_str) - for key, value in data['PORT'].iteritems(): + for key, value in data['PORT'].items(): pattern = '^Ethernet([0-9]{1,})' m = re.match(pattern,key) if m is None: @@ -508,7 +507,7 @@ def split_analyze(self): # Analyze the front panl ports split based on the interfaces alias names # fpp_split is a hash with key=front panel port and values is a list of lists ([alias],[index]) alias_index = PORTCONFIG_HEADER.index('alias') - for idx,ifc in self.portconfig_dict.items(): + for idx, ifc in self.portconfig_dict.items(): pattern = '^etp([0-9]{1,})([a-d]?)' m = re.match(pattern,str(ifc[alias_index])) if int(m.group(1)) not in self.fpp_split : @@ -518,13 +517,13 @@ def split_analyze(self): self.fpp_split[int(m.group(1))][1].append(idx) if (self.verbose): print("split_analyze -> ",m.group(1), " : ", self.fpp_split[int(m.group(1))]) - self.num_of_fpp = len(self.fpp_split.keys()) + self.num_of_fpp = len(list(self.fpp_split.keys())) def get_default_lanes(self): #Internal function to get lanes of the ports accroding to the base default SKU try: with open(self.base_file_path,"r") as f: - line_header = f.next().split() # get the file header split into columns + line_header = next(f).split() # get the file header split into columns if line_header[0] == "#" : del line_header[0] # if hashtag is in a different column, remove it to align column header and data alias_index = line_header.index('alias') lanes_index = line_header.index('lanes') @@ -606,7 +605,7 @@ def create_port_config(self): exit(1) header = PORTCONFIG_HEADER # ["name", "lanes", "alias", "index"] port_config = [] - for line in self.portconfig_dict.values(): + for line in list(self.portconfig_dict.values()): port_config.append(line) port_config.sort(key=lambda x: (int(re.search(('\d+'),x[0]).group(0)))) # sort the list with interface name @@ -617,7 +616,7 @@ def print_port_config(self): #print a port_config.ini file based on the sku definition header = PORTCONFIG_HEADER # ["name", "lanes", "alias", "index"] port_config = [] - for line in self.portconfig_dict.values(): + for line in list(self.portconfig_dict.values()): port_config.append(line) port_config.sort(key=lambda x: (int(re.search(('\d+'),x[0]).group(0)))) # sort the list with interface name @@ -643,7 +642,7 @@ def remove_sku_dir(self): if not os.path.exists(self.new_sku_dir): print("Trying to remove a SKU "+ self.new_sku_dir + " that doesn't exists, Ignoring -r command") while True: - answer = raw_input("You are about to permanently delete the SKU "+ self.new_sku_dir+" !! \nDo you want to continue (Yes/No)?") + answer = input("You are about to permanently delete the SKU "+ self.new_sku_dir+" !! \nDo you want to continue (Yes/No)?") if (answer == "Yes" or answer == "No"): break else: @@ -689,7 +688,6 @@ def msn2700_specific(self): def main(argv): parser = argparse.ArgumentParser(description='Create a new SKU', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter) group = parser.add_mutually_exclusive_group(required=True) group.add_argument('-f', '--file', action='store', nargs=1, help='SKU definition from xml file. -f OR -m or -j must be provided when creating a new SKU', default=None) @@ -702,6 +700,7 @@ def main(argv): parser.add_argument('-p', '--print', action='store_true', help='Print port_config.ini without creating a new SKU', default=False) parser.add_argument('--verbose', action='store_true', help='Verbose output', default=False) parser.add_argument('-d', '--default_sku_path', action='store',nargs=1, help='Specify Default SKU path', default=None) + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() @@ -716,7 +715,7 @@ def main(argv): sku.default_sku_path = args.default_sku_path[0] else: try: - sku.platform = subprocess.check_output("sonic-cfggen -H -v DEVICE_METADATA.localhost.platform",shell=True) #self.metadata['platform'] + sku.platform = subprocess.check_output("sonic-cfggen -H -v DEVICE_METADATA.localhost.platform",shell=True, text=True) #self.metadata['platform'] sku.platform = sku.platform.rstrip() except KeyError: print("Couldn't find platform info in CONFIG_DB DEVICE_METADATA", file=sys.stderr) diff --git a/scripts/tempershow b/scripts/tempershow index 3bfb260e418e..0711bce8171b 100644 --- a/scripts/tempershow +++ b/scripts/tempershow @@ -1,10 +1,8 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Script to show fan status. """ -from __future__ import print_function - from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector from natsort import natsorted diff --git a/scripts/update_json.py b/scripts/update_json.py index 11ea7b8982a2..0e45622f22bf 100755 --- a/scripts/update_json.py +++ b/scripts/update_json.py @@ -8,7 +8,7 @@ BAK_SUFFIX = ".bak" def dict_update(dst, patch): - for k in patch.keys(): + for k in list(patch.keys()): if type(patch[k]) == dict: dst[k] = dict_update(dst[k], patch[k]) else: diff --git a/scripts/watermarkcfg b/scripts/watermarkcfg index 9d2c6be35ecf..274727f70ed5 100644 --- a/scripts/watermarkcfg +++ b/scripts/watermarkcfg @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -36,7 +36,6 @@ class Watermarkcfg(object): def main(): parser = argparse.ArgumentParser(description='Display the watermark counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: @@ -47,6 +46,7 @@ Examples: parser.add_argument('-s', '--show-interval', action='store_true', help='Show currently configured telemetry interval') parser.add_argument('-c', '--config-interval', action='store', type=int, help='Set telemetry interval') + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() watermarkcfg = Watermarkcfg() diff --git a/scripts/watermarkstat b/scripts/watermarkstat index 79c5a201e286..2f086b9a9506 100755 --- a/scripts/watermarkstat +++ b/scripts/watermarkstat @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ##################################################################### # @@ -6,8 +6,6 @@ # ##################################################################### -from __future__ import print_function - import argparse import json import os @@ -195,7 +193,7 @@ class Watermarkstat(object): self.header_list = ['Port'] header_map = wm_type["obj_map"] - single_key = header_map.keys()[0] + single_key = list(header_map.keys())[0] header_len = len(header_map[single_key]) min_idx = sys.maxsize @@ -260,7 +258,6 @@ class Watermarkstat(object): def main(): parser = argparse.ArgumentParser(description='Display the watermark counters', - version='1.0.0', formatter_class=argparse.RawTextHelpFormatter, epilog=""" Examples: @@ -280,6 +277,7 @@ Examples: parser.add_argument('-t', '--type', required=True, action='store', choices=['pg_headroom', 'pg_shared', 'q_shared_uni', 'q_shared_multi', 'buffer_pool'], help='The type of watermark') + parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() watermarkstat = Watermarkstat() diff --git a/setup.py b/setup.py index 058f1c4ea1d8..fad8223c0dea 100644 --- a/setup.py +++ b/setup.py @@ -146,7 +146,7 @@ 'ipaddress==1.0.23', 'jsondiff==1.2.0', 'm2crypto==0.31.0', - 'natsort==6.2.1', # 6.2.1 is the last version which supports Python 2 + 'natsort==6.2.1', # 6.2.1 is the last version which supports Python 2. Can update once we no longer support Python 2 'netaddr==0.8.0', 'netifaces==0.10.7', 'pexpect==4.8.0', @@ -155,7 +155,6 @@ 'swsssdk>=2.0.1', 'tabulate==0.8.2', 'xmltodict==0.12.0', - 'zipp==1.2.0' # Need to pin this down for Python 2, for Python 3 we should be able to remove altogether ], setup_requires= [ 'pytest-runner', @@ -163,7 +162,6 @@ ], tests_require = [ 'pytest', - 'mock>=2.0.0', 'mockredispy>=2.9.3', 'sonic-config-engine' ], @@ -176,7 +174,7 @@ 'License :: OSI Approved :: Apache Software License', 'Natural Language :: English', 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.7', 'Topic :: Utilities', ], keywords='sonic SONiC utilities command line cli CLI', diff --git a/sfputil/main.py b/sfputil/main.py index e745081459e1..931e86b14804 100644 --- a/sfputil/main.py +++ b/sfputil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # diff --git a/show/feature.py b/show/feature.py index c3b13abaf86f..e3dfd907a38a 100644 --- a/show/feature.py +++ b/show/feature.py @@ -23,14 +23,14 @@ def feature_status(db, feature_name): body = [] feature_table = db.cfgdb.get_table('FEATURE') if feature_name: - if feature_table and feature_table.has_key(feature_name): + if feature_table and feature_name in feature_table: body.append([feature_name, feature_table[feature_name]['state'], \ feature_table[feature_name]['auto_restart']]) else: click.echo("Can not find feature {}".format(feature_name)) sys.exit(1) else: - for key in natsorted(feature_table.keys()): + for key in natsorted(list(feature_table.keys())): body.append([key, feature_table[key]['state'], feature_table[key]['auto_restart']]) click.echo(tabulate(body, header)) @@ -45,12 +45,12 @@ def feature_autorestart(db, feature_name): body = [] feature_table = db.cfgdb.get_table('FEATURE') if feature_name: - if feature_table and feature_table.has_key(feature_name): + if feature_table and feature_name in feature_table: body.append([feature_name, feature_table[feature_name]['auto_restart']]) else: click.echo("Can not find feature {}".format(feature_name)) sys.exit(1) else: - for name in natsorted(feature_table.keys()): + for name in natsorted(list(feature_table.keys())): body.append([name, feature_table[name]['auto_restart']]) click.echo(tabulate(body, header)) diff --git a/show/fgnhg.py b/show/fgnhg.py index 0117e684ec12..92431118dc5a 100644 --- a/show/fgnhg.py +++ b/show/fgnhg.py @@ -41,7 +41,7 @@ def active_hops(nhg): if nhg is None: for nhg_prefix in table_keys: t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix) - vals = sorted(set([val for val in t_dict.values()])) + vals = sorted(set([val for val in list(t_dict.values())])) for nh_ip in vals: if nhg_prefix in output_dict: output_dict[nhg_prefix].append(nh_ip.split("@")[0]) @@ -62,14 +62,14 @@ def active_hops(nhg): for key in table_keys: mod_key = key.split("|")[1].split("/")[0] mod_nhg_prefix = nhg_prefix.split("/")[0] - if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded: + if ipaddress.ip_address(mod_key).exploded == ipaddress.ip_address(mod_nhg_prefix).exploded: t_dict = state_db.get_all(state_db.STATE_DB, key) nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix else: nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix) - vals = sorted(set([val for val in t_dict.values()])) + vals = sorted(set([val for val in list(t_dict.values())])) for nh_ip in vals: if nhg_prefix in output_dict: @@ -113,7 +113,7 @@ def hash_view(nhg): for nhg_prefix in table_keys: bank_dict = {} t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix) - vals = sorted(set([val for val in t_dict.values()])) + vals = sorted(set([val for val in list(t_dict.values())])) for nh_ip in vals: bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip]) @@ -143,14 +143,14 @@ def hash_view(nhg): for key in table_keys: mod_key = key.split("|")[1].split("/")[0] mod_nhg_prefix = nhg_prefix.split("/")[0] - if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded: + if ipaddress.ip_address(mod_key).exploded == ipaddress.ip_address(mod_nhg_prefix).exploded: t_dict = state_db.get_all(state_db.STATE_DB, key) nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix else: nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix) - vals = sorted(set([val for val in t_dict.values()])) + vals = sorted(set([val for val in list(t_dict.values())])) for nh_ip in vals: bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip]) diff --git a/show/interfaces/__init__.py b/show/interfaces/__init__.py index ad0038b71ba3..29fa2f86ccde 100644 --- a/show/interfaces/__init__.py +++ b/show/interfaces/__init__.py @@ -1,13 +1,13 @@ import json import click +import utilities_common.cli as clicommon +import utilities_common.multi_asic as multi_asic_util from natsort import natsorted from tabulate import tabulate - from sonic_py_common import multi_asic -import utilities_common.cli as clicommon -import utilities_common.multi_asic as multi_asic_util -import portchannel + +from . import portchannel def try_convert_interfacename_from_alias(ctx, interfacename): """try to convert interface name from alias""" @@ -48,8 +48,8 @@ def alias(interfacename, namespace, display): interfacename = try_convert_interfacename_from_alias(ctx, interfacename) # If we're given an interface name, output name and alias for that interface only - if interfacename in port_dict.keys(): - if port_dict[interfacename].has_key('alias'): + if interfacename in port_dict: + if 'alias' in port_dict[interfacename]: body.append([interfacename, port_dict[interfacename]['alias']]) else: body.append([interfacename, interfacename]) @@ -57,7 +57,7 @@ def alias(interfacename, namespace, display): ctx.fail("Invalid interface name {}".format(interfacename)) else: # Output name and alias for all interfaces - for port_name in natsorted(port_dict.keys()): + for port_name in natsorted(list(port_dict.keys())): if ((display == multi_asic_util.constants.DISPLAY_EXTERNAL) and ('role' in port_dict[port_name]) and (port_dict[port_name]['role'] is multi_asic.INTERNAL_PORT)): @@ -153,7 +153,7 @@ def breakout(ctx): click.echo("Can not load port config from {} or {} file".format(PLATFORM_JSON, HWSKU_JSON)) raise click.Abort() - for port_name in platform_dict.keys(): + for port_name in list(platform_dict.keys()): cur_brkout_mode = cur_brkout_tbl[port_name]["brkout_mode"] # Update deafult breakout mode and current breakout mode to platform_dict @@ -166,7 +166,7 @@ def breakout(ctx): click.echo("Cannot find ports from {} file ".format(PLATFORM_JSON)) raise click.Abort() - child_ports = natsorted(child_port_dict.keys()) + child_ports = natsorted(list(child_port_dict.keys())) children, speeds = [], [] # Update portname and speed of child ports if present @@ -180,7 +180,7 @@ def breakout(ctx): platform_dict[port_name]["child port speeds"] = ",".join(speeds) # Sorted keys by name in natural sort Order for human readability - parsed = OrderedDict((k, platform_dict[k]) for k in natsorted(platform_dict.keys())) + parsed = OrderedDict((k, platform_dict[k]) for k in natsorted(list(platform_dict.keys()))) click.echo(json.dumps(parsed, indent=4)) # 'breakout current-mode' subcommand ("show interfaces breakout current-mode") @@ -208,7 +208,7 @@ def currrent_mode(ctx, interface): return # Show current Breakout Mode for all interfaces - for name in natsorted(cur_brkout_tbl.keys()): + for name in natsorted(list(cur_brkout_tbl.keys())): body.append([name, str(cur_brkout_tbl[name]['brkout_mode'])]) click.echo(tabulate(body, header, tablefmt="grid")) @@ -237,7 +237,7 @@ def expected(db, interfacename): click.echo("DEVICE_NEIGHBOR_METADATA information is not present.") return - for port in natsorted(neighbor_dict.keys()): + for port in natsorted(list(neighbor_dict.keys())): temp_port = port if clicommon.get_interface_naming_mode() == "alias": port = clicommon.InterfaceAliasConverter().name_to_alias(port) @@ -258,7 +258,7 @@ def expected(db, interfacename): click.echo("No neighbor information available for interface {}".format(interfacename)) return else: - for port in natsorted(neighbor_dict.keys()): + for port in natsorted(list(neighbor_dict.keys())): try: device = neighbor_dict[port]['name'] body.append([port, diff --git a/show/main.py b/show/main.py index 0252bd7d4106..0aa2976953fc 100755 --- a/show/main.py +++ b/show/main.py @@ -5,26 +5,25 @@ import sys import click -from natsort import natsorted import netifaces -from pkg_resources import parse_version - -import feature -import interfaces -import kube -import mlnx import utilities_common.cli as clicommon -import vlan -import system_health -import fgnhg -import chassis_modules - +import utilities_common.multi_asic as multi_asic_util +from natsort import natsorted +from pkg_resources import parse_version from sonic_py_common import device_info, multi_asic from swsssdk import ConfigDBConnector from swsscommon.swsscommon import SonicV2Connector from tabulate import tabulate from utilities_common.db import Db -import utilities_common.multi_asic as multi_asic_util + +from . import chassis_modules +from . import feature +from . import fgnhg +from . import interfaces +from . import kube +from . import mlnx +from . import vlan +from . import system_health # Global Variables @@ -44,6 +43,7 @@ def get_routing_stack(): proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, + text=True, stderr=subprocess.STDOUT) stdout = proc.communicate()[0] proc.wait() @@ -78,7 +78,7 @@ def run_command(command, display_cmd=False, return_cmd=False): clicommon.run_command_in_alias_mode(command) raise sys.exit(0) - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) while True: if return_cmd: @@ -126,13 +126,13 @@ def cli(ctx): # Add groups from other modules +cli.add_command(chassis_modules.chassis_modules) cli.add_command(feature.feature) +cli.add_command(fgnhg.fgnhg) cli.add_command(interfaces.interfaces) cli.add_command(kube.kubernetes) cli.add_command(vlan.vlan) cli.add_command(system_health.system_health) -cli.add_command(fgnhg.fgnhg) -cli.add_command(chassis_modules.chassis_modules) # # 'vrf' command ("show vrf") @@ -146,8 +146,8 @@ def get_interface_bind_to_vrf(config_db, vrf_name): for table_name in tables: interface_dict = config_db.get_table(table_name) if interface_dict: - for interface in interface_dict.keys(): - if interface_dict[interface].has_key('vrf_name') and vrf_name == interface_dict[interface]['vrf_name']: + for interface in list(interface_dict.keys()): + if 'vrf_name' in interface_dict[interface] and vrf_name == interface_dict[interface]['vrf_name']: data.append(interface) return data @@ -163,8 +163,8 @@ def vrf(vrf_name): if vrf_dict: vrfs = [] if vrf_name is None: - vrfs = vrf_dict.keys() - elif vrf_name in vrf_dict.keys(): + vrfs = list(vrf_dict.keys()) + elif vrf_name in vrf_dict: vrfs = [vrf_name] for vrf in vrfs: intfs = get_interface_bind_to_vrf(config_db, vrf) @@ -226,7 +226,7 @@ def is_mgmt_vrf_enabled(ctx): if ctx.invoked_subcommand is None: cmd = 'sonic-cfggen -d --var-json "MGMT_VRF_CONFIG"' - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try : mvrf_dict = json.loads(p.stdout.read()) except ValueError: @@ -287,7 +287,7 @@ def address (): # Fetching data from config_db for MGMT_INTERFACE mgmt_ip_data = config_db.get_table('MGMT_INTERFACE') - for key in natsorted(mgmt_ip_data.keys()): + for key in natsorted(list(mgmt_ip_data.keys())): click.echo("Management IP address = {0}".format(key[1])) click.echo("Management Network Default Gateway = {0}".format(mgmt_ip_data[key]['gwaddr'])) @@ -305,7 +305,7 @@ def snmpagentaddress (ctx): header = ['ListenIP', 'ListenPort', 'ListenVrf'] body = [] - for agent in agenttable.keys(): + for agent in list(agenttable.keys()): body.append([agent[0], agent[1], agent[2]]) click.echo(tabulate(body, header)) @@ -323,7 +323,7 @@ def snmptrap (ctx): header = ['Version', 'TrapReceiverIP', 'Port', 'VRF', 'Community'] body = [] - for row in traptable.keys(): + for row in list(traptable.keys()): if row == "v1TrapDest": ver=1 elif row == "v2TrapDest": @@ -777,7 +777,7 @@ def get_bgp_peer(): for table in bgp_neighbor_tables: data = config_db.get_table(table) - for neighbor_ip in data.keys(): + for neighbor_ip in list(data.keys()): local_addr = data[neighbor_ip]['local_addr'] neighbor_name = data[neighbor_ip]['name'] bgp_peer.setdefault(local_addr, [neighbor_name, neighbor_ip]) @@ -1142,9 +1142,9 @@ def version(verbose): version_info = device_info.get_sonic_version_info() hw_info_dict = get_hw_info_dict() serial_number_cmd = "sudo decode-syseeprom -s" - serial_number = subprocess.Popen(serial_number_cmd, shell=True, stdout=subprocess.PIPE) + serial_number = subprocess.Popen(serial_number_cmd, shell=True, text=True, stdout=subprocess.PIPE) sys_uptime_cmd = "uptime" - sys_uptime = subprocess.Popen(sys_uptime_cmd, shell=True, stdout=subprocess.PIPE) + sys_uptime = subprocess.Popen(sys_uptime_cmd, shell=True, text=True, stdout=subprocess.PIPE) click.echo("\nSONiC Software Version: SONiC.{}".format(version_info['build_version'])) click.echo("Distribution: Debian {}".format(version_info['debian_version'])) click.echo("Kernel: {}".format(version_info['kernel_version'])) @@ -1158,7 +1158,7 @@ def version(verbose): click.echo("Uptime: {}".format(sys_uptime.stdout.read().strip())) click.echo("\nDocker images:") cmd = 'sudo docker images --format "table {{.Repository}}\\t{{.Tag}}\\t{{.ID}}\\t{{.Size}}"' - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) + p = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE) click.echo(p.stdout.read()) # @@ -1329,7 +1329,7 @@ def ntp(verbose): ntp_server = line.split(" ")[1] ntp_servers.append(ntp_server) ntp_dict['NTP Servers'] = ntp_servers - print(tabulate(ntp_dict, headers=ntp_dict.keys(), tablefmt="simple", stralign='left', missingval="")) + print(tabulate(ntp_dict, headers=list(ntp_dict.keys()), tablefmt="simple", stralign='left', missingval="")) # 'syslog' subcommand ("show runningconfiguration syslog") @@ -1347,7 +1347,7 @@ def syslog(verbose): server = line[0][5:] syslog_servers.append(server) syslog_dict['Syslog Servers'] = syslog_servers - print(tabulate(syslog_dict, headers=syslog_dict.keys(), tablefmt="simple", stralign='left', missingval="")) + print(tabulate(syslog_dict, headers=list(syslog_dict.keys()), tablefmt="simple", stralign='left', missingval="")) # @@ -1366,7 +1366,7 @@ def startupconfiguration(): def bgp(verbose): """Show BGP startup configuration""" cmd = "sudo docker ps | grep bgp | awk '{print$2}' | cut -d'-' -f3 | cut -d':' -f1" - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) result = proc.stdout.read().rstrip() click.echo("Routing-Stack is: {}".format(result)) if result == "quagga": @@ -1513,14 +1513,14 @@ def log(record, lines): def services(): """Show all daemon services""" cmd = "sudo docker ps --format '{{.Names}}'" - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) while True: line = proc.stdout.readline() if line != '': print(line.rstrip()+'\t'+"docker") print("---------------------------") cmd = "sudo docker exec {} ps aux | sed '$d'".format(line.rstrip()) - proc1 = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc1 = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) print(proc1.stdout.read()) else: break @@ -1648,7 +1648,7 @@ def show_sflow_interface(config_db): click.echo("\nsFlow interface configurations") header = ['Interface', 'Admin State', 'Sampling Rate'] body = [] - for pname in natsorted(port_tbl.keys()): + for pname in natsorted(list(port_tbl.keys())): intf_key = 'SFLOW_SESSION_TABLE:' + pname sess_info = sess_db.get_all(sess_db.APPL_DB, intf_key) if sess_info is None: @@ -1671,13 +1671,13 @@ def show_sflow_global(config_db): click.echo(" sFlow Polling Interval:".ljust(30), nl=False) - if (sflow_info and 'polling_interval' in sflow_info['global'].keys()): + if (sflow_info and 'polling_interval' in sflow_info['global']): click.echo("{}".format(sflow_info['global']['polling_interval'])) else: click.echo("default") click.echo(" sFlow AgentID:".ljust(30), nl=False) - if (sflow_info and 'agent_id' in sflow_info['global'].keys()): + if (sflow_info and 'agent_id' in sflow_info['global']): click.echo("{}".format(sflow_info['global']['agent_id'])) else: click.echo("default") @@ -1793,7 +1793,7 @@ def counts(group, counter_type, verbose): def ecn(): """Show ECN configuration""" cmd = "ecnconfig -l" - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) click.echo(proc.stdout.read()) @@ -1804,7 +1804,7 @@ def ecn(): def boot(): """Show boot configuration""" cmd = "sudo sonic-installer list" - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) click.echo(proc.stdout.read()) @@ -1814,7 +1814,7 @@ def boot(): def mmu(): """Show mmu configuration""" cmd = "mmuconfig -l" - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) click.echo(proc.stdout.read()) @@ -1834,7 +1834,7 @@ def reboot_cause(): click.echo("Unable to determine cause of previous reboot\n") else: cmd = "cat {}".format(PREVIOUS_REBOOT_CAUSE_FILE) - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) click.echo(proc.stdout.read()) @@ -1908,7 +1908,7 @@ def config(redis_unix_socket_path): config_db.connect(wait_for_init=False) data = config_db.get_table('WARM_RESTART') # Python dictionary keys() Method - keys = data.keys() + keys = list(data.keys()) state_db = SonicV2Connector(host='127.0.0.1') state_db.connect(state_db.STATE_DB, False) # Make one attempt only @@ -2173,7 +2173,7 @@ def brief(): # Fetching data from config_db for VNET vnet_data = config_db.get_table('VNET') - vnet_keys = natsorted(vnet_data.keys()) + vnet_keys = natsorted(list(vnet_data.keys())) def tablelize(vnet_keys, vnet_data): table = [] @@ -2198,7 +2198,7 @@ def alias(vnet_alias): # Fetching data from config_db for VNET vnet_data = config_db.get_table('VNET') - vnet_keys = natsorted(vnet_data.keys()) + vnet_keys = natsorted(list(vnet_data.keys())) def tablelize(vnet_keys, vnet_data, vnet_alias): table = [] @@ -2433,7 +2433,7 @@ def tunnel(): # Fetching data from config_db for VXLAN TUNNEL vxlan_data = config_db.get_table('VXLAN_TUNNEL') - vxlan_keys = natsorted(vxlan_data.keys()) + vxlan_keys = natsorted(list(vxlan_data.keys())) table = [] for k in vxlan_keys: diff --git a/show/mlnx.py b/show/mlnx.py index 3dae4e906fea..fa887cf99928 100644 --- a/show/mlnx.py +++ b/show/mlnx.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # @@ -30,7 +30,7 @@ def run_command(command, display_cmd=False, ignore_error=False, print_to_console if display_cmd == True: click.echo(click.style("Running command: ", fg='cyan') + click.style(command, fg='green')) - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) (out, err) = proc.communicate() if len(out) > 0 and print_to_console: diff --git a/show/system_health.py b/show/system_health.py index b1319922fa5d..f3218f5687bb 100644 --- a/show/system_health.py +++ b/show/system_health.py @@ -144,7 +144,7 @@ def detail(): header = ['Name', 'Status', 'Type'] table = [] for category, elements in stat.items(): - for element in sorted(elements.items(), key=lambda (x, y): y['status']): + for element in sorted(elements.items(), key=lambda x: x[1]['status']): entry = [] entry.append(element[0]) entry.append(element[1]['status']) @@ -203,7 +203,7 @@ def monitor_list(): header = ['Name', 'Status', 'Type'] table = [] for category, elements in stat.items(): - for element in sorted(elements.items(), key=lambda (x, y): y['status']): + for element in sorted(elements.items(), key=lambda x: x[1]['status']): entry = [] entry.append(element[0]) entry.append(element[1]['status']) diff --git a/show/vlan.py b/show/vlan.py index f9869e2ba7a6..fcd3bbdba1ea 100644 --- a/show/vlan.py +++ b/show/vlan.py @@ -31,26 +31,25 @@ def brief(db, verbose): vlan_proxy_arp_dict = {} # Parsing DHCP Helpers info - for key in natsorted(vlan_dhcp_helper_data.keys()): + for key in natsorted(list(vlan_dhcp_helper_data.keys())): try: if vlan_dhcp_helper_data[key]['dhcp_servers']: - vlan_dhcp_helper_dict[str(key.strip('Vlan'))] = vlan_dhcp_helper_data[key]['dhcp_servers'] + vlan_dhcp_helper_dict[key.strip('Vlan')] = vlan_dhcp_helper_data[key]['dhcp_servers'] except KeyError: - vlan_dhcp_helper_dict[str(key.strip('Vlan'))] = " " + vlan_dhcp_helper_dict[key.strip('Vlan')] = " " # Parsing VLAN Gateway info - for key in natsorted(vlan_ip_data.keys()): - + for key in list(vlan_ip_data.keys()): if clicommon.is_ip_prefix_in_key(key): - interface_key = str(key[0].strip("Vlan")) - interface_value = str(key[1]) + interface_key = key[0].strip("Vlan") + interface_value = key[1] if interface_key in vlan_ip_dict: vlan_ip_dict[interface_key].append(interface_value) else: vlan_ip_dict[interface_key] = [interface_value] else: - interface_key = str(key.strip("Vlan")) + interface_key = key.strip("Vlan") if 'proxy_arp' in vlan_ip_data[key]: proxy_arp_status = vlan_ip_data[key]['proxy_arp'] else: @@ -63,9 +62,9 @@ def brief(db, verbose): iface_alias_converter = clicommon.InterfaceAliasConverter(db) # Parsing VLAN Ports info - for key in natsorted(vlan_ports_data.keys()): - ports_key = str(key[0].strip("Vlan")) - ports_value = str(key[1]) + for key in natsorted(list(vlan_ports_data.keys())): + ports_key = key[0].strip("Vlan") + ports_value = key[1] ports_tagging = vlan_ports_data[key]['tagging_mode'] if ports_key in vlan_ports_dict: if clicommon.get_interface_naming_mode() == "alias": @@ -83,7 +82,7 @@ def brief(db, verbose): # Printing the following dictionaries in tablular forms: # vlan_dhcp_helper_dict={}, vlan_ip_dict = {}, vlan_ports_dict = {} # vlan_tagging_dict = {} - for key in natsorted(vlan_dhcp_helper_dict.keys()): + for key in natsorted(list(vlan_dhcp_helper_dict.keys())): if key not in vlan_ip_dict: ip_address = "" else: @@ -108,7 +107,7 @@ def brief(db, verbose): @clicommon.pass_db def config(db): data = db.cfgdb.get_table('VLAN') - keys = data.keys() + keys = list(data.keys()) member_data = db.cfgdb.get_table('VLAN_MEMBER') def tablelize(keys, data): @@ -120,7 +119,7 @@ def tablelize(keys, data): if vlan == k: members.add(interface_name) - for m in members: + for m in natsorted(list(members)): r = [] r.append(k) r.append(data[k]['vlanid']) diff --git a/sonic_installer/bootloader/aboot.py b/sonic_installer/bootloader/aboot.py index 3cc43a457f30..eee53de859f9 100644 --- a/sonic_installer/bootloader/aboot.py +++ b/sonic_installer/bootloader/aboot.py @@ -118,7 +118,7 @@ def remove_image(self, image): def get_binary_image_version(self, image_path): try: - version = subprocess.check_output(['/usr/bin/unzip', '-qop', image_path, '.imagehash']) + version = subprocess.check_output(['/usr/bin/unzip', '-qop', image_path, '.imagehash'], text=True) except subprocess.CalledProcessError: return None return IMAGE_PREFIX + version.strip() diff --git a/sonic_installer/bootloader/grub.py b/sonic_installer/bootloader/grub.py index 1d111f41919c..794f1ce20e19 100644 --- a/sonic_installer/bootloader/grub.py +++ b/sonic_installer/bootloader/grub.py @@ -33,7 +33,7 @@ def get_installed_images(self): def get_next_image(self): images = self.get_installed_images() - grubenv = subprocess.check_output(["/usr/bin/grub-editenv", HOST_PATH + "/grub/grubenv", "list"]) + grubenv = subprocess.check_output(["/usr/bin/grub-editenv", HOST_PATH + "/grub/grubenv", "list"], text=True) m = re.search(r"next_entry=(\d+)", grubenv) if m: next_image_index = int(m.group(1)) diff --git a/sonic_installer/bootloader/onie.py b/sonic_installer/bootloader/onie.py index ca16172efa2b..3a0115347634 100644 --- a/sonic_installer/bootloader/onie.py +++ b/sonic_installer/bootloader/onie.py @@ -32,7 +32,7 @@ def get_binary_image_version(self, image_path): """returns the version of the image""" p1 = subprocess.Popen(["cat", "-v", image_path], stdout=subprocess.PIPE, preexec_fn=default_sigpipe) p2 = subprocess.Popen(["grep", "-m 1", "^image_version"], stdin=p1.stdout, stdout=subprocess.PIPE, preexec_fn=default_sigpipe) - p3 = subprocess.Popen(["sed", "-n", r"s/^image_version=\"\(.*\)\"$/\1/p"], stdin=p2.stdout, stdout=subprocess.PIPE, preexec_fn=default_sigpipe) + p3 = subprocess.Popen(["sed", "-n", r"s/^image_version=\"\(.*\)\"$/\1/p"], stdin=p2.stdout, stdout=subprocess.PIPE, preexec_fn=default_sigpipe, text=True) stdout = p3.communicate()[0] p3.wait() diff --git a/sonic_installer/bootloader/uboot.py b/sonic_installer/bootloader/uboot.py index 47252dd6af7b..ae54c64e43c3 100644 --- a/sonic_installer/bootloader/uboot.py +++ b/sonic_installer/bootloader/uboot.py @@ -21,12 +21,12 @@ class UbootBootloader(OnieInstallerBootloader): def get_installed_images(self): images = [] - proc = subprocess.Popen("/usr/bin/fw_printenv -n sonic_version_1", shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen("/usr/bin/fw_printenv -n sonic_version_1", shell=True, text=True, stdout=subprocess.PIPE) (out, _) = proc.communicate() image = out.rstrip() if IMAGE_PREFIX in image: images.append(image) - proc = subprocess.Popen("/usr/bin/fw_printenv -n sonic_version_2", shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen("/usr/bin/fw_printenv -n sonic_version_2", shell=True, text=True, stdout=subprocess.PIPE) (out, _) = proc.communicate() image = out.rstrip() if IMAGE_PREFIX in image: @@ -35,7 +35,7 @@ def get_installed_images(self): def get_next_image(self): images = self.get_installed_images() - proc = subprocess.Popen("/usr/bin/fw_printenv -n boot_next", shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen("/usr/bin/fw_printenv -n boot_next", shell=True, text=True, stdout=subprocess.PIPE) (out, _) = proc.communicate() image = out.rstrip() if "sonic_image_2" in image: diff --git a/sonic_installer/common.py b/sonic_installer/common.py index a9df312a85a7..475edb41ee15 100644 --- a/sonic_installer/common.py +++ b/sonic_installer/common.py @@ -18,7 +18,7 @@ def run_command(command): click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green')) - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) (out, _) = proc.communicate() click.echo(out) @@ -30,7 +30,7 @@ def run_command(command): def run_command_or_raise(argv): click.echo(click.style("Command: ", fg='cyan') + click.style(' '.join(argv), fg='green')) - proc = subprocess.Popen(argv, stdout=subprocess.PIPE) + proc = subprocess.Popen(argv, text=True, stdout=subprocess.PIPE) out, _ = proc.communicate() if proc.returncode != 0: diff --git a/sonic_installer/main.py b/sonic_installer/main.py index 6a6deaf5dea1..6ffd7aaf92fd 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -1,14 +1,10 @@ -try: - import ConfigParser as configparser -except ImportError: - import configparser - +import configparser import os import re import subprocess import sys import time -import urllib +from urllib.request import urlopen, urlretrieve import click from sonic_py_common import logger @@ -117,7 +113,7 @@ def reporthook(count, block_size, total_size): def get_docker_tag_name(image): # Try to get tag name from label metadata cmd = "docker inspect --format '{{.ContainerConfig.Labels.Tag}}' " + image - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) (out, _) = proc.communicate() if proc.returncode != 0: return "unknown" @@ -132,7 +128,7 @@ def get_docker_tag_name(image): def validate_url_or_abort(url): # Attempt to retrieve HTTP response code try: - urlfile = urllib.urlopen(url) + urlfile = urlopen(url) response_code = urlfile.getcode() urlfile.close() except IOError: @@ -157,7 +153,7 @@ def abort_if_false(ctx, param, value): def get_container_image_name(container_name): # example image: docker-lldp-sv2:latest cmd = "docker inspect --format '{{.Config.Image}}' " + container_name - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) (out, _) = proc.communicate() if proc.returncode != 0: sys.exit(proc.returncode) @@ -165,7 +161,7 @@ def get_container_image_name(container_name): # example image_name: docker-lldp-sv2 cmd = "echo " + image_latest + " | cut -d ':' -f 1" - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) image_name = proc.stdout.read().rstrip() return image_name @@ -174,7 +170,7 @@ def get_container_image_id(image_tag): # TODO: extract commond docker info fetching functions # this is image_id for image with tag, like 'docker-teamd:latest' cmd = "docker images --format '{{.ID}}' " + image_tag - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) image_id = proc.stdout.read().rstrip() return image_id @@ -182,7 +178,7 @@ def get_container_image_id(image_tag): def get_container_image_id_all(image_name): # All images id under the image name like 'docker-teamd' cmd = "docker images --format '{{.ID}}' " + image_name - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) image_id_all = proc.stdout.read() image_id_all = image_id_all.splitlines() image_id_all = set(image_id_all) @@ -249,7 +245,7 @@ def umount_next_image_fs(mount_point): ]) os.mkdir(env_dir, 0o755) with open(env_file, "w+") as ef: - print >>ef, sonic_env + print(sonic_env, file=ef) os.chmod(env_file, 0o644) except SonicRuntimeException as ex: click.secho("Warning: SONiC environment variables are not supported for this image: {0}".format(str(ex)), @@ -289,7 +285,7 @@ def install(url, force, skip_migration=False): click.echo('Downloading image...') validate_url_or_abort(url) try: - urllib.urlretrieve(url, bootloader.DEFAULT_IMAGE_PATH, reporthook) + urlretrieve(url, bootloader.DEFAULT_IMAGE_PATH, reporthook) click.echo('') except Exception as e: click.echo("Download error", e) @@ -481,7 +477,7 @@ def upgrade_docker(container_name, url, cleanup_image, skip_check, tag, warm): click.echo('Downloading image...') validate_url_or_abort(url) try: - urllib.urlretrieve(url, DEFAULT_IMAGE_PATH, reporthook) + urlretrieve(url, DEFAULT_IMAGE_PATH, reporthook) except Exception as e: click.echo("Download error", e) raise click.Abort() @@ -525,7 +521,7 @@ def upgrade_docker(container_name, url, cleanup_image, skip_check, tag, warm): cmd = "docker exec -i swss orchagent_restart_check -w 2000 -r 5 " + skipPendingTaskCheck - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) (out, err) = proc.communicate() if proc.returncode != 0: if not skip_check: diff --git a/ssdutil/main.py b/ssdutil/main.py index 7edd8ab66a61..62f43037e7bf 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py # diff --git a/tests/aclshow_test.py b/tests/aclshow_test.py index 6ff09f18cf5c..5003771296d3 100644 --- a/tests/aclshow_test.py +++ b/tests/aclshow_test.py @@ -1,18 +1,20 @@ import sys import os -from StringIO import StringIO -import mock +from imp import load_source +from io import StringIO +from unittest import mock + test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) scripts_path = os.path.join(modules_path, "scripts") sys.path.insert(0, modules_path) -from imp import load_source load_source('aclshow', scripts_path+'/aclshow') from aclshow import * -import mock_tables.dbconnector +from .mock_tables import dbconnector + # Expected output for aclshow default_output = ''+ \ @@ -145,7 +147,7 @@ def nullify_counters(self): if exist in /tmp/.counters_acl.p (by default). """ if os.path.isfile(COUNTER_POSITION): - with open(COUNTER_POSITION, 'wb') as fp: + with open(COUNTER_POSITION, 'w') as fp: json.dump([], fp) def runTest(self): diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index 9f8c4fb21c54..24b4fdeafb16 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -96,7 +96,7 @@ class TestBgpCommands(object): @classmethod def setup_class(cls): print("SETUP") - import mock_tables.dbconnector + from .mock_tables import dbconnector @pytest.mark.parametrize('setup_single_bgp_instance', ['v4'], indirect=['setup_single_bgp_instance']) diff --git a/tests/config_mgmt_test.py b/tests/config_mgmt_test.py index 4133ab2194ef..38d246299a6c 100644 --- a/tests/config_mgmt_test.py +++ b/tests/config_mgmt_test.py @@ -1,5 +1,7 @@ import imp import os +import sys + # import file under test i.e. config_mgmt.py imp.load_source('config_mgmt', \ os.path.join(os.path.dirname(__file__), '..', 'config', 'config_mgmt.py')) @@ -41,15 +43,15 @@ def test_search_keys(self): cmdpb = config_mgmt.ConfigMgmtDPB(source=config_mgmt.CONFIG_DB_JSON_FILE) out = cmdpb.configWithKeys(portBreakOutConfigDbJson, \ ["Ethernet8","Ethernet9"]) - assert "VLAN" not in out.keys() - assert "INTERFACE" not in out.keys() - for k in out['ACL_TABLE'].keys(): + assert "VLAN" not in out + assert "INTERFACE" not in out + for k in list(out['ACL_TABLE'].keys()): # only ports must be chosen len(out['ACL_TABLE'][k]) == 1 out = cmdpb.configWithKeys(portBreakOutConfigDbJson, \ ["Ethernet10","Ethernet11"]) - assert "INTERFACE" in out.keys() - for k in out['ACL_TABLE'].keys(): + assert "INTERFACE" in out + for k in list(out['ACL_TABLE'].keys()): # only ports must be chosen len(out['ACL_TABLE'][k]) == 1 return @@ -107,7 +109,7 @@ def config_mgmt_dpb(self, curConfig): # mock funcs cmdpb.writeConfigDB = MagicMock(return_value=True) cmdpb._verifyAsicDB = MagicMock(return_value=True) - import mock_tables.dbconnector + from .mock_tables import dbconnector return cmdpb def generate_args(self, portIdx, laneIdx, curMode, newMode): @@ -171,7 +173,7 @@ def updateConfig(self, conf, uconf): conf will be updated with uconf, i.e. config diff. ''' try: - for it in uconf.keys(): + for it in list(uconf.keys()): # if conf has the key if conf.get(it): # if marked for deletion @@ -253,35 +255,35 @@ def dpb_port8_1x100G_1x50G_2x25G_f_l(self, curConfig): force=True, loadDefConfig=True) # Expected Result delConfig and addConfig is pushed in order delConfig = { - u'PORT': { - u'Ethernet8': None + 'PORT': { + 'Ethernet8': None } } addConfig = { - u'ACL_TABLE': { - u'NO-NSW-PACL-V4': { - u'ports': ['Ethernet0', 'Ethernet4', 'Ethernet8', 'Ethernet10'] + 'ACL_TABLE': { + 'NO-NSW-PACL-V4': { + 'ports': ['Ethernet0', 'Ethernet4', 'Ethernet8', 'Ethernet10'] }, - u'NO-NSW-PACL-TEST': { - u'ports': ['Ethernet11'] + 'NO-NSW-PACL-TEST': { + 'ports': ['Ethernet11'] } }, - u'INTERFACE': { - u'Ethernet11|2a04:1111:40:a709::1/126': { - u'scope': u'global', - u'family': u'IPv6' + 'INTERFACE': { + 'Ethernet11|2a04:1111:40:a709::1/126': { + 'scope': 'global', + 'family': 'IPv6' }, - u'Ethernet11': {} + 'Ethernet11': {} }, - u'VLAN_MEMBER': { - u'Vlan100|Ethernet8': { - u'tagging_mode': u'untagged' + 'VLAN_MEMBER': { + 'Vlan100|Ethernet8': { + 'tagging_mode': 'untagged' }, - u'Vlan100|Ethernet11': { - u'tagging_mode': u'untagged' + 'Vlan100|Ethernet11': { + 'tagging_mode': 'untagged' } }, - u'PORT': { + 'PORT': { 'Ethernet8': { 'speed': '50000', 'lanes': '73,74' @@ -319,13 +321,13 @@ def dpb_port8_4x25G_1x100G_f(self, curConfig): force=False, loadDefConfig=False) # Expected Result delConfig and addConfig is pushed in order delConfig = { - u'PORT': { - u'Ethernet8': None, - u'Ethernet9': None, - u'Ethernet10': None, - u'Ethernet11': None - } + 'PORT': { + 'Ethernet8': None, + 'Ethernet9': None, + 'Ethernet10': None, + 'Ethernet11': None } + } addConfig = pJson self.checkResult(cmdpb, delConfig, addConfig) self.postUpdateConfig(curConfig, delConfig, addConfig) @@ -349,8 +351,8 @@ def dpb_port8_1x100G_4x25G(self, curConfig): force=False, loadDefConfig=False) # Expected Result delConfig and addConfig is pushed in order delConfig = { - u'PORT': { - u'Ethernet8': None + 'PORT': { + 'Ethernet8': None } } addConfig = pJson @@ -377,17 +379,17 @@ def dpb_port8_2x50G_1x100G_f(self, curConfig): force=True, loadDefConfig=False) # Expected Result delConfig and addConfig is pushed in order delConfig = { - u'ACL_TABLE': { - u'NO-NSW-PACL-V4': { - u'ports': ['Ethernet0', 'Ethernet4'] + 'ACL_TABLE': { + 'NO-NSW-PACL-V4': { + 'ports': ['Ethernet0', 'Ethernet4'] } }, - u'VLAN_MEMBER': { - u'Vlan100|Ethernet8': None + 'VLAN_MEMBER': { + 'Vlan100|Ethernet8': None }, - u'PORT': { - u'Ethernet8': None, - u'Ethernet10': None + 'PORT': { + 'Ethernet8': None, + 'Ethernet10': None } } addConfig = pJson @@ -435,38 +437,38 @@ def dpb_port8_4x25G_2x50G_f_l(self, curConfig): loadDefConfig=True) # Expected Result delConfig and addConfig is pushed in order delConfig = { - u'ACL_TABLE': { - u'NO-NSW-PACL-V4': { - u'ports': ['Ethernet0', 'Ethernet4'] + 'ACL_TABLE': { + 'NO-NSW-PACL-V4': { + 'ports': ['Ethernet0', 'Ethernet4'] }, - u'NO-NSW-PACL-TEST': { - u'ports': None + 'NO-NSW-PACL-TEST': { + 'ports': None } }, - u'INTERFACE': None, - u'VLAN_MEMBER': { - u'Vlan100|Ethernet8': None, - u'Vlan100|Ethernet11': None + 'INTERFACE': None, + 'VLAN_MEMBER': { + 'Vlan100|Ethernet8': None, + 'Vlan100|Ethernet11': None }, - u'PORT': { - u'Ethernet8': None, - u'Ethernet9': None, - u'Ethernet10': None, - u'Ethernet11': None + 'PORT': { + 'Ethernet8': None, + 'Ethernet9': None, + 'Ethernet10': None, + 'Ethernet11': None } } addConfig = { - u'ACL_TABLE': { - u'NO-NSW-PACL-V4': { - u'ports': ['Ethernet0', 'Ethernet4', 'Ethernet8', 'Ethernet10'] + 'ACL_TABLE': { + 'NO-NSW-PACL-V4': { + 'ports': ['Ethernet0', 'Ethernet4', 'Ethernet8', 'Ethernet10'] } }, - u'VLAN_MEMBER': { - u'Vlan100|Ethernet8': { - u'tagging_mode': u'untagged' + 'VLAN_MEMBER': { + 'Vlan100|Ethernet8': { + 'tagging_mode': 'untagged' } }, - u'PORT': { + 'PORT': { 'Ethernet8': { 'speed': '50000', 'lanes': '73,74' @@ -501,25 +503,25 @@ def dpb_port4_4x25G_2x50G_f_l(self, curConfig): loadDefConfig=True) # Expected Result delConfig and addConfig is pushed in order delConfig = { - u'ACL_TABLE': { - u'NO-NSW-PACL-V4': { - u'ports': ['Ethernet0', 'Ethernet8', 'Ethernet10'] + 'ACL_TABLE': { + 'NO-NSW-PACL-V4': { + 'ports': ['Ethernet0', 'Ethernet8', 'Ethernet10'] } }, - u'PORT': { - u'Ethernet4': None, - u'Ethernet5': None, - u'Ethernet6': None, - u'Ethernet7': None + 'PORT': { + 'Ethernet4': None, + 'Ethernet5': None, + 'Ethernet6': None, + 'Ethernet7': None } } addConfig = { - u'ACL_TABLE': { - u'NO-NSW-PACL-V4': { - u'ports': ['Ethernet0', 'Ethernet8', 'Ethernet10', 'Ethernet4'] + 'ACL_TABLE': { + 'NO-NSW-PACL-V4': { + 'ports': ['Ethernet0', 'Ethernet8', 'Ethernet10', 'Ethernet4'] } }, - u'PORT': { + 'PORT': { 'Ethernet4': { 'speed': '50000', 'lanes': '69,70' diff --git a/tests/conftest.py b/tests/conftest.py index f2efb1e0e1e6..31551b3e3b53 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,16 +1,15 @@ +import json import os import sys -import json +from unittest import mock - -import mock import pytest - -import mock_tables.dbconnector - from sonic_py_common import device_info from swsssdk import ConfigDBConnector +from .mock_tables import dbconnector + + test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) sys.path.insert(0, modules_path) @@ -72,9 +71,9 @@ def setup_single_broacom_asic(): @pytest.fixture def setup_t1_topo(): - mock_tables.dbconnector.topo = "t1" + dbconnector.topo = "t1" yield - mock_tables.dbconnector.topo = None + dbconnector.topo = None @pytest.fixture def setup_single_bgp_instance(request): diff --git a/tests/console_test.py b/tests/console_test.py index f672bc9cd79a..03887cf42a66 100644 --- a/tests/console_test.py +++ b/tests/console_test.py @@ -2,8 +2,8 @@ import sys import subprocess import pexpect +from unittest import mock -import mock import pytest import config.main as config @@ -421,7 +421,7 @@ def test_sys_info_provider_init_device_prefix_plugin_nonexists(self): @mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', mock.MagicMock(return_value=("dummy_path", None))) @mock.patch('os.path.exists', mock.MagicMock(return_value=True)) def test_sys_info_provider_init_device_prefix_plugin(self): - with mock.patch("__builtin__.open", mock.mock_open(read_data="C0-")): + with mock.patch("builtins.open", mock.mock_open(read_data="C0-")): SysInfoProvider.init_device_prefix() assert SysInfoProvider.DEVICE_PREFIX == "/dev/C0-" SysInfoProvider.DEVICE_PREFIX = "/dev/ttyUSB" @@ -446,8 +446,8 @@ def test_sys_info_provider_list_console_ttys_device_not_exists(self): def test_sys_info_provider_list_active_console_processes(self): SysInfoProvider.DEVICE_PREFIX == "/dev/ttyUSB" procs = SysInfoProvider.list_active_console_processes() - assert len(procs.keys()) == 1 - assert "0" in procs.keys() + assert len(procs) == 1 + assert "0" in procs assert procs["0"] == ("8", "Mon Nov 2 04:29:41 2020") active_process_output = "13751 Wed Mar 6 08:31:35 2019 /usr/bin/sudo picocom -b 9600 -f n /dev/ttyUSB1" diff --git a/tests/counterpoll_test.py b/tests/counterpoll_test.py index f08695e9bd19..d179553abc32 100644 --- a/tests/counterpoll_test.py +++ b/tests/counterpoll_test.py @@ -14,7 +14,8 @@ sys.path.insert(0, test_path) sys.path.insert(0, modules_path) -import mock_tables.dbconnector +from .mock_tables import dbconnector + import counterpoll.main as counterpoll expected_counterpoll_show = """Type Interval (in ms) Status diff --git a/tests/crm_test.py b/tests/crm_test.py index 57e3f72ad5e9..2ddb1b21084f 100644 --- a/tests/crm_test.py +++ b/tests/crm_test.py @@ -444,10 +444,10 @@ Table ID Resource Name Used Count Available Count --------------- --------------- ------------ ----------------- -0x7000000000670 acl_entry 0 1024 -0x7000000000670 acl_counter 0 1280 0x700000000063f acl_entry 0 2048 0x700000000063f acl_counter 0 2048 +0x7000000000670 acl_entry 0 1024 +0x7000000000670 acl_counter 0 1280 """ @@ -549,10 +549,10 @@ Table ID Resource Name Used Count Available Count --------------- --------------- ------------ ----------------- -0x7000000000670 acl_entry 0 1024 -0x7000000000670 acl_counter 0 1280 0x700000000063f acl_entry 0 2048 0x700000000063f acl_counter 0 2048 +0x7000000000670 acl_entry 0 1024 +0x7000000000670 acl_counter 0 1280 """ @@ -1007,8 +1007,9 @@ def setup_class(cls): print("SETUP") os.environ["UTILITIES_UNIT_TESTING"] = "2" os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = "multi_asic" - import mock_tables.mock_multi_asic - mock_tables.dbconnector.load_namespace_config() + from .mock_tables import dbconnector + from .mock_tables import mock_multi_asic + dbconnector.load_namespace_config() def test_crm_show_summary(self): runner = CliRunner() @@ -1296,4 +1297,4 @@ def teardown_class(cls): print("TEARDOWN") os.environ["UTILITIES_UNIT_TESTING"] = "0" os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = "" - import mock_tables.mock_single_asic + from .mock_tables import mock_single_asic diff --git a/tests/drops_group_test.py b/tests/drops_group_test.py index 06bbfdcc3b3e..21e5c2777a61 100644 --- a/tests/drops_group_test.py +++ b/tests/drops_group_test.py @@ -13,7 +13,8 @@ import show.main as show import clear.main as clear -expected_counter_capabilities = """Counter Type Total +expected_counter_capabilities = """\ +Counter Type Total ------------------- ------- PORT_INGRESS_DROPS 4 SWITCH_EGRESS_DROPS 2 @@ -28,7 +29,8 @@ L3_ANY """ -expected_counter_configuration = """Counter Alias Group Type Reasons Description +expected_counter_configuration = """\ +Counter Alias Group Type Reasons Description ----------------- ----------------- ------------ ------------------- --------- -------------------------------------------------- DEBUG_0 DEBUG_0 N/A PORT_INGRESS_DROPS None N/A DEBUG_1 SWITCH_DROPS PACKET_DROPS SWITCH_EGRESS_DROPS None Outgoing packet drops, tracked at the switch level @@ -36,16 +38,18 @@ lowercase_counter lowercase_counter N/A SWITCH_EGRESS_DROPS None N/A """ -expected_counter_configuration_with_group = """Counter Alias Group Type Reasons Description +expected_counter_configuration_with_group = """\ +Counter Alias Group Type Reasons Description --------- ------------ ------------ ------------------- --------- -------------------------------------------------- DEBUG_1 SWITCH_DROPS PACKET_DROPS SWITCH_EGRESS_DROPS None Outgoing packet drops, tracked at the switch level """ -expected_counts = """ IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_2 DEBUG_0 +expected_counts = """\ + IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 --------- ------- -------- ---------- -------- ---------- --------- --------- -Ethernet0 D 10 100 0 0 20 80 -Ethernet4 N/A 0 1000 0 0 100 800 -Ethernet8 N/A 100 10 0 0 0 10 +Ethernet0 D 10 100 0 0 80 20 +Ethernet4 N/A 0 1000 0 0 800 100 +Ethernet8 N/A 100 10 0 0 10 0 DEVICE SWITCH_DROPS lowercase_counter ---------------- -------------- ------------------- @@ -58,15 +62,17 @@ sonic_drops_test 1000 """ -expected_counts_with_type = """ IFACE STATE RX_ERR RX_DROPS DEBUG_2 DEBUG_0 +expected_counts_with_type = """\ + IFACE STATE RX_ERR RX_DROPS DEBUG_0 DEBUG_2 --------- ------- -------- ---------- --------- --------- -Ethernet0 D 10 100 20 80 -Ethernet4 N/A 0 1000 100 800 -Ethernet8 N/A 100 10 0 10 +Ethernet0 D 10 100 80 20 +Ethernet4 N/A 0 1000 800 100 +Ethernet8 N/A 100 10 10 0 """ -expected_counts_with_clear = """ IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_2 DEBUG_0 +expected_counts_with_clear = """\ + IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 --------- ------- -------- ---------- -------- ---------- --------- --------- Ethernet0 D 0 0 0 0 0 0 Ethernet4 N/A 0 0 0 0 0 0 diff --git a/tests/fgnhg_test.py b/tests/fgnhg_test.py index e120d60e47d0..bfa08509dd83 100644 --- a/tests/fgnhg_test.py +++ b/tests/fgnhg_test.py @@ -12,7 +12,7 @@ +-----------------+--------------------+----------------+ | FG_NHG_PREFIX | Next Hop | Hash buckets | +=================+====================+================+ -| 100.50.25.12/32 | 200.200.200.4 | 0 | +| fc:5::/128 | 200:200:200:200::4 | 0 | | | | 1 | | | | 2 | | | | 3 | @@ -29,7 +29,7 @@ | | | 14 | | | | 15 | +-----------------+--------------------+----------------+ -| 100.50.25.12/32 | 200.200.200.5 | 16 | +| fc:5::/128 | 200:200:200:200::5 | 16 | | | | 17 | | | | 18 | | | | 19 | @@ -46,7 +46,7 @@ | | | 30 | | | | 31 | +-----------------+--------------------+----------------+ -| fc:5::/128 | 200:200:200:200::4 | 0 | +| 100.50.25.12/32 | 200.200.200.4 | 0 | | | | 1 | | | | 2 | | | | 3 | @@ -63,7 +63,7 @@ | | | 14 | | | | 15 | +-----------------+--------------------+----------------+ -| fc:5::/128 | 200:200:200:200::5 | 16 | +| 100.50.25.12/32 | 200.200.200.5 | 16 | | | | 17 | | | | 18 | | | | 19 | @@ -166,12 +166,12 @@ +-----------------+--------------------+ | FG_NHG_PREFIX | Active Next Hops | +=================+====================+ -| 100.50.25.12/32 | 200.200.200.4 | -| | 200.200.200.5 | -+-----------------+--------------------+ | fc:5::/128 | 200:200:200:200::4 | | | 200:200:200:200::5 | +-----------------+--------------------+ +| 100.50.25.12/32 | 200.200.200.4 | +| | 200.200.200.5 | ++-----------------+--------------------+ """ show_fgnhgv4_active_hops_output="""\ diff --git a/tests/filter_fdb_entries_test.py b/tests/filter_fdb_entries_test.py index fbbff67ea9ea..13cb8a44ca72 100644 --- a/tests/filter_fdb_entries_test.py +++ b/tests/filter_fdb_entries_test.py @@ -6,7 +6,7 @@ import subprocess from collections import defaultdict -from filter_fdb_input.test_vectors import filterFdbEntriesTestVector +from .filter_fdb_input.test_vectors import filterFdbEntriesTestVector from fdbutil.filter_fdb_entries import main as filterFdbMain class TestFilterFdbEntries(object): @@ -92,6 +92,7 @@ def __runCommand(self, cmds): process = subprocess.Popen( cmds, shell=False, + text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) diff --git a/tests/gearbox_test.py b/tests/gearbox_test.py index ba7975fed378..658997704f3d 100644 --- a/tests/gearbox_test.py +++ b/tests/gearbox_test.py @@ -9,7 +9,7 @@ sys.path.insert(0, test_path) sys.path.insert(0, modules_path) -import mock_tables.dbconnector # required by tests +from .mock_tables import dbconnector import show.main as show @@ -25,7 +25,7 @@ def setUp(self): def test_gearbox_phys_status_validation(self): result = self.runner.invoke(show.cli.commands["gearbox"].commands["phys"].commands["status"], []) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = ( "PHY Id Name Firmware\n" "-------- ------- ----------\n" @@ -36,7 +36,7 @@ def test_gearbox_phys_status_validation(self): def test_gearbox_interfaces_status_validation(self): result = self.runner.invoke(show.cli.commands["gearbox"].commands["interfaces"].commands["status"], []) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = ( "PHY Id Interface MAC Lanes MAC Lane Speed PHY Lanes PHY Lane Speed Line Lanes Line Lane Speed Oper Admin\n" "-------- ----------- --------------- ---------------- --------------- ---------------- ------------ ----------------- ------ -------\n" diff --git a/tests/intfstat_test.py b/tests/intfstat_test.py index fcf20620e8d6..2f63c7aeaf5c 100644 --- a/tests/intfstat_test.py +++ b/tests/intfstat_test.py @@ -10,7 +10,8 @@ sys.path.insert(0, test_path) sys.path.insert(0, modules_path) -import mock_tables.dbconnector +from .mock_tables import dbconnector + import show.main as show import clear.main as clear diff --git a/tests/intfutil_test.py b/tests/intfutil_test.py index 53e878dcd162..c607ff5b8ed3 100644 --- a/tests/intfutil_test.py +++ b/tests/intfutil_test.py @@ -83,7 +83,7 @@ def test_intf_status(self): assert result.output == show_interface_status_output # Test 'intfutil status' - output = subprocess.check_output('intfutil -c status', stderr=subprocess.STDOUT, shell=True) + output = subprocess.check_output('intfutil -c status', stderr=subprocess.STDOUT, shell=True, text=True) print(output) assert result.output == show_interface_status_output @@ -155,7 +155,7 @@ def test_show_interfaces_description_Ethernet0_verbose(self): def test_subintf_status(self): # Test 'show subinterfaces status' result = self.runner.invoke(show.cli.commands["subinterfaces"].commands["status"], []) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = ( "Sub port interface Speed MTU Vlan Admin Type\n" "-------------------- ------- ----- ------ ------- --------------------\n" @@ -164,14 +164,14 @@ def test_subintf_status(self): self.assertEqual(result.output.strip(), expected_output) # Test 'intfutil status subport' - output = subprocess.check_output('intfutil -c status -i subport', stderr=subprocess.STDOUT, shell=True) - print >> sys.stderr, output + output = subprocess.check_output('intfutil -c status -i subport', stderr=subprocess.STDOUT, shell=True, text=True) + print(output, file=sys.stderr) self.assertEqual(output.strip(), expected_output) # Test 'show subinterfaces status --verbose' def test_subintf_status_verbose(self): result = self.runner.invoke(show.cli.commands["subinterfaces"].commands["status"], ["--verbose"]) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = "Command: intfutil -c status -i subport" self.assertEqual(result.output.split('\n')[0], expected_output) @@ -180,7 +180,7 @@ def test_subintf_status_verbose(self): def test_single_subintf_status(self): # Test 'show subinterfaces status Ethernet0.10' result = self.runner.invoke(show.cli.commands["subinterfaces"].commands["status"], ["Ethernet0.10"]) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = ( "Sub port interface Speed MTU Vlan Admin Type\n" "-------------------- ------- ----- ------ ------- --------------------\n" @@ -189,14 +189,14 @@ def test_single_subintf_status(self): self.assertEqual(result.output.strip(), expected_output) # Test 'intfutil status Ethernet0.10' - output = subprocess.check_output('intfutil -c status -i Ethernet0.10', stderr=subprocess.STDOUT, shell=True) - print >> sys.stderr, output + output = subprocess.check_output('intfutil -c status -i Ethernet0.10', stderr=subprocess.STDOUT, shell=True, text=True) + print(output, file=sys.stderr) self.assertEqual(output.strip(), expected_output) # Test '--verbose' status of single sub interface def test_single_subintf_status_verbose(self): result = self.runner.invoke(show.cli.commands["subinterfaces"].commands["status"], ["Ethernet0.10", "--verbose"]) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = "Command: intfutil -c status -i Ethernet0.10" self.assertEqual(result.output.split('\n')[0], expected_output) @@ -206,7 +206,7 @@ def test_single_subintf_status_alias_mode(self): os.environ["SONIC_CLI_IFACE_MODE"] = "alias" result = self.runner.invoke(show.cli.commands["subinterfaces"].commands["status"], ["etp1.10"]) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = ( "Sub port interface Speed MTU Vlan Admin Type\n" "-------------------- ------- ----- ------ ------- --------------------\n" @@ -221,7 +221,7 @@ def test_single_subintf_status_alias_mode_verbose(self): os.environ["SONIC_CLI_IFACE_MODE"] = "alias" result = self.runner.invoke(show.cli.commands["subinterfaces"].commands["status"], ["etp1.10", "--verbose"]) - print >> sys.stderr, result.output + print(result.output, file=sys.stderr) expected_output = "Command: intfutil -c status -i Ethernet0.10" self.assertEqual(result.output.split('\n')[0], expected_output) diff --git a/tests/kube.py b/tests/kube_test.py similarity index 97% rename from tests/kube.py rename to tests/kube_test.py index fe5795524738..2a39c241d6e6 100644 --- a/tests/kube.py +++ b/tests/kube_test.py @@ -34,8 +34,8 @@ def setup_class(cls): def __check_res(self, result, info, op): print("Running test: {}".format(info)) - print result.exit_code - print result.output + print(result.exit_code) + print(result.output) assert result.exit_code == 0 assert "\n".join([ l.rstrip() for l in result.output.split('\n')]) == op diff --git a/tests/mock_tables/dbconnector.py b/tests/mock_tables/dbconnector.py index 55fff38c368b..871d166acb66 100644 --- a/tests/mock_tables/dbconnector.py +++ b/tests/mock_tables/dbconnector.py @@ -2,8 +2,8 @@ import json import os import sys +from unittest import mock -import mock import mockredis import redis import swsssdk @@ -12,12 +12,6 @@ from swsscommon import swsscommon -if sys.version_info >= (3, 0): - long = int - xrange = range - basestring = str - from functools import reduce - topo = None def clean_up_config(): @@ -110,7 +104,7 @@ def __init__(self, *args, **kwargs): with open(fname) as f: js = json.load(f) for k, v in js.items(): - if v.has_key('expireat') and v.has_key('ttl') and v.has_key('type') and v.has_key('value'): + if 'expireat' in v and 'ttl' in v and 'type' in v and 'value' in v: # database is in redis-dump format if v['type'] == 'hash': # ignore other types for now since sonic has hset keys only in the db @@ -127,11 +121,11 @@ def _encode(self, value): "Return a bytestring representation of the value. Taken from redis-py connection.py" if isinstance(value, bytes): return value - elif isinstance(value, (int, long)): + elif isinstance(value, int): value = str(value).encode('utf-8') elif isinstance(value, float): value = repr(value).encode('utf-8') - elif not isinstance(value, basestring): + elif not isinstance(value, str): value = str(value).encode('utf-8') elif not self.decode_responses: value = value.encode('utf-8', 'strict') @@ -150,7 +144,7 @@ def keys(self, pattern='*'): regex = re.compile(regex) # Find every key that matches the pattern - return [key for key in self.redis.keys() if regex.match(key)] + return [key for key in list(self.redis.keys()) if regex.match(key)] swsssdk.interface.DBInterface._subscribe_keyspace_notification = _subscribe_keyspace_notification diff --git a/tests/mock_tables/mock_multi_asic.py b/tests/mock_tables/mock_multi_asic.py index 801024821652..ad694003f77b 100644 --- a/tests/mock_tables/mock_multi_asic.py +++ b/tests/mock_tables/mock_multi_asic.py @@ -1,5 +1,6 @@ # MONKEY PATCH!!! -import mock +from unittest import mock + from sonic_py_common import multi_asic def mock_get_num_asics(): diff --git a/tests/mock_tables/mock_single_asic.py b/tests/mock_tables/mock_single_asic.py index 418b7f63fada..f6607cf362f0 100644 --- a/tests/mock_tables/mock_single_asic.py +++ b/tests/mock_tables/mock_single_asic.py @@ -1,5 +1,6 @@ # MONKEY PATCH!!! -import mock +from unittest import mock + from sonic_py_common import multi_asic def mock_get_num_asics(): diff --git a/tests/multi_asic_intfutil_test.py b/tests/multi_asic_intfutil_test.py index 2abe4663f495..6e1afb4cfab4 100644 --- a/tests/multi_asic_intfutil_test.py +++ b/tests/multi_asic_intfutil_test.py @@ -3,7 +3,7 @@ from click.testing import CliRunner -from utils import get_result_and_return_code +from .utils import get_result_and_return_code root_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(root_path) diff --git a/tests/neighbor_advertiser_test.py b/tests/neighbor_advertiser_test.py index 3f6cb7b2cf19..3a3aeba39fc1 100644 --- a/tests/neighbor_advertiser_test.py +++ b/tests/neighbor_advertiser_test.py @@ -17,33 +17,35 @@ class TestNeighborAdvertiser(object): def set_up(self): neighbor_advertiser.connect_config_db() neighbor_advertiser.connect_app_db() - + def test_neighbor_advertiser_slice(self, set_up): output = neighbor_advertiser.construct_neighbor_advertiser_slice() - expected_output = dict({ - 'respondingSchemes': {'durationInSec': 300}, - 'switchInfo': {'ipv6Addr': '', 'hwSku': 'Mellanox-SN3800-D112C8', 'ipv4Addr': '', 'name': 'sonic-switch'}, - 'vlanInterfaces': [{ - 'ipv4AddrMappings': [ - {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': '192.168.0.1', 'ipPrefixLen': '32'} - ], - 'ipv6AddrMappings': [ - {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fc02:1000::1', 'ipPrefixLen': '128'} - ], - 'vxlanId': u'1000', - 'vlanId': u'1000', - 'vxlanPort': '13550' - }, - { - 'ipv4AddrMappings': [ - {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': '192.168.0.10', 'ipPrefixLen': '21'} - ], - 'ipv6AddrMappings': [ - {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fc02:1011::1', 'ipPrefixLen': '64'} - ], - 'vxlanId': u'2000', - 'vlanId': u'2000', - 'vxlanPort': '13550' - }] - }) + expected_output = dict( + { + 'respondingSchemes': {'durationInSec': 300}, + 'switchInfo': {'ipv6Addr': '', 'hwSku': 'Mellanox-SN3800-D112C8', 'ipv4Addr': '', 'name': 'sonic-switch'}, + 'vlanInterfaces': [{ + 'ipv4AddrMappings': [ + {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': '192.168.0.1', 'ipPrefixLen': '32'} + ], + 'ipv6AddrMappings': [ + {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fc02:1000::1', 'ipPrefixLen': '128'} + ], + 'vxlanId': '1000', + 'vlanId': '1000', + 'vxlanPort': '13550' + }, + { + 'ipv4AddrMappings': [ + {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': '192.168.0.10', 'ipPrefixLen': '21'} + ], + 'ipv6AddrMappings': [ + {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fc02:1011::1', 'ipPrefixLen': '64'} + ], + 'vxlanId': '2000', + 'vlanId': '2000', + 'vxlanPort': '13550' + }] + } + ) assert output == expected_output diff --git a/tests/pfcstat_test.py b/tests/pfcstat_test.py index 6b0e556e0645..8a63539efbc5 100644 --- a/tests/pfcstat_test.py +++ b/tests/pfcstat_test.py @@ -7,7 +7,7 @@ import show.main as show -from utils import get_result_and_return_code +from .utils import get_result_and_return_code test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) diff --git a/tests/pfcwd_test.py b/tests/pfcwd_test.py index 4fdafc9aeebe..e96d5b7e0c30 100644 --- a/tests/pfcwd_test.py +++ b/tests/pfcwd_test.py @@ -5,7 +5,7 @@ from click.testing import CliRunner from utilities_common.db import Db -from pfcwd_input.pfcwd_test_vectors import ( +from .pfcwd_input.pfcwd_test_vectors import ( testData, show_pfcwd_stats_all, show_pfc_config_all, show_pfcwd_stats_with_queues, show_pfcwd_config_with_ports ) diff --git a/tests/portstat_test.py b/tests/portstat_test.py index 487cdca1bc73..0413c7e238e5 100644 --- a/tests/portstat_test.py +++ b/tests/portstat_test.py @@ -5,7 +5,7 @@ import clear.main as clear import show.main as show -from utils import get_result_and_return_code +from .utils import get_result_and_return_code root_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(root_path) diff --git a/tests/sflow_test.py b/tests/sflow_test.py index 724e22ef9e7f..c4bbba7de242 100644 --- a/tests/sflow_test.py +++ b/tests/sflow_test.py @@ -1,7 +1,7 @@ import os import sys import pytest -import mock +from unittest import mock from click.testing import CliRunner from utilities_common.db import Db diff --git a/tests/sfp_test.py b/tests/sfp_test.py index 1c81ad5051eb..168f8fcbd1f6 100644 --- a/tests/sfp_test.py +++ b/tests/sfp_test.py @@ -1,7 +1,8 @@ import sys import os from click.testing import CliRunner -import mock_tables.dbconnector + +from .mock_tables import dbconnector test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) diff --git a/tests/show_breakout_test.py b/tests/show_breakout_test.py index 5a55c81b6b31..0493d7e2936b 100644 --- a/tests/show_breakout_test.py +++ b/tests/show_breakout_test.py @@ -9,7 +9,8 @@ sys.path.insert(0, test_path) sys.path.insert(0, modules_path) -import mock_tables.dbconnector +from .mock_tables import dbconnector + import show.main as show # Expected output for 'show breakout current-mode' diff --git a/tests/show_platform_test.py b/tests/show_platform_test.py index ee30ea6825b3..bbd83249878b 100644 --- a/tests/show_platform_test.py +++ b/tests/show_platform_test.py @@ -1,8 +1,8 @@ import os import sys import textwrap +from unittest import mock -import mock from click.testing import CliRunner test_path = os.path.dirname(os.path.abspath(__file__)) diff --git a/tests/sku_create_test.py b/tests/sku_create_test.py index 562ff8cdb538..8f4396444121 100644 --- a/tests/sku_create_test.py +++ b/tests/sku_create_test.py @@ -12,7 +12,7 @@ input_path = os.path.join(modules_path, "tests/sku_create_input") output_dir_path = os.path.join(modules_path, "tests/sku_create_input/Mellanox-SN2700-D48C8_NEW") sku_def_file = os.path.join(input_path, "Mellanox-SN2700-D48C8.xml") -sku_create_script = os.path.join(scripts_path, "sonic_sku_create.py") +sku_create_script = "sonic_sku_create.py" output_file_path = os.path.join(modules_path, "tests/sku_create_input/Mellanox-SN2700-D48C8_NEW/port_config.ini") model_file_path = os.path.join(modules_path, "tests/sku_create_input/Mellanox-SN2700-D48C8/port_config.ini") @@ -58,8 +58,8 @@ def test_no_param(self): my_command = sku_create_script + " -f " + sku_def_file + " -d " + input_path #Test case execution without stdout - result = subprocess.check_output(my_command,stderr=subprocess.STDOUT,shell=True) - print result + result = subprocess.check_output(my_command, stderr=subprocess.STDOUT, shell=True, text=True) + print(result) #Check if the Output file exists if (os.path.exists(output_file_path)): diff --git a/tests/system_health_test.py b/tests/system_health_test.py index ea80c15a031c..82e57f7ce38e 100644 --- a/tests/system_health_test.py +++ b/tests/system_health_test.py @@ -3,7 +3,8 @@ import click from click.testing import CliRunner -import mock_tables.dbconnector + +from .mock_tables import dbconnector test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) @@ -149,20 +150,20 @@ def test_health_monitor(self): buffermgrd OK Process portmgrd OK Process staticd OK Process -vlanmgrd OK Process +bgpd OK Process lldp_syncd OK Process bgpcfgd OK Process snmp_subagent OK Process root-overlay OK Filesystem fpmsyncd OK Process -bgpd OK Process +vlanmgrd OK Process nbrmgrd OK Process -fan12 OK Fan +PSU 2 OK PSU psu_1_fan_1 OK Fan psu_2_fan_1 OK Fan fan11 OK Fan fan10 OK Fan -PSU 2 OK PSU +fan12 OK Fan ASIC OK ASIC fan1 OK Fan PSU 1 OK PSU @@ -218,21 +219,21 @@ def test_health_detail(self): buffermgrd OK Process portmgrd OK Process staticd OK Process -vlanmgrd OK Process +bgpd OK Process lldp_syncd OK Process bgpcfgd OK Process snmp_subagent OK Process root-overlay OK Filesystem fpmsyncd OK Process -bgpd OK Process +vlanmgrd OK Process nbrmgrd OK Process PSU 2 Not OK PSU PSU 1 Not OK PSU -fan12 OK Fan psu_1_fan_1 OK Fan psu_2_fan_1 OK Fan fan11 OK Fan fan10 OK Fan +fan12 OK Fan ASIC OK ASIC fan1 OK Fan fan3 OK Fan @@ -288,20 +289,20 @@ def test_health_detail(self): buffermgrd OK Process portmgrd OK Process staticd OK Process -vlanmgrd OK Process +bgpd OK Process lldp_syncd OK Process bgpcfgd OK Process snmp_subagent OK Process root-overlay OK Filesystem fpmsyncd OK Process -bgpd OK Process +vlanmgrd OK Process nbrmgrd OK Process -fan12 OK Fan +PSU 2 OK PSU psu_1_fan_1 OK Fan psu_2_fan_1 OK Fan fan11 OK Fan fan10 OK Fan -PSU 2 OK PSU +fan12 OK Fan ASIC OK ASIC fan1 OK Fan PSU 1 OK PSU diff --git a/tests/utils.py b/tests/utils.py index 92ee422e9c95..ff40865f069e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -8,7 +8,7 @@ def get_result_and_return_code(cmd): return_code = 0 try: output = subprocess.check_output( - cmd, stderr=subprocess.STDOUT, shell=True) + cmd, stderr=subprocess.STDOUT, shell=True, text=True) except subprocess.CalledProcessError as e: return_code = e.returncode # store only the error, no need for the traceback diff --git a/tests/vlan_test.py b/tests/vlan_test.py index b37e438108e0..72a66ccdb320 100644 --- a/tests/vlan_test.py +++ b/tests/vlan_test.py @@ -70,23 +70,23 @@ show_vlan_config_output="""\ Name VID Member Mode -------- ----- ---------- -------- +Vlan1000 1000 Ethernet4 untagged Vlan1000 1000 Ethernet8 untagged -Vlan1000 1000 Ethernet16 untagged Vlan1000 1000 Ethernet12 untagged -Vlan1000 1000 Ethernet4 untagged -Vlan2000 2000 Ethernet28 untagged +Vlan1000 1000 Ethernet16 untagged Vlan2000 2000 Ethernet24 untagged +Vlan2000 2000 Ethernet28 untagged """ show_vlan_config_in_alias_mode_output="""\ Name VID Member Mode -------- ----- -------- -------- +Vlan1000 1000 etp2 untagged Vlan1000 1000 etp3 untagged -Vlan1000 1000 etp5 untagged Vlan1000 1000 etp4 untagged -Vlan1000 1000 etp2 untagged -Vlan2000 2000 etp8 untagged +Vlan1000 1000 etp5 untagged Vlan2000 2000 etp7 untagged +Vlan2000 2000 etp8 untagged """ config_vlan_add_dhcp_relay_output="""\ diff --git a/tests/watermarkstat_test.py b/tests/watermarkstat_test.py index 21ec3ad14828..faf3be11d443 100644 --- a/tests/watermarkstat_test.py +++ b/tests/watermarkstat_test.py @@ -3,9 +3,9 @@ import sys import show.main as show - from click.testing import CliRunner -from wm_input.wm_test_vectors import * + +from .wm_input.wm_test_vectors import * test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) diff --git a/undebug/main.py b/undebug/main.py index 9d77866fa891..3810add68b9b 100644 --- a/undebug/main.py +++ b/undebug/main.py @@ -3,7 +3,7 @@ def run_command(command, pager=False): click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green')) - p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + p = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) output = p.stdout.read() if pager: click.echo_via_pager(output) @@ -22,7 +22,7 @@ def cli(): pass -p = subprocess.check_output(["sudo vtysh -c 'show version'"], shell=True) +p = subprocess.check_output(["sudo vtysh -c 'show version'"], shell=True, text=True) if 'FRRouting' in p: # # 'bgp' group for FRR ### diff --git a/utilities_common/bgp_util.py b/utilities_common/bgp_util.py index 39c0f7d047b7..82fc0729644e 100644 --- a/utilities_common/bgp_util.py +++ b/utilities_common/bgp_util.py @@ -15,7 +15,7 @@ def is_ipv4_address(ip_address): """ Checks if given ip is ipv4 - :param ip_address: unicode ipv4 + :param ip_address: str ipv4 :return: bool """ try: @@ -28,7 +28,7 @@ def is_ipv4_address(ip_address): def is_ipv6_address(ip_address): """ Checks if given ip is ipv6 - :param ip_address: unicode ipv6 + :param ip_address: str ipv6 :return: bool """ try: @@ -48,12 +48,12 @@ def get_dynamic_neighbor_subnet(db): v6_subnet = {} neighbor_data = db.get_table('BGP_PEER_RANGE') try: - for entry in neighbor_data.keys(): + for entry in list(neighbor_data.keys()): new_key = neighbor_data[entry]['ip_range'][0] new_value = neighbor_data[entry]['name'] - if is_ipv4_address(unicode(neighbor_data[entry]['src_address'])): + if is_ipv4_address(neighbor_data[entry]['src_address']): v4_subnet[new_key] = new_value - elif is_ipv6_address(unicode(neighbor_data[entry]['src_address'])): + elif is_ipv6_address(neighbor_data[entry]['src_address']): v6_subnet[new_key] = new_value dynamic_neighbor[constants.IPV4] = v4_subnet dynamic_neighbor[constants.IPV6] = v6_subnet @@ -81,24 +81,20 @@ def get_bgp_neighbors_dict(namespace=multi_asic.DEFAULT_NAMESPACE): def get_bgp_neighbor_ip_to_name(ip, static_neighbors, dynamic_neighbors): """ return neighbor name for the ip provided - :param ip: ip address unicode + :param ip: ip address str :param static_neighbors: statically defined bgp neighbors dict :param dynamic_neighbors: subnet of dynamically defined neighbors dict :return: name of neighbor """ - if ip in static_neighbors.keys(): + if ip in static_neighbors: return static_neighbors[ip] - elif is_ipv4_address(unicode(ip)): - for subnet in dynamic_neighbors[constants.IPV4].keys(): - if ipaddress.IPv4Address( - unicode(ip)) in ipaddress.IPv4Network( - unicode(subnet)): + elif is_ipv4_address(ip): + for subnet in list(dynamic_neighbors[constants.IPV4].keys()): + if ipaddress.IPv4Address(ip) in ipaddress.IPv4Network(subnet): return dynamic_neighbors[constants.IPV4][subnet] - elif is_ipv6_address(unicode(ip)): - for subnet in dynamic_neighbors[constants.IPV6].keys(): - if ipaddress.IPv6Address( - unicode(ip)) in ipaddress.IPv6Network( - unicode(subnet)): + elif is_ipv6_address(ip): + for subnet in list(dynamic_neighbors[constants.IPV6].keys()): + if ipaddress.IPv6Address(ip) in ipaddress.IPv6Network(subnet): return dynamic_neighbors[constants.IPV6][subnet] else: return "NotAvailable" @@ -144,9 +140,9 @@ def get_neighbor_dict_from_table(db, table_name): neighbor_dict = {} neighbor_data = db.get_table(table_name) try: - for entry in neighbor_data.keys(): + for entry in list(neighbor_data.keys()): neighbor_dict[entry] = neighbor_data[entry].get( - 'name') if 'name' in neighbor_data[entry].keys() else 'NotAvailable' + 'name') if 'name' in neighbor_data[entry] else 'NotAvailable' return neighbor_dict except Exception: return neighbor_dict @@ -214,7 +210,7 @@ def display_bgp_summary(bgp_summary, af): click.echo("\nIP{} Unicast Summary:".format(af)) # display the bgp instance information for router_info in bgp_summary['router_info']: - for k in router_info.keys(): + for k in list(router_info.keys()): v = router_info[k] instance = "{}: ".format(k) if k is not "" else "" click.echo( @@ -273,7 +269,7 @@ def process_bgp_summary_json(bgp_summary, cmd_output, device): {device.current_namespace: router_info}) # store all the peers in the list - for peer_ip, value in cmd_output['peers'].iteritems(): + for peer_ip, value in cmd_output['peers'].items(): peers = [] # if display option is 'frontend', internal bgp neighbors will not # be displayed diff --git a/utilities_common/cli.py b/utilities_common/cli.py index cf4b7af2a65f..0ca3d5397d8f 100644 --- a/utilities_common/cli.py +++ b/utilities_common/cli.py @@ -1,3 +1,4 @@ +import configparser import os import re import subprocess @@ -8,9 +9,8 @@ import netaddr from natsort import natsorted - -from utilities_common.db import Db from sonic_py_common import multi_asic +from utilities_common.db import Db VLAN_SUB_INTERFACE_SEPARATOR = '.' @@ -54,10 +54,6 @@ def get_command(self, ctx, cmd_name): ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) -try: - import ConfigParser as configparser -except ImportError: - import configparser # This is from the aliases example: # https://github.com/pallets/click/blob/57c6f09611fc47ca80db0bd010f05998b3c0aa95/examples/aliases/aliases.py @@ -134,7 +130,7 @@ def __init__(self, db=None): click.echo(message="Warning: failed to retrieve PORT table from ConfigDB!", err=True) self.port_dict = {} - for port_name in self.port_dict.keys(): + for port_name in list(self.port_dict.keys()): try: if self.alias_max_length < len( self.port_dict[port_name]['alias']): @@ -156,7 +152,7 @@ def name_to_alias(self, interface_name): # interface_name holds the parent port name interface_name = interface_name[:sub_intf_sep_idx] - for port_name in self.port_dict.keys(): + for port_name in list(self.port_dict.keys()): if interface_name == port_name: return self.port_dict[port_name]['alias'] if sub_intf_sep_idx == -1 \ else self.port_dict[port_name]['alias'] + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id @@ -177,7 +173,7 @@ def alias_to_name(self, interface_alias): # interface_alias holds the parent port alias interface_alias = interface_alias[:sub_intf_sep_idx] - for port_name in self.port_dict.keys(): + for port_name in list(self.port_dict.keys()): if interface_alias == self.port_dict[port_name]['alias']: return port_name if sub_intf_sep_idx == -1 else port_name + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id @@ -216,7 +212,7 @@ def is_valid_port(config_db, port): """Check if port is in PORT table""" port_table = config_db.get_table('PORT') - if port in port_table.keys(): + if port in port_table: return True return False @@ -225,7 +221,7 @@ def is_valid_portchannel(config_db, port): """Check if port is in PORT_CHANNEL table""" pc_table = config_db.get_table('PORTCHANNEL') - if port in pc_table.keys(): + if port in pc_table: return True return False @@ -250,7 +246,7 @@ def is_port_vlan_member(config_db, port, vlan): """Check if port is a member of vlan""" vlan_ports_data = config_db.get_table('VLAN_MEMBER') - for key in vlan_ports_data.keys(): + for key in list(vlan_ports_data.keys()): if key[0] == vlan and key[1] == port: return True @@ -258,7 +254,7 @@ def is_port_vlan_member(config_db, port, vlan): def interface_is_in_vlan(vlan_member_table, interface_name): """ Check if an interface is in a vlan """ - for _,intf in vlan_member_table.keys(): + for _,intf in list(vlan_member_table.keys()): if intf == interface_name: return True @@ -270,7 +266,7 @@ def is_valid_vlan_interface(config_db, interface): def interface_is_in_portchannel(portchannel_member_table, interface_name): """ Check if an interface is part of portchannel """ - for _,intf in portchannel_member_table.keys(): + for _,intf in list(portchannel_member_table.keys()): if intf == interface_name: return True @@ -280,7 +276,7 @@ def is_port_router_interface(config_db, port): """Check if port is a router interface""" interface_table = config_db.get_table('INTERFACE') - for intf in interface_table.keys(): + for intf in list(interface_table.keys()): if port == intf[0]: return True @@ -290,7 +286,7 @@ def is_pc_router_interface(config_db, pc): """Check if portchannel is a router interface""" pc_interface_table = config_db.get_table('PORTCHANNEL_INTERFACE') - for intf in pc_interface_table.keys(): + for intf in list(pc_interface_table.keys()): if pc == intf[0]: return True @@ -337,7 +333,7 @@ def print_output_in_alias_mode(output, index): if word: interface_name = word[index] interface_name = interface_name.replace(':', '') - for port_name in natsorted(iface_alias_converter.port_dict.keys()): + for port_name in natsorted(list(iface_alias_converter.port_dict.keys())): if interface_name == port_name: alias_name = iface_alias_converter.port_dict[port_name]['alias'] if alias_name: @@ -353,7 +349,7 @@ def run_command_in_alias_mode(command): in output with vendor-sepecific interface aliases. """ - process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + process = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) while True: output = process.stdout.readline() @@ -450,7 +446,7 @@ def run_command_in_alias_mode(command): or a comma followed by whitespace """ converted_output = raw_output - for port_name in iface_alias_converter.port_dict.keys(): + for port_name in list(iface_alias_converter.port_dict.keys()): converted_output = re.sub(r"(^|\s){}($|,{{0,1}}\s)".format(port_name), r"\1{}\2".format(iface_alias_converter.name_to_alias(port_name)), converted_output) @@ -477,7 +473,7 @@ def run_command(command, display_cmd=False, ignore_error=False, return_cmd=False run_command_in_alias_mode(command) raise sys.exit(0) - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + proc = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE) if return_cmd: output = proc.communicate()[0].decode("utf-8") diff --git a/utilities_common/util_base.py b/utilities_common/util_base.py index 74646e098c3a..8dc15a4402fd 100644 --- a/utilities_common/util_base.py +++ b/utilities_common/util_base.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python2 - try: import imp import os @@ -51,4 +49,3 @@ def check_pddf_mode(self): return True else: return False - diff --git a/watchdogutil/main.py b/watchdogutil/main.py index 27d61ff7689e..84380ff1d045 100644 --- a/watchdogutil/main.py +++ b/watchdogutil/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # main.py #