<commit_before>#include "tarsnapclient.h"
#include "debug.h"

#include <QEventLoop>

#define DEFAULT_TIMEOUT_MS 10000

TarsnapClient::TarsnapClient(QUuid uuid) : QObject(), _uuid(uuid), _process(NULL), _requiresPassword(false)
{
}

TarsnapClient::~TarsnapClient()
{
}
QString TarsnapClient::command() const
{
    return _command;
}

void TarsnapClient::setCommand(const QString &command)
{
    _command = command;
}
QStringList TarsnapClient::arguments() const
{
    return _arguments;
}

void TarsnapClient::setArguments(const QStringList &arguments)
{
    _arguments = arguments;
}

void TarsnapClient::run()
{
    _process = new QProcess();
    _process->setProcessChannelMode( QProcess::MergedChannels );
//    connect(_process, SIGNAL(started()), this, SIGNAL(clientStarted()), Qt::QueuedConnection);
//    connect(_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readProcessOutput()), Qt::QueuedConnection);
//    connect(_process, SIGNAL(finished(int, QProcess::ExitStatus)), this,
//            SLOT(processFinished(int, QProcess::ExitStatus)), Qt::QueuedConnection);
//    connect(_process, SIGNAL(error(QProcess::ProcessError)), this,
//             SLOT(processError(QProcess::ProcessError)), Qt::QueuedConnection);

    _process->setProgram(_command);
    _process->setArguments(_arguments);
    LOG << tr("Executing [%1 %2]\n").arg(_process->program()).arg(_process->arguments().join(' '));
    _process->start();
    if(_process->waitForStarted(DEFAULT_TIMEOUT_MS))
    {
        emit clientStarted(_uuid);
    }
    else
    {
        processError();
        goto cleanup;
    }
    if(_requiresPassword)
    {
        QByteArray password( _password.toUtf8() + "\n" );
        _process->write( password.data(), password.size() );
    }
    if(_process->waitForFinished(-1))
    {
        readProcessOutput();
        processFinished();
    }
    else
    {
        processError();
        goto cleanup;
    }
cleanup:
    delete _process;
    _process = 0;
}

void TarsnapClient::killClient()
{
    if(_process->state() == QProcess::Running)
    {
        _process->terminate();
        if(false == _process->waitForFinished(1000))
            _process->kill();
    }
}

QProcess::ProcessState TarsnapClient::statusClient()
{
    return _process->state();
}

bool TarsnapClient::waitForClient()
{
    return _process->waitForFinished(-1);
}

void TarsnapClient::readProcessOutput()
{
    _processOutput.append(_process->readAll());
}

void TarsnapClient::processFinished()
{
    QString output(_processOutput);
    if(!output.isEmpty())
        LOG << tr("[%1 %2] finished with exit code %3 and output:\n%4\n").arg(_command).arg(_arguments.join(' ')).arg(_process->exitCode()).arg(output);
    else
        LOG << tr("[%1 %2] finished with exit code %3 and no output.\n").arg(_command).arg(_arguments.join(' ')).arg(_process->exitCode());
    switch (_process->exitStatus())
    {
    case QProcess::NormalExit:
        emit clientFinished(_uuid, _data, _process->exitCode(), output);
        break;
    case QProcess::CrashExit:
        processError();
        break;
    }
}

void TarsnapClient::processError()
{
    LOG << tr("Tarsnap process error %1 (%2) occured:\n%3\n").arg(_process->error()).arg(_process->errorString()).arg(QString(_processOutput));
}
QVariant TarsnapClient::data() const
{
    return _data;
}

void TarsnapClient::setData(const QVariant &data)
{
    _data = data;
}

QUuid TarsnapClient::uuid() const
{<commit_msg>Cleanup tarsnap client code.<commit_after>#include "tarsnapclient.h"
#include "debug.h"

#define DEFAULT_TIMEOUT_MS 1000

TarsnapClient::TarsnapClient(QUuid uuid) : QObject(), _uuid(uuid), _process(NULL), _requiresPassword(false)
{
}

TarsnapClient::~TarsnapClient()
{
}
QString TarsnapClient::command() const
{
    return _command;
}

void TarsnapClient::setCommand(const QString &command)
{
    _command = command;
}
QStringList TarsnapClient::arguments() const
{
    return _arguments;
}

void TarsnapClient::setArguments(const QStringList &arguments)
{
    _arguments = arguments;
}

void TarsnapClient::run()
{
    _process = new QProcess();
    _process->setProcessChannelMode(QProcess::MergedChannels);
    _process->setProgram(_command);
    _process->setArguments(_arguments);
    LOG << tr("Executing [%1 %2]\n").arg(_process->program()).arg(_process->arguments().join(' '));
    _process->start();
    if(_process->waitForStarted(DEFAULT_TIMEOUT_MS))
    {
        emit clientStarted(_uuid);
    }
    else
    {
        processError();
        goto cleanup;
    }
    if(_requiresPassword)
    {
        QByteArray password( _password.toUtf8() + "\n" );
        _process->write( password.data(), password.size() );
    }
    if(_process->waitForFinished(-1))
    {
        readProcessOutput();
        processFinished();
    }
    else
    {
        processError();
        goto cleanup;
    }
cleanup:
    delete _process;
    _process = 0;
}

void TarsnapClient::killClient()
{
    if(_process->state() == QProcess::Running)
    {
        _process->terminate();
        if(false == _process->waitForFinished(DEFAULT_TIMEOUT_MS))
            _process->kill();
    }
}

QProcess::ProcessState TarsnapClient::statusClient()
{
    return _process->state();
}

bool TarsnapClient::waitForClient()
{
    return _process->waitForFinished(-1);
}

void TarsnapClient::readProcessOutput()
{
    _processOutput.append(_process->readAll());
}

void TarsnapClient::processFinished()
{
    QString output(_processOutput);
    if(!output.isEmpty())
        LOG << tr("[%1 %2] finished with exit code %3 and output:\n%4\n").arg(_command)
               .arg(_arguments.join(' ')).arg(_process->exitCode()).arg(output);
    else
        LOG << tr("[%1 %2] finished with exit code %3 and no output.\n").arg(_command)
               .arg(_arguments.join(' ')).arg(_process->exitCode());
    switch (_process->exitStatus())
    {
    case QProcess::NormalExit:
        emit clientFinished(_uuid, _data, _process->exitCode(), output);
        break;
    case QProcess::CrashExit:
        processError();
        break;
    }
}

void TarsnapClient::processError()
{
    LOG << tr("Tarsnap process error %1 (%2) occured:\n%3\n").arg(_process->error())
           .arg(_process->errorString()).arg(QString(_processOutput));
}
QVariant TarsnapClient::data() const
{
    return _data;
}

void TarsnapClient::setData(const QVariant &data)
{
    _data = data;
}

QUuid TarsnapClient::uuid() const
{
{'fraction_non_alphanumeric': 0.15387001706219947, 'fraction_numerical': 0.006979990693345742, 'mean_word_length': 3.7551622418879056, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '881950', 'n_tokens_mistral': 2087, 'n_tokens_neox': 2037, 'n_words': 353}