Skip to content

Commit

Permalink
Bind ChannelContext (#3034)
Browse files Browse the repository at this point in the history
* Bind ChannelContext

* Deprecate Context() in Python

* Remove ChannelContext singleton
  • Loading branch information
AntoinePrv committed Dec 5, 2023
1 parent 4d70901 commit ecd6de0
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 46 deletions.
131 changes: 85 additions & 46 deletions libmambapy/src/libmambapy/bindings/legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,6 @@ namespace mambapy
{
return m_console;
}
mamba::ChannelContext& channel_context()
{
return init_once(
p_channel_context,
[&]() { return mamba::ChannelContext::make_conda_compatible(m_context); }
);
}

mamba::Configuration& config()
{
Expand Down Expand Up @@ -298,33 +291,37 @@ bind_submodule_impl(pybind11::module_ m)

py::class_<MatchSpec>(m, "MatchSpec")
.def(py::init<>())
.def(py::init<>(
[](const std::string& name) {
return MatchSpec{ name,
mambapy::singletons.context(),
mambapy::singletons.channel_context() };
}
))
.def(
py::init<>(
[](const std::string& name, ChannelContext& channel_context) {
return MatchSpec{ name, mambapy::singletons.context(), channel_context };
}
),
py::arg("spec"),
py::arg("channel_context")
)
.def("conda_build_form", &MatchSpec::conda_build_form);

py::class_<MPool>(m, "Pool")
.def(py::init<>(
[]
{ return MPool{ mambapy::singletons.context(), mambapy::singletons.channel_context() }; }
))
.def(
py::init<>(
[](ChannelContext& channel_context) {
return MPool{ mambapy::singletons.context(), channel_context };
}
),
py::arg("channel_context")
)
.def("set_debuglevel", &MPool::set_debuglevel)
.def("create_whatprovides", &MPool::create_whatprovides)
.def("select_solvables", &MPool::select_solvables, py::arg("id"), py::arg("sorted") = false)
.def("matchspec2id", &MPool::matchspec2id, py::arg("ms"))
.def(
"matchspec2id",
[](MPool& self, std::string_view ms)
{
return self.matchspec2id(
{ ms, mambapy::singletons.context(), mambapy::singletons.channel_context() }
);
[](MPool& self, std::string_view spec, ChannelContext& channel_context) {
return self.matchspec2id({ spec, mambapy::singletons.context(), channel_context });
},
py::arg("ms")
py::arg("spec"),
py::arg("channel_context")
)
.def("id2pkginfo", &MPool::id2pkginfo, py::arg("id"));

Expand Down Expand Up @@ -494,11 +491,15 @@ bind_submodule_impl(pybind11::module_ m)
.def("tree_message", [](const CpPbGraph& self) { return problem_tree_msg(self); });

py::class_<History>(m, "History")
.def(py::init(
[](const fs::u8path& path) {
return History{ path, mambapy::singletons.channel_context() };
}
))
.def(
py::init(
[](const fs::u8path& path, ChannelContext& channel_context) {
return History{ path, channel_context };
}
),
py::arg("path"),
py::arg("channel_context")
)
.def(
"get_requested_specs_map",
[](History& self) { return self.get_requested_specs_map(mambapy::singletons.context()); }
Expand Down Expand Up @@ -651,6 +652,7 @@ bind_submodule_impl(pybind11::module_ m)
.def(
"create",
[](SubdirIndex& self,
ChannelContext& channel_context,
const specs::Channel& channel,
const std::string& platform,
const std::string& full_url,
Expand All @@ -660,15 +662,22 @@ bind_submodule_impl(pybind11::module_ m)
{
self.create(
mambapy::singletons.context(),
mambapy::singletons.channel_context(),
channel_context,
channel,
platform,
full_url,
caches,
repodata_fn,
url
);
}
},
py::arg("channel_context"),
py::arg("channel"),
py::arg("platform"),
py::arg("full_url"),
py::arg("caches"),
py::arg("repodata_fn"),
py::arg("url")
)
.def("download", &SubdirIndex::download)
.def("__len__", &SubdirIndex::size)
Expand Down Expand Up @@ -696,10 +705,36 @@ bind_submodule_impl(pybind11::module_ m)
.value("CRITICAL", mamba::log_level::critical)
.value("OFF", mamba::log_level::off);

py::class_<ChannelContext>(m, "ChannelContext")
.def_static("make_simple", &ChannelContext::make_simple)
.def_static("make_conda_compatible", &ChannelContext::make_conda_compatible)
.def(
py::init<specs::ChannelResolveParams, std::vector<specs::Channel>>(),
py::arg("params"),
py::arg("has_zst")
)
.def("make_channel", &ChannelContext::make_channel)
.def("params", &ChannelContext::params)
.def("has_zst", &ChannelContext::has_zst);

py::class_<Context, std::unique_ptr<Context, py::nodelete>> ctx(m, "Context");
ctx.def(py::init(
[] { return std::unique_ptr<Context, py::nodelete>(&mambapy::singletons.context()); }
))
ctx //
.def_static(
// Still need a singleton as long as mambatest::singleton::context is used
"instance",
[]() -> auto& { return mambapy::singletons.context(); },
py::return_value_policy::reference
)
.def(py::init(
// Deprecating would lead to confusing error. Better to make sure people stop using it.
[]() -> std::unique_ptr<Context, py::nodelete>
{
throw std::invalid_argument( //
"Context() will create a new Context object in the future.\n"
"Use Context.instance() to access the global singleton."
);
}
))
.def_readwrite("offline", &Context::offline)
.def_readwrite("local_repodata_ttl", &Context::local_repodata_ttl)
.def_readwrite("use_index_cache", &Context::use_index_cache)
Expand Down Expand Up @@ -996,20 +1031,24 @@ bind_submodule_impl(pybind11::module_ m)
////////////////////////////////////////////

pyPrefixData
.def(py::init(
[](const fs::u8path& prefix_path) -> PrefixData
{
auto sres = PrefixData::create(prefix_path, mambapy::singletons.channel_context());
if (sres.has_value())
{
return std::move(sres.value());
}
else
.def(
py::init(
[](const fs::u8path& prefix_path, ChannelContext& channel_context) -> PrefixData
{
throw sres.error();
auto sres = PrefixData::create(prefix_path, channel_context);
if (sres.has_value())
{
return std::move(sres.value());
}
else
{
throw sres.error();
}
}
}
))
),
py::arg("path"),
py::arg("channel_context")
)
.def_property_readonly("package_records", &PrefixData::records)
.def("add_packages", &PrefixData::add_packages);

Expand Down
21 changes: 21 additions & 0 deletions libmambapy/tests/test_legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import libmambapy


def test_context_singleton():
libmambapy.Context.instance().platform = "mambaos-64"
ctx = libmambapy.Context.instance()
assert ctx.platform == "mambaos-64"


def test_channel_context():
ctx = libmambapy.Context.instance()

cc = libmambapy.ChannelContext.make_conda_compatible(ctx)
assert cc.make_channel("pkgs/main")[0].url.str() == "https://repo.anaconda.com/pkgs/main"
assert "pkgs/main" in cc.params().custom_channels
chan = cc.params().custom_channels["pkgs/main"]
assert isinstance(cc.has_zst(chan), bool) # Not testing value

cc = libmambapy.ChannelContext.make_simple(ctx)
assert cc.make_channel("pkgs/main")[0].url.str() == "https://conda.anaconda.org/pkgs/main"
assert len(cc.params().custom_channels) == 0

0 comments on commit ecd6de0

Please sign in to comment.