Skip to content

Commit

Permalink
MatchSpec use VersionSpec (#3089)
Browse files Browse the repository at this point in the history
* Don't use members as wip vars

* Make conda-build compatible VersionSpec str

* Add VersionSpec::is_explicitly_free

* Strenghten sat attrs ref

* Try pluging VersionSpec in MatchSpec

* Change type of MatchSpec::version

* Fix MatchSpec::conda_build_str

* Adjust MatchSpec tests
  • Loading branch information
AntoinePrv authored Jan 2, 2024
1 parent 108bc9f commit b9706dc
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 143 deletions.
7 changes: 4 additions & 3 deletions libmamba/include/mamba/specs/match_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <unordered_map>

#include "mamba/specs/channel_spec.hpp"
#include "mamba/specs/version_spec.hpp"

namespace mamba::specs
{
Expand All @@ -37,8 +38,8 @@ namespace mamba::specs
[[nodiscard]] auto name() const -> const std::string&;
void set_name(std::string name);

[[nodiscard]] auto version() const -> const std::string&;
void set_version(std::string ver);
[[nodiscard]] auto version() const -> const VersionSpec&;
void set_version(VersionSpec ver);

[[nodiscard]] auto build_number() const -> const std::string&;
void set_build_number(std::string num);
Expand Down Expand Up @@ -67,9 +68,9 @@ namespace mamba::specs
private:

std::optional<ChannelSpec> m_channel;
VersionSpec m_version;
std::string m_name_space;
std::string m_name;
std::string m_version;
std::string m_build_number;
std::string m_build_string;
// TODO can put inside channel spec
Expand Down
33 changes: 33 additions & 0 deletions libmamba/include/mamba/specs/version_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ namespace mamba::specs

[[nodiscard]] auto str() const -> std::string;

/**
* An alternative string representation of the version spec.
*
* Attempts to be compatible with conda-build/libsolv.
*/
[[nodiscard]] auto str_conda_build() const -> std::string;

private:

struct free_interval
Expand Down Expand Up @@ -148,6 +155,15 @@ namespace mamba::specs
VersionSpec() = default;
explicit VersionSpec(tree_type&& tree) noexcept;

/**
* Returns wether the VersionSpec is unconstrained.
*
* Due to the complex nature of VersionSpec expressions, it is not always easy to know
* whether a complex expression can be simpified to the unconstrained one.
* This functions only handles the trivial cases.
*/
[[nodiscard]] auto is_explicitly_free() const -> bool;

/**
* A string representation of the version spec.
*
Expand All @@ -156,6 +172,13 @@ namespace mamba::specs
*/
[[nodiscard]] auto str() const -> std::string;

/**
* An alternative string representation of the version spec.
*
* Attempts to be compatible with conda-build/libsolv.
*/
[[nodiscard]] auto str_conda_build() const -> std::string;

/**
* True if the set described by the VersionSpec contains the given version.
*/
Expand All @@ -177,6 +200,11 @@ namespace mamba::specs
template <>
struct fmt::formatter<mamba::specs::VersionPredicate>
{
/**
* Change the representation of some predicates not understood by conda-build/libsolv.
*/
bool conda_build_form = false;

auto parse(format_parse_context& ctx) -> decltype(ctx.begin());

auto format(const ::mamba::specs::VersionPredicate& pred, format_context& ctx)
Expand All @@ -186,6 +214,11 @@ struct fmt::formatter<mamba::specs::VersionPredicate>
template <>
struct fmt::formatter<mamba::specs::VersionSpec>
{
/**
* Change the representation of some predicates not understood by conda-build/libsolv.
*/
bool conda_build_form = false;

auto parse(format_parse_context& ctx) -> decltype(ctx.begin());

auto format(const ::mamba::specs::VersionSpec& spec, format_context& ctx) -> decltype(ctx.out());
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ namespace mamba
PackageInfo p(ms.name());
p.url = ms.url();
p.build_string = ms.build_string();
p.version = ms.version();
p.version = ms.version().str();
if (ms.channel().has_value())
{
p.channel = ms.channel()->location();
Expand Down
69 changes: 45 additions & 24 deletions libmamba/src/core/satisfiability_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace mamba

std::vector<old_node_id_list> groups{};

std::size_t const n_nodes = node_indices.size();
const std::size_t n_nodes = node_indices.size();
std::vector<bool> node_added_to_a_group(n_nodes, false);
for (std::size_t i = 0; i < n_nodes; ++i)
{
Expand Down Expand Up @@ -538,14 +538,45 @@ namespace mamba
* Implementation of CompressedProblemsGraph::RoughCompare *
*************************************************************/

namespace
{
template <typename T>
auto invoke_name(T&& e) -> decltype(auto)
{
using TT = std::remove_cv_t<std::remove_reference_t<T>>;
return std::invoke(&TT::name, std::forward<T>(e));
}

template <typename T>
auto invoke_version(T&& e) -> decltype(auto)
{
using TT = std::remove_cv_t<std::remove_reference_t<T>>;
using Ver = decltype(std::invoke(&TT::version, std::forward<T>(e)));
Ver v = std::invoke(&TT::version, std::forward<T>(e));
if constexpr (std::is_same_v<std::decay_t<decltype(v)>, specs::VersionSpec>)
{
return std::forward<Ver>(v).str();
}
else
{
return v;
}
}
}

template <typename T>
bool CompressedProblemsGraph::RoughCompare<T>::operator()(const T& a, const T& b) const
auto CompressedProblemsGraph::RoughCompare<T>::operator()(const T& a, const T& b) const -> bool
{
auto attrs = [](const auto& x)
{
return std::tie(
std::invoke(&T::name, x),
std::invoke(&T::version, x),
using Attrs = std::tuple<
decltype(invoke_name(x)),
decltype(invoke_version(x)),
decltype(std::invoke(&T::build_number, x)),
decltype(std::invoke(&T::build_string, x))>;
return Attrs(
invoke_name(x),
invoke_version(x),
std::invoke(&T::build_number, x),
std::invoke(&T::build_string, x)
);
Expand All @@ -562,16 +593,6 @@ namespace mamba
* Implementation of CompressedProblemsGraph::NamedList *
**********************************************************/

namespace
{
template <typename T>
decltype(auto) invoke_name(T&& e)
{
using TT = std::remove_cv_t<std::remove_reference_t<T>>;
return std::invoke(&TT::name, std::forward<T>(e));
}
}

template <typename T, typename A>
template <typename InputIterator>
CompressedProblemsGraph::NamedList<T, A>::NamedList(InputIterator first, InputIterator last)
Expand Down Expand Up @@ -650,13 +671,13 @@ namespace mamba
) const -> std::pair<std::string, std::size_t>
{
auto versions = std::vector<std::string>(size());
auto invoke_version = [](auto&& v) -> decltype(auto)
{
using TT = std::remove_cv_t<std::remove_reference_t<decltype(v)>>;
return std::invoke(&TT::version, std::forward<decltype(v)>(v));
};
// TODO(C++20) *this | std::ranges::transform(invoke_version) | ranges::unique
std::transform(begin(), end(), versions.begin(), invoke_version);
std::transform(
begin(),
end(),
versions.begin(),
[](const auto& x) { return invoke_version(x); }
);
if (remove_duplicates)
{
versions.erase(std::unique(versions.begin(), versions.end()), versions.end());
Expand Down Expand Up @@ -1036,7 +1057,7 @@ namespace mamba

const TreeNodeIter children_begin = out;
// TODO(C++20) an enumerate view ``views::zip(views::iota(), children_ids)``
std::size_t const n_children = children_ids.size();
const std::size_t n_children = children_ids.size();
for (std::size_t i = 0; i < n_children; ++i)
{
const bool last = (i == n_children - 1);
Expand Down Expand Up @@ -1228,7 +1249,7 @@ namespace mamba

void TreeExplainer::write_ancestry(const std::vector<SiblingNumber>& ancestry)
{
std::size_t const size = ancestry.size();
const std::size_t size = ancestry.size();
const auto indents = m_format.indents;
if (size > 0)
{
Expand Down Expand Up @@ -1434,7 +1455,7 @@ namespace mamba

void TreeExplainer::write_path(const std::vector<TreeNode>& path)
{
std::size_t const length = path.size();
const std::size_t length = path.size();
for (std::size_t i = 0; i < length; ++i)
{
const bool last = (i == length - 1);
Expand Down
5 changes: 3 additions & 2 deletions libmamba/src/core/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ namespace mamba
return m_jobs->push_back(job_flag | SOLVER_SOLVABLE_PROVIDES, m_pool.matchspec2id(ms));
}

if (ms.channel().has_value() || !ms.version().empty() || !ms.build_string().empty())
if (ms.channel().has_value() || !ms.version().is_explicitly_free()
|| !ms.build_string().empty())
{
Console::stream() << ms.conda_build_form()
<< ": overriding channel, version and build from "
Expand All @@ -105,7 +106,7 @@ namespace mamba

auto ms_modified = ms;
ms_modified.set_channel(specs::ChannelSpec::parse(solvable->channel()));
ms_modified.set_version(std::string(solvable->version()));
ms_modified.set_version(specs::VersionSpec::parse(solvable->version()));
ms_modified.set_build_string(std::string(solvable->build_string()));

LOG_INFO << "Reinstall " << ms_modified.conda_build_form() << " from channel "
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace mamba
auto& p = out.back();
p.url = ms.url();
p.build_string = ms.build_string();
p.version = ms.version();
p.version = ms.version().str_conda_build();
if (ms.channel().has_value())
{
p.channel = ms.channel()->location();
Expand Down
Loading

0 comments on commit b9706dc

Please sign in to comment.