Skip to content

Commit

Permalink
Refactor MSolver flags
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Jun 19, 2023
1 parent 44ca07f commit 24a3f35
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
28 changes: 19 additions & 9 deletions libmamba/include/mamba/core/solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

#include "match_spec.hpp"

#define MAMBA_NO_DEPS 0b0001
#define MAMBA_ONLY_DEPS 0b0010
#define MAMBA_FORCE_REINSTALL 0b0100
#define PY_MAMBA_NO_DEPS 0b0001
#define PY_MAMBA_ONLY_DEPS 0b0010
#define PY_MAMBA_FORCE_REINSTALL 0b0100

extern "C"
{
Expand Down Expand Up @@ -60,6 +60,16 @@ namespace mamba
{
public:

struct Flags
{
/** Keep the dependencies of the install package in the solution. */
bool keep_dependencies = true;
/** Keep the original required package in the solution. */
bool keep_specs = true;
/** Force reinstallation of jobs. */
bool force_reinstall = false;
};

MSolver(MPool pool, std::vector<std::pair<int, int>> flags = {});
~MSolver();

Expand All @@ -74,8 +84,11 @@ namespace mamba
void add_pin(const std::string& pin);
void add_pins(const std::vector<std::string>& pins);

void set_postsolve_flags(const std::vector<std::pair<int, int>>& flags);
[[deprecated]] void py_set_postsolve_flags(const std::vector<std::pair<int, int>>& flags);

void set_flags(const Flags& flags);
[[nodiscard]] auto flags() const -> const Flags&;
[[nodiscard]] auto flags() -> Flags&;
void set_libsolv_flags(const std::vector<std::pair<int, int>>& flags);
[[nodiscard]] auto libsolv_flags() const -> const std::vector<std::pair<int, int>>&;
[[nodiscard]] auto libsolv_flags() -> std::vector<std::pair<int, int>>&;
Expand Down Expand Up @@ -106,23 +119,20 @@ namespace mamba
auto solver() -> solv::ObjSolver&;
auto solver() const -> const solv::ObjSolver&;

bool only_deps = false;
bool no_deps = false;
bool force_reinstall = false;

private:

std::vector<std::pair<int, int>> m_libsolv_flags;
std::vector<MatchSpec> m_install_specs;
std::vector<MatchSpec> m_remove_specs;
std::vector<MatchSpec> m_neuter_specs;
std::vector<MatchSpec> m_pinned_specs;
bool m_is_solved;
// Order of m_pool and m_solver is critical since m_pool must outlive m_solver.
MPool m_pool;
// Temporary Pimpl all libsolv to keep it private
std::unique_ptr<solv::ObjSolver> m_solver;
std::unique_ptr<solv::ObjQueue> m_jobs;
Flags m_flags = {};
bool m_is_solved;

void add_reinstall_job(MatchSpec& ms, int job_flag);
void apply_libsolv_flags();
Expand Down
10 changes: 6 additions & 4 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
#include "mamba/core/channel.hpp"
#include "mamba/core/env_lockfile.hpp"
#include "mamba/core/environments_manager.hpp"
#include "mamba/core/fetch.hpp"
#include "mamba/core/mamba_fs.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/package_cache.hpp"
#include "mamba/core/package_download.hpp"
#include "mamba/core/pinning.hpp"
#include "mamba/core/transaction.hpp"
#include "mamba/core/util_string.hpp"
Expand Down Expand Up @@ -512,9 +512,11 @@ namespace mamba
}
);

solver.set_postsolve_flags({ { MAMBA_NO_DEPS, no_deps },
{ MAMBA_ONLY_DEPS, only_deps },
{ MAMBA_FORCE_REINSTALL, force_reinstall } });
solver.set_flags({
/* .keep_dependencies= */ !no_deps,
/* .keep_specs= */ !only_deps,
/* .force_reinstall= */ force_reinstall,
});

if (freeze_installed && !prefix_pkgs.empty())
{
Expand Down
33 changes: 24 additions & 9 deletions libmamba/src/core/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace mamba
{
MSolver::MSolver(MPool pool, const std::vector<std::pair<int, int>> flags)
: m_libsolv_flags(std::move(flags))
, m_is_solved(false)
, m_pool(std::move(pool))
, m_solver(nullptr)
, m_jobs(std::make_unique<solv::ObjQueue>())
, m_is_solved(false)
{
// TODO should we lazyly create solver here? Should we what provides?
m_pool.create_whatprovides();
Expand Down Expand Up @@ -151,7 +151,7 @@ namespace mamba
}
m_jobs->push_back(job_flag | SOLVER_SOLVABLE_PROVIDES, job_id);
}
else if ((job_flag & SOLVER_INSTALL) && force_reinstall)
else if ((job_flag & SOLVER_INSTALL) && m_flags.force_reinstall)
{
add_reinstall_job(ms, job_flag);
}
Expand Down Expand Up @@ -241,25 +241,40 @@ namespace mamba
}
}

void MSolver::set_postsolve_flags(const std::vector<std::pair<int, int>>& flags)
void MSolver::py_set_postsolve_flags(const std::vector<std::pair<int, int>>& flags)
{
for (const auto& option : flags)
{
switch (option.first)
{
case MAMBA_NO_DEPS:
no_deps = option.second;
case PY_MAMBA_NO_DEPS:
m_flags.keep_dependencies = !option.second;
break;
case MAMBA_ONLY_DEPS:
only_deps = option.second;
case PY_MAMBA_ONLY_DEPS:
m_flags.keep_specs = !option.second;
break;
case MAMBA_FORCE_REINSTALL:
force_reinstall = option.second;
case PY_MAMBA_FORCE_REINSTALL:
m_flags.force_reinstall = option.second;
break;
}
}
}

void MSolver::set_flags(const Flags& flags)
{
m_flags = flags;
}

auto MSolver::flags() const -> const Flags&
{
return m_flags;
}

auto MSolver::flags() -> Flags&
{
return m_flags;
}

void MSolver::set_libsolv_flags(const std::vector<std::pair<int, int>>& flags)
{
m_libsolv_flags = flags;
Expand Down
6 changes: 3 additions & 3 deletions libmamba/src/core/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ namespace mamba
);
trans().order(pool);

if (solver.no_deps || solver.only_deps)
if (!solver.flags().keep_dependencies || !solver.flags().keep_specs)
{
m_filter_type = solver.only_deps ? FilterType::keep_only : FilterType::ignore;
m_filter_type = !(solver.flags().keep_specs) ? FilterType::keep_only : FilterType::ignore;
for (auto& s : solver.install_specs())
{
m_filter_name_ids.insert(pool.add_string(s.name));
Expand All @@ -201,7 +201,7 @@ namespace mamba
}
}

if (solver.only_deps)
if (!solver.flags().keep_specs)
{
for (const solv::SolvableId r : trans().steps())
{
Expand Down
8 changes: 4 additions & 4 deletions libmambapy/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ PYBIND11_MODULE(bindings, m)
.def("add_constraint", &MSolver::add_constraint)
.def("add_pin", &MSolver::add_pin)
.def("set_flags", &MSolver::set_libsolv_flags)
.def("set_postsolve_flags", &MSolver::set_postsolve_flags)
.def("set_postsolve_flags", &MSolver::py_set_postsolve_flags)
.def("is_solved", &MSolver::is_solved)
.def("problems_to_str", &MSolver::problems_to_str)
.def("all_problems_to_str", &MSolver::all_problems_to_str)
Expand Down Expand Up @@ -1102,9 +1102,9 @@ PYBIND11_MODULE(bindings, m)
.value("SOLVER_RULE_STRICT_REPO_PRIORITY", SolverRuleinfo::SOLVER_RULE_STRICT_REPO_PRIORITY);

// INSTALL FLAGS
m.attr("MAMBA_NO_DEPS") = MAMBA_NO_DEPS;
m.attr("MAMBA_ONLY_DEPS") = MAMBA_ONLY_DEPS;
m.attr("MAMBA_FORCE_REINSTALL") = MAMBA_FORCE_REINSTALL;
m.attr("MAMBA_NO_DEPS") = PY_MAMBA_NO_DEPS;
m.attr("MAMBA_ONLY_DEPS") = PY_MAMBA_ONLY_DEPS;
m.attr("MAMBA_FORCE_REINSTALL") = PY_MAMBA_FORCE_REINSTALL;

// DOWNLOAD FLAGS
m.attr("MAMBA_DOWNLOAD_FAILFAST") = MAMBA_DOWNLOAD_FAILFAST;
Expand Down

0 comments on commit 24a3f35

Please sign in to comment.