From f79b19832ab55d61128e3cf8f5782f0c411779b2 Mon Sep 17 00:00:00 2001 From: Nikita Karetnikov Date: Sun, 28 Jan 2024 17:47:07 +0100 Subject: [PATCH] Rename `print_cmd` to `logged_command`, move it to `utils` --- .../action/generate_constructor_installer.py | 15 +++------------ .../action/generate_lockfile.py | 14 ++++---------- .../conda_store_server/action/utils.py | 10 ++++++++++ 3 files changed, 17 insertions(+), 22 deletions(-) create mode 100644 conda-store-server/conda_store_server/action/utils.py diff --git a/conda-store-server/conda_store_server/action/generate_constructor_installer.py b/conda-store-server/conda_store_server/action/generate_constructor_installer.py index 30dc9aff2..73bf55b75 100644 --- a/conda-store-server/conda_store_server/action/generate_constructor_installer.py +++ b/conda-store-server/conda_store_server/action/generate_constructor_installer.py @@ -1,12 +1,12 @@ import os import pathlib -import subprocess import sys import tempfile import warnings import yaml from conda_store_server import action, schema +from conda_store_server.action.utils import logged_command def get_installer_platform(): @@ -26,15 +26,6 @@ def action_generate_constructor_installer( installer_dir: pathlib.Path, version: str, ): - # Helpers - def print_cmd(cmd, **kwargs): - context.log.info(f"Running command: {' '.join(cmd)}") - context.log.info( - subprocess.check_output( - cmd, stderr=subprocess.STDOUT, encoding="utf-8", **kwargs - ) - ) - def write_file(filename, s): with open(filename, "w") as f: context.log.info(f"{filename}:\n{s}") @@ -46,7 +37,7 @@ def write_file(filename, s): "constructor", "--help", ] - print_cmd(command, timeout=10) + logged_command(context, command, timeout=10) except FileNotFoundError: warnings.warn( "Installer generation requires constructor: https://github.com/conda/constructor" @@ -124,6 +115,6 @@ def write_file(filename, s): get_installer_platform(), str(tmp_dir), ] - print_cmd(command) + logged_command(context, command) return installer_filename diff --git a/conda-store-server/conda_store_server/action/generate_lockfile.py b/conda-store-server/conda_store_server/action/generate_lockfile.py index caf2d9118..550012426 100644 --- a/conda-store-server/conda_store_server/action/generate_lockfile.py +++ b/conda-store-server/conda_store_server/action/generate_lockfile.py @@ -1,12 +1,12 @@ import json import os import pathlib -import subprocess import typing import yaml from conda_lock.conda_lock import run_lock from conda_store_server import action, conda_utils, schema +from conda_store_server.action.utils import logged_command @action.action @@ -25,17 +25,11 @@ def action_solve_lockfile( with environment_filename.open("w") as f: json.dump(specification.dict(), f) - def print_cmd(cmd): - context.log.info(f"Running command: {' '.join(cmd)}") - context.log.info( - subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding="utf-8") - ) - # The info command can be used with either mamba or conda - print_cmd([conda_command, "info"]) + logged_command(context, [conda_command, "info"]) # The config command is not supported by mamba - print_cmd(["conda", "config", "--show"]) - print_cmd(["conda", "config", "--show-sources"]) + logged_command(context, ["conda", "config", "--show"]) + logged_command(context, ["conda", "config", "--show-sources"]) # conda-lock ignores variables defined in the specification, so this code # gets the value of CONDA_OVERRIDE_CUDA and passes it to conda-lock via diff --git a/conda-store-server/conda_store_server/action/utils.py b/conda-store-server/conda_store_server/action/utils.py new file mode 100644 index 000000000..8c7ba5699 --- /dev/null +++ b/conda-store-server/conda_store_server/action/utils.py @@ -0,0 +1,10 @@ +import subprocess + + +def logged_command(context, command, **kwargs): + context.log.info(f"Running command: {' '.join(command)}") + context.log.info( + subprocess.check_output( + command, stderr=subprocess.STDOUT, encoding="utf-8", **kwargs + ) + )