ctkCmdLineModuleBackendLocalProcess.cpp 7.2 KB

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