ctkCmdLineModuleBackendLocalProcess.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleBackendLocalProcess.h"
  16. #include "ctkCmdLineModuleDescription.h"
  17. #include "ctkCmdLineModuleFrontend.h"
  18. #include "ctkCmdLineModuleFuture.h"
  19. #include "ctkCmdLineModuleParameter.h"
  20. #include "ctkCmdLineModuleParameterGroup.h"
  21. #include "ctkCmdLineModuleProcessTask.h"
  22. #include "ctkCmdLineModuleReference.h"
  23. #include "ctkCmdLineModuleRunException.h"
  24. #include <QProcess>
  25. #include <QUrl>
  26. struct ctkCmdLineModuleBackendLocalProcessPrivate
  27. {
  28. QString normalizeFlag(const QString& flag) const
  29. {
  30. return flag.trimmed().remove(QRegExp("^-*"));
  31. }
  32. QStringList commandLineArguments(const QHash<QString,QVariant>& currentValues,
  33. const ctkCmdLineModuleDescription& description) const
  34. {
  35. QStringList cmdLineArgs;
  36. QHash<int, QString> indexedArgs;
  37. QHashIterator<QString,QVariant> valuesIter(currentValues);
  38. while(valuesIter.hasNext())
  39. {
  40. valuesIter.next();
  41. ctkCmdLineModuleParameter parameter = description.parameter(valuesIter.key());
  42. if (parameter.index() > -1)
  43. {
  44. indexedArgs.insert(parameter.index(), valuesIter.value().toString());
  45. }
  46. else
  47. {
  48. QString argFlag;
  49. if (parameter.longFlag().isEmpty())
  50. {
  51. argFlag = QString("-") + this->normalizeFlag(parameter.flag());
  52. }
  53. else
  54. {
  55. argFlag = QString("--") + this->normalizeFlag(parameter.longFlag());
  56. }
  57. QStringList args;
  58. if (parameter.multiple())
  59. {
  60. args = valuesIter.value().toString().split(',', QString::SkipEmptyParts);
  61. }
  62. else if (parameter.tag() == "boolean")
  63. {
  64. if (valuesIter.value().toBool())
  65. {
  66. cmdLineArgs << argFlag;
  67. }
  68. }
  69. else
  70. {
  71. args.push_back(valuesIter.value().toString());
  72. }
  73. foreach(QString arg, args)
  74. {
  75. cmdLineArgs << argFlag << arg;
  76. }
  77. }
  78. }
  79. QList<int> indexes = indexedArgs.keys();
  80. qSort(indexes.begin(), indexes.end());
  81. foreach(int index, indexes)
  82. {
  83. cmdLineArgs << indexedArgs[index];
  84. }
  85. return cmdLineArgs;
  86. }
  87. };
  88. ctkCmdLineModuleBackendLocalProcess::ctkCmdLineModuleBackendLocalProcess()
  89. : d(new ctkCmdLineModuleBackendLocalProcessPrivate){
  90. }
  91. ctkCmdLineModuleBackendLocalProcess::~ctkCmdLineModuleBackendLocalProcess()
  92. {
  93. }
  94. QString ctkCmdLineModuleBackendLocalProcess::name() const
  95. {
  96. return "Local Process";
  97. }
  98. QString ctkCmdLineModuleBackendLocalProcess::description() const
  99. {
  100. return "Runs an executable command line module using a local process.";
  101. }
  102. QList<QString> ctkCmdLineModuleBackendLocalProcess::schemes() const
  103. {
  104. static QList<QString> supportedSchemes = QList<QString>() << "file";
  105. return supportedSchemes;
  106. }
  107. QByteArray ctkCmdLineModuleBackendLocalProcess::rawXmlDescription(const QUrl &location)
  108. {
  109. QProcess process;
  110. process.setReadChannel(QProcess::StandardOutput);
  111. process.start(location.toLocalFile(), QStringList("--xml"));
  112. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  113. process.error() != QProcess::UnknownError)
  114. {
  115. throw ctkCmdLineModuleRunException(location, process.exitCode(), process.errorString());
  116. }
  117. process.waitForReadyRead();
  118. return process.readAllStandardOutput();
  119. }
  120. ctkCmdLineModuleFuture ctkCmdLineModuleBackendLocalProcess::run(ctkCmdLineModuleFrontend* frontend)
  121. {
  122. QStringList args = d->commandLineArguments(frontend->values(), frontend->moduleReference().description());
  123. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
  124. // thread pool.
  125. ctkCmdLineModuleProcessTask* moduleProcess =
  126. new ctkCmdLineModuleProcessTask(frontend->location().toLocalFile(), args);
  127. return moduleProcess->start();
  128. }