Initial commit

This commit is contained in:
2025-04-19 14:48:02 +02:00
commit d10c5c980c
18 changed files with 1180 additions and 0 deletions

96
katja/database.cpp Normal file
View File

@ -0,0 +1,96 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "katja/database.hpp"
#include <filesystem>
namespace katja
{
database_package database_package::create_database_package(const std::string& fullname)
{
std::string::const_reverse_iterator begin_iterator = std::crbegin(fullname);
std::string::const_reverse_iterator end_iterator = begin_iterator;
int minus_counter = 0;
std::string build_tag;
std::string architecture;
std::string version;
for (; begin_iterator != std::crend(fullname) && minus_counter < 3; ++begin_iterator)
{
if (*begin_iterator == '-')
{
if (minus_counter == 0)
{
build_tag = std::string(begin_iterator.base(), end_iterator.base());
}
else if (minus_counter == 1)
{
architecture = std::string(begin_iterator.base(), end_iterator.base());
}
else if (minus_counter == 2)
{
version = std::string(begin_iterator.base(), end_iterator.base());
}
end_iterator = begin_iterator + 1;
++minus_counter;
}
}
return database_package(std::string(fullname.cbegin(), end_iterator.base()),
std::move(version), std::move(architecture), std::move(build_tag));
}
database_package::database_package(std::string&& name, std::string&& version,
std::string&& architecture, std::string&& build_tag)
: name(name), version(version), architecture(architecture), build_tag(build_tag)
{
}
database_package::database_package(const std::string& fullname)
: database_package(create_database_package(fullname))
{
}
bool database_package::operator<(const database_package& that) const
{
return this->name < that.name;
}
bool database_package::operator>(const database_package& that) const
{
return this->name > that.name;
}
std::string database_package::to_string() const
{
std::string package_string;
const std::size_t total_size = this->name.size() + this->version.size()
+ this->architecture.size() + this->build_tag.size() + 3;
package_string.reserve(total_size);
package_string.append(this->name);
package_string.push_back('-');
package_string.append(this->version);
package_string.push_back('-');
package_string.append(this->architecture);
package_string.push_back('-');
package_string.append(this->build_tag);
return package_string;
}
package_database read_installed_database()
{
package_database result;
for (const auto& entry : std::filesystem::directory_iterator(katja::database))
{
database_package database_entry{ entry.path().filename() };
result.emplace(database_entry.name, database_entry);
}
return result;
}
}

27
katja/repository.cpp Normal file
View File

@ -0,0 +1,27 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "katja/repository.hpp"
namespace katja
{
std::string package_identifier::to_string() const
{
std::string identifier;
const std::size_t total_size = this->name.size() + this->version.size()
+ this->architecture.size() + this->data.size() + 3;
identifier.reserve(total_size);
identifier.append(this->name);
identifier.push_back(';');
identifier.append(this->version);
identifier.push_back(';');
identifier.append(this->architecture);
identifier.push_back(';');
identifier.append(this->data);
return identifier;
}
}

202
katja/sbo.cpp Normal file
View File

@ -0,0 +1,202 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <fstream>
#include <boost/algorithm/string.hpp>
#include "katja/sbo.hpp"
namespace katja
{
static
void search_for_slackbuilds(std::map<std::string, std::filesystem::path>& info_files,
const std::filesystem::path& directory)
{
for (const auto& entry : std::filesystem::directory_iterator(directory))
{
std::filesystem::path entry_path = entry;
std::filesystem::path info_filename = entry_path.filename();
info_filename.replace_extension(".info");
std::filesystem::path info_filepath = entry_path / info_filename;
if (std::filesystem::exists(info_filepath))
{
info_files.emplace(entry_path.filename(), info_filepath);
}
else if (std::filesystem::is_directory(entry_path))
{
search_for_slackbuilds(info_files, entry_path);
}
}
}
static
void search_for_slackbuilds(std::vector<info_file>& info_files, const std::filesystem::path& directory)
{
std::map<std::string, std::filesystem::path> info_paths;
search_for_slackbuilds(info_paths, directory);
for (const auto& [_, info_filepath] : info_paths)
{
auto slackbuild_info = read_slackbuild_info(info_filepath);
if (slackbuild_info.has_value())
{
info_files.emplace_back(std::move(slackbuild_info.value()));
}
}
}
info_file::info_file(const std::string& program_name, const std::string& version,
const std::string homepage, const std::string& email, const std::string& maintainer)
: program_name(program_name), version(version), homepage(homepage), email(email), maintainer(maintainer)
{
}
package_identifier info_file::identifier_for(const std::string& architecture)
{
return package_identifier{ this->program_name, this->version, architecture, "SBo" };
}
sbo_repository::sbo_repository(const std::filesystem::path& repository_path)
{
search_for_slackbuilds(this->info_paths, repository_path);
}
std::vector<package_identifier> sbo_repository::get_updates(const package_database& database)
{
std::vector<package_identifier> identifiers;
for (const auto& [package_name, package] : database)
{
auto looked_up_info_path = this->info_paths.find(package_name);
if (looked_up_info_path != this->info_paths.cend())
{
auto slackbuild_info = read_slackbuild_info(looked_up_info_path->second);
if (slackbuild_info.has_value() && slackbuild_info.value().version != package.version)
{
identifiers.push_back(slackbuild_info->identifier_for(package.architecture));
}
}
}
return identifiers;
}
std::vector<package_identifier> sbo_repository::search_names(const std::string& architecture,
const std::string& needle)
{
std::vector<package_identifier> identifiers;
for (const auto& [package_name, info_path] : this->info_paths)
{
if (package_name.find(needle) != std::string::npos)
{
auto slackbuild_info = read_slackbuild_info(info_path);
identifiers.push_back(slackbuild_info->identifier_for(architecture));
}
}
return identifiers;
}
static
bool trim_info_line(std::string& info_value)
{
if (boost::algorithm::ends_with(info_value, "\""))
{
info_value.pop_back();
return false;
}
else if (boost::algorithm::ends_with(info_value, "\\"))
{
info_value.pop_back();
return true;
}
return false;
}
std::optional<info_file> read_slackbuild_info(const std::filesystem::path& info_filepath)
{
std::ifstream info_stream{ info_filepath };
std::string info_line, info_variable, info_value;
std::string program_name, version, homepage, email, maintainer;
std::vector<std::string> download, md5sum, download_x86_64, md5sum_x86_64, requires;
bool continuation{ false };
while (std::getline(info_stream, info_line))
{
if (info_line.empty())
{
continue;
}
else if (!continuation)
{
auto equals_position = std::find(info_line.cbegin(), info_line.cend(), '=');
info_variable = std::string(info_line.cbegin(), equals_position);
if (equals_position == info_line.cend()
|| ++equals_position == info_line.cend() || *equals_position != '"')
{
return std::nullopt;
}
info_value = std::string(std::next(equals_position), info_line.cend());
}
else
{
info_value += info_line;
}
continuation = trim_info_line(info_value);
if (!continuation)
{
if (info_variable == "PRGNAM")
{
std::swap(program_name, info_value);
}
else if (info_variable == "VERSION")
{
std::swap(version, info_value);
}
else if (info_variable == "HOMEPAGE")
{
std::swap(homepage, info_value);
}
else if (info_variable == "EMAIL")
{
std::swap(email, info_value);
}
else if (info_variable == "MAINTAINER")
{
std::swap(maintainer, info_value);
}
else if (info_variable == "DOWNLOAD")
{
boost::split(download, info_value, boost::is_any_of(" "), boost::token_compress_on);
}
else if (info_variable == "MD5SUM")
{
boost::split(md5sum, info_value, boost::is_any_of(" "), boost::token_compress_on);
}
else if (info_variable == "DOWNLOAD_x86_64")
{
boost::split(download_x86_64, info_value, boost::is_any_of(" "), boost::token_compress_on);
}
else if (info_variable == "MD5SUM_x86_64")
{
boost::split(md5sum_x86_64, info_value, boost::is_any_of(" "), boost::token_compress_on);
}
else if (info_variable == "REQUIRES")
{
boost::split(requires, info_value, boost::is_any_of(" "), boost::token_compress_on);
}
}
}
return std::make_optional<info_file>(program_name, version, homepage, email, maintainer);
}
}