Skip to content

Commit

Permalink
Various fixes (#2800)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv authored Aug 31, 2023
1 parent 319f055 commit 7b23f51
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 6 deletions.
2 changes: 2 additions & 0 deletions libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ set(LIBMAMBA_SOURCES
# C++ wrapping of compression libs (zstd and bzlib)
${LIBMAMBA_SOURCE_DIR}/core/compression.cpp
# Implementation of version and matching specs
${LIBMAMBA_SOURCE_DIR}/specs/archive.cpp
${LIBMAMBA_SOURCE_DIR}/specs/platform.cpp
${LIBMAMBA_SOURCE_DIR}/specs/version.cpp
${LIBMAMBA_SOURCE_DIR}/specs/version_spec.cpp
Expand Down Expand Up @@ -220,6 +221,7 @@ set(LIBMAMBA_PUBLIC_HEADERS
${LIBMAMBA_INCLUDE_DIR}/mamba/util/url_manip.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/util/url.hpp
# Implementation of version and matching specs
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/archive.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/platform.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/version.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/version_spec.hpp
Expand Down
52 changes: 52 additions & 0 deletions libmamba/include/mamba/specs/archive.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#ifndef MAMBA_SPECS_ARCHIVE_HPP
#define MAMBA_SPECS_ARCHIVE_HPP

#include <array>
#include <string_view>
#include <type_traits>

#include "mamba/core/mamba_fs.hpp"

namespace mamba::specs
{
inline static constexpr auto ARCHIVE_EXTENSIONS = std::array{
std::string_view(".tar.bz2"),
std::string_view(".conda"),
};

/** Detect if the package path has one of the known archive extension. */
template <typename Str, std::enable_if_t<std::is_convertible_v<Str, std::string_view>, bool> = true>
[[nodiscard]] auto has_archive_extension(const Str& path) -> bool;
[[nodiscard]] auto has_archive_extension(std::string_view path) -> bool;
[[nodiscard]] auto has_archive_extension(const fs::u8path& path) -> bool;

/** Remove the known archive extension from the package path if present. */
template <typename Str, std::enable_if_t<std::is_convertible_v<Str, std::string_view>, bool> = true>
[[nodiscard]] auto strip_archive_extension(const Str& path) -> std::string_view;
[[nodiscard]] auto strip_archive_extension(std::string_view path) -> std::string_view;
[[nodiscard]] auto strip_archive_extension(fs::u8path path) -> fs::u8path;

/********************
* Implementation *
********************/

template <typename Str, std::enable_if_t<std::is_convertible_v<Str, std::string_view>, bool>>
[[nodiscard]] auto has_archive_extension(const Str& path) -> bool
{
return has_archive_extension(std::string_view(path));
}

template <typename Str, std::enable_if_t<std::is_convertible_v<Str, std::string_view>, bool>>
[[nodiscard]] auto strip_archive_extension(const Str& path) -> std::string_view
{
return strip_archive_extension(std::string_view(path));
}

}
#endif
1 change: 1 addition & 0 deletions libmamba/include/mamba/specs/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace mamba::specs
{
enum class Platform
{
noarch,
linux_32,
linux_64,
linux_armv6l,
Expand Down
2 changes: 1 addition & 1 deletion libmamba/include/mamba/specs/repo_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace mamba::specs
*
* This is likely not used as it contains not so useful strings such as "linux".
*/
std::string subdir = {};
std::optional<std::string> subdir = {};

/** Optionally a MD5 hash of the package archive. */
std::optional<std::string> md5 = {};
Expand Down
5 changes: 3 additions & 2 deletions libmamba/src/core/package_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <fmt/format.h>

#include "mamba/core/package_info.hpp"
#include "mamba/specs/archive.hpp"
#include "mamba/util/string.hpp"

namespace mamba
Expand Down Expand Up @@ -277,12 +278,12 @@ namespace mamba

std::string PackageInfo::str() const
{
return util::concat(name, "-", version, "-", build_string);
return std::string(specs::strip_archive_extension(fn));
}

std::string PackageInfo::long_str() const
{
// TODO channel contains subdir right now?!
return util::concat(channel, "::", name, "-", version, "-", build_string);
return util::concat(channel, "::", str());
}
} // namespace mamba
58 changes: 58 additions & 0 deletions libmamba/src/specs/archive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#include "mamba/specs/archive.hpp"
#include "mamba/util/string.hpp"

namespace mamba::specs
{

auto has_archive_extension(std::string_view path) -> bool
{
for (const auto& ext : ARCHIVE_EXTENSIONS)
{
if (util::ends_with(path, ext))
{
return true;
}
}
return false;
}

auto has_archive_extension(const fs::u8path& path) -> bool
{
if (path.has_filename() && path.has_extension())
{
const auto filename = path.filename().string();
return has_archive_extension(std::string_view(filename));
}
return false;
}

auto strip_archive_extension(std::string_view path) -> std::string_view
{
for (const auto& ext : ARCHIVE_EXTENSIONS)
{
const auto stem = util::remove_suffix(path, ext);
if (stem.size() != path.size())
{
return stem;
}
}
return path;
}

auto strip_archive_extension(fs::u8path path) -> fs::u8path
{
if (path.has_filename() && path.has_extension())
{
const auto filename = path.filename().string();
auto new_filename = strip_archive_extension(std::string_view(filename));
path.replace_filename(new_filename);
}
return path;
}
}
3 changes: 3 additions & 0 deletions libmamba/src/specs/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace mamba::specs
{
switch (p)
{
case Platform::noarch:
return "noarch";
case Platform::linux_32:
return "linux-32";
case Platform::linux_64:
Expand Down Expand Up @@ -64,6 +66,7 @@ namespace mamba::specs
{
std::string const str_clean = util::to_lower(util::strip(str));
for (const auto p : {
Platform::noarch,
Platform::linux_32,
Platform::linux_64,
Platform::linux_armv6l,
Expand Down
6 changes: 3 additions & 3 deletions libmamba/src/specs/repo_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace mamba::specs
p.version = Version::parse(j.at("version").template get<std::string_view>());
p.build_string = j.at("build");
p.build_number = j.at("build_number");
p.subdir = j.at("subdir");
deserialize_maybe_missing(j, "subdir", p.subdir);
deserialize_maybe_missing(j, "md5", p.md5);
deserialize_maybe_missing(j, "sha256", p.sha256);
deserialize_maybe_missing(j, "legacy_bz2_md5", p.legacy_bz2_md5);
Expand Down Expand Up @@ -159,7 +159,7 @@ namespace mamba::specs
j["version"] = data.version;
j["info"] = data.info;
j["packages"] = data.packages;
j["conda_packages"] = data.conda_packages;
j["packages.conda"] = data.conda_packages;
j["removed"] = data.removed;
}

Expand All @@ -168,7 +168,7 @@ namespace mamba::specs
deserialize_maybe_missing(j, "version", data.version);
deserialize_maybe_missing(j, "info", data.info);
deserialize_maybe_missing(j, "packages", data.packages);
deserialize_maybe_missing(j, "conda_packages", data.conda_packages);
deserialize_maybe_missing(j, "packages.conda", data.conda_packages);
deserialize_maybe_missing(j, "removed", data.removed);
}
}
1 change: 1 addition & 0 deletions libmamba/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(LIBMAMBA_TEST_SRCS
src/util/test_url_manip.cpp
src/util/test_url.cpp
# Implementation of version and matching specs
src/specs/test_archive.cpp
src/specs/test_platform.cpp
src/specs/test_version.cpp
src/specs/test_version_spec.cpp
Expand Down
74 changes: 74 additions & 0 deletions libmamba/tests/src/specs/test_archive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#include <doctest/doctest.h>

#include "mamba/specs/archive.hpp"

using namespace mamba::specs;

TEST_SUITE("specs::archive")
{
TEST_CASE("has_archive_extension")
{
for (const auto& no_ext_path : {
"",
"hello",
"soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar",
"soupsieve-2.3.2.post1-pyhd8ed1ab_0.bz2",
"/folder.tar.bz2/filename.txt",
})
{
CAPTURE(std::string_view(no_ext_path));
CHECK_FALSE(has_archive_extension(no_ext_path));
CHECK_FALSE(has_archive_extension(fs::u8path(no_ext_path)));
}

for (const auto& ext_path : {
"soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar.bz2",
"folder/soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar.bz2",
})
{
CAPTURE(std::string_view(ext_path));
CHECK(has_archive_extension(ext_path));
CHECK(has_archive_extension(fs::u8path(ext_path)));
}
}

TEST_CASE("strip_archive_extension")
{
for (const auto& no_ext_path : {
"",
"hello",
"soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar",
"soupsieve-2.3.2.post1-pyhd8ed1ab_0.bz2",
"/folder.tar.bz2/filename.txt",
})
{
CAPTURE(std::string_view(no_ext_path));
CHECK_EQ(strip_archive_extension(no_ext_path), no_ext_path);
CHECK_EQ(strip_archive_extension(fs::u8path(no_ext_path)), no_ext_path);
}

CHECK_EQ(
strip_archive_extension("soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar.bz2"),
"soupsieve-2.3.2.post1-pyhd8ed1ab_0"
);
CHECK_EQ(
strip_archive_extension("folder/soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar.bz2"),
"folder/soupsieve-2.3.2.post1-pyhd8ed1ab_0"
);

CHECK_EQ(
strip_archive_extension(fs::u8path("soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar.bz2")),
"soupsieve-2.3.2.post1-pyhd8ed1ab_0"
);
CHECK_EQ(
strip_archive_extension(fs::u8path("folder/soupsieve-2.3.2.post1-pyhd8ed1ab_0.tar.bz2")),
"folder/soupsieve-2.3.2.post1-pyhd8ed1ab_0"
);
}
}

0 comments on commit 7b23f51

Please sign in to comment.