Skip to content

Commit

Permalink
Factorize Context's channels population
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>
  • Loading branch information
jjerphan authored and JohanMabille committed Oct 23, 2024
1 parent 71dfe40 commit 505e5e0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 52 deletions.
4 changes: 2 additions & 2 deletions libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ set(
${LIBMAMBA_SOURCE_DIR}/api/info.cpp
${LIBMAMBA_SOURCE_DIR}/api/install.cpp
${LIBMAMBA_SOURCE_DIR}/api/list.cpp
${LIBMAMBA_SOURCE_DIR}/api/pip_utils.cpp
${LIBMAMBA_SOURCE_DIR}/api/pip_utils.hpp
${LIBMAMBA_SOURCE_DIR}/api/utils.cpp
${LIBMAMBA_SOURCE_DIR}/api/utils.hpp
${LIBMAMBA_SOURCE_DIR}/api/remove.cpp
${LIBMAMBA_SOURCE_DIR}/api/repoquery.cpp
${LIBMAMBA_SOURCE_DIR}/api/shell.cpp
Expand Down
31 changes: 6 additions & 25 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "mamba/util/path_manip.hpp"
#include "mamba/util/string.hpp"

#include "pip_utils.hpp"
#include "utils.hpp"

namespace mamba
{
Expand Down Expand Up @@ -378,7 +378,7 @@ namespace mamba
Context& ctx,
ChannelContext& channel_context,
const Configuration& config,
const std::vector<std::string>& specs,
const std::vector<std::string>& raw_specs,
bool create_env,
bool remove_prefix_on_failure,
bool is_retry
Expand All @@ -405,26 +405,7 @@ namespace mamba

MultiPackageCache package_caches{ ctx.pkgs_dirs, ctx.validation_params };

// add channels from specs
for (const auto& s : specs)
{
if (auto ms = specs::MatchSpec::parse(s); ms && ms->channel().has_value())
{
// Only register channels in the context once
// NOTE: ctx.channels could be a `std::unordered_set` but `yaml-cpp` does not
// support it. See: https://github.com/jbeder/yaml-cpp/issues/1322 Hence we
// perform linear scanning: `ctx.channels` is a short `std::vector` in practice
// so linearly searching is reasonable (probably even faster than using an
// `std::unordered_set`).
auto channel_name = ms->channel()->str();
auto channel_is_absent = std::find(ctx.channels.begin(), ctx.channels.end(), channel_name)
== ctx.channels.end();
if (channel_is_absent)
{
ctx.channels.push_back(channel_name);
}
}
}
populate_context_channels_from_specs(raw_specs, ctx);

if (ctx.channels.empty() && !ctx.offline)
{
Expand Down Expand Up @@ -456,8 +437,8 @@ namespace mamba
load_installed_packages_in_database(ctx, db, prefix_data);


auto request = create_install_request(prefix_data, specs, freeze_installed);
add_pins_to_request(request, ctx, prefix_data, specs, no_pin, no_py_pin);
auto request = create_install_request(prefix_data, raw_specs, freeze_installed);
add_pins_to_request(request, ctx, prefix_data, raw_specs, no_pin, no_py_pin);
request.flags = ctx.solver_flags;

{
Expand Down Expand Up @@ -485,7 +466,7 @@ namespace mamba
ctx,
channel_context,
config,
specs,
raw_specs,
create_env,
remove_prefix_on_failure,
true
Expand Down
23 changes: 2 additions & 21 deletions libmamba/src/api/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "mamba/solver/libsolv/solver.hpp"
#include "mamba/solver/request.hpp"

#include "pip_utils.hpp"
#include "utils.hpp"

namespace mamba
{
Expand Down Expand Up @@ -150,26 +150,7 @@ namespace mamba

auto channel_context = ChannelContext::make_conda_compatible(ctx);

// add channels from specs
for (const auto& s : raw_update_specs)
{
if (auto ms = specs::MatchSpec::parse(s); ms && ms->channel().has_value())
{
// Only register channels in the context once
// NOTE: ctx.channels could be a `std::unordered_set` but `yaml-cpp` does not
// support it. See: https://github.com/jbeder/yaml-cpp/issues/1322 Hence we
// perform linear scanning: `ctx.channels` is a short `std::vector` in practice
// so linearly searching is reasonable (probably even faster than using an
// `std::unordered_set`).
auto channel_name = ms->channel()->str();
auto channel_is_absent = std::find(ctx.channels.begin(), ctx.channels.end(), channel_name)
== ctx.channels.end();
if (channel_is_absent)
{
ctx.channels.push_back(channel_name);
}
}
}
populate_context_channels_from_specs(raw_update_specs, ctx);

solver::libsolv::Database db{ channel_context.params() };
add_spdlog_logger_to_database(db);
Expand Down
24 changes: 23 additions & 1 deletion libmamba/src/api/pip_utils.cpp → libmamba/src/api/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "mamba/fs/filesystem.hpp"
#include "mamba/util/environment.hpp"

#include "pip_utils.hpp"
#include "utils.hpp"

namespace mamba
{
Expand Down Expand Up @@ -184,4 +184,26 @@ namespace mamba

return command;
}

void
populate_context_channels_from_specs(const std::vector<std::string>& raw_matchspecs, Context& context)
{
for (const auto& s : raw_matchspecs)
{
if (auto ms = specs::MatchSpec::parse(s); ms && ms->channel().has_value())
{
auto channel_name = ms->channel()->str();
auto channel_is_absent = std::find(
context.channels.begin(),
context.channels.end(),
channel_name
)
== context.channels.end();
if (channel_is_absent)
{
context.channels.push_back(channel_name);
}
}
}
}
}
9 changes: 6 additions & 3 deletions libmamba/src/api/pip_utils.hpp → libmamba/src/api/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//
// The full license is in the file LICENSE, distributed with this software.

#ifndef MAMBA_PIP_UTILS_HPP
#define MAMBA_PIP_UTILS_HPP
#ifndef MAMBA_UTILS_HPP
#define MAMBA_UTILS_HPP

#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -39,6 +39,9 @@ namespace mamba
pip::Update update
);

void
populate_context_channels_from_specs(const std::vector<std::string>& raw_matchspecs, Context& context);

}

#endif // MAMBA_PIP_UTILS_HPP
#endif // MAMBA_UTILS_HPP

0 comments on commit 505e5e0

Please sign in to comment.