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 #ifndef _U2_EXTERNAL_PROCESS_WORKER_H_
23 #define _U2_EXTERNAL_PROCESS_WORKER_H_
24 
25 #include <U2Core/Task.h>
26 
27 #include <U2Lang/LocalDomain.h>
28 #include <U2Lang/WorkflowEnv.h>
29 #include <U2Lang/WorkflowUtils.h>
30 
31 namespace U2 {
32 namespace LocalWorkflow {
33 
34 class ExternalProcessWorker : public BaseWorker {
35     Q_OBJECT
36 public:
37     ExternalProcessWorker(Actor *a);
38 
39     bool isReady() const;
40     Task *tick();
41     void init();
42     void cleanup();
43 
44 private slots:
45     void sl_onTaskFinishied();
46 
47 private:
48     enum InputsCheckResult {
49         ALL_INPUTS_FINISH,
50         SOME_INPUTS_FINISH,
51         ALL_INPUTS_HAVE_MESSAGE,
52         NOT_ALL_INPUTS_HAVE_MESSAGE,
53         INTERNAL_ERROR
54     };
55 
56     void applySpecialInternalEnvvars(QString &execString, ExternalProcessConfig *cfg);
57     void applyAttributes(QString &execString);
58     static bool applyParamsToExecString(QString &execString, QString parName, QString parValue);
59     void applyEscapedSymbols(QString &execString);
60     QStringList applyInputMessage(QString &execString, const DataConfig &dataCfg, const QVariantMap &data, U2OpStatus &os);
61     QString prepareOutput(QString &execString, const DataConfig &dataCfg, U2OpStatus &os);
62 
63     InputsCheckResult checkInputBusState() const;
64     bool finishWorkIfInputEnded(QString &error);
65     void finish();
66 
67     IntegralBus *output;
68     QList<IntegralBus *> inputs;
69     QString commandLine;
70     ExternalProcessConfig *cfg;
71 
72     QMap<QString, bool> urlsForDashboard;  // url -> open by system
73     QStringList inputUrls;
74 };
75 
76 class ExternalProcessWorkerFactory : public DomainFactory {
77 public:
ExternalProcessWorkerFactory(QString name)78     ExternalProcessWorkerFactory(QString name)
79         : DomainFactory(name) {
80     }
81     static bool init(ExternalProcessConfig *cfg);
createWorker(Actor * a)82     virtual Worker *createWorker(Actor *a) {
83         return new ExternalProcessWorker(a);
84     }
85 };
86 
87 class ExternalProcessWorkerPrompter : public PrompterBase<ExternalProcessWorkerPrompter> {
88     Q_OBJECT
89 public:
90     ExternalProcessWorkerPrompter(Actor *p = nullptr)
91         : PrompterBase<ExternalProcessWorkerPrompter>(p) {
92     }
93     QString composeRichDoc();
94 };
95 
96 class LaunchExternalToolTask : public Task {
97     Q_OBJECT
98     Q_DISABLE_COPY(LaunchExternalToolTask)
99 public:
100     LaunchExternalToolTask(const QString &execString, const QString &workingDir, const QMap<QString, DataConfig> &outputUrls);
101     ~LaunchExternalToolTask();
102 
103     void run();
104 
105     QMap<QString, DataConfig> takeOutputUrls();
106     void addListeners(const QList<ExternalToolListener *> &listenersToAdd);
107 
108 private:
109     QMap<QString, DataConfig> outputUrls;
110     QString execString;
111     QString workingDir;
112     QList<ExternalToolListener *> listeners;
113 };
114 
115 }  // namespace LocalWorkflow
116 }  // namespace U2
117 
118 #endif  // _U2_EXTERNAL_PROCESS_WORKER_H_
119