]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
Issue 0020924: [CEA] 6.1.0rc1 - script fails
authorvsr <vsr@opencascade.com>
Thu, 9 Sep 2010 13:32:11 +0000 (13:32 +0000)
committervsr <vsr@opencascade.com>
Thu, 9 Sep 2010 13:32:11 +0000 (13:32 +0000)
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

index ed874a3f4d1170a03d21a6433df86a90dff8de11..3adecffd4fb3d05baa4f45aeef886bd6570d83b0 100644 (file)
@@ -84,6 +84,7 @@
 #include <QListWidget>
 #include <QGridLayout>
 #include <QMenu>
+#include <QtDebug>
 
 #include <SALOMEDSClient_ClientFactory.hxx>
 #include <Basics_Utils.hxx>
@@ -205,26 +206,45 @@ void SalomeApp_Application::start()
     // import/execute python scripts
     if ( pyfiles.count() > 0 && activeStudy() ) {
       SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( 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];
+             }
             }
           }
         }