From fbc010fa2739c73a2f6f2a3a976ed4e0083368a9 Mon Sep 17 00:00:00 2001 From: vsr Date: Thu, 9 Sep 2010 13:32:11 +0000 Subject: [PATCH] Issue 0020924: [CEA] 6.1.0rc1 - script fails Always use execfile() function instead of "import" command to execute Python scripts (passed via -u option of runSalome.py script) --- src/SalomeApp/SalomeApp_Application.cxx | 52 +++++++++++++++++-------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/SalomeApp/SalomeApp_Application.cxx b/src/SalomeApp/SalomeApp_Application.cxx index ed874a3f4..3adecffd4 100644 --- a/src/SalomeApp/SalomeApp_Application.cxx +++ b/src/SalomeApp/SalomeApp_Application.cxx @@ -84,6 +84,7 @@ #include #include #include +#include #include #include @@ -205,26 +206,45 @@ void SalomeApp_Application::start() // import/execute python scripts if ( pyfiles.count() > 0 && activeStudy() ) { SalomeApp_Study* appStudy = dynamic_cast( activeStudy() ); - if ( appStudy ) { + PyConsole_Console* pyConsole = pythonConsole(); + if ( appStudy && pyConsole ) { _PTR(Study) aStudy = appStudy->studyDS(); if ( !aStudy->GetProperties()->IsLocked() ) { for (uint j = 0; j < pyfiles.count(); j++ ) { QFileInfo fi ( pyfiles[j] ); - PyConsole_Console* pyConsole = pythonConsole(); - if ( pyConsole ) { - QString extension = fi.suffix().toLower(); - if ( fi.exists() ) { - // execute python script - QString command = QString( "execfile(r\"%1\")" ).arg( fi.absoluteFilePath() ); - pyConsole->exec( command ); - } - else { - // import python module - QString command = QString( "import %1" ).arg( pyfiles[j] ); - if ( extension == "py" ) - command = QString( "import %1" ).arg( fi.completeBaseName() ); - pyConsole->exec( command ); - } + QFileInfo fipy ( pyfiles[j] + ".py" ); + QString command = QString( "execfile(r\"%1\")" ); + if ( fi.isAbsolute() ) { + if ( fi.exists() ) + pyConsole->exec( command.arg( fi.absoluteFilePath() ) ); + else if ( fipy.exists() ) + pyConsole->exec( command.arg( fipy.absoluteFilePath() ) ); + else + qDebug() << "Can't execute file" << pyfiles[j]; + } + else { + bool found = false; + QStringList dirs; + dirs << QDir::currentPath(); + if ( ::getenv( "PYTHONPATH" ) ) + dirs += QString( ::getenv( "PYTHONPATH" ) ).split( QRegExp( "[:|;]" ) ); + foreach( QString dir, dirs ) { + qDebug() << "try" << QFileInfo( dir, pyfiles[j] ).absoluteFilePath(); + qDebug() << "try" << QFileInfo( dir, pyfiles[j] + ".py" ).absoluteFilePath(); + if ( QFileInfo( dir, pyfiles[j] ).exists() ) { + pyConsole->exec( command.arg( QFileInfo( dir, pyfiles[j] ).absoluteFilePath() ) ); + found = true; + break; + } + else if ( QFileInfo( dir, pyfiles[j] + ".py" ).exists() ) { + pyConsole->exec( command.arg( QFileInfo( dir, pyfiles[j] + ".py" ).absoluteFilePath() ) ); + found = true; + break; + } + } + if ( !found ) { + qDebug() << "Can't execute file" << pyfiles[j]; + } } } } -- 2.39.2