-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
319f055
commit 7b23f51
Showing
10 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ namespace mamba::specs | |
{ | ||
enum class Platform | ||
{ | ||
noarch, | ||
linux_32, | ||
linux_64, | ||
linux_armv6l, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
} |