ctkCmdLineModuleBackendLocalProcess.cpp 6.9 KB

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