From 199e8c60d61579208cee4deb99e85cd813ae6ea7 Mon Sep 17 00:00:00 2001 From: Hind Montassif Date: Tue, 17 Dec 2024 15:21:43 +0100 Subject: [PATCH] Add no-pip flag to list command --- libmamba/include/mamba/api/list.hpp | 11 --------- libmamba/include/mamba/core/prefix_data.hpp | 4 +-- libmamba/src/api/list.cpp | 9 ++++++- libmamba/src/core/prefix_data.cpp | 14 +++++++---- micromamba/src/list.cpp | 5 ++++ micromamba/tests/test_list.py | 27 ++++++++++++--------- 6 files changed, 40 insertions(+), 30 deletions(-) diff --git a/libmamba/include/mamba/api/list.hpp b/libmamba/include/mamba/api/list.hpp index bd4245321d..6037eb5362 100644 --- a/libmamba/include/mamba/api/list.hpp +++ b/libmamba/include/mamba/api/list.hpp @@ -16,17 +16,6 @@ namespace mamba class Context; void list(Configuration& config, const std::string& regex); - - /*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 diff --git a/libmamba/include/mamba/core/prefix_data.hpp b/libmamba/include/mamba/core/prefix_data.hpp index 3caacb6838..15e796de4c 100644 --- a/libmamba/include/mamba/core/prefix_data.hpp +++ b/libmamba/include/mamba/core/prefix_data.hpp @@ -25,7 +25,7 @@ namespace mamba using package_map = std::map; static expected_t - create(const fs::u8path& prefix_path, ChannelContext& channel_context); + create(const fs::u8path& prefix_path, ChannelContext& channel_context, bool no_pip = false); void add_packages(const std::vector& packages); void load_single_record(const fs::u8path& path); @@ -45,7 +45,7 @@ namespace mamba private: - PrefixData(const fs::u8path& prefix_path, ChannelContext& channel_context); + PrefixData(const fs::u8path& prefix_path, ChannelContext& channel_context, bool no_pip); void load_site_packages(); diff --git a/libmamba/src/api/list.cpp b/libmamba/src/api/list.cpp index 48df94d8be..fafa9f987d 100644 --- a/libmamba/src/api/list.cpp +++ b/libmamba/src/api/list.cpp @@ -23,6 +23,7 @@ namespace mamba struct list_options { bool full_name; + bool no_pip; }; struct formatted_pkg @@ -55,7 +56,11 @@ namespace mamba list_options options ) { - auto sprefix_data = PrefixData::create(ctx.prefix_params.target_prefix, channel_context); + auto sprefix_data = PrefixData::create( + ctx.prefix_params.target_prefix, + channel_context, + options.no_pip + ); if (!sprefix_data) { // TODO: propagate tl::expected mechanism @@ -202,6 +207,8 @@ namespace mamba detail::list_options options; options.full_name = config.at("full_name").value(); + options.no_pip = config.at("no_pip").value(); + auto channel_context = ChannelContext::make_conda_compatible(config.context()); detail::list_packages(config.context(), regex, channel_context, std::move(options)); } diff --git a/libmamba/src/core/prefix_data.cpp b/libmamba/src/core/prefix_data.cpp index 50e574506d..5e70091856 100644 --- a/libmamba/src/core/prefix_data.cpp +++ b/libmamba/src/core/prefix_data.cpp @@ -25,12 +25,13 @@ namespace mamba { - auto PrefixData::create(const fs::u8path& prefix_path, ChannelContext& channel_context) + auto + PrefixData::create(const fs::u8path& prefix_path, ChannelContext& channel_context, bool no_pip) -> expected_t { try { - return PrefixData(prefix_path, channel_context); + return PrefixData(prefix_path, channel_context, no_pip); } catch (std::exception& e) { @@ -46,7 +47,7 @@ namespace mamba } } - PrefixData::PrefixData(const fs::u8path& prefix_path, ChannelContext& channel_context) + PrefixData::PrefixData(const fs::u8path& prefix_path, ChannelContext& channel_context, bool no_pip) : m_history(prefix_path, channel_context) , m_prefix_path(prefix_path) , m_channel_context(channel_context) @@ -62,8 +63,11 @@ namespace mamba } } } - // Load packages installed with pip - load_site_packages(); + // Load packages installed with pip if `no_pip` is not set to `true` + if (!no_pip) + { + load_site_packages(); + } } void PrefixData::add_packages(const std::vector& packages) diff --git a/micromamba/src/list.cpp b/micromamba/src/list.cpp index aeeb7a995d..3d81c27820 100644 --- a/micromamba/src/list.cpp +++ b/micromamba/src/list.cpp @@ -28,6 +28,11 @@ init_list_parser(CLI::App* subcom, Configuration& config) .description("Only search for full names, i.e., ^$.")); subcom->add_flag("-f,--full-name", full_name.get_cli_config(), full_name.description()); + auto& no_pip = config.insert(Configurable("no_pip", false) + .group("cli") + .description("Do not include pip-only installed packages.")); + subcom->add_flag("--no-pip", no_pip.get_cli_config(), no_pip.description()); + // TODO: implement this in libmamba/list.cpp /*auto& canonical = config.insert(Configurable("canonical", false) .group("cli") diff --git a/micromamba/tests/test_list.py b/micromamba/tests/test_list.py index bc6e8bf37e..960826ceda 100644 --- a/micromamba/tests/test_list.py +++ b/micromamba/tests/test_list.py @@ -52,8 +52,9 @@ def test_list_name(tmp_home, tmp_root_prefix, tmp_xtensor_env, quiet_flag): """ +@pytest.mark.parametrize("no_pip_flag", ["", "--no-pip"]) @pytest.mark.parametrize("shared_pkgs_dirs", [True], indirect=True) -def test_list_with_pip(tmp_home, tmp_root_prefix, tmp_path): +def test_list_with_pip(tmp_home, tmp_root_prefix, tmp_path, no_pip_flag): env_name = "env-list_with_pip" tmp_root_prefix / "envs" / env_name @@ -63,16 +64,20 @@ def test_list_with_pip(tmp_home, tmp_root_prefix, tmp_path): helpers.create("-n", env_name, "python=3.12", "--json", no_dry_run=True) helpers.install("-n", env_name, "-f", env_file_yml, "--json", no_dry_run=True) - res = helpers.umamba_list("-n", env_name, "--json") - assert any( - package["name"] == "numpy" - and package["version"] == "1.26.4" - and package["base_url"] == "https://pypi.org/" - and package["build_string"] == "pypi_0" - and package["channel"] == "pypi" - and package["platform"] == sys.platform + "-" + platform.machine() - for package in res - ) + res = helpers.umamba_list("-n", env_name, "--json", no_pip_flag) + if no_pip_flag == "": + assert any( + package["name"] == "numpy" + and package["version"] == "1.26.4" + and package["base_url"] == "https://pypi.org/" + and package["build_string"] == "pypi_0" + and package["channel"] == "pypi" + and package["platform"] == sys.platform + "-" + platform.machine() + for package in res + ) + else: # --no-pip + # Check that numpy installed with pip is not listed + assert all(package["name"] != "numpy" for package in res) @pytest.mark.parametrize("env_selector", ["name", "prefix"])