OpendTect  6.6
oscommand.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: Bert
8  Date: Nov 2013
9 ________________________________________________________________________
10 
11 -*/
12 
13 #include "basicmod.h"
14 
15 #include "bufstringset.h"
16 #include "od_iosfwd.h"
17 #include "uistring.h"
18 
19 mFDQtclass(QProcess);
20 class qstreambuf;
21 class StreamProvider;
22 
23 namespace OS
24 {
25 
28 
30 inline bool isOldStyle( OS::KeyStyle ks ) { return ks == OS::OldStyle; }
31 
36 {
37 public:
39  : launchtype_(lt)
40  , createstreams_(false)
41  , needmonitor_(false)
42  , prioritylevel_(lt>=Batch ? -1.0f : 0.0f)
43  , isconsoleuiprog_(false)
44  , runasadmin_(false) {}
45 
46  mDeprecated ("Use CommandExecPars(LaunchType) instead")
47  explicit CommandExecPars(bool isbatchprog);
48 
50  mDefSetupClssMemb(CommandExecPars,bool,createstreams);
54 
55  mDefSetupClssMemb(CommandExecPars,float,prioritylevel);
57 
58  mDefSetupClssMemb(CommandExecPars,bool,isconsoleuiprog);
61 
65 
67 
68  void usePar(const IOPar&);
69  void fillPar(IOPar&) const;
70  void removeFromPar(IOPar&) const;
71 
72  static const char* sKeyPriority() { return "priority"; }
73 
74  static const StepInterval<int> cMachineUserPriorityRange(bool iswin);
80  static int getMachinePriority(float priolevel,bool iswin);
81 };
82 
83 
84 
85 class CommandLauncher;
86 
94 {
95 public:
96 
97  MachineCommand( const char* prognm=nullptr )
98  : prognm_(prognm) {}
99  MachineCommand( const char* prognm,
100  const BufferStringSet& arguments )
101  : prognm_(prognm)
102  , args_(arguments) {}
103  MachineCommand(const char* prognm,const char* arg1,
104  const char* arg2=0,const char* arg3=0,
105  const char* arg4=0,const char* arg5=0);
106  MachineCommand(const char* prognm,bool isolated);
107  MachineCommand(const MachineCommand&,bool isolated);
108 
109  const char* program() const { return prognm_; }
110  const BufferStringSet& args() const { return args_; }
111  BufferString toString(const CommandExecPars* =nullptr) const;
113 
114  MachineCommand& setProgram( const char* pn )
115  { prognm_.set( pn ); return *this; }
116  MachineCommand& addArg(const char*);
118  MachineCommand& addFlag( const char* flg, KeyStyle ks=NewStyle )
119  { return addKeyedArg(flg,nullptr,ks); }
120  MachineCommand& addKeyedArg(const char* ky,const char* valstr,
121  KeyStyle ks=NewStyle);
122  MachineCommand& addPipe() { return addArg("|"); }
123  MachineCommand& addFileRedirect(const char* fnm,int stdcode=0,
124  bool append=false);
129  // convenience:
130  template <class T>
131  MachineCommand& addArg( const T& t ) { return addArg(::toString(t));}
132  template <class T>
133  MachineCommand& addKeyedArg( const char* ky, const T& t,
134  KeyStyle ks=NewStyle )
135  { return addKeyedArg(ky,::toString(t),ks);}
136 
137  mDeprecatedDef const char* command() const;
138  mDeprecatedDef void setCommand(const char*);
139  mDeprecatedDef const char* getSingleStringRep() const;
141  bool ignorehostname=false);
147  bool hostIsWindows() const { return hostiswin_; }
148  void setHostIsWindows( bool yn ) { hostiswin_ = yn; }
149  const char* hostName() const { return hname_; }
150  void setHostName( const char* hnm ) { hname_ = hnm; }
151  const char* remExec() const { return remexec_; }
152  void setRemExec( const char* sh ) { remexec_ = sh; }
153 
154  bool isBad() const { return prognm_.isEmpty(); }
155  bool hasHostName() const { return !hname_.isEmpty(); }
156 
157  static const char* defaultRemExec() { return defremexec_; }
158  static void setDefaultRemExec( const char* s ) { defremexec_ = s; }
159 
160  static const char* odRemExecCmd() { return "od_remexec"; }
161  static const char* sKeyRemoteHost() { return "machine"; }
162  static const char* sKeyRemoteCmd() { return "cmd"; }
163  static const char* sKeyPrimaryHost() { return "primaryhost"; }
164  static const char* sKeyPrimaryPort() { return "primaryport"; }
165  static const char* sKeyJobID() { return "jobid"; }
166 
168  const char* workdir=nullptr);
169  bool execute(BufferString& output_stdout,
170  BufferString* output_stderr=nullptr,
171  const char* workdir=nullptr);
173  bool execute(const CommandExecPars&);
174 
177 
178 private:
179 
180  void setIsolated(const char* prognm);
189  bool hostiswin_ = __iswin__;
191  BufferString remexec_ = defremexec_;
192  bool needshell_ = false;
193 
195 
196  friend class CommandLauncher;
197  friend class ::StreamProvider;
198 
199 public:
200 
201  mDeprecatedDef BufferString getSingleStringRep(bool noremote=false) const;
202  mDeprecated("Use sKeyPrimaryHost()")
203  static const char* sKeyMasterHost() { return "primaryhost"; }
204  mDeprecated("Use sKeyPrimaryPort()")
205  static const char* sKeyMasterPort() { return "primaryport"; }
206 
207  //Only for expert usage
208  static void setIsolationScript(const char*);
209 
210 };
211 
212 
217 public:
220 
221  void set(const MachineCommand&);
222 
224  const char* workdir=nullptr);
225  bool execute(BufferString& output_stdout,
226  BufferString* output_stderr=nullptr,
227  const char* workdir=nullptr);
229  bool execute(const CommandExecPars&);
230  bool startServer(bool inpythonenv=false,
231  double maxwaittm=20 /* seconds */);
232 
234  const char* monitorFileName() const { return monitorfnm_; }
235  uiString errorMsg() const { return errmsg_; }
236 
237  od_istream* getStdOutput() { return stdoutput_; }
238  od_istream* getStdError() { return stderror_; }
239  od_ostream* getStdInput() { return stdinput_; }
240 
241  mDeprecated("Provide cmd and arguments")
242  static bool openTerminal(const char* workdir);
243  static bool openTerminal(const char* cmd,
244  const BufferStringSet* args,
245  BufferString* errmsg =nullptr,
246  uiString* launchermsg =nullptr,
247  const char* workdir =nullptr);
248 
249 protected:
250 
251  void reset();
252  bool doExecute(const MachineCommand&,const CommandExecPars&);
253  int catchError();
254  bool startDetached(const MachineCommand&,
255  bool inconsole=false,
256  const char* workingdir=nullptr);
257  void startMonitor();
258 
259  MachineCommand machcmd_;
260  BufferString monitorfnm_;
261  bool redirectoutput_;
262 
263  BufferString progvwrcmd_;
265  const BufferString odprogressviewer_;
266 
267  QProcess* process_;
268  PID_Type pid_;
269 
270  od_istream* stdoutput_;
271  od_istream* stderror_;
272  od_ostream* stdinput_;
273 
274  qstreambuf* stdoutputbuf_;
275  qstreambuf* stderrorbuf_;
276  qstreambuf* stdinputbuf_;
277 
278 private:
279  void setIsolated();
280  void adaptForV7(const OS::MachineCommand&);
281 
282 public:
283  mDeprecatedDef static void addShellIfNeeded(BufferString& cmd);
287  mDeprecatedDef static void addQuotesIfNeeded(BufferString& cmd);
290  static void manageQProcess(QProcess*);
292 };
293 
294 
296 mDeprecatedDef mGlobal(Basic) bool ExecCommand(const char* cmd,
298  BufferString* stdoutput=0,
299  BufferString* stderror=0);
300 
301 
302 } // namespace OS
303 
305 mDeprecatedDef mGlobal(Basic) bool ExecODProgram(const char* prognm,
306  const char* args=0,
307  OS::LaunchType lt=OS::RunInBG);
308 
309 
310 namespace OD
311 {
312 
314 mGlobal(Basic) void DisplayErrorMessage(const char*);
315 
316 } // namespace OD
317 
318 
OS::MachineCommand::setProgram
MachineCommand & setProgram(const char *pn)
Definition: oscommand.h:114
OS::LaunchType
LaunchType
Definition: oscommand.h:26
OS::MachineCommand::MachineCommand
MachineCommand(const char *prognm, const char *arg1, const char *arg2=0, const char *arg3=0, const char *arg4=0, const char *arg5=0)
OS::CommandExecPars::CommandExecPars
CommandExecPars(LaunchType lt=Wait4Finish)
Definition: oscommand.h:38
uistring.h
mGlobal
#define mGlobal(module)
Definition: commondefs.h:180
qstreambuf
Adapter to use a qprocess as a stream.
Definition: qstreambuf.h:38
OS::CommandLauncher::execute
bool execute(LaunchType lt=Wait4Finish, const char *workdir=nullptr)
BufferStringSet
Set of BufferString objects.
Definition: bufstringset.h:26
OS::MachineCommand::addKeyedArg
MachineCommand & addKeyedArg(const char *ky, const char *valstr, KeyStyle ks=NewStyle)
OS::MachineCommand::setRemExec
void setRemExec(const char *sh)
Definition: oscommand.h:152
OS::MachineCommand::sKeyPrimaryPort
static const char * sKeyPrimaryPort()
Definition: oscommand.h:164
OD
OpendTect.
Definition: commontypes.h:28
mFDQtclass
#define mFDQtclass(cls)
Definition: commondefs.h:233
OS::MachineCommand::remExec
const char * remExec() const
Definition: oscommand.h:151
OS::MachineCommand::addFileRedirect
MachineCommand & addFileRedirect(const char *fnm, int stdcode=0, bool append=false)
mExpClass
#define mExpClass(module)
Definition: commondefs.h:177
OS::MachineCommand::addArg
MachineCommand & addArg(const char *)
od_istream
OD class for stream read common access to the std::cin.
Definition: od_istream.h:24
OS::MachineCommand::MachineCommand
MachineCommand(const char *prognm, bool isolated)
OS::MachineCommand::toString
BufferString toString(const CommandExecPars *=nullptr) const
Only for messaging purposes.
OS::MachineCommand::setFromSingleStringRep
bool setFromSingleStringRep(const char *, bool ignorehostname=false)
usePar
void usePar(const IOPar &iop, ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:187
OS::KeyStyle
KeyStyle
Definition: oscommand.h:27
PID_Type
od_int32 PID_Type
Definition: commontypes.h:38
OS::MachineCommand::program
const char * program() const
Definition: oscommand.h:109
OS::CommandLauncher::~CommandLauncher
~CommandLauncher()
OS::CommandLauncher::errorMsg
uiString errorMsg() const
Definition: oscommand.h:235
OS::MachineCommand::addKeyedArg
MachineCommand & addKeyedArg(const char *ky, const T &t, KeyStyle ks=NewStyle)
Definition: oscommand.h:133
OS::MachineCommand::setCommand
void setCommand(const char *)
OS::MachineCommand::hasHostName
bool hasHostName() const
Definition: oscommand.h:155
OS::MachineCommand::isBad
bool isBad() const
Definition: oscommand.h:154
OS::MachineCommand::addFlag
MachineCommand & addFlag(const char *flg, KeyStyle ks=NewStyle)
Definition: oscommand.h:118
mODTextTranslationClass
#define mODTextTranslationClass(clss)
Definition: uistring.h:40
OS::CommandLauncher::processID
PID_Type processID() const
OS::MachineCommand::sKeyJobID
static const char * sKeyJobID()
Definition: oscommand.h:165
OS::MachineCommand::execute
bool execute(LaunchType lt=Wait4Finish, const char *workdir=nullptr)
OD::DisplayErrorMessage
void DisplayErrorMessage(const char *)
OS::CommandExecPars
Specifies how to execute a command.
Definition: oscommand.h:36
OS::MachineCommand::setIsolated
void setIsolated(const char *prognm)
OS::MachineCommand::args_
BufferStringSet args_
Definition: oscommand.h:188
OS::MachineCommand::args
const BufferStringSet & args() const
Definition: oscommand.h:110
StepInterval< int >
OS::CommandLauncher::execute
bool execute(const CommandExecPars &)
OS::CommandLauncher::CommandLauncher
CommandLauncher(const MachineCommand &)
OS::MachineCommand::setHostIsWindows
void setHostIsWindows(bool yn)
Definition: oscommand.h:148
OS::ExecCommand
bool ExecCommand(const char *cmd, LaunchType lt=Wait4Finish, BufferString *stdoutput=0, BufferString *stderror=0)
OS::CommandLauncher::set
void set(const MachineCommand &)
OS::MachineCommand::addArg
MachineCommand & addArg(const T &t)
Definition: oscommand.h:131
OS::MachineCommand::addPipe
MachineCommand & addPipe()
Definition: oscommand.h:122
OS::MachineCommand::hname_
BufferString hname_
Definition: oscommand.h:190
OS::MachineCommand::setIsolationScript
static void setIsolationScript(const char *)
OS::MachineCommand::execute
bool execute(BufferString &output_stdout, BufferString *output_stderr=nullptr, const char *workdir=nullptr)
run &, wait until finished, catch output
mDeprecated
#define mDeprecated(msg)
Definition: plfdefs.h:215
StreamProvider
Provides I/O stream for file or system command.
Definition: strmprov.h:39
OS::MachineCommand::getSingleStringRep
const char * getSingleStringRep() const
OS::MachineCommand::getSingleStringRep
BufferString getSingleStringRep(bool noremote=false) const
OS::isOldStyle
bool isOldStyle(OS::KeyStyle ks)
Definition: oscommand.h:30
OS::OldStyle
@ OldStyle
Definition: oscommand.h:27
OS
Definition: od_stream.h:23
OS::MachineCommand::defaultRemExec
static const char * defaultRemExec()
Definition: oscommand.h:157
OS::MachineCommand::execute
bool execute(const CommandExecPars &)
OS::CommandLauncher::getStdInput
od_ostream * getStdInput()
Definition: oscommand.h:239
OS::isBatchProg
bool isBatchProg(OS::LaunchType lt)
OS::Batch
@ Batch
Definition: oscommand.h:26
fillPar
void fillPar(IOPar &iop, const ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:174
OS::MachineCommand::runAndCollectOutput
BufferString runAndCollectOutput(BufferString *errmsg=nullptr)
for quick get-me-the-output-of-this-command
BufferString
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size.
Definition: bufstring.h:40
OS::CommandExecPars::cMachineUserPriorityRange
static const StepInterval< int > cMachineUserPriorityRange(bool iswin)
OS::MachineCommand::command
const char * command() const
OS::CommandLauncher::startServer
bool startServer(bool inpythonenv=false, double maxwaittm=20)
OS::MachineCommand::MachineCommand
MachineCommand(const char *prognm=nullptr)
Definition: oscommand.h:97
bufstringset.h
mDeprecatedObs
#define mDeprecatedObs
Definition: plfdefs.h:217
uiString
String that is able to hold international (UTF-8) strings for the user interface.
Definition: uistring.h:121
OS::CommandLauncher::execute
bool execute(BufferString &output_stdout, BufferString *output_stderr=nullptr, const char *workdir=nullptr)
run &, wait until finished, catch output
OS::MachineCommand::sKeyRemoteCmd
static const char * sKeyRemoteCmd()
Definition: oscommand.h:162
OS::Wait4Finish
@ Wait4Finish
Definition: oscommand.h:26
od_iosfwd.h
OS::MachineCommand::addShellIfNeeded
void addShellIfNeeded()
MPE::errmsg_
BufferString errmsg_
Definition: horizontracker.h:118
OS::RunInBG
@ RunInBG
Definition: oscommand.h:26
OS::MachineCommand
Encapsulates an actual command to execute + the machine to run it on.
Definition: oscommand.h:94
OS::MachineCommand::hostIsWindows
bool hostIsWindows() const
Definition: oscommand.h:147
OS::MachineCommand::odRemExecCmd
static const char * odRemExecCmd()
Definition: oscommand.h:160
OS::MachineCommand::getExecCommand
MachineCommand getExecCommand(const CommandExecPars *=nullptr) const
OS::MachineCommand::MachineCommand
MachineCommand(const char *prognm, const BufferStringSet &arguments)
Definition: oscommand.h:99
OS::MachineCommand::addArgs
MachineCommand & addArgs(const BufferStringSet &)
__iswin__
#define __iswin__
Definition: plfdefs.h:124
Batch
Definition: batchjobdispatch.h:22
OS::MachineCommand::hostName
const char * hostName() const
Definition: oscommand.h:149
OS::CommandExecPars::getMachinePriority
static int getMachinePriority(float priolevel, bool iswin)
append
bool append(OD::ValVec< T, IT > &to, const OD::ValVec< S, J > &from)
append allowing a different type to be merged into set
Definition: typeset.h:239
OS::CommandLauncher::getStdOutput
od_istream * getStdOutput()
Definition: oscommand.h:237
OS::MachineCommand::setHostName
void setHostName(const char *hnm)
Definition: oscommand.h:150
OS::MachineCommand::sKeyRemoteHost
static const char * sKeyRemoteHost()
Definition: oscommand.h:161
OS::MachineCommand::sKeyPrimaryHost
static const char * sKeyPrimaryHost()
Definition: oscommand.h:163
OS::CommandLauncher::getStdError
od_istream * getStdError()
Definition: oscommand.h:238
mDefSetupClssMemb
#define mDefSetupClssMemb(clss, typ, memb)
Definition: commondefs.h:150
mDeprecatedDef
#define mDeprecatedDef
Definition: plfdefs.h:216
OS::MachineCommand::setDefaultRemExec
static void setDefaultRemExec(const char *s)
Definition: oscommand.h:158
OS::CommandLauncher::monitorFileName
const char * monitorFileName() const
Definition: oscommand.h:234
od_ostream
OD class for stream write common access to the user log file, or std::cout in other than od_main.
Definition: od_ostream.h:26
OS::BatchWait
@ BatchWait
Definition: oscommand.h:26
ExecODProgram
bool ExecODProgram(const char *prognm, const char *args=0, OS::LaunchType lt=OS::RunInBG)
OS::MachineCommand::prognm_
BufferString prognm_
Definition: oscommand.h:187
toString
BufferString toString(const BufferStringSet &bss)
Definition: bufstringset.h:156
private
#define private
Definition: winstreambuf.h:13
IOPar
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:55
OS::MachineCommand::MachineCommand
MachineCommand(const MachineCommand &, bool isolated)
OS::CommandLauncher
Launches machine commands.
Definition: oscommand.h:216
OS::NewStyle
@ NewStyle
Definition: oscommand.h:27
OS::MachineCommand::defremexec_
static BufferString defremexec_
Definition: oscommand.h:194

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021