From bd7884b31384d461ea71176d83c925fa4943490f Mon Sep 17 00:00:00 2001 From: "Liu, An-Chi" Date: Wed, 25 Sep 2024 15:50:50 +0900 Subject: [PATCH] add Ruff for Python file format and lint (#1584) --- .pre-commit-config.yaml | 10 +++++--- 3rdParty/OUIDataset/create_oui_data.py | 2 +- Tests/ExamplesTest/tests/test_httpanalyzer.py | 1 - Tests/ExamplesTest/tests/test_pcapsearch.py | 6 ++--- Tests/ExamplesTest/tests/test_pcapsplitter.py | 2 +- Tests/ExamplesTest/tests/test_sslanalyzer.py | 1 - .../tests/test_tlsfingerprinting.py | 1 - ci/run_tests/run_tests_windows.py | 2 +- cmake/setup_dpdk.py | 24 +++++++++---------- 9 files changed, 23 insertions(+), 26 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 99d1460d15..f4fd13a955 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,10 +21,14 @@ repos: - id: mixed-line-ending args: ['--fix=lf'] - id: trailing-whitespace - - repo: https://github.com/psf/black - rev: 24.8.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.6.5 hooks: - - id: black + - id: ruff # Run the linter. + types_or: [ python ] + - id: ruff-format # Run the formatter. + types_or: [ python ] + args: [ --check ] - repo: https://github.com/pocc/pre-commit-hooks rev: v1.3.5 hooks: diff --git a/3rdParty/OUIDataset/create_oui_data.py b/3rdParty/OUIDataset/create_oui_data.py index 02f1f987fc..9a36c06464 100644 --- a/3rdParty/OUIDataset/create_oui_data.py +++ b/3rdParty/OUIDataset/create_oui_data.py @@ -75,7 +75,7 @@ def convert_line(line: str) -> list[str]: def parse_mac_and_vendor(line_parts: list[str]) -> Optional[LineElements]: - if line_parts == None or len(line_parts) < 3: + if line_parts is None or len(line_parts) < 3: return None if len(line_parts[0]) == 6: diff --git a/Tests/ExamplesTest/tests/test_httpanalyzer.py b/Tests/ExamplesTest/tests/test_httpanalyzer.py index 66808b66dd..8aff8f55e1 100644 --- a/Tests/ExamplesTest/tests/test_httpanalyzer.py +++ b/Tests/ExamplesTest/tests/test_httpanalyzer.py @@ -1,5 +1,4 @@ from os import path -from itertools import filterfalse import pytest from .test_utils import ExampleTest, compare_stdout_with_file diff --git a/Tests/ExamplesTest/tests/test_pcapsearch.py b/Tests/ExamplesTest/tests/test_pcapsearch.py index 121e97d421..3e7b2a6dbc 100644 --- a/Tests/ExamplesTest/tests/test_pcapsearch.py +++ b/Tests/ExamplesTest/tests/test_pcapsearch.py @@ -1,6 +1,4 @@ -from os import path import pytest -import re import ntpath from .test_utils import ExampleTest @@ -42,7 +40,7 @@ def test_exact_file_format(self): num_of_packets = int(words[0]) file_name = ntpath.basename(words[-1].replace("'", "")) actual.add((num_of_packets, file_name)) - except: + except Exception: pass assert expected.issubset(actual) @@ -52,7 +50,7 @@ def test_different_file_extensions(self): completed_process = self.run_example(args=args) assert ".dmp'" in completed_process.stdout assert ".pcapng'" in completed_process.stdout - assert not ".pcap'" in completed_process.stdout + assert ".pcap'" not in completed_process.stdout def test_no_args(self): args = {} diff --git a/Tests/ExamplesTest/tests/test_pcapsplitter.py b/Tests/ExamplesTest/tests/test_pcapsplitter.py index 77912eb25f..3d5ba89d18 100644 --- a/Tests/ExamplesTest/tests/test_pcapsplitter.py +++ b/Tests/ExamplesTest/tests/test_pcapsplitter.py @@ -238,7 +238,7 @@ def test_split_by_connection(self, tmpdir): else: conn = frozenset([]) - assert not conn in connection_map + assert conn not in connection_map connection_map[conn] = True if len(conn) == 0: diff --git a/Tests/ExamplesTest/tests/test_sslanalyzer.py b/Tests/ExamplesTest/tests/test_sslanalyzer.py index f11419a6d8..7524da3477 100644 --- a/Tests/ExamplesTest/tests/test_sslanalyzer.py +++ b/Tests/ExamplesTest/tests/test_sslanalyzer.py @@ -1,5 +1,4 @@ from os import path -from itertools import filterfalse import pytest from .test_utils import ExampleTest, compare_stdout_with_file diff --git a/Tests/ExamplesTest/tests/test_tlsfingerprinting.py b/Tests/ExamplesTest/tests/test_tlsfingerprinting.py index 172a89039e..7ec6026790 100644 --- a/Tests/ExamplesTest/tests/test_tlsfingerprinting.py +++ b/Tests/ExamplesTest/tests/test_tlsfingerprinting.py @@ -1,5 +1,4 @@ import os -import filecmp import pytest from .test_utils import ( ExampleTest, diff --git a/ci/run_tests/run_tests_windows.py b/ci/run_tests/run_tests_windows.py index e12d609b29..df9a3daab7 100644 --- a/ci/run_tests/run_tests_windows.py +++ b/ci/run_tests/run_tests_windows.py @@ -57,7 +57,7 @@ def find_interface(): if completed_process.returncode != 0: continue return interface, ip_address - except: + except Exception: pass return None, None diff --git a/cmake/setup_dpdk.py b/cmake/setup_dpdk.py index 1e1f22f031..0d7a2937bf 100755 --- a/cmake/setup_dpdk.py +++ b/cmake/setup_dpdk.py @@ -230,9 +230,7 @@ def usage(): [Restore] sudo python setup_dpdk.py restore\n\n - """.format( - settings_file=_SETTINGS_FILE - ) + """.format(settings_file=_SETTINGS_FILE) # This is roughly compatible with check_output function in subprocess module @@ -439,7 +437,7 @@ def handle_error(error_msg): filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"] try: file_d = open(filename, "a") - except: # pylint:disable=bare-except + except Exception: handle_error( "Error: unbind failed for %s - Cannot open %s" % (dev_id, filename) ) @@ -491,7 +489,7 @@ def handle_error(error_msg): if os.path.exists(filename): try: file_d = open(filename, "w") - except: # pylint:disable=bare-except + except Exception: handle_error( "Error: bind failed for %s - Cannot open %s" % (dev_id, filename) ) @@ -500,7 +498,7 @@ def handle_error(error_msg): file_d.write("%s" % driver) logger.debug("bind_one: write '%s' to '%s'", driver, filename) file_d.close() - except: # pylint:disable=bare-except + except Exception: handle_error( "Error: bind failed for %s - Cannot write driver %s to " "PCI ID " % (dev_id, driver) @@ -511,7 +509,7 @@ def handle_error(error_msg): filename = "/sys/bus/pci/drivers/%s/new_id" % driver try: file_d = open(filename, "w") - except: # pylint:disable=bare-except + except Exception: handle_error( "Error: bind failed for %s - Cannot open %s" % (dev_id, filename) ) @@ -520,7 +518,7 @@ def handle_error(error_msg): # Convert Device and Vendor Id to int to write to new_id file_d.write("%04x %04x" % (int(dev["Vendor"], 16), int(dev["Device"], 16))) file_d.close() - except: # pylint:disable=bare-except + except Exception: handle_error( "Error: bind failed for %s - Cannot write new PCI ID to " "driver %s" % (dev_id, driver) @@ -531,7 +529,7 @@ def handle_error(error_msg): filename = "/sys/bus/pci/drivers/%s/bind" % driver try: file_d = open(filename, "a") - except: # pylint:disable=bare-except + except Exception: logger.error("Error: bind failed for %s - Cannot open %s", dev_id, filename) if saved_driver is not None: # restore any previous driver bind_one(dev_id, saved_driver, quiet, force) @@ -540,7 +538,7 @@ def handle_error(error_msg): file_d.write(dev_id) logger.debug("bind_one: write '%s' to '%s'", dev_id, filename) file_d.close() - except: # pylint:disable=bare-except + except Exception: # for some reason, closing dev_id after adding a new PCI ID to new_id # results in IOError. however, if the device was successfully bound, # we don't care for any errors and can safely ignore IOError @@ -561,7 +559,7 @@ def handle_error(error_msg): if os.path.exists(filename): try: file_d = open(filename, "w") - except: # pylint:disable=bare-except + except Exception: handle_error( "Error: unbind failed for %s - Cannot open %s" % (dev_id, filename) ) @@ -570,7 +568,7 @@ def handle_error(error_msg): file_d.write("\00") logger.debug("bind_one: write '\00' to '%s'", filename) file_d.close() - except: # pylint:disable=bare-except + except Exception: handle_error( "Error: unbind failed for %s - Cannot open %s" % (dev_id, filename) ) @@ -1216,7 +1214,7 @@ def main(): try: args.func(args, settings) settings.save(settings_file_full_path) - except: # pylint:disable=bare-except + except Exception: pass