summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-11-03 13:40:25 +0100
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-11-14 13:35:01 +0100
commit06d780a3d3daac3d9545755e0ef8ccff4a0a52a3 (patch)
treee3a21bc746cba1b38596caa077471a0c86542f6d /src
parente1cb494e522d352f8d06d4f210a4d1dfbf3a890c (diff)
downloadjustbuild-06d780a3d3daac3d9545755e0ef8ccff4a0a52a3.tar.gz
just-mr: Add utility getters for alternative mirrors specifications
Diffstat (limited to 'src')
-rw-r--r--src/other_tools/just_mr/TARGETS2
-rw-r--r--src/other_tools/just_mr/mirrors.hpp72
2 files changed, 73 insertions, 1 deletions
diff --git a/src/other_tools/just_mr/TARGETS b/src/other_tools/just_mr/TARGETS
index d98d2e60..c4a75aef 100644
--- a/src/other_tools/just_mr/TARGETS
+++ b/src/other_tools/just_mr/TARGETS
@@ -190,7 +190,7 @@
{ "type": ["@", "rules", "CC", "library"]
, "name": ["mirrors"]
, "hdrs": ["mirrors.hpp"]
- , "deps": [["@", "json", "", "json"]]
+ , "deps": [["@", "json", "", "json"], ["src/buildtool/logging", "logging"]]
, "stage": ["src", "other_tools", "just_mr"]
}
}
diff --git a/src/other_tools/just_mr/mirrors.hpp b/src/other_tools/just_mr/mirrors.hpp
index d59300c5..286a9f0c 100644
--- a/src/other_tools/just_mr/mirrors.hpp
+++ b/src/other_tools/just_mr/mirrors.hpp
@@ -16,8 +16,11 @@
#define INCLUDED_SRC_OTHER_TOOLS_JUST_MR_MIRRORS_HPP
#include <memory>
+#include <string>
+#include <vector>
#include "nlohmann/json.hpp"
+#include "src/buildtool/logging/logger.hpp"
struct Mirrors {
nlohmann::json local_mirrors{}; // maps URLs to list of local mirrors
@@ -26,4 +29,73 @@ struct Mirrors {
using MirrorsPtr = std::shared_ptr<Mirrors>;
+namespace MirrorsUtils {
+
+/// \brief Get the list of local mirrors for given primary URL.
+[[nodiscard]] static inline auto GetLocalMirrors(
+ MirrorsPtr const& additional_mirrors,
+ std::string const& repo_url) noexcept -> std::vector<std::string> {
+ try {
+ if (additional_mirrors->local_mirrors.contains(repo_url)) {
+ auto const& arr = additional_mirrors->local_mirrors[repo_url];
+ if (arr.is_array()) {
+ std::vector<std::string> res{};
+ res.reserve(arr.size());
+ for (auto const& [_, val] : arr.items()) {
+ if (val.is_string()) {
+ res.emplace_back(val.get<std::string>());
+ }
+ else {
+ Logger::Log(LogLevel::Warning,
+ "Retrieving additional mirrors: found "
+ "non-string list entry {}",
+ val.dump());
+ return {};
+ }
+ }
+ return res;
+ }
+ Logger::Log(
+ LogLevel::Warning,
+ "Retrieving additional mirrors: found non-list value {}",
+ arr.dump());
+ }
+ } catch (std::exception const& ex) {
+ Logger::Log(LogLevel::Warning,
+ "Retrieving additional mirrors failed with:\n{}",
+ ex.what());
+ }
+ return {};
+}
+
+/// \brief Get the list of preferred hostnames for non-local fetches.
+[[nodiscard]] static inline auto GetPreferredHostnames(
+ MirrorsPtr const& additional_mirrors) noexcept -> std::vector<std::string> {
+ try {
+ std::vector<std::string> res{};
+ res.reserve(additional_mirrors->preferred_hostnames.size());
+ for (auto const& [_, val] :
+ additional_mirrors->preferred_hostnames.items()) {
+ if (val.is_string()) {
+ res.emplace_back(val.get<std::string>());
+ }
+ else {
+ Logger::Log(LogLevel::Warning,
+ "Retrieving preferred mirrors: found non-string "
+ "list entry {}",
+ val.dump());
+ return {};
+ }
+ }
+ return res;
+ } catch (std::exception const& ex) {
+ Logger::Log(LogLevel::Warning,
+ "Retrieving preferred mirrors failed with:\n{}",
+ ex.what());
+ return {};
+ };
+}
+
+} // namespace MirrorsUtils
+
#endif // INCLUDED_SRC_OTHER_TOOLS_JUST_MR_MIRRORS_HPP