Salome HOME
More fixes in Cleaner and SurfOpt:
[modules/smesh.git] / src / Tools / YamsPlug / monViewText.py
index 38aeed49bbd1e1e52d3bce8e27191a3d6b41b402..9d92a0c55a7eac476d31cc47d38bc3f10d865d62 100644 (file)
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
 
-# Modules Python
-import string,types,os, sys
-import traceback
+import os
+import sys
+import string
+import types
 import tempfile
+import traceback
+import pprint as PP #pretty print
 
 from qtsalome import *
 
 # Import des panels
-
 from ViewText_ui import Ui_ViewExe
 
+verbose = True
+
+force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
+if force != None:
+    os.environ["DISTENE_LICENSE_FILE"] = force
+    os.environ["DLIM8VAR"] = "NOTHING"
+
 class MonViewText(Ui_ViewExe, QDialog):
     """
     Classe permettant la visualisation de texte
@@ -44,63 +53,74 @@ class MonViewText(Ui_ViewExe, QDialog):
 
         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
-      
-        # Je n arrive pas a utiliser le setEnvironment du QProcess
-        # fonctionne hors Salome mais pas dans Salome ???
-        cmds=''
-        #cmds+='#! /usr/bin/env python\n'
-        #cmds+='# -*- coding: utf-8 -*-\n'
-        cmds+=txt+'\n'
-        cmds+='echo "END_OF_Yams"\n'
-        if os.path.exists(self.parent().fichierOut):
-            os.remove(self.parent().fichierOut)
-
-        ext=''
+        self.monExe.finished.connect( self.finished )
+
+        cmds = ''
+        ext = ''
         if sys.platform == "win32":
+            if os.path.exists(self.parent().fichierOut):
+                cmds += 'del %s\n' % self.parent().fichierOut
             ext = '.bat'
         else:
-            ext = '.sh'
+            cmds += '#!/bin/bash\n'
+            cmds += 'pwd\n'
+            #cmds += 'which mg-surfopt.exe\n'
+            cmds += 'echo "DISTENE_LICENSE_FILE="$DISTENE_LICENSE_FILE\n'
+            cmds += 'echo "DLIM8VAR="$DLIM8VAR\n'
+            cmds += 'rm -f %s\n' % self.parent().fichierOut
+            ext = '.bash'
+
+        cmds += 'echo %s\n' % txt #to see what is compute command
+        cmds += txt+'\n'
+        cmds += 'echo "END_OF_MGSurfOpt"\n'
 
-        nomFichier=tempfile.mktemp(suffix=ext,prefix='Yams_')
-        f=open(nomFichier,'w')
-        f.write(cmds)
-        f.close()
+        nomFichier = os.path.splitext(self.parent().fichierOut)[0] + ext
+        with open(nomFichier, 'w') as f:
+            f.write(cmds)
+        self.make_executable(nomFichier)
 
-        maBidouille=nomFichier
-        self.monExe.start(maBidouille)
+        if verbose: print(("INFO: MGSurfOpt launch script file: %s" % nomFichier))
+
+        self.monExe.start(nomFichier)
         self.monExe.closeWriteChannel()
         self.enregistreResultatsDone=False
         self.show()
 
+    def make_executable(self, path):
+        mode = os.stat(path).st_mode
+        mode |= (mode & 0o444) >> 2    # copy R bits to X
+        os.chmod(path, mode)
+
     def saveFile(self):
         #recuperation du nom du fichier
         savedir=os.environ['HOME']
-        fn = QFileDialog.getSaveFileName(None,"Save File",savedir)
-        if fn.isNull() : return
-        ulfile = os.path.abspath(unicode(fn))
+        fn, mask = QFileDialog.getSaveFileName(None,"Save File",savedir)
+        if not fn: return
+        ulfile = os.path.abspath(str(fn))
         try:
-           f = open(fn, 'wb')
-           f.write(str(self.TB_Exe.toPlainText()))
-           f.close()
-        except IOError, why:
-           QMessageBox.critical(self, 'Save File',
-               'The file <b>%1</b> could not be saved.<br>Reason: %2'%(unicode(fn), str(why)))
+            f = open(fn, 'wb')
+            f.write(self.TB_Exe.toPlainText().encode("utf-8"))
+            f.close()
+        except IOError as why:
+            QMessageBox.critical(self, 'Save File',
+                 'The file <b>%s</b> could not be saved.<br>Reason: %s'%(str(fn), str(why)))
 
     def readFromStdErr(self):
         a=self.monExe.readAllStandardError()
-        self.TB_Exe.append(unicode(a.data().encode()))
+        aa=a.data().decode(errors='ignore')
+        self.TB_Exe.append(aa)
 
     def readFromStdOut(self) :
         a=self.monExe.readAllStandardOutput()
-        aa=unicode(a.data(),len(a))
+        aa=a.data().decode(errors='ignore')
         self.TB_Exe.append(aa)
-        if "END_OF_Yams" in aa:
-          self.parent().enregistreResultat()
-          self.enregistreResultatsDone=True
-          #self.theClose()
-    
-    def theClose(self):
-      if not self.enregistreResultatsDone:
+
+    def finished(self):
         self.parent().enregistreResultat()
         self.enregistreResultatsDone=True
-      self.close()
+
+    def theClose(self):
+        if not self.enregistreResultatsDone:
+            self.parent().enregistreResultat()
+            self.enregistreResultatsDone=True
+        self.close()