ctkCmdLineModuleBackendLocalProcess.cpp 6.4 KB

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