ctkCmdLineModuleBackendLocalProcess.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. cmdLineArgs << argFlag << arg;
  85. }
  86. else
  87. {
  88. QString trimmedArg = arg.trimmed();
  89. if (trimmedArg.length() != 0) // If not string, and no arg, we don't output. We need this policy for integers, doubles, etc.
  90. {
  91. cmdLineArgs << argFlag << trimmedArg;
  92. }
  93. }
  94. } // end foreach
  95. } // end if (args.length() > 0)
  96. }
  97. }
  98. }
  99. QList<int> indexes = indexedArgs.keys();
  100. qSort(indexes.begin(), indexes.end());
  101. foreach(int index, indexes)
  102. {
  103. cmdLineArgs << indexedArgs[index];
  104. }
  105. return cmdLineArgs;
  106. }
  107. };
  108. //----------------------------------------------------------------------------
  109. ctkCmdLineModuleBackendLocalProcess::ctkCmdLineModuleBackendLocalProcess()
  110. : d(new ctkCmdLineModuleBackendLocalProcessPrivate){
  111. }
  112. //----------------------------------------------------------------------------
  113. ctkCmdLineModuleBackendLocalProcess::~ctkCmdLineModuleBackendLocalProcess()
  114. {
  115. }
  116. //----------------------------------------------------------------------------
  117. QString ctkCmdLineModuleBackendLocalProcess::name() const
  118. {
  119. return "Local Process";
  120. }
  121. //----------------------------------------------------------------------------
  122. QString ctkCmdLineModuleBackendLocalProcess::description() const
  123. {
  124. return "Runs an executable command line module using a local process.";
  125. }
  126. //----------------------------------------------------------------------------
  127. QList<QString> ctkCmdLineModuleBackendLocalProcess::schemes() const
  128. {
  129. static QList<QString> supportedSchemes = QList<QString>() << "file";
  130. return supportedSchemes;
  131. }
  132. //----------------------------------------------------------------------------
  133. qint64 ctkCmdLineModuleBackendLocalProcess::timeStamp(const QUrl &location) const
  134. {
  135. QFileInfo fileInfo(location.toLocalFile());
  136. if (fileInfo.exists())
  137. {
  138. QDateTime dateTime = fileInfo.lastModified();
  139. return ctk::msecsTo(QDateTime::fromTime_t(0), dateTime);
  140. }
  141. return 0;
  142. }
  143. //----------------------------------------------------------------------------
  144. QByteArray ctkCmdLineModuleBackendLocalProcess::rawXmlDescription(const QUrl &location)
  145. {
  146. QProcess process;
  147. process.setReadChannel(QProcess::StandardOutput);
  148. process.start(location.toLocalFile(), QStringList("--xml"));
  149. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  150. process.error() != QProcess::UnknownError)
  151. {
  152. throw ctkCmdLineModuleRunException(location, process.exitCode(), process.errorString());
  153. }
  154. process.waitForReadyRead();
  155. return process.readAllStandardOutput();
  156. }
  157. //----------------------------------------------------------------------------
  158. ctkCmdLineModuleFuture ctkCmdLineModuleBackendLocalProcess::run(ctkCmdLineModuleFrontend* frontend)
  159. {
  160. QStringList args = d->commandLineArguments(frontend->values(), frontend->moduleReference().description());
  161. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
  162. // thread pool.
  163. ctkCmdLineModuleProcessTask* moduleProcess =
  164. new ctkCmdLineModuleProcessTask(frontend->location().toLocalFile(), args);
  165. return moduleProcess->start();
  166. }