From b8688943714d1fcf583cade3557f4fa47e9d74db Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 16 Mar 2021 13:50:15 +0100 Subject: [PATCH] [libcalamares] Start a packages service for netinstall and others --- src/libcalamares/CMakeLists.txt | 9 ++++ src/libcalamares/packages/Globals.cpp | 69 +++++++++++++++++++++++++++ src/libcalamares/packages/Globals.h | 32 +++++++++++++ src/libcalamares/packages/Tests.cpp | 44 +++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 src/libcalamares/packages/Globals.cpp create mode 100644 src/libcalamares/packages/Globals.h create mode 100644 src/libcalamares/packages/Tests.cpp diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 826f0bc41..95dad0530 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -59,6 +59,9 @@ set( libSources # Network service network/Manager.cpp + # Packages service + packages/Globals.cpp + # Partition service partition/Mount.cpp partition/PartitionSize.cpp @@ -228,6 +231,12 @@ calamares_add_test( network/Tests.cpp ) +calamares_add_test( + libcalamarespackagestest + SOURCES + packages/Tests.cpp +) + calamares_add_test( libcalamarespartitiontest SOURCES diff --git a/src/libcalamares/packages/Globals.cpp b/src/libcalamares/packages/Globals.cpp new file mode 100644 index 000000000..a488e704b --- /dev/null +++ b/src/libcalamares/packages/Globals.cpp @@ -0,0 +1,69 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#include "Globals.h" + +#include "GlobalStorage.h" +#include "JobQueue.h" +#include "utils/Logger.h" + +bool +CalamaresUtils::Packages::setGSPackageAdditions( const Calamares::ModuleSystem::InstanceKey& module, + const QVariantList& installPackages, + const QVariantList& tryInstallPackages ) +{ + static const char PACKAGEOP[] = "packageOperations"; + + // Check if there's already a PACAKGEOP entry in GS, and if so we'll + // extend that one (overwriting the value in GS at the end of this method) + Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage(); + QVariantList packageOperations = gs->contains( PACKAGEOP ) ? gs->value( PACKAGEOP ).toList() : QVariantList(); + cDebug() << "Existing package operations length" << packageOperations.length(); + + const QString key = module.toString() + QStringLiteral( ";add" ); + + // Clear out existing operations for this module, going backwards: + // Sometimes we remove an item, and we don't want the index to + // fall off the end of the list. + bool somethingRemoved = false; + for ( int index = packageOperations.length() - 1; 0 <= index; index-- ) + { + const QVariantMap op = packageOperations.at( index ).toMap(); + if ( op.contains( "source" ) && op.value( "source" ).toString() == key ) + { + cDebug() << Logger::SubEntry << "Removing existing operations for" << key; + packageOperations.removeAt( index ); + somethingRemoved = true; + } + } + + if ( !installPackages.empty() ) + { + QVariantMap op; + op.insert( "install", QVariant( installPackages ) ); + op.insert( "source", key ); + packageOperations.append( op ); + cDebug() << Logger::SubEntry << installPackages.length() << "critical packages."; + } + if ( !tryInstallPackages.empty() ) + { + QVariantMap op; + op.insert( "try_install", QVariant( tryInstallPackages ) ); + op.insert( "source", key ); + packageOperations.append( op ); + cDebug() << Logger::SubEntry << tryInstallPackages.length() << "non-critical packages."; + } + + if ( somethingRemoved || !packageOperations.isEmpty() ) + { + gs->insert( PACKAGEOP, packageOperations ); + return true; + } + return false; +} diff --git a/src/libcalamares/packages/Globals.h b/src/libcalamares/packages/Globals.h new file mode 100644 index 000000000..ef9fa3801 --- /dev/null +++ b/src/libcalamares/packages/Globals.h @@ -0,0 +1,32 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#ifndef LIBCALAMARES_PACKAGES_GLOBALS_H +#define LIBCALAMARES_PACKAGES_GLOBALS_H + +#include "modulesystem/InstanceKey.h" + +namespace CalamaresUtils +{ +namespace Packages +{ +/** @brief Sets the install-packages GS keys for the given module + * + * This replaces previously-set install-packages lists for the + * given module by the two new lists. + */ +bool setGSPackageAdditions( const Calamares::ModuleSystem::InstanceKey& module, + const QVariantList& installPackages, + const QVariantList& tryInstallPackages ); +// void setGSPackageRemovals( const Calamares::ModuleSystem::InstanceKey& key, const QVariantList& removePackages ); +} // namespace Packages +} // namespace CalamaresUtils + + +#endif diff --git a/src/libcalamares/packages/Tests.cpp b/src/libcalamares/packages/Tests.cpp new file mode 100644 index 000000000..307fb3c41 --- /dev/null +++ b/src/libcalamares/packages/Tests.cpp @@ -0,0 +1,44 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#include "utils/Logger.h" + +#include "GlobalStorage.h" + +#include + +class PackagesTests : public QObject +{ + Q_OBJECT +public: + PackagesTests() {} + ~PackagesTests() override {} +private Q_SLOTS: + void initTestCase(); + + void testEmpty(); +}; + +void +PackagesTests::initTestCase() +{ + Logger::setupLogLevel( Logger::LOGDEBUG ); +} + +void +PackagesTests::testEmpty() +{ +} + + +QTEST_GUILESS_MAIN( PackagesTests ) + +#include "utils/moc-warnings.h" + +#include "Tests.moc"