ctkCmdLineModuleBackendLocalProcess.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. struct ctkCmdLineModuleBackendLocalProcessPrivate
  28. {
  29. QString normalizeFlag(const QString& flag) const
  30. {
  31. return flag.trimmed().remove(QRegExp("^-*"));
  32. }
  33. QStringList commandLineArguments(const QHash<QString,QVariant>& currentValues,
  34. const ctkCmdLineModuleDescription& description) const
  35. {
  36. QStringList cmdLineArgs;
  37. QHash<int, QString> indexedArgs;
  38. QHashIterator<QString,QVariant> valuesIter(currentValues);
  39. while(valuesIter.hasNext())
  40. {
  41. valuesIter.next();
  42. ctkCmdLineModuleParameter parameter = description.parameter(valuesIter.key());
  43. if (parameter.index() > -1)
  44. {
  45. indexedArgs.insert(parameter.index(), valuesIter.value().toString());
  46. }
  47. else
  48. {
  49. QString argFlag;
  50. if (parameter.longFlag().isEmpty())
  51. {
  52. argFlag = QString("-") + this->normalizeFlag(parameter.flag());
  53. }
  54. else
  55. {
  56. argFlag = QString("--") + this->normalizeFlag(parameter.longFlag());
  57. }
  58. QStringList args;
  59. if (parameter.multiple())
  60. {
  61. args = valuesIter.value().toString().split(',', QString::SkipEmptyParts);
  62. }
  63. else if (parameter.tag() == "boolean")
  64. {
  65. if (valuesIter.value().toBool())
  66. {
  67. cmdLineArgs << argFlag;
  68. }
  69. }
  70. else
  71. {
  72. args.push_back(valuesIter.value().toString());
  73. }
  74. foreach(QString arg, args)
  75. {
  76. cmdLineArgs << argFlag << arg;
  77. }
  78. }
  79. }
  80. QList<int> indexes = indexedArgs.keys();
  81. qSort(indexes.begin(), indexes.end());
  82. foreach(int index, indexes)
  83. {
  84. cmdLineArgs << indexedArgs[index];
  85. }
  86. return cmdLineArgs;
  87. }
  88. };
  89. ctkCmdLineModuleBackendLocalProcess::ctkCmdLineModuleBackendLocalProcess()
  90. : d(new ctkCmdLineModuleBackendLocalProcessPrivate){
  91. }
  92. ctkCmdLineModuleBackendLocalProcess::~ctkCmdLineModuleBackendLocalProcess()
  93. {
  94. }
  95. QString ctkCmdLineModuleBackendLocalProcess::name() const
  96. {
  97. return "Local Process";
  98. }
  99. QString ctkCmdLineModuleBackendLocalProcess::description() const
  100. {
  101. return "Runs an executable command line module using a local process.";
  102. }
  103. QList<QString> ctkCmdLineModuleBackendLocalProcess::schemes() const
  104. {
  105. static QList<QString> supportedSchemes = QList<QString>() << "file";
  106. return supportedSchemes;
  107. }
  108. qint64 ctkCmdLineModuleBackendLocalProcess::timeStamp(const QUrl &location) const
  109. {
  110. QFileInfo fileInfo(location.toLocalFile());
  111. if (fileInfo.exists())
  112. {
  113. QDateTime dateTime = fileInfo.lastModified();
  114. return ctk::msecsTo(QDateTime::fromTime_t(0), dateTime);
  115. }
  116. return 0;
  117. }
  118. QByteArray ctkCmdLineModuleBackendLocalProcess::rawXmlDescription(const QUrl &location)
  119. {
  120. QProcess process;
  121. process.setReadChannel(QProcess::StandardOutput);
  122. process.start(location.toLocalFile(), QStringList("--xml"));
  123. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  124. process.error() != QProcess::UnknownError)
  125. {
  126. throw ctkCmdLineModuleRunException(location, process.exitCode(), process.errorString());
  127. }
  128. process.waitForReadyRead();
  129. return process.readAllStandardOutput();
  130. }
  131. ctkCmdLineModuleFuture ctkCmdLineModuleBackendLocalProcess::run(ctkCmdLineModuleFrontend* frontend)
  132. {
  133. QStringList args = d->commandLineArguments(frontend->values(), frontend->moduleReference().description());
  134. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
  135. // thread pool.
  136. ctkCmdLineModuleProcessTask* moduleProcess =
  137. new ctkCmdLineModuleProcessTask(frontend->location().toLocalFile(), args);
  138. return moduleProcess->start();
  139. }