mirror of https://github.com/cutefishos/calamares
commit
5ed1dff655
@ -0,0 +1,68 @@
|
||||
/* === This file is part of Calamares - <https://calamares.io> ===
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2021 Adriaan de Groot <groot@kde.org>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Calamares is Free Software: see the License-Identifier above.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Globals.h"
|
||||
|
||||
#include "GlobalStorage.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
bool
|
||||
CalamaresUtils::Packages::setGSPackageAdditions( Calamares::GlobalStorage* gs,
|
||||
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)
|
||||
QVariantList packageOperations = gs->contains( PACKAGEOP ) ? gs->value( PACKAGEOP ).toList() : QVariantList();
|
||||
cDebug() << "Existing package operations length" << packageOperations.length();
|
||||
|
||||
const QString key = module.toString();
|
||||
|
||||
// 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;
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/* === This file is part of Calamares - <https://calamares.io> ===
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2021 Adriaan de Groot <groot@kde.org>
|
||||
* 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 "GlobalStorage.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.
|
||||
*
|
||||
* Returns @c true if anything was changed, @c false otherwise.
|
||||
*/
|
||||
bool setGSPackageAdditions( Calamares::GlobalStorage* gs,
|
||||
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
|
@ -0,0 +1,88 @@
|
||||
/* === This file is part of Calamares - <https://calamares.io> ===
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2021 Adriaan de Groot <groot@kde.org>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Calamares is Free Software: see the License-Identifier above.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Globals.h"
|
||||
|
||||
#include "GlobalStorage.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
class PackagesTests : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
PackagesTests() {}
|
||||
~PackagesTests() override {}
|
||||
private Q_SLOTS:
|
||||
void initTestCase();
|
||||
|
||||
void testEmpty();
|
||||
void testAdd();
|
||||
};
|
||||
|
||||
void
|
||||
PackagesTests::initTestCase()
|
||||
{
|
||||
Logger::setupLogLevel( Logger::LOGDEBUG );
|
||||
}
|
||||
|
||||
void
|
||||
PackagesTests::testEmpty()
|
||||
{
|
||||
Calamares::GlobalStorage gs;
|
||||
const QString topKey( "packageOperations" );
|
||||
Calamares::ModuleSystem::InstanceKey k( "this", "that" );
|
||||
|
||||
QVERIFY( !gs.contains( topKey ) );
|
||||
QCOMPARE( k.toString(), "this@that" );
|
||||
|
||||
// Adding nothing at all does nothing
|
||||
QVERIFY( !CalamaresUtils::Packages::setGSPackageAdditions( &gs, k, QVariantList(), QVariantList() ) );
|
||||
QVERIFY( !gs.contains( topKey ) );
|
||||
}
|
||||
|
||||
void
|
||||
PackagesTests::testAdd()
|
||||
{
|
||||
Calamares::GlobalStorage gs;
|
||||
const QString topKey( "packageOperations" );
|
||||
Calamares::ModuleSystem::InstanceKey k( "this", "that" );
|
||||
|
||||
QVERIFY( !gs.contains( topKey ) );
|
||||
QVERIFY(
|
||||
CalamaresUtils::Packages::setGSPackageAdditions( &gs, k, QVariantList { QString( "vim" ) }, QVariantList() ) );
|
||||
QVERIFY( gs.contains( topKey ) );
|
||||
auto actionList = gs.value( topKey ).toList();
|
||||
QCOMPARE( actionList.length(), 1 );
|
||||
auto action = actionList[ 0 ].toMap();
|
||||
QVERIFY( action.contains( "install" ) );
|
||||
auto op = action[ "install" ].toList();
|
||||
QCOMPARE( op.length(), 1 );
|
||||
cDebug() << op;
|
||||
|
||||
QVERIFY( CalamaresUtils::Packages::setGSPackageAdditions(
|
||||
&gs, k, QVariantList { QString( "vim" ), QString( "emacs" ) }, QVariantList() ) );
|
||||
QVERIFY( gs.contains( topKey ) );
|
||||
actionList = gs.value( topKey ).toList();
|
||||
QCOMPARE( actionList.length(), 1 );
|
||||
action = actionList[ 0 ].toMap();
|
||||
QVERIFY( action.contains( "install" ) );
|
||||
op = action[ "install" ].toList();
|
||||
QCOMPARE( op.length(), 2 );
|
||||
QCOMPARE( action[ "source" ].toString(), k.toString() );
|
||||
cDebug() << op;
|
||||
}
|
||||
|
||||
|
||||
QTEST_GUILESS_MAIN( PackagesTests )
|
||||
|
||||
#include "utils/moc-warnings.h"
|
||||
|
||||
#include "Tests.moc"
|
@ -0,0 +1,194 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016 Luca Giambonini <almack@chakraos.org>
|
||||
* SPDX-FileCopyrightText: 2016 Lisa Vitolo <shainer@chakraos.org>
|
||||
* SPDX-FileCopyrightText: 2017 Kyle Robbertze <krobbertze@gmail.com>
|
||||
* SPDX-FileCopyrightText: 2017-2018 2020, Adriaan de Groot <groot@kde.org>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Calamares is Free Software: see the License-Identifier above.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "LoaderQueue.h"
|
||||
|
||||
#include "Config.h"
|
||||
#include "network/Manager.h"
|
||||
#include "utils/Logger.h"
|
||||
#include "utils/RAII.h"
|
||||
#include "utils/Yaml.h"
|
||||
|
||||
#include <QNetworkReply>
|
||||
#include <QTimer>
|
||||
|
||||
/** @brief Call fetchNext() on the queue if it can
|
||||
*
|
||||
* On destruction, a new call to fetchNext() is queued, so that
|
||||
* the queue continues loading. Calling release() before the
|
||||
* destructor skips the fetchNext(), ending the queue-loading.
|
||||
*/
|
||||
class FetchNextUnless
|
||||
{
|
||||
public:
|
||||
FetchNextUnless( LoaderQueue* q )
|
||||
: m_q( q )
|
||||
{
|
||||
}
|
||||
~FetchNextUnless()
|
||||
{
|
||||
if ( m_q )
|
||||
{
|
||||
QMetaObject::invokeMethod( m_q, "fetchNext", Qt::QueuedConnection );
|
||||
}
|
||||
}
|
||||
void release() { m_q = nullptr; }
|
||||
|
||||
private:
|
||||
LoaderQueue* m_q = nullptr;
|
||||
};
|
||||
|
||||
SourceItem
|
||||
SourceItem::makeSourceItem( const QString& groupsUrl, const QVariantMap& configurationMap )
|
||||
{
|
||||
if ( groupsUrl == QStringLiteral( "local" ) )
|
||||
{
|
||||
return SourceItem { QUrl(), configurationMap.value( "groups" ).toList() };
|
||||
}
|
||||
else
|
||||
{
|
||||
return SourceItem { QUrl { groupsUrl }, QVariantList() };
|
||||
}
|
||||
}
|
||||
|
||||
LoaderQueue::LoaderQueue( Config* parent )
|
||||
: QObject( parent )
|
||||
, m_config( parent )
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
LoaderQueue::append( SourceItem&& i )
|
||||
{
|
||||
m_queue.append( std::move( i ) );
|
||||
}
|
||||
|
||||
void
|
||||
LoaderQueue::load()
|
||||
{
|
||||
QMetaObject::invokeMethod( this, "fetchNext", Qt::QueuedConnection );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
LoaderQueue::fetchNext()
|
||||
{
|
||||
if ( m_queue.isEmpty() )
|
||||
{
|
||||
m_config->setStatus( Config::Status::FailedBadData );
|
||||
emit done();
|
||||
return;
|
||||
}
|
||||
|
||||
auto source = m_queue.takeFirst();
|
||||
if ( source.isLocal() )
|
||||
{
|
||||
m_config->loadGroupList( source.data );
|
||||
emit done();
|
||||
}
|
||||
else
|
||||
{
|
||||
fetch( source.url );
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
LoaderQueue::fetch( const QUrl& url )
|
||||
{
|
||||
FetchNextUnless next( this );
|
||||
|
||||
if ( !url.isValid() )
|
||||
{
|
||||
m_config->setStatus( Config::Status::FailedBadConfiguration );
|
||||
cDebug() << "Invalid URL" << url;
|
||||
return;
|
||||
}
|
||||
|
||||
using namespace CalamaresUtils::Network;
|
||||
|
||||
cDebug() << "NetInstall loading groups from" << url;
|
||||
QNetworkReply* reply = Manager::instance().asynchronousGet(
|
||||
url,
|
||||
RequestOptions( RequestOptions::FakeUserAgent | RequestOptions::FollowRedirect, std::chrono::seconds( 30 ) ) );
|
||||
|
||||
if ( !reply )
|
||||
{
|
||||
cDebug() << Logger::SubEntry << "Request failed immediately.";
|
||||
// If nobody sets a different status, this will remain
|
||||
m_config->setStatus( Config::Status::FailedBadConfiguration );
|
||||
}
|
||||
else
|
||||
{
|
||||
// When the network request is done, **then** we might
|
||||
// do the next item from the queue, so don't call fetchNext() now.
|
||||
next.release();
|
||||
m_reply = reply;
|
||||
connect( reply, &QNetworkReply::finished, this, &LoaderQueue::dataArrived );
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
LoaderQueue::dataArrived()
|
||||
{
|
||||
FetchNextUnless finished( this );
|
||||
|
||||
if ( !m_reply || !m_reply->isFinished() )
|
||||
{
|
||||
cWarning() << "NetInstall data called too early.";
|
||||
m_config->setStatus( Config::Status::FailedInternalError );
|
||||
return;
|
||||
}
|
||||
|
||||
cDebug() << "NetInstall group data received" << m_reply->size() << "bytes from" << m_reply->url();
|
||||
|
||||
cqDeleter< QNetworkReply > d { m_reply };
|
||||
|
||||
// If m_required is *false* then we still say we're ready
|
||||
// even if the reply is corrupt or missing.
|
||||
if ( m_reply->error() != QNetworkReply::NoError )
|
||||
{
|
||||
cWarning() << "unable to fetch netinstall package lists.";
|
||||
cDebug() << Logger::SubEntry << "Netinstall reply error: " << m_reply->error();
|
||||
cDebug() << Logger::SubEntry << "Request for url: " << m_reply->url().toString()
|
||||
<< " failed with: " << m_reply->errorString();
|
||||
m_config->setStatus( Config::Status::FailedNetworkError );
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray yamlData = m_reply->readAll();
|
||||
try
|
||||
{
|
||||
YAML::Node groups = YAML::Load( yamlData.constData() );
|
||||
|
||||
if ( groups.IsSequence() )
|
||||
{
|
||||
finished.release();
|
||||
m_config->loadGroupList( CalamaresUtils::yamlSequenceToVariant( groups ) );
|
||||
emit done();
|
||||
}
|
||||
else if ( groups.IsMap() )
|
||||
{
|
||||
finished.release();
|
||||
auto map = CalamaresUtils::yamlMapToVariant( groups );
|
||||
m_config->loadGroupList( map.value( "groups" ).toList() );
|
||||
emit done();
|
||||
}
|
||||
else
|
||||
{
|
||||
cWarning() << "NetInstall groups data does not form a sequence.";
|
||||
}
|
||||
}
|
||||
catch ( YAML::Exception& e )
|
||||
{
|
||||
CalamaresUtils::explainYamlException( e, yamlData, "netinstall groups data" );
|
||||
m_config->setStatus( Config::Status::FailedBadData );
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2016 Luca Giambonini <almack@chakraos.org>
|
||||
* SPDX-FileCopyrightText: 2016 Lisa Vitolo <shainer@chakraos.org>
|
||||
* SPDX-FileCopyrightText: 2017 Kyle Robbertze <krobbertze@gmail.com>
|
||||
* SPDX-FileCopyrightText: 2017-2018 2020, Adriaan de Groot <groot@kde.org>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Calamares is Free Software: see the License-Identifier above.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef NETINSTALL_LOADERQUEUE_H
|
||||
#define NETINSTALL_LOADERQUEUE_H
|
||||
|
||||
#include <QQueue>
|
||||
#include <QUrl>
|
||||
#include <QVariantList>
|
||||
|
||||
class Config;
|
||||
class QNetworkReply;
|
||||
|
||||
/** @brief Data about an entry in *groupsUrl*
|
||||
*
|
||||
* This can be a specific URL, or "local" which uses data stored
|
||||
* in the configuration file itself.
|
||||
*/
|
||||
struct SourceItem
|
||||
{
|
||||
QUrl url;
|
||||
QVariantList data;
|
||||
|
||||
bool isUrl() const { return url.isValid(); }
|
||||
bool isLocal() const { return !data.isEmpty(); }
|
||||
bool isValid() const { return isUrl() || isLocal(); }
|
||||
/** @brief Create a SourceItem
|
||||
*
|
||||
* If the @p groupsUrl is @c "local" then the *groups* key in
|
||||
* the @p configurationMap is used as the source; otherwise the
|
||||
* string is used as an actual URL.
|
||||
*/
|
||||
static SourceItem makeSourceItem( const QString& groupsUrl, const QVariantMap& configurationMap );
|
||||
};
|
||||
|
||||
/** @brief Queue of source items to load
|
||||
*
|
||||
* Queue things up by calling append() and then kick things off
|
||||
* by calling load(). This will try to load the items, in order;
|
||||
* the first one that succeeds will end the loading process.
|
||||
*
|
||||
* Signal done() is emitted when done (also when all of the items fail).
|
||||
*/
|
||||
class LoaderQueue : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
LoaderQueue( Config* parent );
|
||||
|
||||
void append( SourceItem&& i );
|
||||
int count() const { return m_queue.count(); }
|
||||
|
||||
public Q_SLOTS:
|
||||
void load();
|
||||
|
||||
void fetchNext();
|
||||
void fetch( const QUrl& url );
|
||||
void dataArrived();
|
||||
|
||||
Q_SIGNALS:
|
||||
void done();
|
||||
|
||||
private:
|
||||
QQueue< SourceItem > m_queue;
|
||||
Config* m_config = nullptr;
|
||||
QNetworkReply* m_reply = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue