Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean libsolv use in Transaction #3171

Merged
merged 9 commits into from
Feb 5, 2024
Merged
52 changes: 40 additions & 12 deletions libmamba/include/mamba/core/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,18 @@ namespace mamba
const solver::libsolv::RepodataOrigin& metadata
) -> expected_t<solver::libsolv::RepoInfo>;

void set_installed_repo(const solver::libsolv::RepoInfo& repo);
[[nodiscard]] auto installed_repo() const -> std::optional<solver::libsolv::RepoInfo>;

void set_installed_repo(solver::libsolv::RepoInfo repo);

void
set_repo_priority(const solver::libsolv::RepoInfo& repo, solver::libsolv::Priorities priorities);
set_repo_priority(solver::libsolv::RepoInfo repo, solver::libsolv::Priorities priorities);

void remove_repo(::Id repo_id, bool reuse_ids);

template <typename Func>
void for_each_package_in_repo(solver::libsolv::RepoInfo repo, Func&&) const;

template <typename Func>
void for_each_package_matching(const specs::MatchSpec& ms, Func&&);

Expand Down Expand Up @@ -147,15 +152,18 @@ namespace mamba
solver::libsolv::PipAsPythonDependency add
);

using SolvableId = int;
enum class PackageId : int;

[[nodiscard]] auto solvable_id_to_package_info(SolvableId id) const -> specs::PackageInfo;
[[nodiscard]] auto package_id_to_package_info(PackageId id) const -> specs::PackageInfo;

[[nodiscard]] auto packages_in_repo(solver::libsolv::RepoInfo repo) const
-> std::vector<PackageId>;

[[nodiscard]] auto packages_matching_ids(const specs::MatchSpec& ms)
-> std::vector<SolvableId>;
-> std::vector<PackageId>;

[[nodiscard]] auto packages_depending_on_ids(const specs::MatchSpec& ms)
-> std::vector<SolvableId>;
-> std::vector<PackageId>;
};

// TODO machinery functions in separate files
Expand Down Expand Up @@ -196,22 +204,42 @@ namespace mamba
return add_repo_from_packages(packages.begin(), packages.end(), name, add);
}

// TODO(C++20): Use ranges::transform
template <typename Func>
void MPool::for_each_package_in_repo(solver::libsolv::RepoInfo repo, Func&& func) const
{
for (auto id : packages_in_repo(repo))
{
if constexpr (std::is_same_v<decltype(func(package_id_to_package_info(id))), util::LoopControl>)
{
if (func(package_id_to_package_info(id)) == util::LoopControl::Break)
{
break;
}
}
else
{
func(package_id_to_package_info(id));
}
}
}

// TODO(C++20): Use ranges::transform
template <typename Func>
void MPool::for_each_package_matching(const specs::MatchSpec& ms, Func&& func)
{
for (auto id : packages_matching_ids(ms))
{
if constexpr (std::is_same_v<decltype(func(solvable_id_to_package_info(id))), util::LoopControl>)
if constexpr (std::is_same_v<decltype(func(package_id_to_package_info(id))), util::LoopControl>)
{
if (func(solvable_id_to_package_info(id)) == util::LoopControl::Break)
if (func(package_id_to_package_info(id)) == util::LoopControl::Break)
{
break;
}
}
else
{
func(solvable_id_to_package_info(id));
func(package_id_to_package_info(id));
}
}
}
Expand All @@ -222,16 +250,16 @@ namespace mamba
{
for (auto id : packages_depending_on_ids(ms))
{
if constexpr (std::is_same_v<decltype(func(solvable_id_to_package_info(id))), util::LoopControl>)
if constexpr (std::is_same_v<decltype(func(package_id_to_package_info(id))), util::LoopControl>)
{
if (func(solvable_id_to_package_info(id)) == util::LoopControl::Break)
if (func(package_id_to_package_info(id)) == util::LoopControl::Break)
{
break;
}
}
else
{
func(solvable_id_to_package_info(id));
func(package_id_to_package_info(id));
}
}
}
Expand Down
30 changes: 19 additions & 11 deletions libmamba/include/mamba/core/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,35 @@ namespace mamba
}

class ChannelContext;
class Context;

class MTransaction
{
public:

MTransaction(
const Context& ctx,
MPool& pool,
const std::vector<specs::MatchSpec>& specs_to_remove,
const std::vector<specs::MatchSpec>& specs_to_install,
std::vector<specs::PackageInfo> pkgs_to_remove,
std::vector<specs::PackageInfo> pkgs_to_install,
MultiPackageCache& caches
);

MTransaction(
const Context& ctx,
MPool& pool,
const solver::Request& request,
solver::Solution solution,
MultiPackageCache& caches
);

// Only use if the packages have been solved previously already.
MTransaction(MPool& pool, const std::vector<specs::PackageInfo>& packages, MultiPackageCache& caches);
MTransaction(
const Context& ctx,
MPool& pool,
std::vector<specs::PackageInfo> packages,
MultiPackageCache& caches
);

MTransaction(const MTransaction&) = delete;
MTransaction(MTransaction&&) = delete;
Expand All @@ -62,17 +71,14 @@ namespace mamba

to_conda_type to_conda();
void log_json();
bool fetch_extract_packages();
bool fetch_extract_packages(const Context& ctx, ChannelContext& channel_context);
bool empty();
bool prompt();
void print();
bool execute(PrefixData& prefix);

[[deprecated]] std::pair<std::string, std::string> py_find_python_version() const;
bool prompt(const Context& ctx, ChannelContext& channel_context);
void print(const Context& ctx, ChannelContext& channel_context);
bool execute(const Context& ctx, ChannelContext& channel_context, PrefixData& prefix);

private:

MPool m_pool;
TransactionContext m_transaction_context;
MultiPackageCache m_multi_cache;
const fs::u8path m_cache_path;
Expand All @@ -82,17 +88,19 @@ namespace mamba

std::vector<specs::MatchSpec> m_requested_specs;

MTransaction(MPool&, MultiPackageCache&);
MTransaction(const Context& ctx, MultiPackageCache&);
};

MTransaction create_explicit_transaction_from_urls(
const Context& ctx,
MPool& pool,
const std::vector<std::string>& urls,
MultiPackageCache& package_caches,
std::vector<detail::other_pkg_mgr_spec>& other_specs
);

MTransaction create_explicit_transaction_from_lockfile(
const Context& ctx,
MPool& pool,
const fs::u8path& env_lockfile_path,
const std::vector<std::string>& categories,
Expand Down
4 changes: 2 additions & 2 deletions libmamba/include/mamba/core/transaction_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ namespace mamba
const Context& context,
const fs::u8path& target_prefix,
const std::pair<std::string, std::string>& py_versions,
const std::vector<specs::MatchSpec>& requested_specs
std::vector<specs::MatchSpec> requested_specs
);

TransactionContext(
const Context& context,
const fs::u8path& target_prefix,
const fs::u8path& relocate_prefix,
const std::pair<std::string, std::string>& py_versions,
const std::vector<specs::MatchSpec>& requested_specs
std::vector<specs::MatchSpec> requested_specs
);
~TransactionContext();
bool try_pyc_compilation(const std::vector<fs::u8path>& py_files);
Expand Down
30 changes: 22 additions & 8 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,13 @@ namespace mamba
}

Console::instance().json_write({ { "success", true } });
auto trans = MTransaction(pool, request, std::get<solver::Solution>(outcome), package_caches);
auto trans = MTransaction(
ctx,
pool,
request,
std::get<solver::Solution>(outcome),
package_caches
);

if (ctx.output_params.json)
{
Expand All @@ -620,14 +626,14 @@ namespace mamba

Console::stream();

if (trans.prompt())
if (trans.prompt(ctx, channel_context))
{
if (create_env && !ctx.dry_run)
{
detail::create_target_directory(ctx, ctx.prefix_params.target_prefix);
}

trans.execute(prefix_data);
trans.execute(ctx, channel_context, prefix_data);

for (auto other_spec : config.at("others_pkg_mgrs_specs")
.value<std::vector<detail::other_pkg_mgr_spec>>())
Expand Down Expand Up @@ -710,14 +716,14 @@ namespace mamba
transaction.log_json();
}

if (transaction.prompt())
if (transaction.prompt(ctx, channel_context))
{
if (create_env && !ctx.dry_run)
{
detail::create_target_directory(ctx, ctx.prefix_params.target_prefix);
}

transaction.execute(prefix_data);
transaction.execute(ctx, channel_context, prefix_data);

for (auto other_spec : others)
{
Expand Down Expand Up @@ -749,7 +755,7 @@ namespace mamba
ctx,
channel_context,
[&](auto& pool, auto& pkg_caches, auto& others)
{ return create_explicit_transaction_from_urls(pool, specs, pkg_caches, others); },
{ return create_explicit_transaction_from_urls(ctx, pool, specs, pkg_caches, others); },
create_env,
remove_prefix_on_failure
);
Expand Down Expand Up @@ -796,8 +802,16 @@ namespace mamba
install_explicit_with_transaction(
ctx,
channel_context,
[&](auto& pool, auto& pkg_caches, auto& others) {
return create_explicit_transaction_from_lockfile(pool, file, categories, pkg_caches, others);
[&](auto& pool, auto& pkg_caches, auto& others)
{
return create_explicit_transaction_from_lockfile(
ctx,
pool,
file,
categories,
pkg_caches,
others
);
},
create_env,
remove_prefix_on_failure
Expand Down
28 changes: 17 additions & 11 deletions libmamba/src/api/remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,28 @@ namespace mamba
transaction.log_json();
}

if (transaction.prompt())
if (transaction.prompt(ctx, channel_context))
{
transaction.execute(prefix_data);
transaction.execute(ctx, channel_context, prefix_data);
}
};

if (force)
{
std::vector<specs::MatchSpec> mspecs;
mspecs.reserve(raw_specs.size());
std::transform(
raw_specs.begin(),
raw_specs.end(),
std::back_inserter(mspecs),
[&](const auto& spec_str) { return specs::MatchSpec::parse(spec_str); }
);
auto transaction = MTransaction(pool, mspecs, {}, package_caches);
std::vector<specs::PackageInfo> pkgs_to_remove;
pkgs_to_remove.reserve(raw_specs.size());
for (const auto& str : raw_specs)
{
auto spec = specs::MatchSpec::parse(str);
const auto& installed = prefix_data.records();
// TODO should itreate over all packages and use MatchSpec.contains
// TODO should move such method over Pool for consitent use
if (auto iter = installed.find(spec.name().str()); iter != installed.cend())
{
pkgs_to_remove.push_back(iter->second);
}
}
auto transaction = MTransaction(ctx, pool, pkgs_to_remove, {}, package_caches);
execute_transaction(transaction);
}
else
Expand Down Expand Up @@ -189,6 +194,7 @@ namespace mamba

Console::instance().json_write({ { "success", true } });
auto transaction = MTransaction(
ctx,
pool,
request,
std::get<solver::Solution>(outcome),
Expand Down
12 changes: 9 additions & 3 deletions libmamba/src/api/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ namespace mamba
}

Console::instance().json_write({ { "success", true } });
auto transaction = MTransaction(pool, request, std::get<solver::Solution>(outcome), package_caches);
auto transaction = MTransaction(
ctx,
pool,
request,
std::get<solver::Solution>(outcome),
package_caches
);


auto execute_transaction = [&](MTransaction& transaction)
Expand All @@ -180,10 +186,10 @@ namespace mamba
transaction.log_json();
}

bool yes = transaction.prompt();
bool yes = transaction.prompt(ctx, channel_context);
if (yes)
{
transaction.execute(prefix_data);
transaction.execute(ctx, channel_context, prefix_data);
}
};

Expand Down
Loading
Loading