Salome HOME
Fix bugs with Cleaner and SurfOpt on Windows:
[modules/smesh.git] / src / Tools / YamsPlug / monViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-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 import os
22 import sys
23 import string
24 import types
25 import tempfile
26 import traceback
27 import pprint as PP #pretty print
28
29 from qtsalome import *
30
31 # Import des panels
32 from ViewText_ui import Ui_ViewExe
33
34 verbose = True
35
36 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
37 if force != None:
38   os.environ["DISTENE_LICENSE_FILE"] = force
39   os.environ["DLIM8VAR"] = "NOTHING"
40
41 class MonViewText(Ui_ViewExe, QDialog):
42     """
43     Classe permettant la visualisation de texte
44     """
45     def __init__(self, parent, txt):
46         QDialog.__init__(self,parent)
47         self.setupUi(self)
48         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
49         # self.PB_Ok.clicked.connect(self.close)
50         self.PB_Ok.clicked.connect( self.theClose )
51         self.PB_Save.clicked.connect( self.saveFile )
52         self.monExe=QProcess(self)
53
54         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
55         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
56         self.monExe.finished.connect( self.finished )
57       
58         cmds = ''
59         ext = ''
60         if sys.platform == "win32":
61             if os.path.exists(self.parent().fichierOut):
62                 cmds += 'del %s\n' % self.parent().fichierOut
63             ext = '.bat'
64         else:
65             cmds += '#!/bin/bash\n'
66             cmds += 'pwd\n'
67             #cmds += 'which mg-surfopt.exe\n'
68             cmds += 'echo "DISTENE_LICENSE_FILE="$DISTENE_LICENSE_FILE\n'
69             cmds += 'echo "DLIM8VAR="$DLIM8VAR\n'
70             cmds += 'rm -f %s\n' % self.parent().fichierOut
71             ext = '.bash'
72
73         cmds += 'echo %s\n' % txt #to see what is compute command
74         cmds += txt+'\n'
75         cmds += 'echo "END_OF_MGSurfOpt"\n'
76         
77         nomFichier = os.path.splitext(self.parent().fichierOut)[0] + ext
78         with open(nomFichier, 'w') as f:
79           f.write(cmds)
80         self.make_executable(nomFichier)
81         
82         if verbose: print("INFO: MGSurfOpt launch script file: %s" % nomFichier)
83         
84         self.monExe.start(nomFichier)
85         self.monExe.closeWriteChannel()
86         self.enregistreResultatsDone=False
87         self.show()
88
89     def make_executable(self, path):
90         mode = os.stat(path).st_mode
91         mode |= (mode & 0o444) >> 2    # copy R bits to X
92         os.chmod(path, mode)
93
94     def saveFile(self):
95         #recuperation du nom du fichier
96         savedir=os.environ['HOME']
97         fn = QFileDialog.getSaveFileName(None,"Save File",savedir)
98         if fn.isNull() : return
99         ulfile = os.path.abspath(unicode(fn))
100         try:
101            f = open(fn, 'wb')
102            f.write(str(self.TB_Exe.toPlainText()))
103            f.close()
104         except IOError, why:
105            QMessageBox.critical(self, 'Save File',
106                 'The file <b>%1</b> could not be saved.<br>Reason: %2'%(unicode(fn), str(why)))
107
108     def readFromStdErr(self):
109         a=self.monExe.readAllStandardError()
110         self.TB_Exe.append(unicode(a.data().encode()))
111
112     def readFromStdOut(self) :
113         a=self.monExe.readAllStandardOutput()
114         aa=unicode(a.data())
115         self.TB_Exe.append(aa)    
116     
117     def finished(self):
118         self.parent().enregistreResultat()
119         self.enregistreResultatsDone=True
120     
121     def theClose(self):
122       if not self.enregistreResultatsDone:
123         self.parent().enregistreResultat()
124         self.enregistreResultatsDone=True
125       self.close()