Commit 0ee6ebc2 authored by Lukas Appelhans's avatar Lukas Appelhans

Hook support is getting there

parent 22877796
......@@ -22,7 +22,7 @@ Akabei::Hook::Hook(const QString& name, const QString &content, QList<Akabei::Pa
Akabei::Hook::~Hook()
{
delete d_ptr;
}
QString Akabei::Hook::name() const
......
......@@ -27,7 +27,7 @@ class HookPrivate {
, file(0)
{}
~HookPrivate() {}
~HookPrivate() { delete file; }
QString name;
QString content;
......
......@@ -15,6 +15,7 @@
#include "akabeidatabase_p.h"
#include "akabeibackend.h"
#include "akabeidelta.h"
#include "akabeihook.h"
#include <QtCore/QFile>
#include <QtCore/QDir>
......@@ -409,13 +410,23 @@ QList< Group* > Package::groups() const
QList< Hook* > Package::retrieveHooks()
{
return QList<Hook*>();//FIXME
Q_D(Package);
if (!d->hasHooks)
return QList<Hook*>();
if (d->hookCache.isEmpty()) {
d->hookCache = d->database->queryHooks("SELECT * FROM hooks WHERE Packages LIKE \"%;" + QString::number(d->databaseId) + ";%\"");
foreach (Hook * hook, d->hookCache)
d->hooks << hook->name();
}
return d->hookCache;
}
QStringList Package::hooks()
{
Q_D(Package);
if (d->hooks.isEmpty() && d->hasHooks) {
retrieveHooks();
/*if (d->hooks.isEmpty() && d->hasHooks) {
// We should cache the files here.
if (d->database != 0 && d->databaseId >= 0) {
// Ok, query the db
......@@ -439,7 +450,7 @@ QStringList Package::hooks()
sqlite3_finalize(statement);
}
}
}*/
return d->hooks;
}
......
......@@ -72,6 +72,7 @@ public:
bool hasHooks;
QList<Group*> groups;
QList<Hook*> hookCache;
QMap<Akabei::Package::Version, Delta*> deltaCache;//FIXME: maybe switch to hash, but has the downside to have a qHash method
QString pathToArchive;
......
......@@ -10,6 +10,8 @@
#include "akabeiplainhookoperation.h"
#include "akabeiplainscriptletoperation_p.h"
#include <akabeidatabase.h>
#include <akabeiconfig.h>
#include <QMap>
......@@ -17,30 +19,20 @@
#include <QDebug>
namespace Akabei {
class Hook
{
public:
Hook(Akabei::Database* database, const QString &string)
: db(database), name(string)
{}
~Hook() {}
bool operator==(const Hook &other) const { return ((other.db == db) && other.name == name);}
Akabei::Database * db;
QString name;
};
uint qHash(const Hook &hook) { return (qHash(hook.name) ^ qHash(hook.db->name()));}
class AkabeiPlainHookOperation::Private
{
public:
Private() {}
Private(AkabeiPlainHookOperation * op) : q(op) {}
~Private() {}
//QMap<Akabei::Database*, QSet<QString> > hooks;
QSet<Hook> hooks;
void process();
void slotErrors(QList<Akabei::Error*> errors);
void slotFinished(bool fin);
void slotMessage(const QString &mes);
AkabeiPlainHookOperation *q;
QSet<Hook*> hooks;
QSet<Hook*>::iterator it;
};
class HookOpHelper
......@@ -67,7 +59,7 @@ AkabeiPlainHookOperation *AkabeiPlainHookOperation::instance()
AkabeiPlainHookOperation::AkabeiPlainHookOperation()
: QObject(0),
Operation("Hooks"),
d(new Private())
d(new Private(this))
{
Q_ASSERT(!s_globalHookOp()->q);
s_globalHookOp()->q = this;
......@@ -82,44 +74,57 @@ AkabeiPlainHookOperation::~AkabeiPlainHookOperation()
delete d;
}
void AkabeiPlainHookOperation::add(Database* db, const QString& hook)
void AkabeiPlainHookOperation::add(Hook* hook)
{
Hook h(db, hook);
d->hooks.insert(h);
qDebug() << "Add hook" << hook->name();
d->hooks.insert(hook);
}
void AkabeiPlainHookOperation::add(Database* db, const QStringList& hooks)
void AkabeiPlainHookOperation::add(const QList<Hook*> &hooks)
{
foreach (const QString &hook, hooks) //TODO: Optimize to only fetch entry from QSet once
add(db, hook);
foreach (Hook* hook, hooks)
add(hook);
}
void AkabeiPlainHookOperation::run()
{
qDebug() << "Run hookoperation";
/*QString root = Config::instance()->root();
d->it = d->hooks.begin();
d->process();
setFinished(true);
}
QDir tempRoot = Config::instance()->rootDir().absoluteFilePath("tmp");
if (!tempRoot.exists()) {
// Create
tempRoot.mkpath(tempRoot.absolutePath());
void AkabeiPlainHookOperation::Private::process()
{
if (it == hooks.end()) {
q->setFinished(true);
return;
}
QString root = Config::instance()->root();
ChrootedProcess * process = new ChrootedProcess(q, root);
q->connect(process, SIGNAL(errorsOccured(QList<Akabei::Error*>)), SLOT(slotErrors(QList<Akabei::Error*>)));
q->connect(process, SIGNAL(finished(bool)), SLOT(slotFinished(bool)));
q->connect(process, SIGNAL(addMessage(QString)), SLOT(slotMessage(QString)));
process->run(QString("sh %1").arg((*it)->path()));
}
ChrootedProcess * process = new ChrootedProcess(this, root);
connect(process, SIGNAL(errorsOccured(QList<Akabei::Error*>)), SLOT(slotErrors(QList<Akabei::Error*>)));
connect(process, SIGNAL(finished(bool)), SLOT(slotFinished(bool)));
connect(process, SIGNAL(addMessage(QString)), SLOT(slotMessage(QString)));
foreach (Akabei::Database * db, d->hooks) {
foreach (const QString &hook, d->hooks[db]) {
QTemporaryFile temp = new QTemporaryFile(tempRoot.absoluteFilePath("akabei_XXXXXX"));
temp->open();
temp->write(d->package->retrieveScriptlet().toUtf8());
temp->flush();
QString toRun = QString("sh %1").arg(tmpFile->fileName());
process->run(toRun, tmpFile);
}
}*/
setFinished(true);
void AkabeiPlainHookOperation::Private::slotErrors(QList< Error* > errors)
{
q->setErrors(errors);
}
void AkabeiPlainHookOperation::Private::slotFinished(bool fin)
{
if (fin) {
it++;
process();
}
}
void AkabeiPlainHookOperation::Private::slotMessage(const QString& mes)
{
q->addMessage(mes);
}
void AkabeiPlainHookOperation::validate()
......
......@@ -12,18 +12,20 @@
#include <akabeioperation.h>
#include <QStringList>
#include <akabeihook.h>
namespace Akabei {
class Database;
class AKABEICORESHARED_EXPORT AkabeiPlainHookOperation : public QObject, public Operation
{
Q_OBJECT
public:
virtual ~AkabeiPlainHookOperation();
static AkabeiPlainHookOperation * instance();
void add(Akabei::Database *db, const QString &hook);
void add(Akabei::Database *db, const QStringList &hooks);
void add(Hook* hook);
void add(const QList<Hook*> &hooks);
protected:
virtual void run();
......@@ -35,6 +37,10 @@ class AKABEICORESHARED_EXPORT AkabeiPlainHookOperation : public QObject, public
Private * const d;
friend class ChrootedProcess;
Q_PRIVATE_SLOT(d, void slotErrors(QList<Akabei::Error*>));
Q_PRIVATE_SLOT(d, void slotFinished(bool));
Q_PRIVATE_SLOT(d, void slotMessage(QString));
};
}
......
......@@ -260,7 +260,8 @@ void PlainInstallOperation::validate()
// Same for hooks
if (d->package->hasHooks()) {
//AkabeiPlainHookOperation::instance()->add(d->package->database(), d->package->retrieveHooks());
qDebug() << "We have hooks" << d->package->hooks();
AkabeiPlainHookOperation::instance()->add(d->package->retrieveHooks());
}
}
......
......@@ -26,7 +26,7 @@ class ChrootedProcess : public QProcess
explicit ChrootedProcess(Operation *op, const QString &chroot, QObject* parent = 0);
virtual ~ChrootedProcess();
void run(const QString &cmd, QFile * file);
void run(const QString &cmd, QFile * file = 0);
protected:
virtual void setupChildProcess();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment