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

Add type hints for Bugzilla and FuzzManager connectors #427

Merged
merged 1 commit into from
May 13, 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
29 changes: 16 additions & 13 deletions grizzly/common/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from pathlib import Path
from shutil import rmtree
from tempfile import mkdtemp
from typing import Any, Generator, List, Optional, Tuple
from zipfile import ZipFile

from bugsy import Bugsy
from bugsy import Bug, Bugsy
from bugsy.errors import BugsyException
from requests.exceptions import ConnectionError as RequestsConnectionError

Expand All @@ -26,15 +27,15 @@
class BugzillaBug:
__slots__ = ("_bug", "_data")

def __init__(self, bug):
def __init__(self, bug: Bug) -> None:
self._bug = bug
self._data = Path(mkdtemp(prefix=f"bug{bug.id}-", dir=grz_tmp("bugzilla")))
self._fetch_attachments()

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

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

def _fetch_attachments(self) -> None:
Expand Down Expand Up @@ -62,7 +63,7 @@ def _fetch_attachments(self) -> None:
continue
(self._data / attachment.file_name).write_bytes(data)

def _unpack_archives(self):
def _unpack_archives(self) -> None:
"""Unpack and remove archives.

Arguments:
Expand All @@ -80,22 +81,24 @@ def _unpack_archives(self):
entry.unlink()
# TODO: add support for other archive types

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

Arguments:
ignore (list(str)): Assets not to include in output.
ignore: Assets not to include in output.

Yields:
tuple(str, Path): Name and path to asset.
Asset name and path.
"""
for asset, file in KNOWN_ASSETS.items():
if not ignore or asset not in ignore:
asset_path = self._data / file
if asset_path.is_file():
yield asset, asset_path

def cleanup(self):
def cleanup(self) -> None:
"""Remove attachment data.

Arguments:
Expand All @@ -107,11 +110,11 @@ def cleanup(self):
rmtree(self._data)

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

Arguments:
bug_id (int): Bug to load.
bug_id: Bug to load.

Returns:
BugzillaBug
Expand All @@ -128,14 +131,14 @@ def load(cls, bug_id):
LOG.error("Unable to connect to %r (%s)", bugzilla.bugzilla_url, exc)
return None

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

Arguments:
None

Returns:
list(Path): Files and directories that could potentially be test cases.
Files and directories that could potentially be test cases.
"""
# unpack archives
self._unpack_archives()
Expand Down
Loading
Loading