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

feat: add base flag to info command #3779

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
205 changes: 115 additions & 90 deletions libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include <iostream>

jjerphan marked this conversation as resolved.
Show resolved Hide resolved
#include "mamba/api/configuration.hpp"
#include "mamba/api/info.hpp"
#include "mamba/core/channel_context.hpp"
Expand All @@ -23,25 +25,13 @@ extern "C"

namespace mamba
{
void info(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_default_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
);
config.load();

auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::print_info(config.context(), channel_context, config);

config.operation_teardown();
}

namespace detail
{
struct list_options
{
bool base;
};

void info_pretty_print(
std::vector<std::tuple<std::string, nlohmann::json>> items,
const Context::OutputParams& params
Expand Down Expand Up @@ -91,102 +81,137 @@ namespace mamba
Console::instance().json_write(items_map);
}

void print_info(Context& ctx, ChannelContext& channel_context, const Configuration& config)
void print_info(
Context& ctx,
ChannelContext& channel_context,
const Configuration& config,
list_options options
)
{
assert(&ctx == &config.context());
std::vector<std::tuple<std::string, nlohmann::json>> items;

items.push_back({ "libmamba version", version() });

if (ctx.command_params.is_mamba_exe && !ctx.command_params.caller_version.empty())
if (options.base)
{
items.push_back({
fmt::format("{} version", get_self_exe_path().stem().string()),
ctx.command_params.caller_version,
});
std::cout << ctx.prefix_params.root_prefix.string() << std::endl;
}
else
jjerphan marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<std::tuple<std::string, nlohmann::json>> items;

items.push_back({ "curl version", curl_version() });
items.push_back({ "libarchive version", archive_version_details() });
items.push_back({ "libmamba version", version() });

items.push_back({ "envs directories", ctx.envs_dirs });
items.push_back({ "package cache", ctx.pkgs_dirs });
if (ctx.command_params.is_mamba_exe && !ctx.command_params.caller_version.empty())
{
items.push_back({
fmt::format("{} version", get_self_exe_path().stem().string()),
ctx.command_params.caller_version,
});
}

std::string name, location;
if (!ctx.prefix_params.target_prefix.empty())
{
name = env_name(ctx);
location = ctx.prefix_params.target_prefix.string();
}
else
{
name = "None";
location = "-";
}
items.push_back({ "curl version", curl_version() });
items.push_back({ "libarchive version", archive_version_details() });

if (auto prefix = util::get_env("CONDA_PREFIX"); prefix == ctx.prefix_params.target_prefix)
{
name += " (active)";
}
else if (fs::exists(ctx.prefix_params.target_prefix))
{
if (!(fs::exists(ctx.prefix_params.target_prefix / "conda-meta")
|| (ctx.prefix_params.target_prefix == ctx.prefix_params.root_prefix)))
items.push_back({ "envs directories", ctx.envs_dirs });
items.push_back({ "package cache", ctx.pkgs_dirs });

std::string name, location;
if (!ctx.prefix_params.target_prefix.empty())
{
name += " (not env)";
name = env_name(ctx);
location = ctx.prefix_params.target_prefix.string();
}
else
{
name = "None";
location = "-";
}

if (auto prefix = util::get_env("CONDA_PREFIX");
prefix == ctx.prefix_params.target_prefix)
{
name += " (active)";
}
else if (fs::exists(ctx.prefix_params.target_prefix))
{
if (!(fs::exists(ctx.prefix_params.target_prefix / "conda-meta")
|| (ctx.prefix_params.target_prefix == ctx.prefix_params.root_prefix)))
{
name += " (not env)";
}
}
else
{
name += " (not found)";
}
}
else
{
name += " (not found)";
}

items.push_back({ "environment", name });
items.push_back({ "env location", location });
items.push_back({ "environment", name });
items.push_back({ "env location", location });

// items.insert( { "shell level", { 1 } });
items.push_back({
"user config files",
{ util::path_concat(util::user_home_dir(), ".mambarc") },
});
// items.insert( { "shell level", { 1 } });
items.push_back({
"user config files",
{ util::path_concat(util::user_home_dir(), ".mambarc") },
});

std::vector<std::string> sources;
for (auto s : config.valid_sources())
{
sources.push_back(s.string());
};
items.push_back({ "populated config files", sources });
std::vector<std::string> sources;
for (auto s : config.valid_sources())
{
sources.push_back(s.string());
};
items.push_back({ "populated config files", sources });

std::vector<std::string> virtual_pkgs;
for (auto pkg : get_virtual_packages(ctx.platform))
{
virtual_pkgs.push_back(util::concat(pkg.name, "=", pkg.version, "=", pkg.build_string)
);
}
items.push_back({ "virtual packages", virtual_pkgs });
std::vector<std::string> virtual_pkgs;
for (auto pkg : get_virtual_packages(ctx.platform))
{
virtual_pkgs.push_back(
util::concat(pkg.name, "=", pkg.version, "=", pkg.build_string)
);
}
items.push_back({ "virtual packages", virtual_pkgs });

// Always append context channels
std::vector<std::string> channel_urls;
using Credentials = specs::CondaURL::Credentials;
channel_urls.reserve(ctx.channels.size() * 2); // Lower bound * (platform + noarch)
for (const auto& loc : ctx.channels)
{
for (auto channel : channel_context.make_channel(loc))
// Always append context channels
std::vector<std::string> channel_urls;
using Credentials = specs::CondaURL::Credentials;
channel_urls.reserve(ctx.channels.size() * 2); // Lower bound * (platform + noarch)
for (const auto& loc : ctx.channels)
{
for (auto url : channel.platform_urls())
for (auto channel : channel_context.make_channel(loc))
{
channel_urls.push_back(std::move(url).str(Credentials::Remove));
for (auto url : channel.platform_urls())
{
channel_urls.push_back(std::move(url).str(Credentials::Remove));
}
}
}
}
items.push_back({ "channels", channel_urls });
items.push_back({ "channels", channel_urls });

items.push_back({ "base environment", ctx.prefix_params.root_prefix.string() });
items.push_back({ "base environment", ctx.prefix_params.root_prefix.string() });

items.push_back({ "platform", ctx.platform });
items.push_back({ "platform", ctx.platform });

info_json_print(items);
info_pretty_print(items, ctx.output_params);
info_json_print(items);
info_pretty_print(items, ctx.output_params);
}
}
} // detail

void info(Configuration& config)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_default_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
);
config.load();

detail::list_options options;
options.base = config.at("base").value<bool>();

auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::print_info(config.context(), channel_context, config, std::move(options));

config.operation_teardown();
}
} // mamba
8 changes: 8 additions & 0 deletions micromamba/src/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include "mamba/api/configuration.hpp"
#include "mamba/api/info.hpp"
#include "mamba/core/context.hpp"

#include "common_options.hpp"

using namespace mamba;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this used for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for Configurable, which is a mamba::Configurable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, using namespace directive should be avoided as much as possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done in /mamba/micromamba/src/list.cpp , which is why I originally went for it in /mamba/micromamba/src/info.cpp (but it used only once here, and more in list.cpp).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, we can remove (broad) using namespace directives in another PR.

void
init_info_parser(CLI::App* subcom, mamba::Configuration& config)
{
Expand All @@ -22,6 +25,11 @@ set_info_command(CLI::App* subcom, mamba::Configuration& config)
init_info_parser(subcom, config);
static bool print_licenses;

auto& base = config.insert(
Configurable("base", false).group("cli").description("Display base environment path.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using this if this is possible?

Suggested change
Configurable("base", false).group("cli").description("Display base environment path.")
mamba::Configurable("base", false).group("cli").description("Display base environment path.")

);
subcom->add_flag("--base", base.get_cli_config<bool>(), base.description());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can mamba info --base be tested?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how to test it. Open to suggestions!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of testing that the usual information is not present and that an existing path is output?


subcom->add_flag("--licenses", print_licenses, "Print licenses");

subcom->callback(
Expand Down
Loading