diff options
author | Weblate <noreply@weblate.org> | 2017-11-04 19:28:17 +0000 |
---|---|---|
committer | Weblate <noreply@weblate.org> | 2017-11-04 19:28:17 +0000 |
commit | 7787f617656751ee7e0a58c813d0bfd5757c5c97 (patch) | |
tree | 48aa053bbd4fda9c64f89a061a2f442c860ab13d /src-qt5/core/libLumina/LUtils.cpp | |
parent | Translated using Weblate (Lithuanian) (diff) | |
parent | Merge pull request #497 from a-stjohn/patch-1 (diff) | |
download | lumina-7787f617656751ee7e0a58c813d0bfd5757c5c97.tar.gz lumina-7787f617656751ee7e0a58c813d0bfd5757c5c97.tar.bz2 lumina-7787f617656751ee7e0a58c813d0bfd5757c5c97.zip |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'src-qt5/core/libLumina/LUtils.cpp')
-rw-r--r-- | src-qt5/core/libLumina/LUtils.cpp | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/src-qt5/core/libLumina/LUtils.cpp b/src-qt5/core/libLumina/LUtils.cpp index 491778ca..3d3c878a 100644 --- a/src-qt5/core/libLumina/LUtils.cpp +++ b/src-qt5/core/libLumina/LUtils.cpp @@ -14,7 +14,7 @@ #include <unistd.h> -inline QStringList ProcessRun(QString cmd, QStringList args){ +/*inline QStringList ProcessRun(QString cmd, QStringList args){ //Assemble outputs QStringList out; out << "1" << ""; //error code, string output QProcess proc; @@ -38,50 +38,58 @@ inline QStringList ProcessRun(QString cmd, QStringList args){ out[0] = QString::number(proc.exitCode()); out[1] = info+QString(proc.readAllStandardOutput()); return out; -} +}*/ + //============= // LUtils Functions //============= -int LUtils::runCmd(QString cmd, QStringList args){ - /*QProcess proc; - proc.setProcessChannelMode(QProcess::MergedChannels); - if(args.isEmpty()){ - proc.start(cmd); - }else{ - proc.start(cmd, args); +QString LUtils::runCommand(bool &success, QString command, QStringList arguments, QString workdir, QStringList env){ + QProcess proc; + proc.setProcessChannelMode(QProcess::MergedChannels); //need output + //First setup the process environment as necessary + QProcessEnvironment PE = QProcessEnvironment::systemEnvironment(); + if(!env.isEmpty()){ + for(int i=0; i<env.length(); i++){ + if(!env[i].contains("=")){ continue; } + PE.insert(env[i].section("=",0,0), env[i].section("=",1,100)); + } + } + proc.setProcessEnvironment(PE); + //if a working directory is specified, check it and use it + if(!workdir.isEmpty()){ + proc.setWorkingDirectory(workdir); } - //if(!proc.waitForStarted(30000)){ return 1; } //process never started - max wait of 30 seconds - while(!proc.waitForFinished(300)){ + //Now run the command (with any optional arguments) + if(arguments.isEmpty()){ proc.start(command); } + else{ proc.start(command, arguments); } + //Wait for the process to finish (but don't block the event loop) + QString info; + while(!proc.waitForFinished(1000)){ if(proc.state() == QProcess::NotRunning){ break; } //somehow missed the finished signal - QCoreApplication::processEvents(); + QString tmp = proc.readAllStandardOutput(); + if(tmp.isEmpty()){ proc.terminate(); } + else{ info.append(tmp); } } - int ret = proc.exitCode(); - return ret;*/ - QFuture<QStringList> future = QtConcurrent::run(ProcessRun, cmd, args); - return future.result()[0].toInt(); //turn it back into an integer return code + info.append(proc.readAllStandardOutput()); //make sure we don't miss anything in the output + success = (proc.exitCode()==0); //return success/failure + return info; +} + +int LUtils::runCmd(QString cmd, QStringList args){ + bool success; + LUtils::runCommand(success, cmd, args); + return success; + + /*QFuture<QStringList> future = QtConcurrent::run(ProcessRun, cmd, args); + return future.result()[0].toInt(); //turn it back into an integer return code*/ } QStringList LUtils::getCmdOutput(QString cmd, QStringList args){ - /*QProcess proc; - QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - env.insert("LANG", "C"); - env.insert("LC_MESSAGES", "C"); - proc.setProcessEnvironment(env); - proc.setProcessChannelMode(QProcess::MergedChannels); - if(args.isEmpty()){ - proc.start(cmd); - }else{ - proc.start(cmd,args); - } - //if(!proc.waitForStarted(30000)){ return QStringList(); } //process never started - max wait of 30 seconds - while(!proc.waitForFinished(300)){ - if(proc.state() == QProcess::NotRunning){ break; } //somehow missed the finished signal - QCoreApplication::processEvents(); - } - QStringList out = QString(proc.readAllStandardOutput()).split("\n"); - return out;*/ - QFuture<QStringList> future = QtConcurrent::run(ProcessRun, cmd, args); - return future.result()[1].split("\n"); //Split the return message into lines + bool success; + QString log = LUtils::runCommand(success, cmd, args); + return log.split("\n"); + /*QFuture<QStringList> future = QtConcurrent::run(ProcessRun, cmd, args); + return future.result()[1].split("\n"); //Split the return message into lines*/ } QStringList LUtils::readFile(QString filepath){ @@ -113,6 +121,10 @@ bool LUtils::writeFile(QString filepath, QStringList contents, bool overwrite){ } bool LUtils::isValidBinary(QString& bin){ + //Trim off any quotes + if(bin.startsWith("\"") && bin.endsWith("\"")){ bin.chop(1); bin = bin.remove(0,1); } + if(bin.startsWith("\'") && bin.endsWith("\'")){ bin.chop(1); bin = bin.remove(0,1); } + //Now look for relative/absolute path if(!bin.startsWith("/")){ //Relative path: search for it on the current "PATH" settings QStringList paths = QString(qgetenv("PATH")).split(":"); |