summaryrefslogtreecommitdiffstats
path: root/src/b2qt-flashing-wizard/scriptwriter.cpp
blob: 59996ed3f17c1726a46487bba7b116565dcd5911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use the contact form at
** http://qt.digia.com/
**
** This file is part of Qt Enterprise Embedded.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** the contact form at http://qt.digia.com/
**
****************************************************************************/

#include "scriptwriter.h"
#include <QPair>
#include <QStringList>
#include <QFileInfo>
#include <QDebug>
#include <QTimer>
#include <QProcessEnvironment>
#include <QElapsedTimer>
#include <sys/stat.h>
#include "elevator.h"
#include "common.h"

ScriptWriter::ScriptWriter(QObject *parent)
    : Actor(parent)
    , mElapsed(new QElapsedTimer)
    , mDebug(false)
{
    mProcess.setProcessChannelMode(QProcess::MergedChannels);
    mDebug = qEnvironmentVariableIsSet("DEBUG");
}

ScriptWriter::~ScriptWriter()
{
    delete mElapsed;
}

void ScriptWriter::setScriptFile(const QString &fileName)
{
    mScriptName = fileName;
}

bool ScriptWriter::ready(QString &error) const
{
    if (mScriptName.isEmpty()) {
        error = "File name is empty";
        return false;
    }

    QFileInfo file(mScriptName);
    if (!file.exists()) {
        error = "File does not exist: " + mScriptName;
        return false;
    }
    if (!file.isFile()) {
        error = "File " + mScriptName + " is not a file";
        return false;
    }
    if (!file.isReadable()) {
        error = "File is not readable";
        return false;
    }
    if (!file.isExecutable()) {
        error = "File is not executable";
        return false;
    }
    if (file.size() == 0) {
        error = "File is empty";
        return false;
    }

    return true;
}

void ScriptWriter::start()
{
    connect(&mProcess, &QProcess::readyReadStandardOutput, this, &ScriptWriter::readOutput);
    connect(&mProcess, (void (QProcess::*)(QProcess::ProcessError))&QProcess::error, this, &ScriptWriter::processError);
    connect(&mProcess, (void (QProcess::*)(int, QProcess::ExitStatus))&QProcess::finished, this, &ScriptWriter::processFinished);

    // Due to some random convenience output in the deploy scripts "set -x" has to be used to synchronize
    QStringList args = elevate() << "/bin/sh" << "-x" << mScriptName << mAdditionalArgs;
    qDebug() << "Executing" << args;

/*    QProcessEnvironment pe ;
    QString var = qgetenv("XDG_SESSION_COOKIE");
    pe.insert("XDG_SESSION_COOKIE", var);
    var = qgetenv("HOME");
    pe.insert("HOME", var);
    mProcess.setProcessEnvironment(pe);
    */
    mProcess.setProcessEnvironment(QProcessEnvironment::systemEnvironment());

    mProcess.start(args.takeFirst(), args);
    if (!mProcess.waitForStarted())
        qFatal("Failed to start script");
    mElapsed->start();
    mProcess.write("y\n");
}

void ScriptWriter::readOutput()
{
    QByteArray ba = mProcess.readAllStandardOutput();
    QList<QByteArray> baList = ba.split('\n');

    static unsigned int currentProgress = 0;
    unsigned int nextProgress = 0;
    unsigned int milliseconds = 0;

    unsigned int whole = 0;
    if (!mTimings.isEmpty())
        whole = mTimings.last().second;

    foreach (const QByteArray &line, baList) {
        if (line.startsWith("+")) {
            // Command line
            while (!mTimings.isEmpty() && !mTimings.first().first)
                mTimings.removeFirst();

            // Should not happen
            if (mTimings.isEmpty())
                break;

            currentProgress = mTimings.takeFirst().second;
        } else {
            // Output line
            if (!mTimings.isEmpty() && !mTimings.first().first)
                currentProgress = mTimings.takeFirst().second;
        }
    }

    if (!mTimings.isEmpty()) {
        nextProgress = mTimings.first().second;
        milliseconds = mTimings.last().second / 100 * (nextProgress - currentProgress);
    } else {
        nextProgress = currentProgress;
    }

    double slowness = ((double)currentProgress / mElapsed->elapsed()) * (whole/100.0);
//    if (slowness == 0)
        slowness = 1;

    if (mDebug) {
        qDebug() << ba;
        qDebug() << "slowness:" << slowness << "currentProgress:" << currentProgress << "elapsed:" << mElapsed->elapsed() << "milliseconds:" << milliseconds << "nextProgress:" << nextProgress << "whole:" << whole << "stack:" << mTimings.size();
    }

    if (currentProgress > 100)
        currentProgress = 100;
    if (nextProgress > 100)
        nextProgress = 100;

    if (currentProgress || nextProgress)
        emit progress(currentProgress, nextProgress, milliseconds/slowness);

    emit details(ba);
}

void ScriptWriter::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    if (exitStatus != QProcess::NormalExit) {
        qWarning("process crashed");
        return;
    }

    if (exitCode != 0) {
        qWarning("process failed");
        return;
    }
    emit finished();
}

void ScriptWriter::processError(QProcess::ProcessError error)
{
    qWarning() << "processError" << error;
}

void ScriptWriter::setEnvironment(const QString &key, const QString &value)
{
    QProcessEnvironment env = mProcess.processEnvironment();
    env.insert(key, value);
    mProcess.setProcessEnvironment(env);
}

void ScriptWriter::setProgressFile(const QString &fileName)
{
    mTimings.clear();

    QFile f(fileName);
    if (!f.open(QFile::ReadOnly)) {
        qWarning() << "Could not open progress file";
        return;
    }

    bool ok;

    while (!f.atEnd()) {
        QByteArray line = f.readLine().simplified();

        int i = line.toInt(&ok);
        if (!ok) {
            qWarning() << "Could not parse progress file:" << line;
            mTimings.clear();
            return;
        }
        if (i < 0)
            mTimings += qMakePair(true, -i);
        else if (i == 0)
            mTimings += qMakePair(line.startsWith('-'), 0);
        else
            mTimings += qMakePair(false, i);
    }

    qDebug() << "steps" << mTimings.size();
}

void ScriptWriter::setAdditionalArgs(const QStringList &args)
{
    mAdditionalArgs = args;
}