Skip to content

Commit

Permalink
Revert "kernel"
Browse files Browse the repository at this point in the history
This reverts commit cac6673.
  • Loading branch information
chantra committed Oct 16, 2024
1 parent 459573f commit b17f87a
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 140 deletions.
2 changes: 1 addition & 1 deletion kernel_patches_daemon/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ScriptMetricsExporter:
def __init__(self, script: str) -> None:
self.script = script

def export(self, project: str, metrics: dict) -> None:
def export(self, project: str, metrics: Dict) -> None:
if os.path.isfile(self.script) and os.access(self.script, os.X_OK):
p = Popen([self.script], stdout=PIPE, stdin=PIPE, stderr=PIPE)
p.communicate(input=json.dumps(metrics).encode())
Expand Down
61 changes: 32 additions & 29 deletions kernel_patches_daemon/branch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import tempfile
import time
from collections import namedtuple
from collections.abc import Generator, Sequence
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from email.mime.application import MIMEApplication
Expand All @@ -28,7 +27,7 @@
from enum import Enum
from pathlib import Path
from subprocess import PIPE
from typing import Any, Dict, Final, IO, List, Optional, Tuple
from typing import Any, Dict, Final, Generator, IO, List, Optional, Sequence, Tuple

import dateutil.parser
import git
Expand Down Expand Up @@ -161,7 +160,7 @@ def __str__(self):
)


def get_ci_base(series: Series) -> dict:
def get_ci_base(series: Series) -> Dict:
"""Retrieve the object (cover letter or patch) that we use as the base for
sending emails in response to.
"""
Expand Down Expand Up @@ -260,7 +259,7 @@ def build_email(
msg_id: str,
body: str,
boundary: str = "",
) -> tuple[list[str], str]:
) -> Tuple[List[str], str]:
"""
Builds complete email (including headers) to be sent along with curl command
necessary to send it.
Expand Down Expand Up @@ -354,11 +353,13 @@ def execute_command(cmd: str) -> None:
os.system(cmd)


def _uniq_tmp_folder(url: str | None, branch: str | None, base_directory: str) -> str:
def _uniq_tmp_folder(
url: Optional[str], branch: Optional[str], base_directory: str
) -> str:
# use same folder for multiple invocation to avoid cloning whole tree every time
# but use different folder for different workers identified by url and branch name
sha = hashlib.sha256()
sha.update(f"{url}/{branch}".encode())
sha.update(f"{url}/{branch}".encode("utf-8"))
# pyre-fixme[6]: For 1st argument expected `PathLike[Variable[AnyStr <: [str,
# bytes]]]` but got `Optional[str]`.
repo_name = remove_unsafe_chars(os.path.basename(url))
Expand All @@ -379,9 +380,9 @@ def temporary_patch_file(content: bytes) -> Generator[IO, None, None]:
tmp_patch_file.close()


def create_color_labels(labels_cfg: dict[str, str], repo: Repository) -> None:
repo_labels: dict[str, GithubLabel] = {x.name.lower(): x for x in repo.get_labels()}
labels_cfg: dict[str, str] = {k.lower(): v for k, v in labels_cfg.items()}
def create_color_labels(labels_cfg: Dict[str, str], repo: Repository) -> None:
repo_labels: Dict[str, GithubLabel] = {x.name.lower(): x for x in repo.get_labels()}
labels_cfg: Dict[str, str] = {k.lower(): v for k, v in labels_cfg.items()}

for label, color in labels_cfg.items():
if repo_label := repo_labels.get(label):
Expand Down Expand Up @@ -503,7 +504,7 @@ def slugify_context(s: str):
def __init__(
self,
patchwork: Patchwork,
labels_cfg: dict[str, Any],
labels_cfg: Dict[str, Any],
repo_branch: str,
repo_url: str,
upstream_url: str,
Expand All @@ -512,10 +513,10 @@ def __init__(
ci_repo_url: str,
ci_branch: str,
log_extractor: GithubLogExtractor,
github_oauth_token: str | None = None,
app_auth: Auth.AppInstallationAuth | None = None,
email: EmailConfig | None = None,
http_retries: int | None = None,
github_oauth_token: Optional[str] = None,
app_auth: Optional[Auth.AppInstallationAuth] = None,
email: Optional[EmailConfig] = None,
http_retries: Optional[int] = None,
) -> None:
super().__init__(
repo_url=repo_url,
Expand Down Expand Up @@ -547,7 +548,7 @@ def __init__(
create_color_labels(labels_cfg, self.repo)
# member variables
self.branches = {}
self.prs: dict[str, PullRequest] = {}
self.prs: Dict[str, PullRequest] = {}
self.all_prs = {}
self._closed_prs = None

Expand All @@ -564,7 +565,7 @@ def _add_pull_request_comment(self, pr: PullRequest, message: str) -> None:
try:
pr.create_issue_comment(message)
except GithubException as e:
if not isinstance(e.data, dict):
if not isinstance(e.data, Dict):
raise e
emsg = e.data.get("message")
if emsg is not None and emsg in KNOWN_OK_COMMENT_EXCEPTIONS:
Expand All @@ -576,7 +577,7 @@ def _add_pull_request_comment(self, pr: PullRequest, message: str) -> None:

def _update_e2e_pr(
self, title: str, base_branch: str, branch: str, has_codechange: bool
) -> PullRequest | None:
) -> Optional[PullRequest]:
"""Check if there is open PR on e2e branch, reopen if necessary."""
pr = None

Expand All @@ -597,7 +598,9 @@ def _update_e2e_pr(

return pr

def update_e2e_test_branch_and_update_pr(self, branch: str) -> PullRequest | None:
def update_e2e_test_branch_and_update_pr(
self, branch: str
) -> Optional[PullRequest]:
base_branch = branch + "_base"
branch_name = branch + "_test"

Expand Down Expand Up @@ -721,8 +724,8 @@ def _close_pr(self, pr: PullRequest) -> None:
pr.edit(state="closed")

async def _guess_pr(
self, series: Series, branch: str | None = None
) -> PullRequest | None:
self, series: Series, branch: Optional[str] = None
) -> Optional[PullRequest]:
"""
Series could change name
first series in a subject could be changed as well
Expand Down Expand Up @@ -754,11 +757,11 @@ async def _comment_series_pr(
self,
series: Series,
branch_name: str,
message: str | None = None,
message: Optional[str] = None,
can_create: bool = False,
close: bool = False,
has_merge_conflict: bool = False,
) -> PullRequest | None:
) -> Optional[PullRequest]:
"""
Appends comment to a PR.
"""
Expand Down Expand Up @@ -894,7 +897,7 @@ def _add_ci_files(self) -> None:

async def try_apply_mailbox_series(
self, branch_name: str, series: Series
) -> tuple[bool, Exception | None, Any | None]:
) -> Tuple[bool, Optional[Exception], Optional[Any]]:
"""Try to apply a mailbox series and return (True, None, None) if successful"""
# The pull request will be created against `repo_pr_base_branch`. So
# prepare it for that.
Expand All @@ -916,7 +919,7 @@ async def try_apply_mailbox_series(

async def apply_push_comment(
self, branch_name: str, series: Series
) -> PullRequest | None:
) -> Optional[PullRequest]:
comment = (
f"Upstream branch: {self.upstream_sha}\nseries: {series.web_url}\n"
f"version: {series.version}\n"
Expand Down Expand Up @@ -999,7 +1002,7 @@ async def apply_push_comment(

async def checkout_and_patch(
self, branch_name: str, series_to_apply: Series
) -> PullRequest | None:
) -> Optional[PullRequest]:
"""
Patch in place and push.
Returns true if whole series applied.
Expand Down Expand Up @@ -1049,7 +1052,7 @@ def _is_relevant_pr(self, pr: PullRequest) -> bool:
return True
return False

def closed_prs(self) -> list[Any]:
def closed_prs(self) -> List[Any]:
# GH api is not working: https://github.community/t/is-api-head-filter-even-working/135530
# so i have to implement local cache
# and local search
Expand All @@ -1061,7 +1064,7 @@ def closed_prs(self) -> list[Any]:
)
return self._closed_prs

def filter_closed_pr(self, head: str) -> PullRequest | None:
def filter_closed_pr(self, head: str) -> Optional[PullRequest]:
# this assumes only the most recent one closed PR per head
res = None
for pr in self.closed_prs():
Expand Down Expand Up @@ -1092,7 +1095,7 @@ async def sync_checks(self, pr: PullRequest, series: Series) -> None:

logger.info(f"Fetching workflow runs for {pr}: {pr.head.ref} (@ {pr.head.sha})")

statuses: list[Status] = []
statuses: List[Status] = []
jobs = []

# Note that we are interested in listing *all* runs and not just, say,
Expand Down Expand Up @@ -1162,7 +1165,7 @@ async def sync_checks(self, pr: PullRequest, series: Series) -> None:
await self.evaluate_ci_result(status, series, pr, jobs)

async def evaluate_ci_result(
self, status: Status, series: Series, pr: PullRequest, jobs: list[WorkflowJob]
self, status: Status, series: Series, pr: PullRequest, jobs: List[WorkflowJob]
) -> None:
"""Evaluate the result of a CI run and send an email as necessary."""
email = self.email
Expand Down
36 changes: 18 additions & 18 deletions kernel_patches_daemon/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _read_private_key(cls, private_key_path: os.PathLike) -> str:
) from e

@classmethod
def from_json(cls, json: dict) -> "GithubAppAuthConfig":
def from_json(cls, json: Dict) -> "GithubAppAuthConfig":
private_key_config = json.keys() & {
"private_key",
"private_key_path",
Expand Down Expand Up @@ -78,12 +78,12 @@ class BranchConfig:
upstream_branch: str
ci_repo: str
ci_branch: str
github_oauth_token: str | None
github_app_auth: GithubAppAuthConfig | None
github_oauth_token: Optional[str]
github_app_auth: Optional[GithubAppAuthConfig]

@classmethod
def from_json(cls, json: dict) -> "BranchConfig":
github_app_auth_config: GithubAppAuthConfig | None = None
def from_json(cls, json: Dict) -> "BranchConfig":
github_app_auth_config: Optional[GithubAppAuthConfig] = None
if app_auth_json := json.get("github_app_auth"):
try:
github_app_auth_config = GithubAppAuthConfig.from_json(app_auth_json)
Expand All @@ -108,21 +108,21 @@ class EmailConfig:
smtp_user: str
smtp_from: str
smtp_pass: str
smtp_to: list[str]
smtp_cc: list[str]
smtp_http_proxy: str | None
smtp_to: List[str]
smtp_cc: List[str]
smtp_http_proxy: Optional[str]
# List of email addresses of patch submitters to whom we send email
# notifications only for *their* very submission. This attribute is meant to
# be temporary while the email notification feature is being rolled out.
# Once we send email notifications to all patch submitters it can be
# removed.
submitter_allowlist: list[re.Pattern]
submitter_allowlist: List[re.Pattern]
# Ignore the `submitter_allowlist` entries and send emails to all patch
# submitters, unconditionally.
ignore_allowlist: bool

@classmethod
def from_json(cls, json: dict) -> "EmailConfig":
def from_json(cls, json: Dict) -> "EmailConfig":
return cls(
smtp_host=json["host"],
smtp_port=json.get("port", 465),
Expand All @@ -143,13 +143,13 @@ def from_json(cls, json: dict) -> "EmailConfig":
class PatchworksConfig:
base_url: str
project: str
search_patterns: list[dict]
search_patterns: List[Dict]
lookback: int
user: str | None
token: str | None
user: Optional[str]
token: Optional[str]

@classmethod
def from_json(cls, json: dict) -> "PatchworksConfig":
def from_json(cls, json: Dict) -> "PatchworksConfig":
return cls(
base_url=json["server"],
project=json["project"],
Expand All @@ -164,13 +164,13 @@ def from_json(cls, json: dict) -> "PatchworksConfig":
class KPDConfig:
version: int
patchwork: PatchworksConfig
email: EmailConfig | None
branches: dict[str, BranchConfig]
tag_to_branch_mapping: dict[str, list[str]]
email: Optional[EmailConfig]
branches: Dict[str, BranchConfig]
tag_to_branch_mapping: Dict[str, List[str]]
base_directory: str

@classmethod
def from_json(cls, json: dict) -> "KPDConfig":
def from_json(cls, json: Dict) -> "KPDConfig":
try:
version = int(json["version"])
except (KeyError, IndexError) as e:
Expand Down
13 changes: 6 additions & 7 deletions kernel_patches_daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import logging
import signal
import threading
from collections.abc import Callable
from typing import Dict, Final, Optional
from typing import Callable, Dict, Final, Optional

from kernel_patches_daemon.config import KPDConfig
from kernel_patches_daemon.github_sync import GithubSync
Expand All @@ -33,8 +32,8 @@ class KernelPatchesWorker:
def __init__(
self,
kpd_config: KPDConfig,
labels_cfg: dict[str, str],
metrics_logger: Callable | None = None,
labels_cfg: Dict[str, str],
metrics_logger: Optional[Callable] = None,
loop_delay: int = DEFAULT_LOOP_DELAY,
max_concurrent_restarts: int = DEFAULT_MAX_CONCURRENT_RESTARTS,
) -> None:
Expand Down Expand Up @@ -77,12 +76,12 @@ class KernelPatchesDaemon:
def __init__(
self,
kpd_config: KPDConfig,
labels_cfg: dict[str, str],
metrics_logger: Callable | None = None,
labels_cfg: Dict[str, str],
metrics_logger: Optional[Callable] = None,
max_concurrent_restarts: int = 1,
) -> None:
self._stopping_lock = threading.Lock()
self._task: asyncio.Task | None = None
self._task: Optional[asyncio.Task] = None
self.worker = KernelPatchesWorker(
kpd_config=kpd_config,
labels_cfg=labels_cfg,
Expand Down
6 changes: 3 additions & 3 deletions kernel_patches_daemon/github_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class GithubConnector:
def __init__(
self,
repo_url: str,
github_oauth_token: str | None = None,
app_auth: Auth.AppInstallationAuth | None = None,
http_retries: int | None = None,
github_oauth_token: Optional[str] = None,
app_auth: Optional[Auth.AppInstallationAuth] = None,
http_retries: Optional[int] = None,
) -> None:

assert bool(github_oauth_token) ^ bool(
Expand Down
Loading

0 comments on commit b17f87a

Please sign in to comment.