feat: add sha256 flag to list command (#3885)

This commit is contained in:
Sandrine Pataut 2025-04-23 11:40:00 +02:00 committed by GitHub
parent 8660875d5e
commit 88695684da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 10 deletions

View File

@ -6,6 +6,7 @@
#include <iostream>
#include <regex>
#include <stdexcept>
#include "mamba/api/configuration.hpp"
#include "mamba/api/list.hpp"
@ -27,6 +28,7 @@ namespace mamba
bool reverse = false;
bool explicit_ = false;
bool md5 = false;
bool sha256 = false;
bool canonical = false;
bool export_ = false;
bool revisions = false;
@ -34,7 +36,7 @@ namespace mamba
struct formatted_pkg
{
std::string name, version, build, channel, url, md5, build_string, platform;
std::string name, version, build, channel, url, md5, sha256, build_string, platform;
};
bool compare_alphabetically(const formatted_pkg& a, const formatted_pkg& b)
@ -192,6 +194,7 @@ namespace mamba
obj["base_url"] = get_base_url(pkg_info, channels.front());
obj["url"] = pkg_info.package_url;
obj["md5"] = pkg_info.md5;
obj["sha256"] = pkg_info.sha256;
obj["build_number"] = pkg_info.build_number;
obj["build_string"] = pkg_info.build_string;
obj["dist_name"] = pkg_info.str();
@ -226,6 +229,7 @@ namespace mamba
formatted_pkgs.build = package.second.build_string;
formatted_pkgs.url = package.second.package_url;
formatted_pkgs.md5 = package.second.md5;
formatted_pkgs.sha256 = package.second.sha256;
formatted_pkgs.build_string = package.second.build_string;
formatted_pkgs.platform = package.second.platform;
packages.push_back(formatted_pkgs);
@ -286,10 +290,21 @@ namespace mamba
}
for (auto p : packages)
{
if (options.md5 && options.sha256)
{
throw mamba_error(
"Only one of --md5 and --sha256 can be specified at the same time.",
mamba_error_code::incorrect_usage
);
}
if (options.md5)
{
std::cout << p.url << "#" << p.md5 << std::endl;
}
else if (options.sha256)
{
std::cout << p.url << "#" << p.sha256 << std::endl;
}
else
{
std::cout << p.url << std::endl;
@ -360,6 +375,7 @@ namespace mamba
options.reverse = config.at("reverse").value<bool>();
options.explicit_ = config.at("explicit").value<bool>();
options.md5 = config.at("md5").value<bool>();
options.sha256 = config.at("sha256").value<bool>();
options.canonical = config.at("canonical").value<bool>();
options.export_ = config.at("export").value<bool>();
options.revisions = config.at("revisions").value<bool>();

View File

@ -53,6 +53,11 @@ init_list_parser(CLI::App* subcom, Configuration& config)
);
subcom->add_flag("--md5", md5.get_cli_config<bool>(), md5.description());
auto& sha256 = config.insert(
Configurable("sha256", false).group("cli").description("Add SHA256 hashsum when using --explicit")
);
subcom->add_flag("--sha256", sha256.get_cli_config<bool>(), sha256.description());
auto& canonical = config.insert(
Configurable("canonical", false)
.group("cli")

View File

@ -82,6 +82,7 @@ def test_list_no_json(
@pytest.mark.parametrize("explicit_flag", ["", "--explicit"])
@pytest.mark.parametrize("md5_flag", ["", "--md5"])
@pytest.mark.parametrize("sha256_flag", ["", "--sha256"])
@pytest.mark.parametrize("canonical_flag", ["", "-c", "--canonical"])
@pytest.mark.parametrize("export_flag", ["", "-e", "--export"])
@pytest.mark.parametrize("env_selector", ["", "name", "prefix"])
@ -94,19 +95,24 @@ def test_list_subcommands(
env_selector,
explicit_flag,
md5_flag,
sha256_flag,
canonical_flag,
export_flag,
):
args = []
if env_selector == "prefix":
res = helpers.umamba_list(
"-p", tmp_xtensor_env, explicit_flag, md5_flag, canonical_flag, export_flag
)
args += ["-p", tmp_xtensor_env]
elif env_selector == "name":
res = helpers.umamba_list(
"-n", tmp_env_name, explicit_flag, md5_flag, canonical_flag, export_flag
)
else:
res = helpers.umamba_list(explicit_flag, md5_flag, canonical_flag, export_flag)
args += ["-n", tmp_env_name]
args += [explicit_flag, md5_flag, sha256_flag, canonical_flag, export_flag]
if (explicit_flag == "--explicit") and (md5_flag == "--md5") and (sha256_flag == "--sha256"):
with pytest.raises(subprocess.CalledProcessError) as excinfo:
helpers.umamba_list(*args)
assert "Only one of --md5 and --sha256 can be specified at the same time." in excinfo
return None
res = helpers.umamba_list(*args)
outputs_list = res.strip().split("\n")[2:]
outputs_list = [i for i in outputs_list if i != "" and not i.startswith("Warning")]
@ -114,8 +120,14 @@ def test_list_subcommands(
if explicit_flag == "--explicit":
for output in outputs_list:
assert "/conda-forge/" in output
if md5_flag == "--md5":
if (md5_flag == "--md5") or (sha256_flag == "--sha256"):
assert "#" in output
hash = output.split("#")[-1]
hash = hash.replace("\r", "")
if md5_flag == "--md5":
assert len(hash) == 32
else:
assert len(hash) == 64
else:
assert "#" not in output
elif canonical_flag in ["-c", "--canonical"]: