Skip to content

Commit

Permalink
add base subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
SandrineP committed Jan 28, 2025
1 parent 7b6516d commit 44bc606
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 90 deletions.
206 changes: 116 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>

#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,138 @@ 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 << "base environment: " << ctx.prefix_params.root_prefix.string()
<< std::endl;
}
else
{
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;

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.")
);
subcom->add_flag("--base", base.get_cli_config<bool>(), base.description());

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

subcom->callback(
Expand Down

0 comments on commit 44bc606

Please sign in to comment.