ctkCmdLineModuleBackendLocalProcess.cpp 6.6 KB

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