Salome HOME
23288: [CEA 1626] Meshgems v2.3
[modules/smesh.git] / src / Tools / MGCleanerPlug / MGCleanerMonViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2013-2016  EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # Modules Python
22 import os
23 import sys
24 import string
25 import types
26 import tempfile
27 import traceback
28 import pprint as PP #pretty print
29
30 from qtsalome import *
31
32 # Import des panels
33
34 from MGCleanerViewText_ui import Ui_ViewExe
35
36 verbose = True
37
38 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
39 if force != None:
40   os.environ["DISTENE_LICENSE_FILE"] = force
41   os.environ["DLIM8VAR"] = "NOTHING"
42
43 class MGCleanerMonViewText(Ui_ViewExe, QDialog):
44     """
45     Classe permettant la visualisation de texte
46     """
47     def __init__(self, parent, txt, ):
48         QDialog.__init__(self,parent)
49         self.setupUi(self)
50         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
51         #self.connect( self.PB_Ok,SIGNAL("clicked()"), self, SLOT("close()") )
52         self.PB_Ok.clicked.connect( self.theClose )
53         self.PB_Save.clicked.connect( self.saveFile )
54         self.PB_Save.setToolTip("Save trace in log file")
55         self.PB_Ok.setToolTip("Close view")
56         self.monExe=QProcess(self)
57
58         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
59         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
60
61         """ for test set environment
62         env = QProcessEnvironment().systemEnvironment()
63         env.insert("HELLO", "bonjour") #Add an environment variable for debug
64         self.monExe.setProcessEnvironment(env)
65         if verbose: 
66           PP.pprint([str(i) for i in sorted(self.monExe.processEnvironment().toStringList()) if 'DISTENE' in i])
67         """
68         
69         cmds = ''
70         ext = ''
71         if sys.platform == "win32":
72             cmds += 'delete %s\n' % self.parent().fichierOut
73             ext = '.bat'
74         else:
75             cmds += '#!/bin/bash\n'
76             cmds += 'pwd\n'
77             #cmds += 'which mg-cleaner.exe\n'
78             cmds += 'echo "DISTENE_LICENSE_FILE="$DISTENE_LICENSE_FILE\n'
79             cmds += 'echo "DLIM8VAR="$DLIM8VAR\n'
80             cmds += 'rm -f %s\n' % self.parent().fichierOut
81             ext = '.bash'
82
83         cmds += 'echo %s\n' % txt #to see what is compute command
84         cmds += txt+'\n'
85         cmds += 'echo "END_OF_MGCleaner"\n'
86         
87         nomFichier = os.path.splitext(self.parent().fichierOut)[0] + ext
88         with open(nomFichier, 'w') as f:
89           f.write(cmds)
90         self.make_executable(nomFichier)
91         
92         if verbose: print("INFO: MGCleaner launch script file: %s" % nomFichier)
93         
94         self.monExe.start(nomFichier)
95         self.monExe.closeWriteChannel()
96         self.enregistreResultatsDone=False
97         self.show()
98
99     def make_executable(self, path):
100         mode = os.stat(path).st_mode
101         mode |= (mode & 0o444) >> 2    # copy R bits to X
102         os.chmod(path, mode)
103
104     def saveFile(self):
105         #recuperation du nom du fichier
106         savedir=os.environ['HOME']
107         fn = QFileDialog.getSaveFileName(None, self.trUtf8("Save File"),savedir)
108         if fn.isNull() : return
109         ulfile = os.path.abspath(unicode(fn))
110         try:
111            f = open(fn, 'wb')
112            f.write(str(self.TB_Exe.toPlainText()))
113            f.close()
114         except IOError, why:
115            QMessageBox.critical(self, self.trUtf8('Save File'),
116                 self.trUtf8('The file <b>%1</b> could not be saved.<br>Reason: %2')
117                     .arg(unicode(fn)).arg(str(why)))
118
119     def readFromStdErr(self):
120         a=self.monExe.readAllStandardError()
121         self.TB_Exe.append(unicode(a.data()))
122
123     def readFromStdOut(self) :
124         a=self.monExe.readAllStandardOutput()
125         aa=unicode(a.data())
126         self.TB_Exe.append(aa)
127         if "END_OF_MGCleaner" in aa:
128           self.parent().enregistreResultat()
129           self.enregistreResultatsDone=True
130           #self.theClose()
131     
132     def theClose(self):
133       if not self.enregistreResultatsDone:
134         self.parent().enregistreResultat()
135         self.enregistreResultatsDone=True
136       self.close()