Salome HOME
spns #26801 : bug sat package, le lanceur ne faisait pas le réinit
[tools/sat.git] / src / utilsSat.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 #  Copyright (C) 2010-2018  CEA/DEN
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19
20 """
21 utilities for sat
22 general useful simple methods
23 all-in-one import srs.utilsSat as UTS
24
25 | Usage:
26 | >> import srsc.utilsSat as UTS
27 | >> UTS.Popen('ls && etc...', ...)
28 """
29
30 import os
31 import shutil
32 import errno
33 import stat
34 import time
35
36 import re
37 import tempfile
38 import subprocess as SP
39
40 import src.returnCode as RCO
41 import src.debug as DBG # Easy print stderr (for DEBUG only)
42
43
44 ##############################################################################
45 # subprocess utilities, with logger functionnalities (trace etc.)
46 ##############################################################################
47     
48 def Popen(command, shell=True, cwd=None, env=None, stdout=SP.PIPE, stderr=SP.PIPE, logger=None):
49   """
50   make subprocess.Popen(cmd), with 
51   call logger.trace and logger.error if problem as returncode != 0 
52   """
53   if True: #try:  
54     proc = SP.Popen(command, shell=shell, cwd=cwd, env=env, stdout=stdout, stderr=SP.STDOUT)
55     res_out, res_err = proc.communicate() # res_err = None as stderr=SP.STDOUT
56     rc = proc.returncode
57     
58     DBG.write("Popen logger returncode", (rc, res_out))
59     
60     if rc == 0:
61       if logger is not None:
62         logger.trace("<OK> launch command rc=%s cwd=%s:\n%s" % (rc, cwd, command))
63         logger.trace("<OK> result command stdout&stderr:\n%s" % res_out)
64       return RCO.ReturnCode("OK", "Popen command done", value=res_out)
65     else:
66       if logger is not None:
67         logger.warning("<KO> launch command rc=%s cwd=%s:\n%s" % (rc, cwd, command))
68         logger.warning("<KO> result command stdout&stderr:\n%s" % res_out)
69       return RCO.ReturnCode("KO", "Popen command problem", value=res_out)
70   else: #except Exception as e:
71     logger.error("<KO> launch command cwd=%s:\n%s" % (cwd, command))
72     logger.error("launch command exception:\n%s" % e)
73     return RCO.ReturnCode("KO", "Popen command problem")
74
75
76 def sleep(sec):
77     time.sleep(sec)