ctkCmdLineModuleBackendLocalProcess.cpp 6.8 KB

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