Skip to content

Commit

Permalink
Replace Context with Context::platform where possible
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>
  • Loading branch information
jjerphan committed Jul 26, 2024
1 parent 4b8cc14 commit 296cefc
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
5 changes: 2 additions & 3 deletions libmamba/include/mamba/core/virtual_packages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ namespace mamba
{
class Context;

std::vector<specs::PackageInfo> get_virtual_packages(const Context& context);
std::vector<specs::PackageInfo> get_virtual_packages(const std::string& platform);

namespace detail
{
std::string cuda_version();
std::string get_arch();

auto make_virtual_package(
std::string name,
Expand All @@ -30,7 +29,7 @@ namespace mamba
std::string build_string = ""
) -> specs::PackageInfo;

std::vector<specs::PackageInfo> dist_packages(const Context& context);
std::vector<specs::PackageInfo> dist_packages(const std::string& platform);
}
}

Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace mamba
items.push_back({ "populated config files", sources });

std::vector<std::string> virtual_pkgs;
for (auto pkg : get_virtual_packages(ctx))
for (auto pkg : get_virtual_packages(ctx.platform))
{
virtual_pkgs.push_back(util::concat(pkg.name, "=", pkg.version, "=", pkg.build_string)
);
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/package_database_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace mamba
// TODO(C++20): We could do a PrefixData range that returns packages without storing thems.
auto pkgs = prefix.sorted_records();
// TODO(C++20): We only need a range that concatenate both
for (auto&& pkg : get_virtual_packages(ctx))
for (auto&& pkg : get_virtual_packages(ctx.platform))
{
pkgs.push_back(std::move(pkg));
}
Expand Down
9 changes: 4 additions & 5 deletions libmamba/src/core/virtual_packages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,11 @@ namespace mamba
return util::windows_version();
}

std::vector<specs::PackageInfo> dist_packages(const Context& context)
std::vector<specs::PackageInfo> dist_packages(const std::string& platform)
{
LOG_DEBUG << "Loading distribution virtual packages";

std::vector<specs::PackageInfo> res;
const auto platform = context.platform;
const auto split_platform = util::split(platform, "-", 1);

if (split_platform.size() != 2)
Expand Down Expand Up @@ -342,15 +341,15 @@ namespace mamba
}
}

std::vector<specs::PackageInfo> get_virtual_packages(const Context& context)
std::vector<specs::PackageInfo> get_virtual_packages(const std::string& platform)
{
LOG_DEBUG << "Loading virtual packages";
auto res = detail::dist_packages(context);
auto res = detail::dist_packages(platform);

auto cuda_ver = detail::cuda_version();
if (!cuda_ver.empty())
{
res.push_back(detail::make_virtual_package("__cuda", context.platform, cuda_ver));
res.push_back(detail::make_virtual_package("__cuda", platform, cuda_ver));
}

return res;
Expand Down
18 changes: 7 additions & 11 deletions libmamba/tests/src/core/test_virtual_packages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace mamba
using Version = specs::Version;

auto& ctx = mambatests::context();
auto pkgs = detail::dist_packages(ctx);
auto pkgs = detail::dist_packages(ctx.platform);

if (util::on_win)
{
Expand Down Expand Up @@ -86,9 +86,8 @@ namespace mamba
auto restore_ctx = [&ctx, old_plat = ctx.platform]() { ctx.platform = old_plat; };
auto finally = Finally<decltype(restore_ctx)>{ restore_ctx };

ctx.platform = "osx-arm";
util::set_env("CONDA_OVERRIDE_OSX", "12.1");
pkgs = detail::dist_packages(ctx);
pkgs = detail::dist_packages("osx-arm");
REQUIRE_EQ(pkgs.size(), 3);
CHECK_EQ(pkgs[0].name, "__unix");
CHECK_EQ(pkgs[1].name, "__osx");
Expand All @@ -97,10 +96,9 @@ namespace mamba
CHECK_EQ(pkgs[2].build_string, "arm");

util::unset_env("CONDA_OVERRIDE_OSX");
ctx.platform = "linux-32";
util::set_env("CONDA_OVERRIDE_LINUX", "5.7");
util::set_env("CONDA_OVERRIDE_GLIBC", "2.15");
pkgs = detail::dist_packages(ctx);
pkgs = detail::dist_packages("linux-32");
REQUIRE_EQ(pkgs.size(), 4);
CHECK_EQ(pkgs[0].name, "__unix");
CHECK_EQ(pkgs[1].name, "__linux");
Expand All @@ -112,15 +110,13 @@ namespace mamba
util::unset_env("CONDA_OVERRIDE_GLIBC");
util::unset_env("CONDA_OVERRIDE_LINUX");

ctx.platform = "lin-850";
pkgs = detail::dist_packages(ctx);
pkgs = detail::dist_packages("lin-850");
REQUIRE_EQ(pkgs.size(), 1);
CHECK_EQ(pkgs[0].name, "__archspec");
CHECK_EQ(pkgs[0].build_string, "850");
util::unset_env("CONDA_SUBDIR");

ctx.platform = "linux";
pkgs = detail::dist_packages(ctx);
pkgs = detail::dist_packages("linux");
REQUIRE_EQ(pkgs.size(), 0);

ctx.platform = ctx.host_platform;
Expand All @@ -130,7 +126,7 @@ namespace mamba
{
util::set_env("CONDA_OVERRIDE_CUDA", "9.0");
const auto& context = mambatests::context();
auto pkgs = get_virtual_packages(context);
auto pkgs = get_virtual_packages(context.platform);
int pkgs_count;

if (util::on_win)
Expand All @@ -152,7 +148,7 @@ namespace mamba
CHECK_EQ(pkgs.back().version, "9.0");

util::unset_env("CONDA_OVERRIDE_CUDA");
pkgs = get_virtual_packages(context);
pkgs = get_virtual_packages(context.platform);

if (!detail::cuda_version().empty())
{
Expand Down
5 changes: 4 additions & 1 deletion libmambapy/src/libmambapy/bindings/legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,10 @@ bind_submodule_impl(pybind11::module_ m)
// py::arg("out_package"), py::arg("compression_level"), py::arg("compression_threads") = 1);


m.def("get_virtual_packages", [](Context& context) { return get_virtual_packages(context); });
m.def(
"get_virtual_packages",
[](Context& context) { return get_virtual_packages(context.platform); }
);

m.def("cancel_json_output", [](Context&) { mambapy::singletons().console().cancel_json_print(); });

Expand Down

0 comments on commit 296cefc

Please sign in to comment.