Skip to content

Commit

Permalink
Resolve 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Nov 13, 2023
1 parent e351589 commit 1b8f371
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 68 deletions.
5 changes: 2 additions & 3 deletions libmamba/include/mamba/core/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace mamba
namespace specs
{
class ChannelSpec;
class AuthenticationDataBase;
}

std::vector<std::string> get_known_platforms();
Expand All @@ -45,6 +46,7 @@ namespace mamba
const platform_list& platforms;
const specs::CondaURL& channel_alias;
const channel_map& custom_channels;
const specs::AuthenticationDataBase& auth_db;

// TODO add CWD and home
};
Expand Down Expand Up @@ -132,9 +134,6 @@ namespace mamba

void init_custom_channels();

Channel from_any_url(specs::ChannelSpec&& spec);
Channel from_package_url(specs::ChannelSpec&& spec);
Channel from_url(specs::ChannelSpec&& spec);
Channel from_name(specs::ChannelSpec&& spec);
Channel from_value(const std::string& value);
};
Expand Down
99 changes: 34 additions & 65 deletions libmamba/src/core/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ namespace mamba
return filters;
}

auto canonical_name_from_path(const specs::CondaURL& uri, Channel::ResolveParams params)
auto resolve_path_name(const specs::CondaURL& uri, Channel::ResolveParams params)
-> std::string
{
for (const auto& [canonical_name, chan] : params.custom_channels)
Expand All @@ -256,10 +256,10 @@ namespace mamba
return uri.pretty_str();
}

auto resolve_any_path(specs::ChannelSpec&& spec, Channel::ResolveParams params) -> Channel
auto resolve_path(specs::ChannelSpec&& spec, Channel::ResolveParams params) -> Channel
{
auto uri = specs::CondaURL::parse(util::path_or_url_to_url(spec.location()));
auto canonical_name = canonical_name_from_path(uri, params);
auto canonical_name = resolve_path_name(uri, params);
auto platforms = Channel::ResolveParams::platform_list{};
if (spec.type() == specs::ChannelSpec::Type::Path)
{
Expand All @@ -269,72 +269,46 @@ namespace mamba
return Channel(std::move(uri), std::move(canonical_name), std::move(platforms));
}

auto resolve_package_path(specs::ChannelSpec&& spec, Channel::ResolveParams params) -> Channel
{
assert(spec.type() == specs::ChannelSpec::Type::PackagePath);
return resolve_any_path(std::move(spec), params);
}

auto resolve_path(specs::ChannelSpec&& spec, Channel::ResolveParams params) -> Channel
auto resolve_url_name(const specs::CondaURL& url, Channel::ResolveParams params) -> std::string
{
assert(spec.type() == specs::ChannelSpec::Type::Path);
return resolve_any_path(std::move(spec), params);
}
}
using StripScheme = typename specs::CondaURL::StripScheme;
using Credentials = typename specs::CondaURL::Credentials;

Channel ChannelContext::from_any_url(specs::ChannelSpec&& spec)
{
assert(util::url_has_scheme(spec.location()));
auto url = specs::CondaURL::parse(spec.location());
assert(url.scheme() != "file");
std::string url_str = url.pretty_str(StripScheme::yes, '/', Credentials::Remove);

using StripScheme = typename specs::CondaURL::StripScheme;
using Credentials = typename specs::CondaURL::Credentials;

std::string url_str = url.pretty_str(StripScheme::yes, '/', Credentials::Remove);
for (const auto& [canonical_name, chan] : params.custom_channels)
{
if (url_match(chan.url(), url))
{
return std::string(canonical_name);
}
}

for (const auto& [canonical_name, chan] : get_custom_channels())
{
if (url_match(chan.url(), url))
if (const auto& ca = params.channel_alias; url_match(ca, url))
{
return Channel(
/* url= */ std::move(url),
/* canonical_name= */ std::string(canonical_name)
);
auto ca_str = ca.pretty_str(StripScheme::yes, '/', Credentials::Remove);
return std::string(util::strip(util::remove_prefix(url_str, ca_str), '/'));
}
}

if (const auto& ca = get_channel_alias(); url_match(ca, url))
{
auto ca_str = ca.pretty_str(StripScheme::yes, '/', Credentials::Remove);
auto name = std::string(util::strip(util::remove_prefix(url_str, ca_str), '/'));
return Channel(
/* url= */ std::move(url),
/* canonical_name= */ name
);
return url.pretty_str(StripScheme::no, '/', Credentials::Remove);
}

auto canonical_name = url.pretty_str(StripScheme::no, '/', Credentials::Remove);
return Channel(
/* url= */ std::move(url),
/* canonical_name= */ std::move(canonical_name)
);
}
auto resolve_url(specs::ChannelSpec&& spec, Channel::ResolveParams params) -> Channel
{
assert(util::url_has_scheme(spec.location()));
assert(util::url_get_scheme(spec.location()) != "file");

Channel ChannelContext::from_package_url(specs::ChannelSpec&& spec)
{
auto chan = from_any_url(std ::move(spec));
set_fallback_credential_from_db(chan.m_url, m_context.authentication_info());
return chan;
}
auto url = specs::CondaURL::parse(spec.location());
auto canonical_name = resolve_url_name(url, params);
set_fallback_credential_from_db(url, params.auth_db);
auto platforms = Channel::ResolveParams::platform_list{};
if (spec.type() == specs::ChannelSpec::Type::URL)
{
platforms = make_platforms(spec.clear_platform_filters(), params.platforms);
}

Channel ChannelContext::from_url(specs::ChannelSpec&& spec)
{
auto platforms = make_platforms(spec.clear_platform_filters(), m_context.platforms());
auto chan = from_any_url(std::move(spec));
chan.m_platforms = std::move(platforms);
set_fallback_credential_from_db(chan.m_url, m_context.authentication_info());
return chan;
return Channel(std::move(url), std::move(canonical_name), std::move(platforms));
}
}

Channel ChannelContext::from_name(specs::ChannelSpec&& spec)
Expand Down Expand Up @@ -408,25 +382,20 @@ namespace mamba
/* .platforms */ platforms,
/* .channel_alias */ m_channel_alias,
/* .custom_channels */ m_custom_channels,
/* .auth_db */ m_context.authentication_info(),
};

switch (spec.type())
{
case specs::ChannelSpec::Type::PackagePath:
{
return resolve_package_path(std::move(spec), params);
}
case specs::ChannelSpec::Type::Path:
{
return resolve_path(std::move(spec), params);
}
case specs::ChannelSpec::Type::PackageURL:
{
return from_package_url(std::move(spec));
}
case specs::ChannelSpec::Type::URL:
{
return from_url(std::move(spec));
return resolve_url(std::move(spec), params);
}
case specs::ChannelSpec::Type::Name:
{
Expand Down

0 comments on commit 1b8f371

Please sign in to comment.