mirror of https://github.com/mamba-org/mamba.git
Clean PackageInfo interface (#3103)
* Refactor PackageInfo operator== * Remove unused headers * Simplify PackageInfo interface * Move PackageInfo signature order * Change type for PackageInfo::defaulted_keys * Use nlohman::from_json for PackageInfo * Use nlohman::to_json for PackageInfo * Fallback in PackageInfo::str * Rename PackageInfo::fn > filename
This commit is contained in:
parent
f99c4288c6
commit
53a5171360
|
@ -7,11 +7,10 @@
|
|||
#ifndef MAMBA_CORE_PACKAGE_INFO
|
||||
#define MAMBA_CORE_PACKAGE_INFO
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
|
||||
namespace mamba
|
||||
{
|
||||
|
@ -19,25 +18,6 @@ namespace mamba
|
|||
{
|
||||
public:
|
||||
|
||||
using field_getter = std::function<std::string(const PackageInfo&)>;
|
||||
using compare_fun = std::function<bool(const PackageInfo&, const PackageInfo&)>;
|
||||
|
||||
static field_getter get_field_getter(std::string_view field_name);
|
||||
static compare_fun less(std::string_view member);
|
||||
static compare_fun equal(std::string_view member);
|
||||
|
||||
PackageInfo() = default;
|
||||
explicit PackageInfo(nlohmann::json&& j);
|
||||
explicit PackageInfo(std::string name);
|
||||
PackageInfo(std::string name, std::string version, std::string build_string, std::size_t build_number);
|
||||
|
||||
bool operator==(const PackageInfo& other) const;
|
||||
|
||||
nlohmann::json json_record() const;
|
||||
nlohmann::json json_signable() const;
|
||||
std::string str() const;
|
||||
std::string long_str() const;
|
||||
|
||||
std::string name = {};
|
||||
std::string version = {};
|
||||
std::string build_string = {};
|
||||
|
@ -51,7 +31,7 @@ namespace mamba
|
|||
std::string channel = {};
|
||||
std::string url = {};
|
||||
std::string subdir = {};
|
||||
std::string fn = {};
|
||||
std::string filename = {};
|
||||
std::string license = {};
|
||||
std::size_t size = 0;
|
||||
std::size_t timestamp = 0;
|
||||
|
@ -61,8 +41,28 @@ namespace mamba
|
|||
std::vector<std::string> depends = {};
|
||||
std::vector<std::string> constrains = {};
|
||||
std::string signatures = {};
|
||||
std::set<std::string> defaulted_keys = {};
|
||||
};
|
||||
} // namespace mamba
|
||||
std::vector<std::string> defaulted_keys = {};
|
||||
|
||||
PackageInfo() = default;
|
||||
explicit PackageInfo(std::string name);
|
||||
PackageInfo(std::string name, std::string version, std::string build_string, std::size_t build_number);
|
||||
|
||||
[[nodiscard]] auto json_signable() const -> nlohmann::json;
|
||||
[[nodiscard]] auto str() const -> std::string;
|
||||
[[nodiscard]] auto long_str() const -> std::string;
|
||||
|
||||
/**
|
||||
* Dynamically get a field (e.g. name, version) as a string.
|
||||
*/
|
||||
[[nodiscard]] auto field(std::string_view name) const -> std::string;
|
||||
};
|
||||
|
||||
auto operator==(const PackageInfo& lhs, const PackageInfo& rhs) -> bool;
|
||||
|
||||
auto operator!=(const PackageInfo& lhs, const PackageInfo& rhs) -> bool;
|
||||
|
||||
void to_json(nlohmann::json& j, const PackageInfo& pkg);
|
||||
|
||||
void from_json(const nlohmann::json& j, PackageInfo& pkg);
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -87,8 +87,8 @@ namespace mamba
|
|||
QueryType query_type() const;
|
||||
const std::string& query() const;
|
||||
|
||||
query_result& sort(std::string field);
|
||||
query_result& groupby(std::string field);
|
||||
query_result& sort(std::string_view field);
|
||||
query_result& groupby(std::string_view field);
|
||||
query_result& reset();
|
||||
|
||||
std::ostream& table(std::ostream&) const;
|
||||
|
|
|
@ -369,7 +369,7 @@ namespace mamba
|
|||
p.subdir = ms.channel()->platform_filters().front();
|
||||
}
|
||||
}
|
||||
p.fn = ms.filename();
|
||||
p.filename = ms.filename();
|
||||
|
||||
if (hash != std::string::npos)
|
||||
{
|
||||
|
|
|
@ -57,7 +57,7 @@ namespace mamba
|
|||
|
||||
package.info.url = package_node["url"].as<std::string>();
|
||||
const auto spec = specs::MatchSpec::parse(package.info.url);
|
||||
package.info.fn = spec.filename();
|
||||
package.info.filename = spec.filename();
|
||||
package.info.build_string = spec.build_string().str();
|
||||
if (spec.channel().has_value())
|
||||
{
|
||||
|
|
|
@ -135,15 +135,15 @@ namespace mamba
|
|||
return m_valid_tarballs[pkg];
|
||||
}
|
||||
|
||||
assert(!s.fn.empty());
|
||||
const auto pkg_name = specs::strip_archive_extension(s.fn);
|
||||
assert(!s.filename.empty());
|
||||
const auto pkg_name = specs::strip_archive_extension(s.filename);
|
||||
LOG_DEBUG << "Verify cache '" << m_path.string() << "' for package tarball '" << pkg_name
|
||||
<< "'";
|
||||
|
||||
bool valid = false;
|
||||
if (fs::exists(m_path / s.fn))
|
||||
if (fs::exists(m_path / s.filename))
|
||||
{
|
||||
fs::u8path tarball_path = m_path / s.fn;
|
||||
fs::u8path tarball_path = m_path / s.filename;
|
||||
// validate that this tarball has the right size and MD5 sum
|
||||
// we handle the case where s.size == 0 (explicit packages) or md5 is unknown
|
||||
valid = s.size == 0 || validation::file_size(tarball_path, s.size);
|
||||
|
@ -201,7 +201,7 @@ namespace mamba
|
|||
return m_valid_extracted_dir[pkg];
|
||||
}
|
||||
|
||||
auto pkg_name = specs::strip_archive_extension(s.fn);
|
||||
auto pkg_name = specs::strip_archive_extension(s.filename);
|
||||
fs::u8path extracted_dir = m_path / pkg_name;
|
||||
LOG_DEBUG << "Verify cache '" << m_path.string() << "' for package extracted directory '"
|
||||
<< pkg_name << "'";
|
||||
|
@ -440,7 +440,7 @@ namespace mamba
|
|||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR << "Cannot find tarball cache for '" << s.fn << "'";
|
||||
LOG_ERROR << "Cannot find tarball cache for '" << s.filename << "'";
|
||||
throw std::runtime_error("Package cache error.");
|
||||
}
|
||||
}
|
||||
|
@ -469,7 +469,7 @@ namespace mamba
|
|||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR << "Cannot find a valid extracted directory cache for '" << s.fn << "'";
|
||||
LOG_ERROR << "Cannot find a valid extracted directory cache for '" << s.filename << "'";
|
||||
throw std::runtime_error("Package cache error.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -308,7 +308,7 @@ namespace mamba
|
|||
|
||||
const std::string& PackageFetcher::filename() const
|
||||
{
|
||||
return m_package_info.fn;
|
||||
return m_package_info.filename;
|
||||
}
|
||||
|
||||
const std::string& PackageFetcher::url() const
|
||||
|
@ -366,11 +366,11 @@ namespace mamba
|
|||
const fs::u8path repodata_record_path = base_path / "info" / "repodata_record.json";
|
||||
const fs::u8path index_path = base_path / "info" / "index.json";
|
||||
|
||||
nlohmann::json index, solvable_json;
|
||||
nlohmann::json index;
|
||||
std::ifstream index_file = open_ifstream(index_path);
|
||||
index_file >> index;
|
||||
|
||||
solvable_json = m_package_info.json_record();
|
||||
const nlohmann::json solvable_json = m_package_info;
|
||||
index.insert(solvable_json.cbegin(), solvable_json.cend());
|
||||
|
||||
if (index.find("size") == index.end() || index["size"] == 0)
|
||||
|
|
|
@ -6,11 +6,12 @@
|
|||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <fmt/format.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "mamba/core/package_info.hpp"
|
||||
#include "mamba/specs/archive.hpp"
|
||||
|
@ -18,126 +19,6 @@
|
|||
|
||||
namespace mamba
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template <class T>
|
||||
std::string get_package_info_field(const PackageInfo&, T PackageInfo::*field);
|
||||
|
||||
template <>
|
||||
std::string
|
||||
get_package_info_field<std::string>(const PackageInfo& pkg, std::string PackageInfo::*field)
|
||||
{
|
||||
return pkg.*field;
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string
|
||||
get_package_info_field<std::size_t>(const PackageInfo& pkg, std::size_t PackageInfo::*field)
|
||||
{
|
||||
return std::to_string(pkg.*field);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
PackageInfo::field_getter build_field_getter(T PackageInfo::*field)
|
||||
{
|
||||
return std::bind(get_package_info_field<T>, std::placeholders::_1, field);
|
||||
}
|
||||
|
||||
using field_getter_map = std::map<std::string_view, PackageInfo::field_getter>;
|
||||
|
||||
field_getter_map build_field_getter_map()
|
||||
{
|
||||
field_getter_map res;
|
||||
res["name"] = build_field_getter(&PackageInfo::name);
|
||||
res["version"] = build_field_getter(&PackageInfo::version);
|
||||
res["build_string"] = build_field_getter(&PackageInfo::build_string);
|
||||
res["build_number"] = build_field_getter(&PackageInfo::build_number);
|
||||
res["noarch"] = build_field_getter(&PackageInfo::noarch);
|
||||
res["channel"] = build_field_getter(&PackageInfo::channel);
|
||||
res["url"] = build_field_getter(&PackageInfo::url);
|
||||
res["subdir"] = build_field_getter(&PackageInfo::subdir);
|
||||
res["fn"] = build_field_getter(&PackageInfo::fn);
|
||||
res["license"] = build_field_getter(&PackageInfo::license);
|
||||
res["size"] = build_field_getter(&PackageInfo::size);
|
||||
res["timestamp"] = build_field_getter(&PackageInfo::timestamp);
|
||||
return res;
|
||||
}
|
||||
|
||||
const field_getter_map& get_field_getter_map()
|
||||
{
|
||||
static field_getter_map m = build_field_getter_map();
|
||||
return m;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
PackageInfo::field_getter PackageInfo::get_field_getter(std::string_view field_name)
|
||||
{
|
||||
auto it = get_field_getter_map().find(field_name);
|
||||
if (it == get_field_getter_map().end())
|
||||
{
|
||||
throw std::runtime_error("field_getter function not found");
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
PackageInfo::compare_fun PackageInfo::less(std::string_view member)
|
||||
{
|
||||
auto getter = get_field_getter(member);
|
||||
return [getter](const PackageInfo& lhs, const PackageInfo& rhs)
|
||||
{ return getter(lhs) < getter(rhs); };
|
||||
}
|
||||
|
||||
PackageInfo::compare_fun PackageInfo::equal(std::string_view member)
|
||||
{
|
||||
auto getter = get_field_getter(member);
|
||||
return [getter](const PackageInfo& lhs, const PackageInfo& rhs)
|
||||
{ return getter(lhs) == getter(rhs); };
|
||||
}
|
||||
|
||||
PackageInfo::PackageInfo(nlohmann::json&& j)
|
||||
{
|
||||
name = j.value("name", "");
|
||||
version = j.value("version", "");
|
||||
channel = j.value("channel", "");
|
||||
url = j.value("url", "");
|
||||
subdir = j.value("subdir", "");
|
||||
fn = j.value("fn", "");
|
||||
size = j.value("size", std::size_t(0));
|
||||
timestamp = j.value("timestamp", std::size_t(0));
|
||||
if (std::string build = j.value("build", "<UNKNOWN>"); build != "<UNKNOWN>")
|
||||
{
|
||||
build_string = std::move(build);
|
||||
}
|
||||
else
|
||||
{
|
||||
build_string = j.value("build_string", "");
|
||||
}
|
||||
build_number = j.value("build_number", std::size_t(0));
|
||||
license = j.value("license", "");
|
||||
md5 = j.value("md5", "");
|
||||
sha256 = j.value("sha256", "");
|
||||
if (std::string feat = j.value("track_features", ""); !feat.empty())
|
||||
{
|
||||
// Split empty string would have an empty element
|
||||
track_features = util::split(feat, ",");
|
||||
}
|
||||
|
||||
// add the noarch type if we know it (only known for installed packages)
|
||||
if (j.contains("noarch"))
|
||||
{
|
||||
if (j["noarch"].type() == nlohmann::json::value_t::boolean)
|
||||
{
|
||||
noarch = "generic_v1";
|
||||
}
|
||||
else
|
||||
{
|
||||
noarch = j.value("noarch", "");
|
||||
}
|
||||
}
|
||||
|
||||
depends = j.value("depends", std::vector<std::string>());
|
||||
constrains = j.value("constrains", std::vector<std::string>());
|
||||
}
|
||||
|
||||
PackageInfo::PackageInfo(std::string n)
|
||||
: name(std::move(n))
|
||||
|
@ -152,84 +33,16 @@ namespace mamba
|
|||
{
|
||||
}
|
||||
|
||||
bool PackageInfo::operator==(const PackageInfo& other) const
|
||||
namespace
|
||||
{
|
||||
auto attrs = [](const PackageInfo& p)
|
||||
template <typename T, typename U>
|
||||
auto contains(const std::vector<T>& v, const U& val)
|
||||
{
|
||||
return std::tie(
|
||||
p.name,
|
||||
p.version,
|
||||
p.build_string,
|
||||
p.noarch,
|
||||
p.build_number,
|
||||
p.channel,
|
||||
p.url,
|
||||
p.subdir,
|
||||
p.fn,
|
||||
p.license,
|
||||
p.size,
|
||||
p.timestamp,
|
||||
p.md5,
|
||||
p.sha256,
|
||||
p.track_features,
|
||||
p.depends,
|
||||
p.constrains,
|
||||
p.signatures,
|
||||
p.defaulted_keys
|
||||
);
|
||||
};
|
||||
return attrs(*this) == attrs(other);
|
||||
return std::find(v.cbegin(), v.cend(), val) != v.cend();
|
||||
}
|
||||
}
|
||||
|
||||
nlohmann::json PackageInfo::json_record() const
|
||||
{
|
||||
nlohmann::json j;
|
||||
j["name"] = name;
|
||||
j["version"] = version;
|
||||
j["channel"] = channel;
|
||||
j["url"] = url;
|
||||
j["subdir"] = subdir;
|
||||
j["fn"] = fn;
|
||||
j["size"] = size;
|
||||
j["timestamp"] = timestamp;
|
||||
j["build"] = build_string;
|
||||
j["build_string"] = build_string;
|
||||
j["build_number"] = build_number;
|
||||
if (!noarch.empty())
|
||||
{
|
||||
j["noarch"] = noarch;
|
||||
}
|
||||
j["license"] = license;
|
||||
j["track_features"] = fmt::format("{}", fmt::join(track_features, ","));
|
||||
if (!md5.empty())
|
||||
{
|
||||
j["md5"] = md5;
|
||||
}
|
||||
if (!sha256.empty())
|
||||
{
|
||||
j["sha256"] = sha256;
|
||||
}
|
||||
if (depends.empty())
|
||||
{
|
||||
j["depends"] = nlohmann::json::array();
|
||||
}
|
||||
else
|
||||
{
|
||||
j["depends"] = depends;
|
||||
}
|
||||
|
||||
if (constrains.empty())
|
||||
{
|
||||
j["constrains"] = nlohmann::json::array();
|
||||
}
|
||||
else
|
||||
{
|
||||
j["constrains"] = constrains;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
nlohmann::json PackageInfo::json_signable() const
|
||||
auto PackageInfo::json_signable() const -> nlohmann::json
|
||||
{
|
||||
nlohmann::json j;
|
||||
|
||||
|
@ -252,7 +65,7 @@ namespace mamba
|
|||
// Defaulted keys to empty arrays
|
||||
if (depends.empty())
|
||||
{
|
||||
if (defaulted_keys.find("depends") == defaulted_keys.end())
|
||||
if (!contains(defaulted_keys, "depends"))
|
||||
{
|
||||
j["depends"] = nlohmann::json::array();
|
||||
}
|
||||
|
@ -263,7 +76,7 @@ namespace mamba
|
|||
}
|
||||
if (constrains.empty())
|
||||
{
|
||||
if (defaulted_keys.find("constrains") == defaulted_keys.end())
|
||||
if (!contains(defaulted_keys, "constrains"))
|
||||
{
|
||||
j["constrains"] = nlohmann::json::array();
|
||||
}
|
||||
|
@ -276,14 +89,232 @@ namespace mamba
|
|||
return j;
|
||||
}
|
||||
|
||||
std::string PackageInfo::str() const
|
||||
auto PackageInfo::str() const -> std::string
|
||||
{
|
||||
return std::string(specs::strip_archive_extension(fn));
|
||||
if (!filename.empty())
|
||||
{
|
||||
return std::string(specs::strip_archive_extension(filename));
|
||||
}
|
||||
return fmt::format("{}-{}-{}", name, version, build_string);
|
||||
}
|
||||
|
||||
std::string PackageInfo::long_str() const
|
||||
auto PackageInfo::long_str() const -> std::string
|
||||
{
|
||||
// TODO channel contains subdir right now?!
|
||||
return util::concat(channel, "::", str());
|
||||
}
|
||||
} // namespace mamba
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename Func>
|
||||
auto invoke_field_string(const PackageInfo& p, Func&& field) -> std::string
|
||||
{
|
||||
using Out = std::decay_t<std::invoke_result_t<Func, PackageInfo>>;
|
||||
|
||||
if constexpr (std::is_same_v<Out, const char*>)
|
||||
{
|
||||
return std::string{ std::invoke(field, p) };
|
||||
}
|
||||
else if constexpr (std::is_integral_v<Out> || std::is_floating_point_v<Out>)
|
||||
{
|
||||
return std::to_string(std::invoke(field, p));
|
||||
}
|
||||
else if constexpr (std::is_convertible_v<Out, std::string>)
|
||||
{
|
||||
return static_cast<std::string>(std::invoke(field, p));
|
||||
}
|
||||
else if constexpr (std::is_constructible_v<Out, std::string>)
|
||||
{
|
||||
return std::string(std::invoke(field, p));
|
||||
}
|
||||
else if constexpr (fmt::is_formattable<Out>::value)
|
||||
{
|
||||
return fmt::format("{}", std::invoke(field, p));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
auto PackageInfo::field(std::string_view field_name) const -> std::string
|
||||
{
|
||||
field_name = util::strip(field_name);
|
||||
if (field_name == "name")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::name);
|
||||
}
|
||||
if (field_name == "version")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::version);
|
||||
}
|
||||
if (field_name == "build_string")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::build_string);
|
||||
}
|
||||
if (field_name == "build_number")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::build_number);
|
||||
}
|
||||
if (field_name == "noarch")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::noarch);
|
||||
}
|
||||
if (field_name == "channel")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::channel);
|
||||
}
|
||||
if (field_name == "url")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::url);
|
||||
}
|
||||
if (field_name == "subdir")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::subdir);
|
||||
}
|
||||
if (field_name == "fn" || field_name == "filename")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::filename);
|
||||
}
|
||||
if (field_name == "license")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::license);
|
||||
}
|
||||
if (field_name == "size")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::size);
|
||||
}
|
||||
if (field_name == "timestamp")
|
||||
{
|
||||
return invoke_field_string(*this, &PackageInfo::timestamp);
|
||||
}
|
||||
throw std::invalid_argument(fmt::format(R"(Invalid field "{}")", field_name));
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
auto attrs(const PackageInfo& p)
|
||||
{
|
||||
return std::tie(
|
||||
p.name,
|
||||
p.version,
|
||||
p.build_string,
|
||||
p.noarch,
|
||||
p.build_number,
|
||||
p.channel,
|
||||
p.url,
|
||||
p.subdir,
|
||||
p.filename,
|
||||
p.license,
|
||||
p.size,
|
||||
p.timestamp,
|
||||
p.md5,
|
||||
p.sha256,
|
||||
p.track_features,
|
||||
p.depends,
|
||||
p.constrains,
|
||||
p.signatures,
|
||||
p.defaulted_keys
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
auto operator==(const PackageInfo& lhs, const PackageInfo& rhs) -> bool
|
||||
{
|
||||
return attrs(lhs) == attrs(rhs);
|
||||
}
|
||||
|
||||
auto operator!=(const PackageInfo& lhs, const PackageInfo& rhs) -> bool
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json& j, const PackageInfo& pkg)
|
||||
{
|
||||
j["name"] = pkg.name;
|
||||
j["version"] = pkg.version;
|
||||
j["channel"] = pkg.channel;
|
||||
j["url"] = pkg.url;
|
||||
j["subdir"] = pkg.subdir;
|
||||
j["fn"] = pkg.filename;
|
||||
j["size"] = pkg.size;
|
||||
j["timestamp"] = pkg.timestamp;
|
||||
j["build"] = pkg.build_string;
|
||||
j["build_string"] = pkg.build_string;
|
||||
j["build_number"] = pkg.build_number;
|
||||
if (!pkg.noarch.empty())
|
||||
{
|
||||
j["noarch"] = pkg.noarch;
|
||||
}
|
||||
j["license"] = pkg.license;
|
||||
j["track_features"] = fmt::format("{}", fmt::join(pkg.track_features, ","));
|
||||
if (!pkg.md5.empty())
|
||||
{
|
||||
j["md5"] = pkg.md5;
|
||||
}
|
||||
if (!pkg.sha256.empty())
|
||||
{
|
||||
j["sha256"] = pkg.sha256;
|
||||
}
|
||||
if (pkg.depends.empty())
|
||||
{
|
||||
j["depends"] = nlohmann::json::array();
|
||||
}
|
||||
else
|
||||
{
|
||||
j["depends"] = pkg.depends;
|
||||
}
|
||||
|
||||
if (pkg.constrains.empty())
|
||||
{
|
||||
j["constrains"] = nlohmann::json::array();
|
||||
}
|
||||
else
|
||||
{
|
||||
j["constrains"] = pkg.constrains;
|
||||
}
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json& j, PackageInfo& pkg)
|
||||
{
|
||||
pkg.name = j.value("name", "");
|
||||
pkg.version = j.value("version", "");
|
||||
pkg.channel = j.value("channel", "");
|
||||
pkg.url = j.value("url", "");
|
||||
pkg.subdir = j.value("subdir", "");
|
||||
pkg.filename = j.value("fn", "");
|
||||
pkg.size = j.value("size", std::size_t(0));
|
||||
pkg.timestamp = j.value("timestamp", std::size_t(0));
|
||||
if (std::string build = j.value("build", "<UNKNOWN>"); build != "<UNKNOWN>")
|
||||
{
|
||||
pkg.build_string = std::move(build);
|
||||
}
|
||||
else
|
||||
{
|
||||
pkg.build_string = j.value("build_string", "");
|
||||
}
|
||||
pkg.build_number = j.value("build_number", std::size_t(0));
|
||||
pkg.license = j.value("license", "");
|
||||
pkg.md5 = j.value("md5", "");
|
||||
pkg.sha256 = j.value("sha256", "");
|
||||
if (std::string feat = j.value("track_features", ""); !feat.empty())
|
||||
{
|
||||
// Split empty string would have an empty element
|
||||
pkg.track_features = util::split(feat, ",");
|
||||
}
|
||||
|
||||
// add the noarch type if we know it (only known for installed packages)
|
||||
if (j.contains("noarch"))
|
||||
{
|
||||
if (j["noarch"].type() == nlohmann::json::value_t::boolean)
|
||||
{
|
||||
pkg.noarch = "generic_v1";
|
||||
}
|
||||
else
|
||||
{
|
||||
pkg.noarch = j.value("noarch", "");
|
||||
}
|
||||
}
|
||||
|
||||
pkg.depends = j.value("depends", std::vector<std::string>());
|
||||
pkg.constrains = j.value("constrains", std::vector<std::string>());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -301,7 +301,7 @@ namespace mamba
|
|||
out.channel = s.channel();
|
||||
out.url = s.url();
|
||||
out.subdir = s.subdir();
|
||||
out.fn = s.file_name();
|
||||
out.filename = s.file_name();
|
||||
out.license = s.license();
|
||||
out.size = s.size();
|
||||
out.timestamp = s.timestamp();
|
||||
|
|
|
@ -160,7 +160,8 @@ namespace mamba
|
|||
auto infile = open_ifstream(path);
|
||||
nlohmann::json j;
|
||||
infile >> j;
|
||||
auto prec = PackageInfo(std::move(j));
|
||||
auto prec = j.get<PackageInfo>();
|
||||
|
||||
// Some versions of micromamba constructor generate repodata_record.json
|
||||
// and conda-meta json files with channel names while mamba expects
|
||||
// PackageInfo channels to be platform urls. This fixes the issue described
|
||||
|
|
|
@ -178,7 +178,7 @@ namespace mamba
|
|||
fmt::print(out, " {:<15} {} kB\n", "Size", pkg.size / 1000);
|
||||
fmt::print(out, fmtstring, "License", pkg.license);
|
||||
fmt::print(out, fmtstring, "Subdir", pkg.subdir);
|
||||
fmt::print(out, fmtstring, "File Name", pkg.fn);
|
||||
fmt::print(out, fmtstring, "File Name", pkg.filename);
|
||||
|
||||
using CondaURL = typename specs::CondaURL;
|
||||
auto url = CondaURL::parse(pkg.url);
|
||||
|
@ -527,10 +527,10 @@ namespace mamba
|
|||
return m_query;
|
||||
}
|
||||
|
||||
query_result& query_result::sort(std::string field)
|
||||
query_result& query_result::sort(std::string_view field)
|
||||
{
|
||||
auto compare_ids = [&, fun = PackageInfo::less(field)](node_id lhs, node_id rhs)
|
||||
{ return fun(m_dep_graph.node(lhs), m_dep_graph.node(rhs)); };
|
||||
auto compare_ids = [&](node_id lhs, node_id rhs)
|
||||
{ return m_dep_graph.node(lhs).field(field) < m_dep_graph.node(rhs).field(field); };
|
||||
|
||||
if (!m_ordered_pkg_id_list.empty())
|
||||
{
|
||||
|
@ -547,14 +547,13 @@ namespace mamba
|
|||
return *this;
|
||||
}
|
||||
|
||||
query_result& query_result::groupby(std::string field)
|
||||
query_result& query_result::groupby(std::string_view field)
|
||||
{
|
||||
auto fun = PackageInfo::get_field_getter(field);
|
||||
if (m_ordered_pkg_id_list.empty())
|
||||
{
|
||||
for (auto& id : m_pkg_id_list)
|
||||
{
|
||||
m_ordered_pkg_id_list[fun(m_dep_graph.node(id))].push_back(id);
|
||||
m_ordered_pkg_id_list[m_dep_graph.node(id).field(field)].push_back(id);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -564,7 +563,7 @@ namespace mamba
|
|||
{
|
||||
for (auto& id : entry.second)
|
||||
{
|
||||
std::string key = entry.first + '/' + fun(m_dep_graph.node(id));
|
||||
std::string key = entry.first + '/' + m_dep_graph.node(id).field(field);
|
||||
tmp[std::move(key)].push_back(id);
|
||||
}
|
||||
}
|
||||
|
@ -882,7 +881,7 @@ namespace mamba
|
|||
j["result"]["pkgs"] = nlohmann::json::array();
|
||||
for (size_t i = 0; i < m_pkg_id_list.size(); ++i)
|
||||
{
|
||||
auto pkg_info_json = m_dep_graph.node(m_pkg_id_list[i]).json_record();
|
||||
nlohmann::json pkg_info_json = m_dep_graph.node(m_pkg_id_list[i]);
|
||||
// We want the cannonical channel name here.
|
||||
// We do not know what is in the `channel` field so we need to make sure.
|
||||
// This is most likely legacy and should be updated on the next major release.
|
||||
|
@ -897,7 +896,7 @@ namespace mamba
|
|||
j["result"]["graph_roots"] = nlohmann::json::array();
|
||||
if (!m_dep_graph.successors(0).empty())
|
||||
{
|
||||
auto pkg_info_json = m_dep_graph.node(0).json_record();
|
||||
nlohmann::json pkg_info_json = m_dep_graph.node(0);
|
||||
// We want the cannonical channel name here.
|
||||
// We do not know what is in the `channel` field so we need to make sure.
|
||||
// This is most likely legacy and should be updated on the next major release.
|
||||
|
|
|
@ -99,7 +99,7 @@ namespace mamba
|
|||
solv.set_channel(pkg.channel);
|
||||
solv.set_url(pkg.url);
|
||||
solv.set_subdir(pkg.subdir);
|
||||
solv.set_file_name(pkg.fn);
|
||||
solv.set_file_name(pkg.filename);
|
||||
solv.set_license(pkg.license);
|
||||
solv.set_size(pkg.size);
|
||||
// TODO conda timestamp are not Unix timestamp.
|
||||
|
|
|
@ -46,6 +46,8 @@ extern "C" // Incomplete header
|
|||
|
||||
namespace mamba
|
||||
{
|
||||
namespace nl = nlohmann;
|
||||
|
||||
namespace
|
||||
{
|
||||
bool need_pkg_download(const PackageInfo& pkg_info, MultiPackageCache& caches)
|
||||
|
@ -84,7 +86,7 @@ namespace mamba
|
|||
p.subdir = ms.channel()->platform_filters().front();
|
||||
}
|
||||
}
|
||||
p.fn = ms.filename();
|
||||
p.filename = ms.filename();
|
||||
p.md5 = ms.md5();
|
||||
p.sha256 = ms.sha256();
|
||||
}
|
||||
|
@ -766,7 +768,7 @@ namespace mamba
|
|||
m_solution.actions,
|
||||
[&](const auto& pkg)
|
||||
{
|
||||
to_remove_structured.emplace_back(pkg.channel, pkg.fn); //
|
||||
to_remove_structured.emplace_back(pkg.channel, pkg.filename); //
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -776,7 +778,7 @@ namespace mamba
|
|||
m_solution.actions,
|
||||
[&](const auto& pkg)
|
||||
{
|
||||
to_install_structured.emplace_back(pkg.channel, pkg.fn, pkg.json_record().dump(4)); //
|
||||
to_install_structured.emplace_back(pkg.channel, pkg.filename, nl::json(pkg).dump(4)); //
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -789,7 +791,7 @@ namespace mamba
|
|||
|
||||
void MTransaction::log_json()
|
||||
{
|
||||
std::vector<nlohmann::json> to_fetch, to_link, to_unlink;
|
||||
std::vector<nl::json> to_fetch, to_link, to_unlink;
|
||||
|
||||
for_each_to_install(
|
||||
m_solution.actions,
|
||||
|
@ -797,9 +799,9 @@ namespace mamba
|
|||
{
|
||||
if (need_pkg_download(pkg, m_multi_cache))
|
||||
{
|
||||
to_fetch.push_back(pkg.json_record());
|
||||
to_fetch.push_back(nl::json(pkg));
|
||||
}
|
||||
to_link.push_back(pkg.json_record());
|
||||
to_link.push_back(nl::json(pkg));
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -807,7 +809,7 @@ namespace mamba
|
|||
m_solution.actions,
|
||||
[&](const auto& pkg)
|
||||
{
|
||||
to_unlink.push_back(pkg.json_record()); //
|
||||
to_unlink.push_back(nl::json(pkg)); //
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -816,7 +818,7 @@ namespace mamba
|
|||
if (!jlist.empty())
|
||||
{
|
||||
Console::instance().json_down(s);
|
||||
for (nlohmann::json j : jlist)
|
||||
for (nl::json j : jlist)
|
||||
{
|
||||
Console::instance().json_append(j);
|
||||
}
|
||||
|
@ -1245,7 +1247,7 @@ namespace mamba
|
|||
{
|
||||
if (str == "explicit_specs")
|
||||
{
|
||||
chan_name = s.fn;
|
||||
chan_name = s.filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -159,7 +159,7 @@ namespace mamba
|
|||
res.channel = "@";
|
||||
res.subdir = subdir;
|
||||
res.md5 = "12345678901234567890123456789012";
|
||||
res.fn = name;
|
||||
res.filename = name;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
|
@ -123,7 +123,7 @@ namespace
|
|||
for (const auto& pkg : packages)
|
||||
{
|
||||
auto fname = fmt::format("{}-{}-{}.tar.bz2", pkg.name, pkg.version, pkg.build_string);
|
||||
packages_j[std::move(fname)] = pkg.json_record();
|
||||
packages_j[std::move(fname)] = nl::json(pkg);
|
||||
}
|
||||
auto repodata_j = nl::json::object();
|
||||
repodata_j["packages"] = std::move(packages_j);
|
||||
|
|
|
@ -43,7 +43,7 @@ namespace mamba
|
|||
CHECK_EQ(pkg.channel, "@");
|
||||
CHECK_EQ(pkg.subdir, context.platform);
|
||||
CHECK_EQ(pkg.md5, "12345678901234567890123456789012");
|
||||
CHECK_EQ(pkg.fn, pkg.name);
|
||||
CHECK_EQ(pkg.filename, pkg.name);
|
||||
}
|
||||
|
||||
TEST_CASE("dist_packages")
|
||||
|
|
|
@ -1048,7 +1048,7 @@ bind_submodule_impl(pybind11::module_ m)
|
|||
.def_readwrite("channel", &PackageInfo::channel)
|
||||
.def_readwrite("url", &PackageInfo::url)
|
||||
.def_readwrite("subdir", &PackageInfo::subdir)
|
||||
.def_readwrite("fn", &PackageInfo::fn)
|
||||
.def_readwrite("fn", &PackageInfo::filename)
|
||||
.def_readwrite("license", &PackageInfo::license)
|
||||
.def_readwrite("size", &PackageInfo::size)
|
||||
.def_readwrite("timestamp", &PackageInfo::timestamp)
|
||||
|
|
|
@ -105,9 +105,9 @@ construct(Configuration& config, const fs::u8path& prefix, bool extract_conda_pk
|
|||
|
||||
for (const auto& pkg_info : package_details)
|
||||
{
|
||||
fs::u8path entry = pkgs_dir / pkg_info.fn;
|
||||
LOG_TRACE << "Extracting " << pkg_info.fn << std::endl;
|
||||
std::cout << "Extracting " << pkg_info.fn << std::endl;
|
||||
fs::u8path entry = pkgs_dir / pkg_info.filename;
|
||||
LOG_TRACE << "Extracting " << pkg_info.filename << std::endl;
|
||||
std::cout << "Extracting " << pkg_info.filename << std::endl;
|
||||
|
||||
fs::u8path base_path = extract(entry, ExtractOptions::from_context(config.context()));
|
||||
|
||||
|
@ -115,9 +115,9 @@ construct(Configuration& config, const fs::u8path& prefix, bool extract_conda_pk
|
|||
fs::u8path index_path = base_path / "info" / "index.json";
|
||||
|
||||
std::string channel_url;
|
||||
if (pkg_info.url.size() > pkg_info.fn.size())
|
||||
if (pkg_info.url.size() > pkg_info.filename.size())
|
||||
{
|
||||
channel_url = pkg_info.url.substr(0, pkg_info.url.size() - pkg_info.fn.size());
|
||||
channel_url = pkg_info.url.substr(0, pkg_info.url.size() - pkg_info.filename.size());
|
||||
}
|
||||
std::string repodata_cache_name = util::concat(cache_name_from_url(channel_url), ".json");
|
||||
fs::u8path repodata_location = pkgs_dir / "cache" / repodata_cache_name;
|
||||
|
@ -133,7 +133,7 @@ construct(Configuration& config, const fs::u8path& prefix, bool extract_conda_pk
|
|||
repodatas[repodata_cache_name] = j;
|
||||
}
|
||||
auto& j = repodatas[repodata_cache_name];
|
||||
repodata_record = find_package(j, pkg_info.fn);
|
||||
repodata_record = find_package(j, pkg_info.filename);
|
||||
}
|
||||
|
||||
nlohmann::json index;
|
||||
|
@ -162,7 +162,7 @@ construct(Configuration& config, const fs::u8path& prefix, bool extract_conda_pk
|
|||
}
|
||||
}
|
||||
|
||||
repodata_record["fn"] = pkg_info.fn;
|
||||
repodata_record["fn"] = pkg_info.filename;
|
||||
repodata_record["url"] = pkg_info.url;
|
||||
repodata_record["channel"] = pkg_info.channel;
|
||||
|
||||
|
|
Loading…
Reference in New Issue