Skip to content

Commit

Permalink
--full-name option for list (#3485)
Browse files Browse the repository at this point in the history
Added --full-name option to the list command
  • Loading branch information
JohanMabille authored Sep 30, 2024
1 parent 0b824fe commit b23a079
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
6 changes: 4 additions & 2 deletions libmamba/include/mamba/api/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ namespace mamba

void list(Configuration& config, const std::string& regex);

namespace detail
/*namespace detail
{
struct list_options;
void list_packages(const Context& ctx, std::string regex, ChannelContext& channel_context);
struct formatted_pkg;
bool compare_alphabetically(const formatted_pkg& a, const formatted_pkg& b);
}
}*/
}

#endif
48 changes: 33 additions & 15 deletions libmamba/src/api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,15 @@

namespace mamba
{
void list(Configuration& config, const std::string& regex)
{
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_NOT_ALLOW_NOT_ENV_PREFIX | MAMBA_EXPECT_EXISTING_PREFIX
);
config.load();

auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::list_packages(config.context(), regex, channel_context);
}

namespace detail
{
struct list_options
{
bool full_name;
};

struct formatted_pkg
{
std::string name, version, build, channel;
Expand All @@ -43,7 +34,12 @@ namespace mamba
return a.name < b.name;
}

void list_packages(const Context& ctx, std::string regex, ChannelContext& channel_context)
void list_packages(
const Context& ctx,
std::string regex,
ChannelContext& channel_context,
list_options options
)
{
auto sprefix_data = PrefixData::create(ctx.prefix_params.target_prefix, channel_context);
if (!sprefix_data)
Expand All @@ -53,6 +49,10 @@ namespace mamba
}
PrefixData& prefix_data = sprefix_data.value();

if (options.full_name)
{
regex = '^' + regex + '$';
}
std::regex spec_pat(regex);

if (ctx.output_params.json)
Expand Down Expand Up @@ -145,4 +145,22 @@ namespace mamba
t.print(std::cout);
}
}

void list(Configuration& config, const std::string& regex)
{
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_NOT_ALLOW_NOT_ENV_PREFIX | MAMBA_EXPECT_EXISTING_PREFIX
);
config.load();

detail::list_options options;
options.full_name = config.at("full_name").value<bool>();
auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::list_packages(config.context(), regex, channel_context, std::move(options));
}
}
11 changes: 11 additions & 0 deletions micromamba/src/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ init_list_parser(CLI::App* subcom, Configuration& config)
.group("cli")
.description("List only packages matching a regular expression"));
subcom->add_option("regex", regex.get_cli_config<std::string>(), regex.description());

auto& full_name = config.insert(Configurable("full_name", false)
.group("cli")
.description("Only search for full names, i.e., ^<regex>$."));
subcom->add_flag("-f,--full-name", full_name.get_cli_config<bool>(), full_name.description());

// TODO: implement this in libmamba/list.cpp
/*auto& canonical = config.insert(Configurable("canonical", false)
.group("cli")
.description("Output canonical names of packages only."));
subcom->add_flag("-c,--canonical", canonical.get_cli_config<bool>(), canonical.description());*/
}

void
Expand Down
13 changes: 13 additions & 0 deletions micromamba/tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def test_list(tmp_home, tmp_root_prefix, tmp_env_name, tmp_xtensor_env, env_sele
assert "xtl" in names


@pytest.mark.parametrize("quiet_flag", ["", "-q", "--quiet"])
@pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True)
def test_list_name(tmp_home, tmp_root_prefix, tmp_xtensor_env, quiet_flag):
helpers.install("xtensor-python")
res = helpers.umamba_list("xt", "--json", quiet_flag)
names = sorted([i["name"] for i in res])
assert names == ["xtensor", "xtensor-python", "xtl"]

full_res = helpers.umamba_list("xtensor", "--full-name", "--json", quiet_flag)
full_names = sorted([i["name"] for i in full_res])
assert full_names == ["xtensor"]


@pytest.mark.parametrize("env_selector", ["name", "prefix"])
@pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True)
def test_not_existing(tmp_home, tmp_root_prefix, tmp_xtensor_env, env_selector):
Expand Down

0 comments on commit b23a079

Please sign in to comment.