Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update typing and enable more linters #463

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ repos:
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
rev: v3.16.0
hooks:
- id: pyupgrade
args: ['--py38-plus']
- repo: https://github.com/ambv/black
rev: 24.4.2
rev: 24.8.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.1
rev: v0.6.4
hooks:
- id: ruff
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down Expand Up @@ -46,7 +46,7 @@ repos:
hooks:
- id: taskcluster_yml
- repo: https://github.com/MozillaSecurity/orion-ci
rev: v0.0.8
rev: v0.0.10
hooks:
- id: orion_ci
- repo: meta
Expand Down
36 changes: 20 additions & 16 deletions grizzly/adapter/adapter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from abc import ABCMeta, abstractmethod
from pathlib import Path
from typing import Any, Dict, Generator, Optional, Tuple, final

from sapphire import ServerMap
from typing import TYPE_CHECKING, Any, Generator, final

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

if TYPE_CHECKING:
tysmith marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -54,16 +58,16 @@ def __init__(self, name: str) -> None:
raise AdapterError("name must not be empty")
if len(name.split()) != 1 or name.strip() != name:
raise AdapterError("name must not contain whitespace")
self._harness: Optional[bytes] = None
self.fuzz: Dict[str, Any] = {}
self.monitor: Optional[TargetMonitor] = None
self._harness: bytes | None = None
self.fuzz: dict[str, Any] = {}
self.monitor: TargetMonitor | None = None
self.name = name
self.remaining: Optional[int] = None
self.remaining: int | None = None

def __enter__(self) -> "Adapter":
def __enter__(self) -> Adapter:
return self

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

@final
Expand Down Expand Up @@ -94,7 +98,7 @@ def enable_harness(self, path: Path = HARNESS_FILE) -> None:
assert self._harness, f"empty harness file '{path.resolve()}'"

@final
def get_harness(self) -> Optional[bytes]:
def get_harness(self) -> bytes | None:
"""Get the harness. Used internally by Grizzly.
*** DO NOT OVERRIDE! ***

Expand All @@ -110,7 +114,7 @@ def get_harness(self) -> Optional[bytes]:
@staticmethod
def scan_path(
path: str,
ignore: Tuple[str, ...] = IGNORE_FILES,
ignore: tuple[str, ...] = IGNORE_FILES,
recursive: bool = False,
) -> Generator[str, None, None]:
"""Scan a path and yield the files within it. This is available as
Expand Down Expand Up @@ -149,7 +153,7 @@ def generate(self, testcase: TestCase, server_map: ServerMap) -> None:
None
"""

def on_served(self, testcase: TestCase, served: Tuple[str, ...]) -> None:
def on_served(self, testcase: TestCase, served: tuple[str, ...]) -> None:
"""Optional. Automatically called after a test case is successfully served.

Args:
Expand All @@ -160,7 +164,7 @@ def on_served(self, testcase: TestCase, served: Tuple[str, ...]) -> None:
None
"""

def on_timeout(self, testcase: TestCase, served: Tuple[str, ...]) -> None:
def on_timeout(self, testcase: TestCase, served: tuple[str, ...]) -> None:
"""Optional. Automatically called if timeout occurs while attempting to
serve a test case. By default it calls `self.on_served()`.

Expand All @@ -184,7 +188,7 @@ def pre_launch(self) -> None:
"""

# TODO: update input_path type (str -> Path)
def setup(self, input_path: Optional[str], server_map: ServerMap) -> None:
def setup(self, input_path: str | None, server_map: ServerMap) -> None:
"""Optional. Automatically called once at startup.

Args:
Expand Down
13 changes: 8 additions & 5 deletions grizzly/adapter/no_op_adapter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from typing import Optional
from typing import TYPE_CHECKING

from sapphire import ServerMap

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 All @@ -20,7 +23,7 @@ class NoOpAdapter(Adapter):

NAME = "no-op"

def setup(self, input_path: Optional[str], server_map: ServerMap) -> None:
def setup(self, input_path: str | None, server_map: ServerMap) -> None:
"""Generate a static test case that calls `window.close()` when run.
Normally this is done in generate() but since the test is static only
do it once. Use the default harness to allow running multiple test cases
Expand Down
20 changes: 10 additions & 10 deletions grizzly/args.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from argparse import (
Action,
ArgumentParser,
Expand All @@ -14,7 +16,7 @@
from pathlib import Path
from platform import system
from types import MappingProxyType
from typing import Iterable, List, Optional
from typing import Iterable

from FTB.ProgramConfiguration import ProgramConfiguration

Expand All @@ -26,18 +28,18 @@
# ref: https://stackoverflow.com/questions/12268602/sort-argparse-help-alphabetically
class SortingHelpFormatter(HelpFormatter):
@staticmethod
def __sort_key(action: Action) -> List[str]:
def __sort_key(action: Action) -> list[str]:
for opt in action.option_strings:
if opt.startswith("--"):
return [opt]
return list(action.option_strings)

def add_usage(
self,
usage: Optional[str],
usage: str | None,
actions: Iterable[Action],
groups: Iterable[_MutuallyExclusiveGroup],
prefix: Optional[str] = None,
prefix: str | None = None,
) -> None:
actions = sorted(actions, key=self.__sort_key)
super().add_usage(usage, actions, groups, prefix)
Expand Down Expand Up @@ -266,22 +268,20 @@ 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: Optional[List[str]] = None) -> Namespace:
def parse_args(self, argv: list[str] | None = None) -> Namespace:
args = self.parser.parse_args(argv)
self.sanity_check(args)
return args

def sanity_check(self, args: Namespace) -> None:
if not args.binary.is_file():
self.parser.error(f"file not found: '{args.binary!s}'")
self.parser.error(f"file not found: '{args.binary}'")

# fuzzmanager reporter related checks
if args.fuzzmanager:
Expand Down
18 changes: 10 additions & 8 deletions grizzly/common/bugzilla.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

import binascii
from base64 import b64decode
from logging import getLogger
from os import environ
from pathlib import Path
from shutil import rmtree
from tempfile import mkdtemp
from typing import Any, Generator, List, Optional, Tuple
from typing import Generator
from zipfile import ZipFile

from bugsy import Bug, Bugsy
Expand All @@ -32,10 +34,10 @@ def __init__(self, bug: Bug) -> None:
self._data = Path(mkdtemp(prefix=f"bug{bug.id}-", dir=grz_tmp("bugzilla")))
self._fetch_attachments()

def __enter__(self) -> "BugzillaBug":
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 @@ -82,8 +84,8 @@ def _unpack_archives(self) -> None:
# TODO: add support for other archive types

def assets(
self, ignore: Optional[Tuple[str]] = None
) -> Generator[Tuple[str, Path], None, None]:
self, ignore: tuple[str] | None = None
) -> Generator[tuple[str, Path], None, None]:
"""Scan files for assets.

Arguments:
Expand All @@ -110,7 +112,7 @@ def cleanup(self) -> None:
rmtree(self._data)

@classmethod
def load(cls, bug_id: int) -> Optional["BugzillaBug"]:
def load(cls, bug_id: int) -> BugzillaBug | None:
"""Load bug information from a Bugzilla instance.

Arguments:
Expand All @@ -137,7 +139,7 @@ def load(cls, bug_id: int) -> Optional["BugzillaBug"]:
LOG.error("Unable to connect to %r (%s)", bugzilla.bugzilla_url, exc)
return None

def testcases(self) -> List[Path]:
def testcases(self) -> list[Path]:
"""Create a list of potential test cases.

Arguments:
Expand All @@ -148,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
Loading
Loading