Prevent pip "rich" ouput (#3607)

Co-authored-by: Johan Mabille <johan.mabille@gmail.com>
Co-authored-by: Hind-M <70631848+Hind-M@users.noreply.github.com>
This commit is contained in:
Klaim (Joël Lamotte) 2024-11-20 11:27:55 +01:00 committed by GitHub
parent d42b8c2481
commit e1e0f8c88d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 48 additions and 26 deletions

View File

@ -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"
@ -215,37 +216,58 @@ 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<std::string, 5>{ get_python_path(),
"-m",
"pip",
"inspect",
"--local" };
const auto args = std::array<std::string, 6>{ get_python_path(), "-q", "-m", "pip",
"inspect", "--local" };
const std::vector<std::pair<std::string, std::string>> env{ { "PYTHONIOENCODING", "utf-8" } };
const std::vector<std::pair<std::string, std::string>> 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 };
LOG_TRACE << "Running command: "
<< fmt::format("{}\n env options:{}", fmt::join(args, " "), fmt::join(env, " "));
{ // Scoped 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`
@ -261,11 +283,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,