1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "WorkflowCMDLineTasks.h"
23 
24 #include <QFile>
25 
26 #include <U2Core/AppContext.h>
27 #include <U2Core/CMDLineCoreOptions.h>
28 #include <U2Core/CMDLineRegistry.h>
29 #include <U2Core/CMDLineUtils.h>
30 #include <U2Core/Counter.h>
31 #include <U2Core/L10n.h>
32 #include <U2Core/Log.h>
33 #include <U2Core/Settings.h>
34 
35 #include <U2Lang/URLAttribute.h>
36 #include <U2Lang/WorkflowEnv.h>
37 #include <U2Lang/WorkflowManager.h>
38 #include <U2Lang/WorkflowRunTask.h>
39 #include <U2Lang/WorkflowUtils.h>
40 
41 #include "WorkflowDesignerPlugin.h"
42 
43 #define WORKFLOW_CMDLINE_TASK_LOG_CAT "Workflow cmdline tasks"
44 
45 namespace U2 {
46 
47 /*******************************************
48  * WorkflowRunFromCMDLineBase
49  *******************************************/
WorkflowRunFromCMDLineBase()50 WorkflowRunFromCMDLineBase::WorkflowRunFromCMDLineBase()
51     : Task(tr("Workflow run from cmdline"), TaskFlag_None),
52       schema(nullptr),
53       optionsStartAt(-1),
54       loadTask(nullptr),
55       workflowRunTask(nullptr) {
56     GCOUNTER(cvar, "workflow_run_from_cmdline");
57 
58     CMDLineRegistry *cmdLineRegistry = AppContext::getCMDLineRegistry();
59 
60     // try to process schema without 'task' option (it can only be the first one)
61     QStringList pureValues = CMDLineRegistryUtils::getPureValues();
62     if (!pureValues.isEmpty()) {
63         QString schemaName = pureValues.first();
64         processLoadSchemaTask(schemaName, 1);  // because after program name
65     }
66     if (loadTask != nullptr) {
67         addSubTask(loadTask);
68         return;
69     }
70 
71     // process schema with 'task' option
72     int taskOptionIdx = CMDLineRegistryUtils::getParameterIndex(WorkflowDesignerPlugin::RUN_WORKFLOW);
73     if (taskOptionIdx != -1) {
74         processLoadSchemaTask(cmdLineRegistry->getParameterValue(WorkflowDesignerPlugin::RUN_WORKFLOW, taskOptionIdx), taskOptionIdx);
75     }
76     if (loadTask == nullptr) {
77         setError(tr("no task to run"));
78         return;
79     }
80     addSubTask(loadTask);
81 }
82 
processLoadSchemaTask(const QString & schemaNameCandidate,int optionIdx)83 void WorkflowRunFromCMDLineBase::processLoadSchemaTask(const QString &schemaNameCandidate, int optionIdx) {
84     loadTask = prepareLoadSchemaTask(schemaNameCandidate);
85     if (loadTask != nullptr) {
86         schemaName = schemaNameCandidate;
87         optionsStartAt = optionIdx + 1;
88     }
89 }
90 
prepareLoadSchemaTask(const QString & schemaName)91 LoadWorkflowTask *WorkflowRunFromCMDLineBase::prepareLoadSchemaTask(const QString &schemaName) {
92     QString pathToSchema = WorkflowUtils::findPathToSchemaFile(schemaName);
93     if (pathToSchema.isEmpty()) {
94         coreLog.error(tr("Cannot find workflow: %1").arg(schemaName));
95         return nullptr;
96     }
97 
98     schema = QSharedPointer<Schema>::create();
99     schema->setDeepCopyFlag(true);
100     return new LoadWorkflowTask(schema, nullptr, pathToSchema);
101 }
102 
setSchemaCMDLineOptions(Schema * schema,int optionsStartAtIdx)103 static void setSchemaCMDLineOptions(Schema *schema, int optionsStartAtIdx) {
104     assert(schema != nullptr && optionsStartAtIdx > 0);
105 
106     QList<StrStrPair> parameters = AppContext::getCMDLineRegistry()->getParameters();
107     int sz = parameters.size();
108     for (int i = optionsStartAtIdx; i < sz; ++i) {
109         const StrStrPair &param = parameters.at(i);
110         if (param.first.isEmpty()) {  // TODO: unnamed parameters not supported yet
111             continue;
112         }
113 
114         QString paramAlias = param.first;
115         QString paramName;
116         Actor *actor = WorkflowUtils::findActorByParamAlias(schema->getProcesses(), paramAlias, paramName);
117         if (actor == nullptr) {
118             assert(paramName.isEmpty());
119             coreLog.details(WorkflowRunFromCMDLineBase::tr("alias '%1' not set in workflow").arg(paramAlias));
120             continue;
121         }
122 
123         Attribute *attr = actor->getParameter(paramName);
124         if (attr == nullptr) {
125             coreLog.error(WorkflowRunFromCMDLineBase::tr("actor parameter '%1' not found").arg(paramName));
126             continue;
127         }
128 
129         DataTypeValueFactory *valueFactory = WorkflowEnv::getDataTypeValueFactoryRegistry()->getById(attr->getAttributeType()->getId());
130         if (valueFactory == nullptr) {
131             coreLog.error(WorkflowRunFromCMDLineBase::tr("cannot parse value from '%1'").arg(param.second));
132             continue;
133         }
134 
135         ActorId id = actor->getId();
136         bool isOk;
137         QVariant value = valueFactory->getValueFromString(param.second, &isOk);
138         if (!isOk) {
139             coreLog.error(WorkflowRunFromCMDLineBase::tr("Incorrect value for '%1', null or default value passed to workflow").arg(param.first));
140             continue;
141         }
142         attr->setAttributeValue(value);
143     }
144 }
145 
onSubTaskFinished(Task * subTask)146 QList<Task *> WorkflowRunFromCMDLineBase::onSubTaskFinished(Task *subTask) {
147     assert(subTask != nullptr);
148     QList<Task *> res;
149 
150     propagateSubtaskError();
151     if (hasError() || isCanceled()) {
152         return res;
153     }
154     assert(!hasError());  // if error, we won't be here
155 
156     if (loadTask == subTask) {
157         const QSharedPointer<Schema> schema = loadTask->getSchema();
158         assert(schema != nullptr);
159         remapping = loadTask->getRemapping();
160 
161         setSchemaCMDLineOptions(schema.get(), optionsStartAt);
162         if (schema->getDomain().isEmpty()) {
163             QList<QString> domainsId = WorkflowEnv::getDomainRegistry()->getAllIds();
164             assert(!domainsId.isEmpty());
165             if (!domainsId.isEmpty()) {
166                 schema->setDomain(domainsId.first());
167             }
168         }
169 
170         QStringList l;
171         bool good = WorkflowUtils::validate(*schema, l);
172         if (!good) {
173             QString schemaHelpStr = QString("\n\nsee 'ugene --help=%1' for details").arg(schemaName);
174             setError("\n\n" + l.join("\n\n") + schemaHelpStr);
175             return res;
176         }
177 
178         workflowRunTask = getWorkflowRunTask();
179         res << workflowRunTask;
180     }
181     return res;
182 }
183 
run()184 void WorkflowRunFromCMDLineBase::run() {
185     CMDLineRegistry *cmdLineRegistry = AppContext::getCMDLineRegistry();
186     SAFE_POINT(nullptr != cmdLineRegistry, "CMDLineRegistry is NULL", );
187     CHECK(nullptr != workflowRunTask, );
188 
189     const QString reportFilePath = cmdLineRegistry->getParameterValue(CmdlineTaskRunner::REPORT_FILE_ARG);
190     CHECK(!reportFilePath.isEmpty(), );
191 
192     QFile reportFile(reportFilePath);
193     const bool opened = reportFile.open(QIODevice::WriteOnly);
194     CHECK_EXT(opened, setError(L10N::errorOpeningFileWrite(reportFilePath)), );
195 
196     reportFile.write(workflowRunTask->generateReport().toLocal8Bit());
197 }
198 
199 /*******************************************
200  * WorkflowRunFromCMDLineTask
201  *******************************************/
getWorkflowRunTask() const202 Task *WorkflowRunFromCMDLineTask::getWorkflowRunTask() const {
203     return new WorkflowRunTask(*schema, remapping);
204 }
205 
206 }  // namespace U2
207