Skip to content

Commit bd7884b

Browse files
authored
add Ruff for Python file format and lint (#1584)
1 parent 4417695 commit bd7884b

File tree

9 files changed

+23
-26
lines changed

9 files changed

+23
-26
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ repos:
2121
- id: mixed-line-ending
2222
args: ['--fix=lf']
2323
- id: trailing-whitespace
24-
- repo: https://github.com/psf/black
25-
rev: 24.8.0
24+
- repo: https://github.com/astral-sh/ruff-pre-commit
25+
rev: v0.6.5
2626
hooks:
27-
- id: black
27+
- id: ruff # Run the linter.
28+
types_or: [ python ]
29+
- id: ruff-format # Run the formatter.
30+
types_or: [ python ]
31+
args: [ --check ]
2832
- repo: https://github.com/pocc/pre-commit-hooks
2933
rev: v1.3.5
3034
hooks:

3rdParty/OUIDataset/create_oui_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def convert_line(line: str) -> list[str]:
7575

7676

7777
def parse_mac_and_vendor(line_parts: list[str]) -> Optional[LineElements]:
78-
if line_parts == None or len(line_parts) < 3:
78+
if line_parts is None or len(line_parts) < 3:
7979
return None
8080

8181
if len(line_parts[0]) == 6:

Tests/ExamplesTest/tests/test_httpanalyzer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from os import path
2-
from itertools import filterfalse
32
import pytest
43
from .test_utils import ExampleTest, compare_stdout_with_file
54

Tests/ExamplesTest/tests/test_pcapsearch.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from os import path
21
import pytest
3-
import re
42
import ntpath
53
from .test_utils import ExampleTest
64

@@ -42,7 +40,7 @@ def test_exact_file_format(self):
4240
num_of_packets = int(words[0])
4341
file_name = ntpath.basename(words[-1].replace("'", ""))
4442
actual.add((num_of_packets, file_name))
45-
except:
43+
except Exception:
4644
pass
4745

4846
assert expected.issubset(actual)
@@ -52,7 +50,7 @@ def test_different_file_extensions(self):
5250
completed_process = self.run_example(args=args)
5351
assert ".dmp'" in completed_process.stdout
5452
assert ".pcapng'" in completed_process.stdout
55-
assert not ".pcap'" in completed_process.stdout
53+
assert ".pcap'" not in completed_process.stdout
5654

5755
def test_no_args(self):
5856
args = {}

Tests/ExamplesTest/tests/test_pcapsplitter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def test_split_by_connection(self, tmpdir):
238238
else:
239239
conn = frozenset([])
240240

241-
assert not conn in connection_map
241+
assert conn not in connection_map
242242
connection_map[conn] = True
243243

244244
if len(conn) == 0:

Tests/ExamplesTest/tests/test_sslanalyzer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from os import path
2-
from itertools import filterfalse
32
import pytest
43
from .test_utils import ExampleTest, compare_stdout_with_file
54

Tests/ExamplesTest/tests/test_tlsfingerprinting.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import filecmp
32
import pytest
43
from .test_utils import (
54
ExampleTest,

ci/run_tests/run_tests_windows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def find_interface():
5757
if completed_process.returncode != 0:
5858
continue
5959
return interface, ip_address
60-
except:
60+
except Exception:
6161
pass
6262
return None, None
6363

cmake/setup_dpdk.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ def usage():
230230
[Restore]
231231
232232
sudo python setup_dpdk.py restore\n\n
233-
""".format(
234-
settings_file=_SETTINGS_FILE
235-
)
233+
""".format(settings_file=_SETTINGS_FILE)
236234

237235

238236
# This is roughly compatible with check_output function in subprocess module
@@ -439,7 +437,7 @@ def handle_error(error_msg):
439437
filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
440438
try:
441439
file_d = open(filename, "a")
442-
except: # pylint:disable=bare-except
440+
except Exception:
443441
handle_error(
444442
"Error: unbind failed for %s - Cannot open %s" % (dev_id, filename)
445443
)
@@ -491,7 +489,7 @@ def handle_error(error_msg):
491489
if os.path.exists(filename):
492490
try:
493491
file_d = open(filename, "w")
494-
except: # pylint:disable=bare-except
492+
except Exception:
495493
handle_error(
496494
"Error: bind failed for %s - Cannot open %s" % (dev_id, filename)
497495
)
@@ -500,7 +498,7 @@ def handle_error(error_msg):
500498
file_d.write("%s" % driver)
501499
logger.debug("bind_one: write '%s' to '%s'", driver, filename)
502500
file_d.close()
503-
except: # pylint:disable=bare-except
501+
except Exception:
504502
handle_error(
505503
"Error: bind failed for %s - Cannot write driver %s to "
506504
"PCI ID " % (dev_id, driver)
@@ -511,7 +509,7 @@ def handle_error(error_msg):
511509
filename = "/sys/bus/pci/drivers/%s/new_id" % driver
512510
try:
513511
file_d = open(filename, "w")
514-
except: # pylint:disable=bare-except
512+
except Exception:
515513
handle_error(
516514
"Error: bind failed for %s - Cannot open %s" % (dev_id, filename)
517515
)
@@ -520,7 +518,7 @@ def handle_error(error_msg):
520518
# Convert Device and Vendor Id to int to write to new_id
521519
file_d.write("%04x %04x" % (int(dev["Vendor"], 16), int(dev["Device"], 16)))
522520
file_d.close()
523-
except: # pylint:disable=bare-except
521+
except Exception:
524522
handle_error(
525523
"Error: bind failed for %s - Cannot write new PCI ID to "
526524
"driver %s" % (dev_id, driver)
@@ -531,7 +529,7 @@ def handle_error(error_msg):
531529
filename = "/sys/bus/pci/drivers/%s/bind" % driver
532530
try:
533531
file_d = open(filename, "a")
534-
except: # pylint:disable=bare-except
532+
except Exception:
535533
logger.error("Error: bind failed for %s - Cannot open %s", dev_id, filename)
536534
if saved_driver is not None: # restore any previous driver
537535
bind_one(dev_id, saved_driver, quiet, force)
@@ -540,7 +538,7 @@ def handle_error(error_msg):
540538
file_d.write(dev_id)
541539
logger.debug("bind_one: write '%s' to '%s'", dev_id, filename)
542540
file_d.close()
543-
except: # pylint:disable=bare-except
541+
except Exception:
544542
# for some reason, closing dev_id after adding a new PCI ID to new_id
545543
# results in IOError. however, if the device was successfully bound,
546544
# we don't care for any errors and can safely ignore IOError
@@ -561,7 +559,7 @@ def handle_error(error_msg):
561559
if os.path.exists(filename):
562560
try:
563561
file_d = open(filename, "w")
564-
except: # pylint:disable=bare-except
562+
except Exception:
565563
handle_error(
566564
"Error: unbind failed for %s - Cannot open %s" % (dev_id, filename)
567565
)
@@ -570,7 +568,7 @@ def handle_error(error_msg):
570568
file_d.write("\00")
571569
logger.debug("bind_one: write '\00' to '%s'", filename)
572570
file_d.close()
573-
except: # pylint:disable=bare-except
571+
except Exception:
574572
handle_error(
575573
"Error: unbind failed for %s - Cannot open %s" % (dev_id, filename)
576574
)
@@ -1216,7 +1214,7 @@ def main():
12161214
try:
12171215
args.func(args, settings)
12181216
settings.save(settings_file_full_path)
1219-
except: # pylint:disable=bare-except
1217+
except Exception:
12201218
pass
12211219

12221220

0 commit comments

Comments
 (0)