Utils: Add a synchronous property to QtcProcess interface

Use it to avoid IDevice interface clutter.

Change-Id: I4a04f04e2c343593d937a402060e56ef94fcabf1
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-04-29 15:57:56 +02:00
parent ba36cbfdaa
commit fe8fbf1a4a
6 changed files with 28 additions and 29 deletions

View File

@@ -763,6 +763,9 @@ void QtcProcess::start()
}
QProcess::start(command, arguments.toUnixArgs());
}
if (m_synchronous)
QProcess::waitForFinished();
}
#ifdef Q_OS_WIN
@@ -1262,6 +1265,16 @@ void QtcProcess::setupChildProcess_impl()
#endif
}
bool QtcProcess::isSynchronous() const
{
return m_synchronous;
}
void QtcProcess::setSynchronous(bool on)
{
m_synchronous = on;
}
bool QtcProcess::ArgIterator::next()
{
// We delay the setting of m_prev so we can still delete the last argument

View File

@@ -146,6 +146,9 @@ public:
static void setRemoteStartProcessHook(const std::function<void (QtcProcess &)> &hook);
bool isSynchronous() const;
void setSynchronous(bool on);
private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void setupChildProcess() override;
@@ -158,6 +161,8 @@ private:
bool m_haveEnv = false;
bool m_useCtrlCStub = false;
bool m_lowPriority = false;
bool m_synchronous = false;
};
} // namespace Utils

View File

@@ -278,6 +278,8 @@ public:
~DockerDevicePrivate() { delete m_shell; }
int runSynchronously(const CommandLine &cmd) const;
DockerDeviceData m_data;
// For local file access
@@ -542,7 +544,7 @@ bool DockerDevice::isExecutableFile(const FilePath &filePath) const
}
const QString path = filePath.toUrl().path();
const CommandLine cmd("test", {"-x", path});
const int exitCode = runSynchronously(cmd);
const int exitCode = d->runSynchronously(cmd);
return exitCode == 0;
}
@@ -558,7 +560,7 @@ bool DockerDevice::isReadableFile(const FilePath &filePath) const
}
const QString path = filePath.toUrl().path();
const CommandLine cmd("test", {"-r", path, "-a", "-f", path});
const int exitCode = runSynchronously(cmd);
const int exitCode = d->runSynchronously(cmd);
return exitCode == 0;
}
@@ -574,7 +576,7 @@ bool DockerDevice::isReadableDirectory(const FilePath &filePath) const
}
const QString path = filePath.toUrl().path();
const CommandLine cmd("test", {"-x", path, "-a", "-d", path});
const int exitCode = runSynchronously(cmd);
const int exitCode = d->runSynchronously(cmd);
return exitCode == 0;
}
@@ -590,7 +592,7 @@ bool DockerDevice::isWritableDirectory(const FilePath &filePath) const
}
const QString path = filePath.toUrl().path();
const CommandLine cmd("test", {"-x", path, "-a", "-d", path});
const int exitCode = runSynchronously(cmd);
const int exitCode = d->runSynchronously(cmd);
return exitCode == 0;
}
@@ -606,7 +608,7 @@ bool DockerDevice::createDirectory(const FilePath &filePath) const
}
const QString path = filePath.toUrl().path();
const CommandLine cmd("mkdir", {"-p", path});
const int exitCode = runSynchronously(cmd);
const int exitCode = d->runSynchronously(cmd);
return exitCode == 0;
}
@@ -651,21 +653,16 @@ void DockerDevice::runProcess(QtcProcess &process) const
process.start();
}
int DockerDevice::runSynchronously(const CommandLine &cmd, QByteArray *out, QByteArray *err) const
int DockerDevicePrivate::runSynchronously(const CommandLine &cmd) const
{
CommandLine dcmd{"docker", {"exec", d->m_container}};
CommandLine dcmd{"docker", {"exec", m_container}};
dcmd.addArgs(cmd);
QtcProcess proc;
proc.setCommand(dcmd);
proc.setWorkingDirectory("/tmp");
proc.setSynchronous(true);
proc.start();
proc.waitForFinished();
if (out)
*out = proc.readAllStandardOutput();
if (err)
*err = proc.readAllStandardError();
LOG("Run sync:" << dcmd.toUserOutput() << " result: " << proc.exitCode());
return proc.exitCode();

View File

@@ -84,10 +84,6 @@ public:
QByteArray fileContents(const Utils::FilePath &filePath, int limit) const override;
void runProcess(Utils::QtcProcess &process) const override;
int runSynchronously(const Utils::CommandLine &cmd,
QByteArray *out = nullptr,
QByteArray *err = nullptr) const override;
const DockerDeviceData &data() const;
void autoDetectQtVersion() const;
void autoDetectToolChains();

View File

@@ -277,15 +277,6 @@ void IDevice::runProcess(QtcProcess &process) const
QTC_CHECK(false);
}
int IDevice::runSynchronously(const CommandLine &cmd, QByteArray *out, QByteArray *err) const
{
Q_UNUSED(cmd);
Q_UNUSED(out);
Q_UNUSED(err);
QTC_CHECK(false);
return 0;
}
Environment IDevice::systemEnvironment() const
{
QTC_CHECK(false);

View File

@@ -246,9 +246,6 @@ public:
QDir::Filters filters) const;
virtual QByteArray fileContents(const Utils::FilePath &filePath, int limit) const;
virtual void runProcess(Utils::QtcProcess &process) const;
virtual int runSynchronously(const Utils::CommandLine &cmd,
QByteArray *out = nullptr,
QByteArray *err = nullptr) const;
virtual Utils::Environment systemEnvironment() const;
protected: