Skip to content

Commit

Permalink
feat: install packages by priority
Browse files Browse the repository at this point in the history
Install the package according to the repo priority.
  • Loading branch information
ice909 committed Mar 3, 2025
1 parent 0c0c9dc commit acda3a4
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 83 deletions.
1 change: 1 addition & 0 deletions libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <QCryptographicHash>

Check warning on line 39 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCryptographicHash> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QEventLoop>

Check warning on line 40 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QEventLoop> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 41 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusReply>

Check warning on line 42 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusReply> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <charconv>

Check warning on line 44 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <charconv> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdlib>

Check warning on line 45 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstdlib> not found. Please note: Cppcheck does not need standard library headers to get proper results.
Expand Down
9 changes: 6 additions & 3 deletions libs/linglong/src/linglong/package/reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ Reference::fromPackageInfo(const api::types::v1::PackageInfoV2 &info) noexcept
utils::error::Result<Reference> Reference::create(const QString &channel,
const QString &id,
const Version &version,
const Architecture &arch) noexcept
const Architecture &arch,
const linglong::api::types::v1::Repo &repo) noexcept
try {
return Reference(channel, id, version, arch);
return Reference(channel, id, version, arch,repo);
} catch (const std::exception &e) {
LINGLONG_TRACE("create reference");
return LINGLONG_ERR("invalid reference", e);
Expand All @@ -94,11 +95,13 @@ try {
Reference::Reference(const QString &channel,
const QString &id,
const Version &version,
const Architecture &arch)
const Architecture &arch,
const linglong::api::types::v1::Repo &repo)
: channel(channel)
, id(id)
, version(version)
, arch(arch)
, repo(repo)
{
if (channel.isEmpty()) {
throw std::runtime_error("empty channel");
Expand Down
8 changes: 6 additions & 2 deletions libs/linglong/src/linglong/package/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "linglong/api/types/v1/PackageInfoV2.hpp"
#include "linglong/package/architecture.h"
#include "linglong/package/version.h"
#include "linglong/api/types/v1/Repo.hpp"

#include <QString>

Expand All @@ -22,7 +23,8 @@ class Reference final
static utils::error::Result<Reference> create(const QString &channel,
const QString &id,
const Version &version,
const Architecture &architecture) noexcept;
const Architecture &architecture,
const linglong::api::types::v1::Repo &repo = {}) noexcept;
static utils::error::Result<Reference>
fromPackageInfo(const api::types::v1::PackageInfoV2 &info) noexcept;
static QVariantMap toVariantMap(const Reference &ref) noexcept;
Expand All @@ -32,6 +34,7 @@ class Reference final
QString id;
Version version;
Architecture arch;
linglong::api::types::v1::Repo repo;

[[nodiscard]] QString toString() const noexcept;
friend bool operator!=(const Reference &lhs, const Reference &rhs) noexcept;
Expand All @@ -41,7 +44,8 @@ class Reference final
Reference(const QString &channel,
const QString &id,
const Version &version,
const Architecture &architecture);
const Architecture &architecture,
const api::types::v1::Repo &repo = {});
};

} // namespace linglong::package
Expand Down
36 changes: 28 additions & 8 deletions libs/linglong/src/linglong/package_manager/package_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "linglong/utils/transaction.h"

Check warning on line 24 in libs/linglong/src/linglong/package_manager/package_manager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/transaction.h" not found.
#include "ocppi/runtime/RunOption.hpp"

Check warning on line 25 in libs/linglong/src/linglong/package_manager/package_manager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ocppi/runtime/RunOption.hpp" not found.

#include <QDBusInterface>
#include <QDBusReply>

Check warning on line 27 in libs/linglong/src/linglong/package_manager/package_manager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusReply> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusUnixFileDescriptor>

Check warning on line 28 in libs/linglong/src/linglong/package_manager/package_manager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusUnixFileDescriptor> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>
Expand Down Expand Up @@ -1210,6 +1209,8 @@ auto PackageManager::Install(const QVariantMap &parameters) noexcept -> QVariant
auto remoteRef = *remoteRefRet;
additionalMessage.remoteRef = remoteRef.toString().toStdString();

qDebug() << "clearReference remoteRef: " << remoteRef.toString();

// 如果远程版本大于本地版本就升级,否则需要加--force降级,如果本地没有则直接安装,如果本地版本和远程版本相等就提示已安装
auto msgType = api::types::v1::InteractionMessageType::Install;
if (!additionalMessage.localRef.empty()) {
Expand All @@ -1228,12 +1229,13 @@ auto PackageManager::Install(const QVariantMap &parameters) noexcept -> QVariant
}
}

const auto defaultRepo = linglong::repo::getDefaultRepo(this->repo.getConfig());
auto refSpec = QString{ "%1:%2/%3/%4/%5" }.arg(QString::fromStdString(defaultRepo.name),
remoteRef.channel,
remoteRef.id,
remoteRef.arch.toString(),
QString::fromStdString(curModule));
auto refSpec = QString{ "%1:%2/%3/%4/%5" }.arg(
QString::fromStdString(remoteRef.repo.alias.value_or(remoteRef.repo.name)),
remoteRef.channel,
remoteRef.id,
remoteRef.arch.toString(),
QString::fromStdString(curModule));

// Note: do not capture any reference of variable which defined in this func.
// it will be a dangling reference.
auto installer = [this,
Expand Down Expand Up @@ -1316,6 +1318,9 @@ void PackageManager::Install(PackageTask &taskContext,
+ QString::fromStdString(list));
return;
}
for (const auto module : *installModules) {
qWarning() << module.c_str();
}
transaction.addRollBack([this, &newRef, installModules = *installModules]() noexcept {
auto tmp = PackageTask::createTemporaryTask();
UninstallRef(tmp, newRef, installModules);
Expand All @@ -1331,11 +1336,15 @@ void PackageManager::Install(PackageTask &taskContext,
taskContext.updateSubState(linglong::api::types::v1::SubState::PostAction,
"processing after install");

qDebug() << "\033[33m" << "merge Modules!" << "\033[0m";

auto mergeRet = this->repo.mergeModules();
if (!mergeRet) {
qCritical() << "merge modules failed: " << mergeRet.error().message();
}

qDebug() << "\033[32m" << "merge modules done" << "\033[0m";

auto layer = this->repo.getLayerItem(newRef);
if (!layer) {
taskContext.reportError(std::move(layer).error());
Expand Down Expand Up @@ -1446,6 +1455,7 @@ void PackageManager::InstallRef(PackageTask &taskContext,
}

for (const auto &module : modules) {
qWarning() << "\033[33m" << "pull module: " << module.c_str() << "\033[0m";
if (isTaskDone(taskContext.subState())) {
return;
}
Expand Down Expand Up @@ -1481,6 +1491,10 @@ void PackageManager::InstallRef(PackageTask &taskContext,
return;
}

qDebug() << "\033[33m"
<< "pull Dependency: " << info->runtime.value_or("no set runtime").c_str()
<< "\033[0m";

// Note: Do not set module by app's module here
pullDependency(taskContext, *info, "binary");
}
Expand Down Expand Up @@ -1883,23 +1897,29 @@ void PackageManager::pullDependency(PackageTask &taskContext,
{
.forceRemote = false,
.fallbackToRemote = true,
.queryOriginRepo = true,
});
if (!runtime) {
taskContext.updateState(linglong::api::types::v1::State::Failed,
runtime.error().message());
return;
}

qDebug() << "\033[33m" << "find runtime: " << runtime->toString() << " in : "
<< QString::fromStdString(runtime->repo.alias.value_or(runtime->repo.name))
<< "\033[0m";

// 如果runtime已存在,则直接使用, 否则从远程拉取
auto runtimeLayerDir = repo.getLayerDir(*runtime);
qDebug() << "\033[33m" << "runtime 是否存在:" << runtimeLayerDir.has_value() << "\033[0m";
if (!runtimeLayerDir) {
if (isTaskDone(taskContext.subState())) {
return;
}

taskContext.updateSubState(linglong::api::types::v1::SubState::InstallRuntime,
"Installing runtime " + runtime->toString());

qDebug() << runtime->repo.alias.value_or(runtime->repo.name).c_str();
this->repo.pull(taskContext, *runtime, module);
if (isTaskDone(taskContext.subState())) {
return;
Expand Down
7 changes: 7 additions & 0 deletions libs/linglong/src/linglong/repo/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,11 @@ int64_t getRepoMaxPriority(const api::types::v1::RepoConfigV2 &cfg) noexcept
return maxElement->priority;
}

void sortRepoByPriority(api::types::v1::RepoConfigV2 &cfg) noexcept
{
std::sort(cfg.repos.begin(), cfg.repos.end(),[](const auto &repo1, const auto &repo2) {
return repo1.priority > repo2.priority;
});
}

} // namespace linglong::repo
1 change: 1 addition & 0 deletions libs/linglong/src/linglong/repo/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ int64_t getRepoMaxPriority(const api::types::v1::RepoConfigV2 &cfg) noexcept;
api::types::v1::Repo getDefaultRepo(const api::types::v1::RepoConfigV2 &cfg) noexcept;

api::types::v1::RepoConfigV2 convertToV2(const api::types::v1::RepoConfig &cfg) noexcept;
void sortRepoByPriority(api::types::v1::RepoConfigV2 &cfg) noexcept;

} // namespace linglong::repo
Loading

0 comments on commit acda3a4

Please sign in to comment.