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 export flag to list command #3780

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion libmamba/src/api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace mamba
bool explicit_ = false;
bool md5 = false;
bool canonical = false;
bool export_ = false;
};

struct formatted_pkg
Expand Down Expand Up @@ -218,7 +219,11 @@ namespace mamba
{
if (options.canonical)
{
LOG_WARNING << "Option --canonical ignored because of --explicit";
LOG_WARNING << "Option --canonical ignored because of --explicit \n";
}
if (options.export_)
{
LOG_WARNING << "Option --export ignored because of --explicit \n";
}
for (auto p : packages)
{
Expand All @@ -234,12 +239,23 @@ namespace mamba
}
else if (options.canonical)
{
if (options.export_)
{
LOG_WARNING << "Option --export ignored because of --explicit \n";
}
for (auto p : packages)
{
std::cout << p.channel << "/" << p.platform << "::" << p.name << "-"
<< p.version << "-" << p.build_string << std::endl;
}
}
else if (options.export_)
{
for (auto p : packages)
{
std::cout << p.name << "=" << p.version << "=" << p.build_string << std::endl;
}
}
else
{
auto requested_specs = prefix_data.history().get_requested_specs_map();
Expand Down Expand Up @@ -285,6 +301,7 @@ namespace mamba
options.explicit_ = config.at("explicit").value<bool>();
options.md5 = config.at("md5").value<bool>();
options.canonical = config.at("canonical").value<bool>();
options.export_ = config.at("export").value<bool>();

auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::list_packages(config.context(), regex, channel_context, std::move(options));
Expand Down
9 changes: 9 additions & 0 deletions micromamba/src/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ init_list_parser(CLI::App* subcom, Configuration& config)
.description("Output canonical names of packages only. Ignored if --explicit.")
);
subcom->add_flag("-c,--canonical", canonical.get_cli_config<bool>(), canonical.description());

auto& export_ = config.insert(
Configurable("export", false)
.group("cli")
.description(
"Output explicit, machine-readable requirement strings instead of human-readable lists of packages. Ignored if --explicit or --canonical."
)
);
subcom->add_flag("-e,--export", export_.get_cli_config<bool>(), export_.description());
}

void
Expand Down
22 changes: 18 additions & 4 deletions micromamba/tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_list_no_json(
@pytest.mark.parametrize("explicit_flag", ["", "--explicit"])
@pytest.mark.parametrize("md5_flag", ["", "--md5"])
@pytest.mark.parametrize("canonical_flag", ["", "-c", "--canonical"])
@pytest.mark.parametrize("export_flag", ["", "-e", "--export"])
@pytest.mark.parametrize("env_selector", ["", "name", "prefix"])
@pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True)
def test_list_subcommands(
Expand All @@ -93,15 +94,22 @@ def test_list_subcommands(
explicit_flag,
md5_flag,
canonical_flag,
export_flag,
):
if env_selector == "prefix":
res = helpers.umamba_list("-p", tmp_xtensor_env, explicit_flag, md5_flag, canonical_flag)
res = helpers.umamba_list(
"-p", tmp_xtensor_env, explicit_flag, md5_flag, canonical_flag, export_flag
)
elif env_selector == "name":
res = helpers.umamba_list("-n", tmp_env_name, explicit_flag, md5_flag, canonical_flag)
res = helpers.umamba_list(
"-n", tmp_env_name, explicit_flag, md5_flag, canonical_flag, export_flag
)
else:
res = helpers.umamba_list(explicit_flag, md5_flag, canonical_flag)
res = helpers.umamba_list(explicit_flag, md5_flag, canonical_flag, export_flag)

outputs_list = res.strip().split("\n")[2:]
outputs_list = [i for i in outputs_list if not i.startswith("Warning")]
outputs_list = [i for i in outputs_list if i != ""]
if explicit_flag == "--explicit":
for output in outputs_list:
assert "/conda-forge/" in output
Expand All @@ -110,11 +118,17 @@ def test_list_subcommands(
else:
assert "#" not in output
else:
if canonical_flag == "--canonical":
if canonical_flag in ["-c", "--canonical"]:
items = ["conda-forge/", "::"]
for output in outputs_list:
assert all(i in output for i in items)
assert " " not in output
else:
if export_flag in ["-e", "--export"]:
items = ["conda-forge/", "::", " "]
for output in outputs_list:
# assert all(i not in output for i in items)
assert output.count("=") == 2


@pytest.mark.parametrize("quiet_flag", ["", "-q", "--quiet"])
Expand Down
Loading