4 # Copyright (C) 2010-2018 CEA/DEN
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.
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.
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
22 general useful simple methods
23 all-in-one import srs.utilsSat as UTS
26 | >> import srsc.utilsSat as UTS
27 | >> UTS.Popen('ls && etc...', ...)
38 import subprocess as SP
40 import src.returnCode as RCO
41 import src.debug as DBG # Easy print stderr (for DEBUG only)
44 ##############################################################################
45 # subprocess utilities, with logger functionnalities (trace etc.)
46 ##############################################################################
48 def Popen(command, shell=True, cwd=None, env=None, stdout=SP.PIPE, stderr=SP.PIPE, logger=None):
50 make subprocess.Popen(cmd), with
51 call logger.trace and logger.error if problem as returncode != 0
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
58 DBG.write("Popen logger returncode", (rc, res_out))
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)
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")