Salome HOME
393c9e20973f39d211e0eda8d2258d72b6475b10
[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         self.monExe.finished.connect( self.finished )
61
62         """ for test set environment
63         env = QProcessEnvironment().systemEnvironment()
64         env.insert("HELLO", "bonjour") #Add an environment variable for debug
65         self.monExe.setProcessEnvironment(env)
66         if verbose: 
67           PP.pprint([str(i) for i in sorted(self.monExe.processEnvironment().toStringList()) if 'DISTENE' in i])
68         """
69         
70         cmds = ''
71         ext = ''
72         if sys.platform == "win32":
73             if os.path.exists(self.parent().fichierOut):
74                 cmds += 'del %s\n' % self.parent().fichierOut
75             ext = '.bat'
76         else:
77             cmds += '#!/bin/bash\n'
78             cmds += 'pwd\n'
79             #cmds += 'which mg-cleaner.exe\n'
80             cmds += 'echo "DISTENE_LICENSE_FILE="$DISTENE_LICENSE_FILE\n'
81             cmds += 'echo "DLIM8VAR="$DLIM8VAR\n'
82             cmds += 'rm -f %s\n' % self.parent().fichierOut
83             ext = '.bash'
84
85         cmds += 'echo %s\n' % txt #to see what is compute command
86         cmds += txt+'\n'
87         cmds += 'echo "END_OF_MGCleaner"\n'
88         
89         nomFichier = os.path.splitext(self.parent().fichierOut)[0] + ext
90         with open(nomFichier, 'w') as f:
91           f.write(cmds)
92         self.make_executable(nomFichier)
93         
94         if verbose: print(("INFO: MGCleaner launch script file: %s" % nomFichier))
95         
96         self.monExe.start(nomFichier)
97         self.monExe.closeWriteChannel()
98         self.enregistreResultatsDone=False
99         self.show()
100
101     def make_executable(self, path):
102         mode = os.stat(path).st_mode
103         mode |= (mode & 0o444) >> 2    # copy R bits to X
104         os.chmod(path, mode)
105
106     def saveFile(self):
107         #recuperation du nom du fichier
108         savedir=os.environ['HOME']
109         fn = QFileDialog.getSaveFileName(None, self.trUtf8("Save File"),savedir)
110         if fn.isNull() : return
111         ulfile = os.path.abspath(str(fn))
112         try:
113            f = open(fn, 'wb')
114            f.write(str(self.TB_Exe.toPlainText()))
115            f.close()
116         except IOError as why:
117            QMessageBox.critical(self, self.trUtf8('Save File'),
118                 self.trUtf8('The file <b>%1</b> could not be saved.<br>Reason: %2')
119                     .arg(str(fn)).arg(str(why)))
120
121     def readFromStdErr(self):
122         a=self.monExe.readAllStandardError()
123         aa=a.data().decode(errors='ignore')
124         self.TB_Exe.append(aa)
125
126     def readFromStdOut(self) :
127         a=self.monExe.readAllStandardOutput()
128         aa=a.data().decode(errors='ignore')
129         self.TB_Exe.append(aa)
130     
131     def finished(self):
132         self.parent().enregistreResultat()
133         self.enregistreResultatsDone=True
134     
135     def theClose(self):
136       if not self.enregistreResultatsDone:
137         self.parent().enregistreResultat()
138         self.enregistreResultatsDone=True
139       self.close()