Skip to content

Commit b3a48ce

Browse files
authored
Support SHA256 hashes in @explicit files (#3866)
1 parent d1ca95e commit b3a48ce

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

libmamba/src/specs/package_info.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,24 @@ namespace mamba::specs
215215
return parse_url(url).transform(
216216
[&](PackageInfo&& pkg) -> PackageInfo
217217
{
218-
if (is_hash(hash))
218+
if (util::starts_with(hash, "sha256:"))
219219
{
220-
pkg.md5 = hash;
220+
hash = hash.substr(7);
221+
if (hash.size() == 64 && is_hash(hash))
222+
{
223+
pkg.sha256 = hash;
224+
}
225+
}
226+
else if (is_hash(hash))
227+
{
228+
if (hash.size() == 32)
229+
{
230+
pkg.md5 = hash;
231+
}
232+
else if (hash.size() == 64)
233+
{
234+
pkg.sha256 = hash;
235+
}
221236
}
222237
return pkg;
223238
}

libmamba/tests/src/specs/test_package_info.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace
3131
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
3232
REQUIRE(pkg.package_url == url);
3333
REQUIRE(pkg.md5 == "");
34+
REQUIRE(pkg.sha256 == "");
3435
REQUIRE(pkg.platform == "linux-64");
3536
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
3637
}
@@ -48,6 +49,43 @@ namespace
4849
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
4950
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
5051
REQUIRE(pkg.md5 == url.substr(url.rfind('#') + 1));
52+
REQUIRE(pkg.sha256 == "");
53+
REQUIRE(pkg.platform == "linux-64");
54+
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
55+
}
56+
57+
SECTION("https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0"
58+
)
59+
{
60+
static constexpr std::string_view url = "https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0";
61+
62+
auto pkg = PackageInfo::from_url(url).value();
63+
64+
REQUIRE(pkg.name == "pkg");
65+
REQUIRE(pkg.version == "6.4");
66+
REQUIRE(pkg.build_string == "bld");
67+
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
68+
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
69+
REQUIRE(pkg.md5 == "");
70+
REQUIRE(pkg.sha256 == url.substr(url.rfind('#') + 1));
71+
REQUIRE(pkg.platform == "linux-64");
72+
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
73+
}
74+
75+
SECTION("https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#sha256:7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0"
76+
)
77+
{
78+
static constexpr std::string_view url = "https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#sha256:7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0";
79+
80+
auto pkg = PackageInfo::from_url(url).value();
81+
82+
REQUIRE(pkg.name == "pkg");
83+
REQUIRE(pkg.version == "6.4");
84+
REQUIRE(pkg.build_string == "bld");
85+
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
86+
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
87+
REQUIRE(pkg.md5 == "");
88+
REQUIRE(pkg.sha256 == url.substr(url.rfind("#sha256:") + 8));
5189
REQUIRE(pkg.platform == "linux-64");
5290
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
5391
}
@@ -65,6 +103,7 @@ namespace
65103
REQUIRE(pkg.filename == "_libgcc_mutex-0.1-conda_forge.tar.bz2");
66104
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
67105
REQUIRE(pkg.md5 == url.substr(url.rfind('#') + 1));
106+
REQUIRE(pkg.sha256 == "");
68107
REQUIRE(pkg.platform == "linux-64");
69108
// Make sure the token is not censored when setting the channel
70109
REQUIRE(

libmambapy/tests/test_specs.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,27 @@ def test_PackageInfo():
767767
# str
768768
assert str(pkg) == "pkg-1.0-bld"
769769

770-
# from_url
771-
pkg = PackageInfo.from_url("https://repo.mamba.pm/conda-forge/linux-64/bar-5.1-xld.conda#01234")
770+
# from_url with md5
771+
pkg = PackageInfo.from_url(
772+
"https://repo.mamba.pm/conda-forge/linux-64/bar-5.1-xld.conda"
773+
"#01234012340123401234012340123401"
774+
)
775+
assert pkg.name == "bar"
776+
assert pkg.version == "5.1"
777+
assert pkg.build_string == "xld"
778+
assert pkg.md5 == "01234012340123401234012340123401"
779+
assert pkg.sha256 == ""
780+
781+
# from_url with sha256
782+
pkg = PackageInfo.from_url(
783+
"https://repo.mamba.pm/conda-forge/linux-64/bar-5.1-xld.conda"
784+
"#0123401234012340123401234012340101234012340123401234012340123401"
785+
)
772786
assert pkg.name == "bar"
773787
assert pkg.version == "5.1"
774788
assert pkg.build_string == "xld"
775-
assert pkg.md5 == "01234"
789+
assert pkg.md5 == ""
790+
assert pkg.sha256 == "0123401234012340123401234012340101234012340123401234012340123401"
776791

777792
# getters and setters
778793
pkg.name = "foo"

0 commit comments

Comments
 (0)