From 719d58f6e52c02e1050dd946005d69eecd9a4348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Michalak-Szmaci=C5=84ski?= Date: Wed, 12 Jul 2023 12:54:52 +0200 Subject: [PATCH] [python tools] Add to flake8 in workflow and fix python files (part #25193) (#27881) * Flake8 fix error in python tools * Restyled by isort --------- Co-authored-by: Restyled.io --- .flake8 | 12 ------------ scripts/tools/check_zcl_file_sync.py | 8 ++++---- scripts/tools/memory/memdf/__init__.py | 4 +++- scripts/tools/memory/report_summary.py | 1 - scripts/tools/silabs/FactoryDataProvider.py | 7 ++----- scripts/tools/zap/generate.py | 12 +++++------- scripts/tools/zap/prune_outputs.py | 1 - scripts/tools/zap/version_update.py | 2 +- scripts/tools/zap/zap_download.py | 8 ++++---- src/app/ota_image_tool.py | 2 +- src/tools/chip-cert/dacs.py | 2 +- 11 files changed, 21 insertions(+), 38 deletions(-) diff --git a/.flake8 b/.flake8 index 2481f916e89300..934102b064d886 100644 --- a/.flake8 +++ b/.flake8 @@ -6,7 +6,6 @@ exclude = third_party ./examples/common/QRCode/* # temporarily scan only directories with fixed files # TODO: Remove the paths below when all bugs are fixed - src/tools/chip-cert/* build/chip/java/tests/* build/chip/linux/* build/config/linux/* @@ -14,17 +13,6 @@ exclude = third_party docs/_extensions/external_content.py examples/common/pigweed/rpc_console/py/chip_rpc/console.py examples/lighting-app/python/lighting.py - scripts/tools/check_zcl_file_sync.py - scripts/tools/convert_ini.py - scripts/tools/memory/memdf/__init__.py - scripts/tools/memory/report_summary.py - scripts/tools/silabs/FactoryDataProvider.py - scripts/tools/zap/generate.py - scripts/tools/zap/prune_outputs.py - scripts/tools/zap/version_update.py - scripts/tools/zap/zap_download.py - scripts/tools/zap_convert_all.py - src/app/ota_image_tool.py src/app/zap_cluster_list.py src/controller/python/chip/yaml/__init__.py src/controller/python/chip/yaml/format_converter.py diff --git a/scripts/tools/check_zcl_file_sync.py b/scripts/tools/check_zcl_file_sync.py index 4a68415a96bca4..a9fd9363001a2a 100755 --- a/scripts/tools/check_zcl_file_sync.py +++ b/scripts/tools/check_zcl_file_sync.py @@ -60,15 +60,15 @@ def main(): print("%s and %s have unexpected differences." % (base_name, ext_name)) print("Differences between expected and actual:") - for l in difflib.unified_diff( + for line in difflib.unified_diff( json.dumps(ext_data, indent=2).split('\n'), json.dumps(base_data, indent=2).split('\n'), fromfile=ext_name, tofile="", ): - if l.endswith('\n'): - l = l[:-1] - print(l) + if line.endswith('\n'): + line = line[:-1] + print(line) return 1 diff --git a/scripts/tools/memory/memdf/__init__.py b/scripts/tools/memory/memdf/__init__.py index 60bd2105c15b8c..fa0e1070112ae4 100644 --- a/scripts/tools/memory/memdf/__init__.py +++ b/scripts/tools/memory/memdf/__init__.py @@ -15,5 +15,7 @@ # """Package for working with memory usage information using Pandas DataFrame.""" -from memdf.df import DF, SymbolDF, SectionDF, SegmentDF, ExtentDF, DFs +from memdf.df import DF, DFs, ExtentDF, SectionDF, SegmentDF, SymbolDF from memdf.util.config import Config, ConfigDescription + +__all__ = [DF, SymbolDF, SectionDF, SegmentDF, ExtentDF, DFs, Config, ConfigDescription] diff --git a/scripts/tools/memory/report_summary.py b/scripts/tools/memory/report_summary.py index 88ba23c2a37550..e528e411465f85 100755 --- a/scripts/tools/memory/report_summary.py +++ b/scripts/tools/memory/report_summary.py @@ -31,7 +31,6 @@ import memdf.collect import memdf.report import memdf.select -import numpy # type: ignore from memdf import Config, DFs, SymbolDF diff --git a/scripts/tools/silabs/FactoryDataProvider.py b/scripts/tools/silabs/FactoryDataProvider.py index 5d3ef39752d9d8..8cb489587cb122 100644 --- a/scripts/tools/silabs/FactoryDataProvider.py +++ b/scripts/tools/silabs/FactoryDataProvider.py @@ -15,12 +15,9 @@ # import argparse -import base64 import datetime import os -import struct import subprocess -import sys class FactoryDataWriter: @@ -185,7 +182,7 @@ def add_SerialNo_To_CMD(self, cmdList): cmdList.extend(["--serialno", self._args.jtagSerial]) def create_nvm3injected_image(self): - """ Use commander command lines create a binary flashable to the EFR32 + """ Use commander command lines create a binary flashable to the EFR32 containing the factory commissioning data in NVM3 section """ isDeviceConnected = True @@ -212,7 +209,7 @@ def create_nvm3injected_image(self): inputImage = self.BASE_MG24_FILE else: raise Exception('Invalid MCU') - except: + except Exception: isDeviceConnected = False print("Device not connected") # When no device is connected user needs to provide the mcu family for which those credentials are to be created diff --git a/scripts/tools/zap/generate.py b/scripts/tools/zap/generate.py index 46ae0ebf95a187..003279d47f48f5 100755 --- a/scripts/tools/zap/generate.py +++ b/scripts/tools/zap/generate.py @@ -24,7 +24,6 @@ import sys import tempfile import traceback -import urllib.request from dataclasses import dataclass from pathlib import Path from typing import Optional @@ -195,7 +194,7 @@ def matterPathFromZapPath(zap_config_path): if not target_path.endswith(".matter"): # We expect "something.zap" and don't handle corner cases of # multiple extensions. This is to work with existing codebase only - raise Error("Unexpected input zap file %s" % self.zap_config) + raise Exception("Unexpected input zap file %s" % zap_config_path) return target_path @@ -290,7 +289,7 @@ def getClangFormatBinary(): print('WARNING: clang-format may not be the right version:') print(' PIGWEED TAG: %s' % clang_config['tags'][0]) print(' ACTUAL VERSION: %s' % version_string) - except: + except Exception: print("Failed to validate clang version.") traceback.print_last() @@ -318,12 +317,11 @@ def runClangPrettifier(templates_file, output_dir): args = [clang_format, '-i'] args.extend(clangOutputs) subprocess.check_call(args) - err = None print('Formatted using %s (%s)' % (clang_format, subprocess.check_output([clang_format, '--version']))) for outputName in clangOutputs: print(' - %s' % outputName) - except Exception as err: - print('clang-format error:', err) + except subprocess.CalledProcessError as err: + print('clang-format error: %s', err) class LockFileSerializer: @@ -351,7 +349,7 @@ def main(): checkPythonVersion() cmdLineArgs = runArgumentsParser() - with LockFileSerializer(cmdLineArgs.lock_file) as lock: + with LockFileSerializer(cmdLineArgs.lock_file) as _: if cmdLineArgs.runBootstrap: subprocess.check_call(getFilePath("scripts/tools/zap/zap_bootstrap.sh"), shell=True) diff --git a/scripts/tools/zap/prune_outputs.py b/scripts/tools/zap/prune_outputs.py index bc223fa2a010c6..09f92a9378ab33 100644 --- a/scripts/tools/zap/prune_outputs.py +++ b/scripts/tools/zap/prune_outputs.py @@ -16,7 +16,6 @@ import argparse import os import shutil -import sys def main(): diff --git a/scripts/tools/zap/version_update.py b/scripts/tools/zap/version_update.py index ba40ca575c53f2..4e603132788889 100755 --- a/scripts/tools/zap/version_update.py +++ b/scripts/tools/zap/version_update.py @@ -125,7 +125,7 @@ def version_update(log_level, update, new_version): found_versions = set() for m in ZAP_VERSION_RE.finditer(file_data): version = file_data[m.start():m.end()] - if not version in found_versions: + if version not in found_versions: logging.info('%s currently used in %s', version, name) found_versions.add(version) diff --git a/scripts/tools/zap/zap_download.py b/scripts/tools/zap/zap_download.py index 4e814c6f1fcffb..c2f6f38235fc0c 100755 --- a/scripts/tools/zap/zap_download.py +++ b/scripts/tools/zap/zap_download.py @@ -32,7 +32,7 @@ try: import coloredlogs _has_coloredlogs = True -except: +except ImportError: _has_coloredlogs = False # Supported log levels, mapping string values required for argument @@ -58,10 +58,10 @@ def _GetDefaultExtractRoot(): def _LogPipeLines(pipe, prefix): - l = logging.getLogger().getChild(prefix) + log = logging.getLogger().getChild(prefix) for line in iter(pipe.readline, b''): line = line.strip().decode('utf-8', errors="ignore") - l.info('%s' % line) + log.info('%s' % line) def _ExecuteProcess(cmd, cwd): @@ -93,7 +93,7 @@ def _SetupSourceZap(install_directory: str, zap_version: str): install_directory ) - _ExecuteProcess(f"npm ci".split(), install_directory) + _ExecuteProcess("npm ci".split(), install_directory) def _SetupReleaseZap(install_directory: str, zap_version: str): diff --git a/src/app/ota_image_tool.py b/src/app/ota_image_tool.py index c666c229ca6ca2..9f2bf041a0ee7f 100755 --- a/src/app/ota_image_tool.py +++ b/src/app/ota_image_tool.py @@ -262,7 +262,7 @@ def show_header(args: object): print(f'Magic: {magic:x}') print(f'Total Size: {total_size}') print(f'Header Size: {header_size}') - print(f'Header TLV:') + print('Header TLV:') for tag in header_tlv: tag_name = HeaderTag(tag).name.replace('_', ' ').title() diff --git a/src/tools/chip-cert/dacs.py b/src/tools/chip-cert/dacs.py index 76705162fd741c..96722b3aca9746 100755 --- a/src/tools/chip-cert/dacs.py +++ b/src/tools/chip-cert/dacs.py @@ -5,7 +5,7 @@ import subprocess import sys import typing -from binascii import hexlify, unhexlify +from binascii import unhexlify from enum import Enum copyrightNotice = """/*