ctkCmdLineModuleBackendLocalProcess.cpp 5.9 KB

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