Skip to content

Commit

Permalink
Clean PackageInfo interface (#3103)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AntoinePrv committed Jan 5, 2024
1 parent f99c428 commit 53a5171
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 247 deletions.
48 changes: 24 additions & 24 deletions libmamba/include/mamba/core/package_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,17 @@
#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
{
class PackageInfo
{
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 = {};
Expand All @@ -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;
Expand All @@ -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 = {};
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;
};
} // namespace mamba

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
4 changes: 2 additions & 2 deletions libmamba/include/mamba/core/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/env_lockfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
14 changes: 7 additions & 7 deletions libmamba/src/core/package_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 << "'";
Expand Down Expand Up @@ -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.");
}
}
Expand Down Expand Up @@ -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.");
}
}
Expand Down
6 changes: 3 additions & 3 deletions libmamba/src/core/package_fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 53a5171

Please sign in to comment.