Add `no-pip` flag to `list` command (#3696)

This commit is contained in:
Hind-M 2024-12-18 10:57:32 +01:00 committed by GitHub
parent 26819b67f8
commit 7f3f481b47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 30 deletions

View File

@ -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

View File

@ -25,7 +25,7 @@ namespace mamba
using package_map = std::map<std::string, specs::PackageInfo>;
static expected_t<PrefixData>
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<specs::PackageInfo>& 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();

View File

@ -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<bool>();
options.no_pip = config.at("no_pip").value<bool>();
auto channel_context = ChannelContext::make_conda_compatible(config.context());
detail::list_packages(config.context(), regex, channel_context, std::move(options));
}

View File

@ -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<PrefixData>
{
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<specs::PackageInfo>& packages)

View File

@ -28,6 +28,11 @@ init_list_parser(CLI::App* subcom, Configuration& config)
.description("Only search for full names, i.e., ^<regex>$."));
subcom->add_flag("-f,--full-name", full_name.get_cli_config<bool>(), 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<bool>(), no_pip.description());
// TODO: implement this in libmamba/list.cpp
/*auto& canonical = config.insert(Configurable("canonical", false)
.group("cli")

View File

@ -52,8 +52,9 @@ dependencies:
"""
@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"])