ctkCmdLineModuleBackendLocalProcess.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "ctkCmdLineModuleTimeoutException.h"
  25. #include "ctkUtils.h"
  26. #include <iostream>
  27. #include <QProcess>
  28. #include <QUrl>
  29. //----------------------------------------------------------------------------
  30. struct ctkCmdLineModuleBackendLocalProcessPrivate
  31. {
  32. QString normalizeFlag(const QString& flag) const
  33. {
  34. return flag.trimmed().remove(QRegExp("^-*"));
  35. }
  36. QStringList commandLineArguments(const QHash<QString,QVariant>& currentValues,
  37. const ctkCmdLineModuleDescription& description) const
  38. {
  39. QStringList cmdLineArgs;
  40. QHash<int, QString> indexedArgs;
  41. QHashIterator<QString,QVariant> valuesIter(currentValues);
  42. while(valuesIter.hasNext())
  43. {
  44. valuesIter.next();
  45. ctkCmdLineModuleParameter parameter = description.parameter(valuesIter.key());
  46. if (parameter.index() > -1)
  47. {
  48. indexedArgs.insert(parameter.index(), valuesIter.value().toString());
  49. }
  50. else
  51. {
  52. QString argFlag;
  53. if (parameter.longFlag().isEmpty())
  54. {
  55. argFlag = QString("-") + this->normalizeFlag(parameter.flag());
  56. }
  57. else
  58. {
  59. argFlag = QString("--") + this->normalizeFlag(parameter.longFlag());
  60. }
  61. if (parameter.tag() == "boolean")
  62. {
  63. if (valuesIter.value().toBool())
  64. {
  65. cmdLineArgs << argFlag;
  66. }
  67. }
  68. else
  69. {
  70. QStringList args;
  71. if (parameter.multiple())
  72. {
  73. args = valuesIter.value().toString().split(',', QString::SkipEmptyParts);
  74. }
  75. else
  76. {
  77. args.push_back(valuesIter.value().toString());
  78. }
  79. if (args.length() > 0)
  80. {
  81. foreach(QString arg, args)
  82. {
  83. if (parameter.tag() == "string")
  84. {
  85. cmdLineArgs << argFlag << arg;
  86. }
  87. else
  88. {
  89. QString trimmedArg = arg.trimmed();
  90. if (trimmedArg.length() != 0) // If not string, and no arg, we don't output. We need this policy for integers, doubles, etc.
  91. {
  92. cmdLineArgs << argFlag << trimmedArg;
  93. }
  94. }
  95. } // end foreach
  96. } // end if (args.length() > 0)
  97. }
  98. }
  99. }
  100. QList<int> indexes = indexedArgs.keys();
  101. qSort(indexes.begin(), indexes.end());
  102. foreach(int index, indexes)
  103. {
  104. cmdLineArgs << indexedArgs[index];
  105. }
  106. return cmdLineArgs;
  107. }
  108. };
  109. //----------------------------------------------------------------------------
  110. ctkCmdLineModuleBackendLocalProcess::ctkCmdLineModuleBackendLocalProcess()
  111. : d(new ctkCmdLineModuleBackendLocalProcessPrivate){
  112. }
  113. //----------------------------------------------------------------------------
  114. ctkCmdLineModuleBackendLocalProcess::~ctkCmdLineModuleBackendLocalProcess()
  115. {
  116. }
  117. //----------------------------------------------------------------------------
  118. QString ctkCmdLineModuleBackendLocalProcess::name() const
  119. {
  120. return "Local Process";
  121. }
  122. //----------------------------------------------------------------------------
  123. QString ctkCmdLineModuleBackendLocalProcess::description() const
  124. {
  125. return "Runs an executable command line module using a local process.";
  126. }
  127. //----------------------------------------------------------------------------
  128. QList<QString> ctkCmdLineModuleBackendLocalProcess::schemes() const
  129. {
  130. static QList<QString> supportedSchemes = QList<QString>() << "file";
  131. return supportedSchemes;
  132. }
  133. //----------------------------------------------------------------------------
  134. qint64 ctkCmdLineModuleBackendLocalProcess::timeStamp(const QUrl &location) const
  135. {
  136. QFileInfo fileInfo(location.toLocalFile());
  137. if (fileInfo.exists())
  138. {
  139. QDateTime dateTime = fileInfo.lastModified();
  140. return ctk::msecsTo(QDateTime::fromTime_t(0), dateTime);
  141. }
  142. return 0;
  143. }
  144. //----------------------------------------------------------------------------
  145. QByteArray ctkCmdLineModuleBackendLocalProcess::rawXmlDescription(const QUrl &location, int timeout)
  146. {
  147. QProcess process;
  148. process.setReadChannel(QProcess::StandardOutput);
  149. process.start(location.toLocalFile(), QStringList("--xml"));
  150. if (!process.waitForFinished(timeout))
  151. {
  152. if (process.error() == QProcess::Timedout)
  153. {
  154. throw ctkCmdLineModuleTimeoutException(location, process.errorString());
  155. }
  156. else if (process.exitStatus() == QProcess::CrashExit ||
  157. process.error() != QProcess::UnknownError)
  158. {
  159. throw ctkCmdLineModuleRunException(location, process.exitCode(), process.errorString());
  160. }
  161. }
  162. process.waitForReadyRead();
  163. return process.readAllStandardOutput();
  164. }
  165. //----------------------------------------------------------------------------
  166. ctkCmdLineModuleFuture ctkCmdLineModuleBackendLocalProcess::run(ctkCmdLineModuleFrontend* frontend)
  167. {
  168. QStringList args = d->commandLineArguments(frontend->values(), frontend->moduleReference().description());
  169. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
  170. // thread pool.
  171. ctkCmdLineModuleProcessTask* moduleProcess =
  172. new ctkCmdLineModuleProcessTask(frontend->location().toLocalFile(), args);
  173. return moduleProcess->start();
  174. }