Make sure launching external processes works after switch to qt6

This commit is contained in:
jonjenssen 2024-10-15 00:34:51 +02:00 committed by jonjenssen
parent eeb5f297c2
commit 2fe432b43d
5 changed files with 49 additions and 26 deletions

View File

@ -94,7 +94,7 @@ void RicRunFaultReactModelingFeature::onActionTriggered( bool isChecked )
RimProcess process; RimProcess process;
process.setCommand( command ); process.setCommand( command );
process.setParameters( parameters ); process.addParameters( parameters );
if ( !process.execute() ) if ( !process.execute() )
{ {

View File

@ -107,7 +107,7 @@ void RicRunWellIntegrityAnalysisFeature::onActionTriggered( bool isChecked )
RimProcess process; RimProcess process;
process.setCommand( command ); process.setCommand( command );
process.setParameters( parameters ); process.addParameters( parameters );
if ( !process.execute() ) if ( !process.execute() )
{ {

View File

@ -174,7 +174,7 @@ bool RicImportSeismicFeature::runSEGYConversion( RimSEGYConvertOptions* options
} }
process.setCommand( command ); process.setCommand( command );
process.setParameters( parameters ); process.addParameters( parameters );
bool showStdOut = false; bool showStdOut = false;
bool showStdErr = true; bool showStdErr = true;

View File

@ -27,14 +27,6 @@
#include <QProcess> #include <QProcess>
#include <QProcessEnvironment> #include <QProcessEnvironment>
// Disable deprecation warning for QProcess::start()
#ifdef _MSC_VER
#pragma warning( disable : 4996 )
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
CAF_PDM_SOURCE_INIT( RimProcess, "RimProcess" ); CAF_PDM_SOURCE_INIT( RimProcess, "RimProcess" );
int RimProcess::m_nextProcessId = 1; int RimProcess::m_nextProcessId = 1;
@ -83,9 +75,8 @@ void RimProcess::addParameter( QString paramStr )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RimProcess::setParameters( QStringList parameterList ) void RimProcess::addParameters( QStringList parameterList )
{ {
m_arguments.clear();
for ( int i = 0; i < parameterList.size(); i++ ) for ( int i = 0; i < parameterList.size(); i++ )
{ {
addParameter( parameterList[i] ); addParameter( parameterList[i] );
@ -97,7 +88,16 @@ void RimProcess::setParameters( QStringList parameterList )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RimProcess::setCommand( QString cmdStr ) void RimProcess::setCommand( QString cmdStr )
{ {
m_command = cmdStr; m_command = cmdStr.trimmed();
QString shell = optionalCommandInterpreter();
if ( shell.isEmpty() ) return;
QString preParam = optionalPreParameters();
if ( !preParam.isEmpty() ) m_arguments.append( preParam );
m_arguments.append( cmdStr.trimmed() );
m_command = shell;
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -156,7 +156,8 @@ bool RimProcess::execute( bool enableStdOut, bool enableStdErr )
} }
proc->setProcessEnvironment( env ); proc->setProcessEnvironment( env );
proc->start( cmd ); proc->start( m_command, m_arguments );
auto error = proc->errorString();
if ( proc->waitForStarted( -1 ) ) if ( proc->waitForStarted( -1 ) )
{ {
while ( !proc->waitForFinished( 500 ) ) while ( !proc->waitForFinished( 500 ) )
@ -167,7 +168,7 @@ bool RimProcess::execute( bool enableStdOut, bool enableStdErr )
} }
else else
{ {
RiaLogging::error( QString( "Failed to start process %1." ).arg( m_id ) ); RiaLogging::error( QString( "Failed to start process %1. %2." ).arg( m_id ).arg( error ) );
} }
proc->deleteLater(); proc->deleteLater();
@ -182,31 +183,50 @@ QString RimProcess::optionalCommandInterpreter() const
{ {
if ( m_command.value().isNull() ) return ""; if ( m_command.value().isNull() ) return "";
if ( m_command.value().endsWith( ".cmd", Qt::CaseInsensitive ) || m_command.value().endsWith( ".bat", Qt::CaseInsensitive ) ) if ( isWindowsBatchFile() )
{ {
return "cmd.exe /c "; return "cmd.exe";
} }
if ( m_command.value().endsWith( ".sh", Qt::CaseInsensitive ) ) if ( m_command.value().endsWith( ".sh", Qt::CaseInsensitive ) )
{ {
return "bash "; return "bash";
} }
if ( m_command.value().endsWith( ".csh", Qt::CaseInsensitive ) ) if ( m_command.value().endsWith( ".csh", Qt::CaseInsensitive ) )
{ {
return "csh "; return "csh";
} }
return ""; return "";
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimProcess::optionalPreParameters() const
{
if ( m_command.value().isNull() ) return "";
if ( isWindowsBatchFile() )
{
return "/c";
}
return "";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimProcess::isWindowsBatchFile() const
{
return ( m_command.value().endsWith( ".cmd", Qt::CaseInsensitive ) || m_command.value().endsWith( ".bat", Qt::CaseInsensitive ) );
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
QString RimProcess::commandLine() const QString RimProcess::commandLine() const
{ {
QString cmdline; QString cmdline = handleSpaces( m_command );
cmdline += optionalCommandInterpreter();
cmdline += handleSpaces( m_command );
for ( int i = 0; i < m_arguments.size(); i++ ) for ( int i = 0; i < m_arguments.size(); i++ )
{ {

View File

@ -40,7 +40,7 @@ public:
void setDescription( QString desc ); void setDescription( QString desc );
void setCommand( QString cmdStr ); void setCommand( QString cmdStr );
void addParameter( QString paramStr ); void addParameter( QString paramStr );
void setParameters( QStringList parameterList ); void addParameters( QStringList parameterList );
void addEnvironmentVariable( QString name, QString value ); void addEnvironmentVariable( QString name, QString value );
@ -57,8 +57,11 @@ protected:
private: private:
QString optionalCommandInterpreter() const; QString optionalCommandInterpreter() const;
QString optionalPreParameters() const;
QString handleSpaces( QString argument ) const; QString handleSpaces( QString argument ) const;
bool isWindowsBatchFile() const;
caf::PdmField<QString> m_command; caf::PdmField<QString> m_command;
QStringList m_arguments; QStringList m_arguments;
caf::PdmField<QString> m_description; caf::PdmField<QString> m_description;