Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent pip "rich" ouput #3607

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{ // Scopped environment changes
JohanMabille marked this conversation as resolved.
Show resolved Hide resolved

// 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);
Hind-M marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading