From d513f66170a240f94811804c6aaf5d1ea3a5225f Mon Sep 17 00:00:00 2001 From: Qubasa Date: Sun, 2 Jun 2024 10:00:19 +0200 Subject: [PATCH] clan-cli: Refactor machines/update.py to cmd.run --- pkgs/clan-cli/clan_cli/cmd.py | 4 ++-- pkgs/clan-cli/clan_cli/machines/update.py | 17 +++++------------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/cmd.py b/pkgs/clan-cli/clan_cli/cmd.py index a7bc567bf..69f29bb41 100644 --- a/pkgs/clan-cli/clan_cli/cmd.py +++ b/pkgs/clan-cli/clan_cli/cmd.py @@ -56,7 +56,7 @@ def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]: sys.stderr.buffer.write(ret) sys.stderr.flush() stderr_buf += ret - return stdout_buf.decode("utf-8"), stderr_buf.decode("utf-8") + return stdout_buf.decode("utf-8", "replace"), stderr_buf.decode("utf-8", "replace") class TimeTable: @@ -110,7 +110,7 @@ def run( ) -> CmdOut: if input: glog.debug( - f"""$: echo "{input.decode('utf-8')}" | {shlex.join(cmd)} \nCaller: {get_caller()}""" + f"""$: echo "{input.decode('utf-8', 'replace')}" | {shlex.join(cmd)} \nCaller: {get_caller()}""" ) else: glog.debug(f"$: {shlex.join(cmd)} \nCaller: {get_caller()}") diff --git a/pkgs/clan-cli/clan_cli/machines/update.py b/pkgs/clan-cli/clan_cli/machines/update.py index 3a774ee9f..1532cb6d7 100644 --- a/pkgs/clan-cli/clan_cli/machines/update.py +++ b/pkgs/clan-cli/clan_cli/machines/update.py @@ -3,9 +3,9 @@ import json import logging import os import shlex -import subprocess import sys +from ..cmd import run from ..completions import add_dynamic_completer, complete_machines from ..errors import ClanError from ..facts.generate import generate_facts @@ -54,11 +54,7 @@ def upload_sources( path, ] ) - proc = subprocess.run(cmd, stdout=subprocess.PIPE, env=env, check=False) - if proc.returncode != 0: - raise ClanError( - f"failed to upload sources: {shlex.join(cmd)} failed with {proc.returncode}" - ) + run(cmd, env=env, error_msg="failed to upload sources") return path # Slow path: we need to upload all sources to the remote machine @@ -74,16 +70,13 @@ def upload_sources( ] ) log.info("run %s", shlex.join(cmd)) - proc = subprocess.run(cmd, stdout=subprocess.PIPE, check=False) - if proc.returncode != 0: - raise ClanError( - f"failed to upload sources: {shlex.join(cmd)} failed with {proc.returncode}" - ) + proc = run(cmd, error_msg="failed to upload sources") + try: return json.loads(proc.stdout)["path"] except (json.JSONDecodeError, OSError) as e: raise ClanError( - f"failed to parse output of {shlex.join(cmd)}: {e}\nGot: {proc.stdout.decode('utf-8', 'replace')}" + f"failed to parse output of {shlex.join(cmd)}: {e}\nGot: {proc.stdout}" )