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

list refactoring #3768

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 108 additions & 91 deletions libmamba/src/api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,64 @@ namespace mamba
));
}

std::vector<std::string>
get_record_keys(list_options options, const PrefixData::package_map& all_records)
{
std::vector<std::string> keys;

for (const auto& pkg : all_records)
{
keys.push_back(pkg.first);
}
if (options.reverse)
{
std::sort(
keys.begin(),
keys.end(),
[](const std::string& a, const std::string& b) { return a >= b; }
);
}
else
{
std::sort(keys.begin(), keys.end());
}

return keys;
}

std::string
get_formatted_channel(const specs::PackageInfo& pkg_info, const specs::Channel& channel)
{
if (pkg_info.channel == "pypi")
{
return "pypi";
}
else
{
return strip_from_filename_and_platform(
channel.display_name(),
pkg_info.filename,
pkg_info.platform
);
}
}

std::string get_base_url(const specs::PackageInfo& pkg_info, const specs::Channel& channel)
{
if (pkg_info.channel == "pypi")
{
return "https://pypi.org/";
}
else
{
return strip_from_filename_and_platform(
channel.url().str(specs::CondaURL::Credentials::Remove),
pkg_info.filename,
pkg_info.platform
);
}
}

void list_packages(
const Context& ctx,
std::string regex,
Expand Down Expand Up @@ -83,58 +141,31 @@ namespace mamba
}

std::regex spec_pat(regex);

auto accept_package = [&regex, &spec_pat](const specs::PackageInfo& pkg_info) -> bool
{ return (regex.empty() || std::regex_search(pkg_info.name, spec_pat)); };

auto all_records = prefix_data.all_pkg_mgr_records();

if (ctx.output_params.json)
{
auto jout = nlohmann::json::array();
std::vector<std::string> keys;

for (const auto& pkg : all_records)
{
keys.push_back(pkg.first);
}
if (options.reverse)
{
std::sort(
keys.begin(),
keys.end(),
[](const std::string& a, const std::string& b) { return a >= b; }
);
}
else
{
std::sort(keys.begin(), keys.end());
}
keys = get_record_keys(options, all_records);

for (const auto& key : keys)
{
auto obj = nlohmann::json();
const auto& pkg_info = all_records.find(key)->second;

if (regex.empty() || std::regex_search(pkg_info.name, spec_pat))
if (accept_package(pkg_info))
{
auto channels = channel_context.make_channel(pkg_info.package_url);
assert(channels.size() == 1); // A URL can only resolve to one channel

if (pkg_info.package_url.empty() && (pkg_info.channel == "pypi"))
{
obj["base_url"] = "https://pypi.org/";
obj["channel"] = pkg_info.channel;
}
else
{
obj["base_url"] = strip_from_filename_and_platform(
channels.front().url().str(specs::CondaURL::Credentials::Remove),
pkg_info.filename,
pkg_info.platform
);
obj["channel"] = strip_from_filename_and_platform(
channels.front().display_name(),
pkg_info.filename,
pkg_info.platform
);
}
obj["channel"] = get_formatted_channel(pkg_info, channels.front());
obj["base_url"] = get_base_url(pkg_info, channels.front());
obj["url"] = pkg_info.package_url;
obj["build_number"] = pkg_info.build_number;
obj["build_string"] = pkg_info.build_string;
Expand All @@ -146,80 +177,66 @@ namespace mamba
}
}
std::cout << jout.dump(4) << std::endl;
return;
}
else
{
std::cout << "List of packages in environment: " << ctx.prefix_params.target_prefix
<< "\n\n";

std::cout << "List of packages in environment: " << ctx.prefix_params.target_prefix
<< "\n\n";

formatted_pkg formatted_pkgs;
formatted_pkg formatted_pkgs;

std::vector<formatted_pkg> packages;
auto requested_specs = prefix_data.history().get_requested_specs_map();
std::vector<formatted_pkg> packages;

// order list of packages from prefix_data by alphabetical order
for (const auto& package : all_records)
{
if (regex.empty() || std::regex_search(package.second.name, spec_pat))
// order list of packages from prefix_data by alphabetical order
for (const auto& package : all_records)
{
formatted_pkgs.name = package.second.name;
formatted_pkgs.version = package.second.version;
formatted_pkgs.build = package.second.build_string;
formatted_pkgs.url = package.second.package_url;
if (package.second.channel.find("https://repo.anaconda.com/pkgs/") == 0)
{
formatted_pkgs.channel = "";
}
else if (package.second.channel == "pypi")
{
formatted_pkgs.channel = package.second.channel;
}
else
if (accept_package(package.second))
{
auto channels = channel_context.make_channel(package.second.channel);
assert(channels.size() == 1); // A URL can only resolve to one channel
formatted_pkgs.channel = strip_from_filename_and_platform(
channels.front().display_name(),
package.second.filename,
package.second.platform
);
formatted_pkgs.channel = get_formatted_channel(package.second, channels.front());
formatted_pkgs.name = package.second.name;
formatted_pkgs.version = package.second.version;
formatted_pkgs.build = package.second.build_string;
formatted_pkgs.url = package.second.package_url;
packages.push_back(formatted_pkgs);
}
packages.push_back(formatted_pkgs);
}
}

auto comparator = options.reverse ? compare_reverse_alphabetically
: compare_alphabetically;
std::sort(packages.begin(), packages.end(), comparator);
auto comparator = options.reverse ? compare_reverse_alphabetically
: compare_alphabetically;
std::sort(packages.begin(), packages.end(), comparator);

// format and print table
if (options.explicit_)
{
for (auto p : packages)
// format and print table
if (options.explicit_)
{
std::cout << p.url << std::endl;
for (auto p : packages)
{
std::cout << p.url << std::endl;
}
}
}
else
{
printers::Table t({ "Name", "Version", "Build", "Channel" });
t.set_alignment({ printers::alignment::left,
printers::alignment::left,
printers::alignment::left,
printers::alignment::left });
t.set_padding({ 2, 2, 2, 2 });

for (auto p : packages)
else
{
printers::FormattedString formatted_name(p.name);
if (requested_specs.find(p.name) != requested_specs.end())
auto requested_specs = prefix_data.history().get_requested_specs_map();
printers::Table t({ "Name", "Version", "Build", "Channel" });
t.set_alignment({ printers::alignment::left,
printers::alignment::left,
printers::alignment::left,
printers::alignment::left });
t.set_padding({ 2, 2, 2, 2 });

for (auto p : packages)
{
formatted_name = printers::FormattedString(p.name);
formatted_name.style = ctx.graphics_params.palette.user;
printers::FormattedString formatted_name(p.name);
if (requested_specs.find(p.name) != requested_specs.end())
{
formatted_name = printers::FormattedString(p.name);
formatted_name.style = ctx.graphics_params.palette.user;
}
t.add_row({ formatted_name, p.version, p.build, p.channel });
}
t.add_row({ formatted_name, p.version, p.build, p.channel });
t.print(std::cout);
}
t.print(std::cout);
}
}
}
Expand Down
Loading