From b36d0be52450c78b6a771ed47c2b0e14c3907e32 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Wed, 10 Jan 2024 18:39:19 +0100 Subject: [PATCH 1/3] cmd.py part 3 refactor --- pkgs/clan-cli/clan_cli/config/machine.py | 8 +++----- pkgs/clan-cli/clan_cli/flakes/inspect.py | 16 ++-------------- pkgs/clan-cli/clan_cli/git.py | 21 ++++++++++----------- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/config/machine.py b/pkgs/clan-cli/clan_cli/config/machine.py index eeb20087f..9c406fd30 100644 --- a/pkgs/clan-cli/clan_cli/config/machine.py +++ b/pkgs/clan-cli/clan_cli/config/machine.py @@ -1,10 +1,10 @@ import json import os import re -import subprocess from pathlib import Path from tempfile import NamedTemporaryFile +from clan_cli.cmd import run from clan_cli.dirs import machine_settings_file, nixpkgs_source, specific_machine_dir from clan_cli.errors import ClanError, ClanHttpError from clan_cli.git import commit_file @@ -60,11 +60,9 @@ def verify_machine_config( """, ], ) - # repro_env_break(work_dir=flake, env=env, cmd=cmd) - proc = subprocess.run( + + proc = run( cmd, - capture_output=True, - text=True, cwd=flake, env=env, ) diff --git a/pkgs/clan-cli/clan_cli/flakes/inspect.py b/pkgs/clan-cli/clan_cli/flakes/inspect.py index ade5561c8..aa0a875b9 100644 --- a/pkgs/clan-cli/clan_cli/flakes/inspect.py +++ b/pkgs/clan-cli/clan_cli/flakes/inspect.py @@ -1,9 +1,8 @@ import argparse -import shlex -import subprocess from dataclasses import dataclass from pathlib import Path +from ..cmd import run from ..dirs import specific_groot_dir from ..errors import ClanError from ..machines.list import list_machines @@ -26,18 +25,7 @@ class FlakeConfig: def run_cmd(cmd: list[str]) -> str: - proc = subprocess.run(cmd, text=True, stdout=subprocess.PIPE) - assert proc.stdout is not None - if proc.returncode != 0: - raise ClanError( - f""" -command: {shlex.join(cmd)} -exit code: {proc.returncode} -stdout: -{proc.stdout} -""" - ) - + proc = run(cmd) return proc.stdout.strip() diff --git a/pkgs/clan-cli/clan_cli/git.py b/pkgs/clan-cli/clan_cli/git.py index 126f8587d..be318c32a 100644 --- a/pkgs/clan-cli/clan_cli/git.py +++ b/pkgs/clan-cli/clan_cli/git.py @@ -1,11 +1,11 @@ -import shlex -import subprocess from pathlib import Path # from clan_cli.dirs import find_git_repo_root -from clan_cli.errors import ClanError +from clan_cli.errors import ClanCmdError, ClanError from clan_cli.nix import nix_shell +from .cmd import run + # generic vcs agnostic commit function def commit_file( @@ -43,10 +43,10 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) -> ) # add the file to the git index try: - subprocess.run(cmd, check=True) - except subprocess.CalledProcessError as e: + run(cmd) + except ClanCmdError as e: raise ClanError( - f"Failed to add {file_path} to git repository {repo_dir}:\n{shlex.join(cmd)}\n exited with {e.returncode}" + f"Failed to add {file_path} to git repository {repo_dir}:\n{e.cmd.command}\n exited with {e.cmd.returncode}" ) from e # check if there is a diff @@ -54,7 +54,7 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) -> ["nixpkgs#git"], ["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code", str(file_path)], ) - result = subprocess.run(cmd, cwd=repo_dir) + result = run(cmd, cwd=repo_dir) # if there is no diff, return if result.returncode == 0: return @@ -73,11 +73,10 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) -> ], ) try: - subprocess.run( + run( cmd, - check=True, ) - except subprocess.CalledProcessError as e: + except ClanCmdError as e: raise ClanError( - f"Failed to commit {file_path} to git repository {repo_dir}:\n{shlex.join(cmd)}\n exited with {e.returncode}" + f"Failed to commit {file_path} to git repository {repo_dir}:\n{e.cmd.command}\n exited with {e.cmd.returncode}" ) from e From 64846eb3953c7cf4657c4607d19ea41ace64ae6a Mon Sep 17 00:00:00 2001 From: Qubasa Date: Wed, 10 Jan 2024 18:46:54 +0100 Subject: [PATCH 2/3] Fixed bug, where exception is raised where there shouldn't be raised one --- pkgs/clan-cli/clan_cli/cmd.py | 7 ++++--- pkgs/clan-cli/clan_cli/git.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/cmd.py b/pkgs/clan-cli/clan_cli/cmd.py index 5aeaab904..5c7f8a4dc 100644 --- a/pkgs/clan-cli/clan_cli/cmd.py +++ b/pkgs/clan-cli/clan_cli/cmd.py @@ -54,6 +54,7 @@ def run( env: dict[str, str] | None = None, cwd: Path = Path.cwd(), log: Log = Log.STDERR, + check: bool = True, ) -> CmdOut: # Start the subprocess process = subprocess.Popen( @@ -70,14 +71,14 @@ def run( # Wait for the subprocess to finish rc = process.wait() cmd_out = CmdOut( - stdout_buf, - stderr_buf, + stdout=stdout_buf, + stderr=stderr_buf, cwd=cwd, command=shlex.join(cmd), returncode=process.returncode, ) - if rc != 0: + if check and rc != 0: raise ClanCmdError(cmd_out) return cmd_out diff --git a/pkgs/clan-cli/clan_cli/git.py b/pkgs/clan-cli/clan_cli/git.py index be318c32a..977581015 100644 --- a/pkgs/clan-cli/clan_cli/git.py +++ b/pkgs/clan-cli/clan_cli/git.py @@ -54,7 +54,7 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) -> ["nixpkgs#git"], ["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code", str(file_path)], ) - result = run(cmd, cwd=repo_dir) + result = run(cmd, check=False, cwd=repo_dir) # if there is no diff, return if result.returncode == 0: return From 359275eee7f0b06c06053c2b087e7df842342ec1 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Wed, 10 Jan 2024 18:54:34 +0100 Subject: [PATCH 3/3] Improved error message. Fixed incorrect ret code check in git.py --- pkgs/clan-cli/clan_cli/errors.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkgs/clan-cli/clan_cli/errors.py b/pkgs/clan-cli/clan_cli/errors.py index 95955690f..f3f2b4a7e 100644 --- a/pkgs/clan-cli/clan_cli/errors.py +++ b/pkgs/clan-cli/clan_cli/errors.py @@ -9,6 +9,18 @@ class CmdOut(NamedTuple): command: str returncode: int + def __str__(self) -> str: + return f""" +Working Directory: '{self.cwd}' +Return Code: {self.returncode} +=================== Command =================== +{self.command} +=================== STDERR =================== +{self.stderr} +=================== STDOUT =================== +{self.stdout} +""" + class ClanError(Exception): """Base class for exceptions in this module.""" @@ -32,3 +44,9 @@ class ClanCmdError(ClanError): def __init__(self, cmd: CmdOut) -> None: self.cmd = cmd super().__init__() + + def __str__(self) -> str: + return str(self.cmd) + + def __repr__(self) -> str: + return f"ClanCmdError({self.cmd})"