From f24cb3274a1b901b85735a9ba40738f23bb6ef76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Fri, 15 Nov 2024 16:23:12 +0100 Subject: [PATCH 1/4] prevent pip "rich" output --- libmamba/src/core/prefix_data.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/libmamba/src/core/prefix_data.cpp b/libmamba/src/core/prefix_data.cpp index c7031dbaff..d8bbebfce1 100644 --- a/libmamba/src/core/prefix_data.cpp +++ b/libmamba/src/core/prefix_data.cpp @@ -215,13 +215,19 @@ namespace mamba const auto get_python_path = [&] { return util::which_in("python", util::get_path_dirs(m_prefix_path)).string(); }; - const auto args = std::array{ get_python_path(), + const auto args = std::array{ get_python_path(), + "-q", "-m", "pip", "inspect", "--local" }; - const std::vector> env{ { "PYTHONIOENCODING", "utf-8" } }; + const std::vector> env{ + { "PYTHONIOENCODING", "utf-8" }, + { "NO_COLOR", "1" }, + { "PIP_NO_COLOR", "1" }, + { "PIP_NO_PYTHON_VERSION_WARNING", "1" }, + }; reproc::options run_options; run_options.env.extra = reproc::env{ env }; @@ -261,11 +267,11 @@ namespace mamba { j = nlohmann::json::parse(out); } - catch (const std::exception& exc) + catch (const std::exception& parse_error) { const auto message = fmt::format( "failed to parse python command output:\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}", - exc.what(), + parse_error.what(), fmt::join(args, " "), fmt::join(env, " "), out, From 8fcb9300578776201db6cd7bb60fed78dd93e6df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Mon, 18 Nov 2024 12:57:34 +0100 Subject: [PATCH 2/4] remove FORCE_COLOR from env variables when running pip --- libmamba/src/core/prefix_data.cpp | 52 ++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/libmamba/src/core/prefix_data.cpp b/libmamba/src/core/prefix_data.cpp index d8bbebfce1..b8ac28e308 100644 --- a/libmamba/src/core/prefix_data.cpp +++ b/libmamba/src/core/prefix_data.cpp @@ -17,6 +17,7 @@ #include "mamba/core/output.hpp" #include "mamba/core/prefix_data.hpp" #include "mamba/core/util.hpp" +#include "mamba/core/util_scope.hpp" #include "mamba/specs/conda_url.hpp" #include "mamba/util/environment.hpp" #include "mamba/util/graph.hpp" @@ -231,27 +232,42 @@ namespace mamba reproc::options run_options; run_options.env.extra = reproc::env{ env }; - LOG_TRACE << "Running command: " - << fmt::format("{}\n env options:{}", fmt::join(args, " "), fmt::join(env, " ")); + { // Scopped environment changes - auto [status, ec] = reproc::run( - args, - run_options, - reproc::sink::string(out), - reproc::sink::string(err) - ); + // We need FORCE_COLOR to be removed to avoid rich output, + // we restore it as soon as the command is run. + const auto maybe_previous_force_color = util::get_env("FORCE_COLOR"); + util::unset_env("FORCE_COLOR"); + on_scope_exit _{[&]{ + if(maybe_previous_force_color) + { + util::set_env("FORCE_COLOR", maybe_previous_force_color.value()); + } + }}; - if (ec) - { - const auto message = fmt::format( - "failed to run python command :\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}", - ec.message(), - fmt::join(args, " "), - fmt::join(env, " "), - out, - err + LOG_TRACE << "Running command: " + << fmt::format("{}\n env options (FORCE_COLOR is unset):{}", fmt::join(args, " "), fmt::join(env, " ")); + + auto [status, ec] = reproc::run( + args, + run_options, + reproc::sink::string(out), + reproc::sink::string(err) ); - throw mamba_error{ message, mamba_error_code::internal_failure }; + + if (ec) + { + const auto message = fmt::format( + "failed to run python command :\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}", + ec.message(), + fmt::join(args, " "), + fmt::join(env, " "), + out, + err + ); + throw mamba_error{ message, mamba_error_code::internal_failure }; + } + } // Nothing installed with `pip` From 0d3cf2ea64a8b17245888321f9db9db918230583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Lamotte=20=28Klaim=29?= Date: Mon, 18 Nov 2024 13:52:20 +0100 Subject: [PATCH 3/4] formatting --- libmamba/src/core/prefix_data.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/libmamba/src/core/prefix_data.cpp b/libmamba/src/core/prefix_data.cpp index b8ac28e308..6ee54b8a0d 100644 --- a/libmamba/src/core/prefix_data.cpp +++ b/libmamba/src/core/prefix_data.cpp @@ -216,12 +216,8 @@ namespace mamba const auto get_python_path = [&] { return util::which_in("python", util::get_path_dirs(m_prefix_path)).string(); }; - const auto args = std::array{ get_python_path(), - "-q", - "-m", - "pip", - "inspect", - "--local" }; + const auto args = std::array{ get_python_path(), "-q", "-m", "pip", + "inspect", "--local" }; const std::vector> env{ { "PYTHONIOENCODING", "utf-8" }, @@ -232,21 +228,26 @@ namespace mamba reproc::options run_options; run_options.env.extra = reproc::env{ env }; - { // Scopped environment changes + { // Scopped environment changes // We need FORCE_COLOR to be removed to avoid rich output, // we restore it as soon as the command is run. const auto maybe_previous_force_color = util::get_env("FORCE_COLOR"); util::unset_env("FORCE_COLOR"); - on_scope_exit _{[&]{ - if(maybe_previous_force_color) - { - util::set_env("FORCE_COLOR", maybe_previous_force_color.value()); - } - }}; + on_scope_exit _{ [&] + { + if (maybe_previous_force_color) + { + util::set_env("FORCE_COLOR", maybe_previous_force_color.value()); + } + } }; LOG_TRACE << "Running command: " - << fmt::format("{}\n env options (FORCE_COLOR is unset):{}", fmt::join(args, " "), fmt::join(env, " ")); + << fmt::format( + "{}\n env options (FORCE_COLOR is unset):{}", + fmt::join(args, " "), + fmt::join(env, " ") + ); auto [status, ec] = reproc::run( args, @@ -267,7 +268,6 @@ namespace mamba ); throw mamba_error{ message, mamba_error_code::internal_failure }; } - } // Nothing installed with `pip` From a167668501a8844773af13e1af5c5edc7b618d53 Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Wed, 20 Nov 2024 09:46:31 +0100 Subject: [PATCH 4/4] Update libmamba/src/core/prefix_data.cpp Co-authored-by: Hind-M <70631848+Hind-M@users.noreply.github.com> --- libmamba/src/core/prefix_data.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libmamba/src/core/prefix_data.cpp b/libmamba/src/core/prefix_data.cpp index 6ee54b8a0d..50e574506d 100644 --- a/libmamba/src/core/prefix_data.cpp +++ b/libmamba/src/core/prefix_data.cpp @@ -228,7 +228,7 @@ namespace mamba reproc::options run_options; run_options.env.extra = reproc::env{ env }; - { // Scopped environment changes + { // Scoped environment changes // We need FORCE_COLOR to be removed to avoid rich output, // we restore it as soon as the command is run.