Skip to content

Commit

Permalink
Enable additional ruff checks (C4, SIM, TCH)
Browse files Browse the repository at this point in the history
C4 = flake8-comprehensions
SIM = flake8-simplify
TCH = flake8-type-checking
  • Loading branch information
tysmith committed Sep 12, 2024
1 parent 5a0cc77 commit 2e0716b
Show file tree
Hide file tree
Showing 35 changed files with 161 additions and 120 deletions.
14 changes: 8 additions & 6 deletions grizzly/adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

from abc import ABCMeta, abstractmethod
from pathlib import Path
from typing import Any, Generator, final
from typing import TYPE_CHECKING, Any, Generator, final

from sapphire import ServerMap

from ..common.storage import TestCase
from ..common.utils import DEFAULT_TIME_LIMIT, HARNESS_FILE
from ..target.target_monitor import TargetMonitor

if TYPE_CHECKING:
from sapphire import ServerMap

from ..common.storage import TestCase
from ..target.target_monitor import TargetMonitor

__all__ = ("Adapter", "AdapterError")
__author__ = "Tyson Smith"
Expand Down Expand Up @@ -65,7 +67,7 @@ def __init__(self, name: str) -> None:
def __enter__(self) -> Adapter:
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(self, *exc: object) -> None:
self.cleanup()

@final
Expand Down
8 changes: 6 additions & 2 deletions grizzly/adapter/no_op_adapter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from sapphire import ServerMap
from typing import TYPE_CHECKING

from ...common.storage import TestCase
from ..adapter import Adapter

if TYPE_CHECKING:
from sapphire import ServerMap

from ...common.storage import TestCase

__author__ = "Tyson Smith"
__credits__ = ["Tyson Smith"]

Expand Down
6 changes: 2 additions & 4 deletions grizzly/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,11 @@ def __init__(self) -> None:

@staticmethod
def is_headless() -> bool:
if (
return (
system().startswith("Linux")
and not getenv("DISPLAY")
and not getenv("WAYLAND_DISPLAY")
):
return True
return False
)

def parse_args(self, argv: list[str] | None = None) -> Namespace:
args = self.parser.parse_args(argv)
Expand Down
6 changes: 3 additions & 3 deletions grizzly/common/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pathlib import Path
from shutil import rmtree
from tempfile import mkdtemp
from typing import Any, Generator
from typing import Generator
from zipfile import ZipFile

from bugsy import Bug, Bugsy
Expand All @@ -37,7 +37,7 @@ def __init__(self, bug: Bug) -> None:
def __enter__(self) -> BugzillaBug:
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(self, *exc: object) -> None:
self.cleanup()

def _fetch_attachments(self) -> None:
Expand Down Expand Up @@ -150,7 +150,7 @@ def testcases(self) -> list[Path]:
"""
# unpack archives
self._unpack_archives()
testcases = list(x for x in self._data.iterdir() if x.is_dir())
testcases = [x for x in self._data.iterdir() if x.is_dir()]
# scan base directory for files, filtering out assets
files = tuple(
x
Expand Down
4 changes: 2 additions & 2 deletions grizzly/common/fuzzmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def crash_id(self) -> int:
def __enter__(self) -> CrashEntry:
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(self, *exc: object) -> None:
self.cleanup()

def __getattr__(self, name: str) -> Any:
Expand Down Expand Up @@ -287,7 +287,7 @@ def bucket_id(self) -> int:
def __enter__(self) -> Bucket:
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(self, *exc: object) -> None:
self.cleanup()

def __getattr__(self, name: str) -> Any:
Expand Down
4 changes: 1 addition & 3 deletions grizzly/common/iomanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from typing import Any

from sapphire.server_map import ServerMap

from .storage import TestCase
Expand Down Expand Up @@ -36,7 +34,7 @@ def __init__(self, report_size: int = 1) -> None:
def __enter__(self) -> IOManager:
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(self, *exc: object) -> None:
self.cleanup()

def cleanup(self) -> None:
Expand Down
6 changes: 3 additions & 3 deletions grizzly/common/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ def crash_signature_max_frames(
ignore = 0
for count, entry in enumerate(crash_info.backtrace, start=1):
# Rust panics add frames of noise to the top of the stack (std::panicking)
if any(entry.startswith(x) for x in IGNORED_FRAMES):
ignore += 1
# Sanitizer heap profile also have more noise on the stack (alloc::alloc)
elif entry.startswith("alloc::alloc"):
if any(entry.startswith(x) for x in IGNORED_FRAMES) or entry.startswith(
"alloc::alloc"
):
ignore += 1
# only look at the top of the stack
if count - ignore == suggested_frames:
Expand Down
6 changes: 4 additions & 2 deletions grizzly/common/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
from pathlib import Path
from shutil import copyfile, move, rmtree
from tempfile import TemporaryDirectory
from typing import Any, cast, final
from typing import TYPE_CHECKING, Any, cast, final
from zipfile import ZIP_DEFLATED, ZipFile

from Collector.Collector import Collector
from fasteners.process_lock import InterProcessLock
from psutil import disk_usage

from .report import Report
from .storage import TestCase
from .utils import grz_tmp

if TYPE_CHECKING:
from .storage import TestCase

__all__ = ("FilesystemReporter", "FuzzManagerReporter", "Quality")
__author__ = "Tyson Smith"
__credits__ = ["Tyson Smith"]
Expand Down
21 changes: 10 additions & 11 deletions grizzly/common/stack_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from contextlib import suppress
from hashlib import sha1
from logging import DEBUG, INFO, basicConfig, getLogger
from os.path import basename
Expand Down Expand Up @@ -155,10 +156,8 @@ def from_line(cls, input_line: str) -> GdbStackFrame | None:
# find file name and line number
if ") at " in input_line:
input_line = input_line.split(") at ")[-1]
try:
with suppress(ValueError):
input_line, sframe.offset = input_line.split(":")
except ValueError:
pass
sframe.location = basename(input_line).split()[0]
return sframe

Expand Down Expand Up @@ -379,14 +378,14 @@ def _calculate_hash(self, major: bool = False) -> str | None:
major_depth = 0
for frame in self.frames[offset:]:
# only track depth when needed
if major and self._major_depth > 0:
# don't count ignored frames towards major hash depth
if not frame.function or not any(
frame.function.startswith(x) for x in IGNORED_FRAMES
):
major_depth += 1
if major_depth > self._major_depth:
break
# and don't count ignored frames towards major hash depth
if (major and self._major_depth > 0) and (
not frame.function
or not any(frame.function.startswith(x) for x in IGNORED_FRAMES)
):
major_depth += 1
if major_depth > self._major_depth:
break

if frame.location is not None:
bucket_hash.update(frame.location.encode(errors="replace"))
Expand Down
9 changes: 6 additions & 3 deletions grizzly/common/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
from json import dumps, loads
from logging import getLogger
from os import getpid
from pathlib import Path
from sqlite3 import Connection, OperationalError, connect
from time import perf_counter, time
from typing import Any, Callable, Generator, cast
from typing import TYPE_CHECKING, Any, Callable, Generator, cast

from .reporter import FuzzManagerReporter
from .utils import grz_tmp

if TYPE_CHECKING:
from pathlib import Path

from .reporter import FuzzManagerReporter

__all__ = ("ReadOnlyStatus", "ReductionStatus", "Status", "SimpleStatus")
__author__ = "Tyson Smith"
__credits__ = ["Tyson Smith"]
Expand Down
10 changes: 2 additions & 8 deletions grizzly/common/status_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,7 @@ def _format_seconds(duration: float) -> str:
def _format_duration(duration: int | None, total: float = 0) -> str:
result = ""
if duration is not None:
if total == 0:
percent = 0
else:
percent = int(100 * duration / total)
percent = 0 if total == 0 else int(100 * duration / total)
result = _format_seconds(duration)
result += f" ({percent:3d}%)"
return result
Expand All @@ -687,10 +684,7 @@ def _format_duration(duration: int | None, total: float = 0) -> str:
def _format_number(number: int | None, total: float = 0) -> str:
result = ""
if number is not None:
if total == 0:
percent = 0
else:
percent = int(100 * number / total)
percent = 0 if total == 0 else int(100 * number / total)
result = f"{number:n} ({percent:3d}%)"
return result

Expand Down
6 changes: 3 additions & 3 deletions grizzly/common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from shutil import copyfile, copytree, move, rmtree
from tempfile import NamedTemporaryFile, mkdtemp
from time import time
from typing import Any, Generator, cast
from typing import Any, Generator, Iterator, cast

from .utils import grz_tmp, package_version

Expand Down Expand Up @@ -114,10 +114,10 @@ def __getitem__(self, key: str) -> Path:
def __enter__(self) -> TestCase:
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(self, *exc: object) -> None:
self.cleanup()

def __iter__(self) -> Generator[str, None, None]:
def __iter__(self) -> Iterator[str]:
"""TestCase contents.
Args:
Expand Down
2 changes: 1 addition & 1 deletion grizzly/common/test_fuzzmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def test_crash_03(mocker, tmp_path, file_name, passed_ext, expected):
"id": 234,
"testcase": file_name,
}
with open(tmp_path / file_name, "w") as zip_fp:
with (tmp_path / file_name).open("w") as zip_fp:
zip_fp.write("data")
with CrashEntry(234) as crash:
assert crash.testcase == file_name # pre-load data dict so I can re-patch get
Expand Down
8 changes: 4 additions & 4 deletions grizzly/common/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_report_01(tmp_path):
assert report._logs.stdout.name == "log_stdout.txt"
assert report.preferred.name == "log_stderr.txt"
assert report.stack is None
assert Report.DEFAULT_MAJOR == report.major
assert Report.DEFAULT_MINOR == report.minor
assert report.major == Report.DEFAULT_MAJOR
assert report.minor == Report.DEFAULT_MINOR
assert report.prefix is not None
report.cleanup()
assert not tmp_path.exists()
Expand All @@ -53,8 +53,8 @@ def test_report_02(tmp_path):
assert report._logs.stdout.name == "log_stdout.txt"
assert report.preferred.name == "log_asan_blah.txt"
assert report.stack is not None
assert Report.DEFAULT_MAJOR != report.major
assert Report.DEFAULT_MINOR != report.minor
assert report.major != Report.DEFAULT_MAJOR
assert report.minor != Report.DEFAULT_MINOR
assert report.prefix is not None
report.cleanup()

Expand Down
4 changes: 2 additions & 2 deletions grizzly/common/test_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_filesystem_reporter_02(tmp_path, mocker):
(log_path / "log_stderr.txt").write_bytes(b"STDERR log")
(log_path / "log_stdout.txt").write_bytes(b"STDOUT log")
_create_crash_log(log_path / "log_asan_blah.txt")
tests = list(mocker.Mock(spec_set=TestCase, timestamp=x) for x in range(10))
tests = [mocker.Mock(spec_set=TestCase, timestamp=x) for x in range(10)]
report_path = tmp_path / "reports"
assert not report_path.exists()
reporter = FilesystemReporter(report_path)
Expand All @@ -102,7 +102,7 @@ def test_filesystem_reporter_02(tmp_path, mocker):
log_path.mkdir()
(log_path / "log_stderr.txt").write_bytes(b"STDERR log")
(log_path / "log_stdout.txt").write_bytes(b"STDOUT log")
tests = list(mocker.Mock(spec_set=TestCase, timestamp=1) for _ in range(2))
tests = [mocker.Mock(spec_set=TestCase, timestamp=1) for _ in range(2)]
reporter.submit(tests, Report(log_path, Path("bin")))
assert all(x.dump.call_count == 1 for x in tests)
assert len(tuple(report_path.iterdir())) == 2
Expand Down
6 changes: 4 additions & 2 deletions grizzly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from argparse import Namespace
from logging import DEBUG, getLogger
from os import getpid
from typing import cast
from typing import TYPE_CHECKING, cast

from sapphire import CertificateBundle, Sapphire

Expand All @@ -28,6 +27,9 @@
from .session import LogRate, Session
from .target import Target, TargetLaunchError, TargetLaunchTimeout

if TYPE_CHECKING:
from argparse import Namespace

__author__ = "Tyson Smith"
__credits__ = ["Tyson Smith", "Jesse Schwartzentruber"]

Expand Down
8 changes: 5 additions & 3 deletions grizzly/reduce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

import json
import os
from argparse import Namespace
from itertools import chain
from locale import LC_ALL, setlocale
from logging import getLogger
from math import ceil, log
from pathlib import Path
from time import time
from typing import Any
from typing import TYPE_CHECKING

from FTB.Signatures.CrashInfo import CrashSignature

Expand Down Expand Up @@ -43,6 +42,9 @@
from .exceptions import GrizzlyReduceBaseException, NotReproducible
from .strategies import STRATEGIES

if TYPE_CHECKING:
from argparse import Namespace

__author__ = "Jesse Schwartzentruber"
__credits__ = ["Jesse Schwartzentruber", "Tyson Smith"]

Expand Down Expand Up @@ -159,7 +161,7 @@ def __init__(
def __enter__(self) -> ReduceManager:
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(self, *exc: object) -> None:
self.cleanup()

def cleanup(self) -> None:
Expand Down
Loading

0 comments on commit 2e0716b

Please sign in to comment.