Skip to content

Commit

Permalink
Prevent pip "rich" ouput (#3607)
Browse files Browse the repository at this point in the history
Co-authored-by: Johan Mabille <[email protected]>
Co-authored-by: Hind-M <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2024
1 parent d42b8c2 commit e1e0f8c
Showing 1 changed file with 50 additions and 28 deletions.
78 changes: 50 additions & 28 deletions libmamba/src/core/prefix_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, " "));

auto [status, ec] = reproc::run(
args,
run_options,
reproc::sink::string(out),
reproc::sink::string(err)
);

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
{ // Scoped 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());
}
} };

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`
Expand All @@ -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,
Expand Down

0 comments on commit e1e0f8c

Please sign in to comment.