cmd.py part 3 refactor

This commit is contained in:
Qubasa
2024-01-10 18:39:19 +01:00
parent cbc4c966f1
commit 513f48bab1
3 changed files with 15 additions and 30 deletions

View File

@@ -1,10 +1,10 @@
import json import json
import os import os
import re import re
import subprocess
from pathlib import Path from pathlib import Path
from tempfile import NamedTemporaryFile 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.dirs import machine_settings_file, nixpkgs_source, specific_machine_dir
from clan_cli.errors import ClanError, ClanHttpError from clan_cli.errors import ClanError, ClanHttpError
from clan_cli.git import commit_file 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, cmd,
capture_output=True,
text=True,
cwd=flake, cwd=flake,
env=env, env=env,
) )

View File

@@ -1,9 +1,8 @@
import argparse import argparse
import shlex
import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from ..cmd import run
from ..dirs import specific_groot_dir from ..dirs import specific_groot_dir
from ..errors import ClanError from ..errors import ClanError
from ..machines.list import list_machines from ..machines.list import list_machines
@@ -26,18 +25,7 @@ class FlakeConfig:
def run_cmd(cmd: list[str]) -> str: def run_cmd(cmd: list[str]) -> str:
proc = subprocess.run(cmd, text=True, stdout=subprocess.PIPE) proc = run(cmd)
assert proc.stdout is not None
if proc.returncode != 0:
raise ClanError(
f"""
command: {shlex.join(cmd)}
exit code: {proc.returncode}
stdout:
{proc.stdout}
"""
)
return proc.stdout.strip() return proc.stdout.strip()

View File

@@ -1,11 +1,11 @@
import shlex
import subprocess
from pathlib import Path from pathlib import Path
# from clan_cli.dirs import find_git_repo_root # 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 clan_cli.nix import nix_shell
from .cmd import run
# generic vcs agnostic commit function # generic vcs agnostic commit function
def commit_file( 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 # add the file to the git index
try: try:
subprocess.run(cmd, check=True) run(cmd)
except subprocess.CalledProcessError as e: except ClanCmdError as e:
raise ClanError( 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 ) from e
# check if there is a diff # 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"], ["nixpkgs#git"],
["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code", str(file_path)], ["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 there is no diff, return
if result.returncode == 0: if result.returncode == 0:
return return
@@ -73,11 +73,10 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) ->
], ],
) )
try: try:
subprocess.run( run(
cmd, cmd,
check=True,
) )
except subprocess.CalledProcessError as e: except ClanCmdError as e:
raise ClanError( 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 e