diff --git a/nixpkgs_review/builddir.py b/nixpkgs_review/builddir.py index af055e4..2ccfc79 100644 --- a/nixpkgs_review/builddir.py +++ b/nixpkgs_review/builddir.py @@ -30,8 +30,8 @@ def create_cache_directory(name: str) -> Union[Path, "TemporaryDirectory[str]"]: if home is None: # we are in a temporary directory return TemporaryDirectory() - else: - xdg_cache = Path(home).joinpath(".cache") + + xdg_cache = Path(home).joinpath(".cache") counter = 0 while True: diff --git a/nixpkgs_review/cli/__init__.py b/nixpkgs_review/cli/__init__.py index 4316403..69a1663 100644 --- a/nixpkgs_review/cli/__init__.py +++ b/nixpkgs_review/cli/__init__.py @@ -120,13 +120,13 @@ def hub_config_path() -> Path: raw_hub_path = os.environ.get("HUB_CONFIG", None) if raw_hub_path: return Path(raw_hub_path) + + raw_config_home = os.environ.get("XDG_CONFIG_HOME", None) + if raw_config_home is None: + config_home = Path.home().joinpath(".config") else: - raw_config_home = os.environ.get("XDG_CONFIG_HOME", None) - if raw_config_home is None: - config_home = Path.home().joinpath(".config") - else: - config_home = Path(raw_config_home) - return config_home.joinpath("hub") + config_home = Path(raw_config_home) + return config_home.joinpath("hub") def read_github_token() -> str | None: @@ -313,7 +313,7 @@ def parse_args(command: str, args: list[str]) -> argparse.Namespace: if args == []: main_parser.print_help() - exit(2) + sys.exit(2) return main_parser.parse_args(args) diff --git a/nixpkgs_review/errors.py b/nixpkgs_review/errors.py index 743d88f..8549cf9 100644 --- a/nixpkgs_review/errors.py +++ b/nixpkgs_review/errors.py @@ -1,4 +1,2 @@ class NixpkgsReviewError(Exception): """Base class for exceptions in this module.""" - - pass diff --git a/nixpkgs_review/github.py b/nixpkgs_review/github.py index 56a704e..6e18911 100644 --- a/nixpkgs_review/github.py +++ b/nixpkgs_review/github.py @@ -26,8 +26,8 @@ def _request( body = json.dumps(data).encode("ascii") req = urllib.request.Request(url, headers=headers, method=method, data=body) - resp = urllib.request.urlopen(req) - return json.loads(resp.read()) + with urllib.request.urlopen(req) as resp: + return json.loads(resp.read()) def get(self, path: str) -> Any: return self._request(path, "GET") @@ -42,7 +42,7 @@ def comment_issue(self, pr: int, msg: str) -> Any: "Post a comment on a PR with nixpkgs-review report" print(f"Posting result comment on {pr_url(pr)}") return self.post( - f"/repos/NixOS/nixpkgs/issues/{pr}/comments", data=dict(body=msg) + f"/repos/NixOS/nixpkgs/issues/{pr}/comments", data={"body": msg} ) def approve_pr(self, pr: int) -> Any: @@ -50,7 +50,7 @@ def approve_pr(self, pr: int) -> Any: print(f"Approving {pr_url(pr)}") return self.post( f"/repos/NixOS/nixpkgs/pulls/{pr}/reviews", - data=dict(event="APPROVE"), + data={"event": "APPROVE"}, ) def merge_pr(self, pr: int) -> Any: @@ -59,7 +59,7 @@ def merge_pr(self, pr: int) -> Any: return self.put(f"/repos/NixOS/nixpkgs/pulls/{pr}/merge") def graphql(self, query: str) -> dict[str, Any]: - resp = self.post("/graphql", data=dict(query=query)) + resp = self.post("/graphql", data={"query": query}) if "errors" in resp: raise RuntimeError(f"Expected data from graphql api, got: {resp}") data: dict[str, Any] = resp["data"] @@ -88,10 +88,13 @@ def get_borg_eval_gist(self, pr: dict[str, Any]) -> dict[str, set[str]] | None: raw_gist_url = ( f"https://gist.githubusercontent.com/GrahamcOfBorg/{gist_hash}/raw/" ) - for line in urllib.request.urlopen(raw_gist_url): - if line == b"": - break - system, attribute = line.decode("utf-8").split() - packages_per_system[system].add(attribute) + + with urllib.request.urlopen(raw_gist_url) as resp: + for line in resp: + if line == b"": + break + system, attribute = line.decode("utf-8").split() + packages_per_system[system].add(attribute) + return packages_per_system return None diff --git a/nixpkgs_review/review.py b/nixpkgs_review/review.py index 278b1ab..84e72f2 100644 --- a/nixpkgs_review/review.py +++ b/nixpkgs_review/review.py @@ -3,12 +3,12 @@ import subprocess import sys import tempfile -import xml.etree.ElementTree as ElementTree from dataclasses import dataclass, field from enum import Enum from pathlib import Path from re import Pattern from typing import IO +from xml.etree import ElementTree from .allow import AllowedFeatures from .builddir import Builddir @@ -135,9 +135,9 @@ def git_merge(self, commit: str) -> None: def apply_unstaged(self, staged: bool = False) -> None: args = ["git", "--no-pager", "diff", "--no-ext-diff"] args.extend(["--staged"] if staged else []) - diff_proc = subprocess.Popen(args, stdout=subprocess.PIPE) - assert diff_proc.stdout - diff = diff_proc.stdout.read() + with subprocess.Popen(args, stdout=subprocess.PIPE) as diff_proc: + assert diff_proc.stdout + diff = diff_proc.stdout.read() if not diff: info("No diff detected, stopping review...") diff --git a/nixpkgs_review/utils.py b/nixpkgs_review/utils.py index 0f98a7b..3245f4c 100644 --- a/nixpkgs_review/utils.py +++ b/nixpkgs_review/utils.py @@ -70,5 +70,5 @@ def nix_nom_tool() -> str: "Return `nom` if found in $PATH" if shutil.which("nom"): return "nom" - else: - return "nix" + + return "nix"