Salome HOME
[EDF28974] : Move to python implementation of driver and integrate monitoring into...
[modules/yacs.git] / src / yacsloader / driver
1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2024  CEA, EDF
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 import driver_internal
23 import subprocess as sp
24 import salome
25 import SALOME_Embedded_NamingService_ClientPy
26 import NamingService
27 from SALOME_PyNode import FileDeleter,FileHolder
28
29 import signal
30 import os
31
32 proc = None
33 ior_file = None
34
35 def handler(signum, frame):
36   global proc,ior_file
37   try:
38     ns = SALOME_Embedded_NamingService_ClientPy.SALOME_Embedded_NamingService_ClientPy( NamingService.NamingService.LoadIORInFile( ior_file.filename ) )
39     del ior_file
40     ior_file = None # to avoid raises in __main__ function
41     cm = ns.Resolve(salome.CM_NAME_IN_NS)
42     cm.ShutdownContainersNow()
43   except:
44     pass
45   os.kill( proc.pid, signal.SIGKILL )
46
47 def generateCmdForInternal():
48   import sys
49   import tempfile
50   global ior_file
51   args, _ = driver_internal.parseArgs()
52   iorNS = args[ driver_internal.IORKeyInARGS ]
53   firstPart = ["python3",driver_internal.__file__] 
54   lastPart = [elt for elt in sys.argv[1:] if elt != ""]
55   middlePart = []
56   #
57   if not iorNS:
58     with tempfile.NamedTemporaryFile(prefix="ior_driver_",suffix=".ior") as f:
59       ior_file = FileDeleter( f.name )
60     middlePart = [ "{}={}".format( driver_internal.IOREntryInCMD, ior_file.filename) ]
61   else:
62     ior_file = FileHolder( iorNS )
63   print(100*"*")
64   print( firstPart + middlePart + lastPart )
65   return firstPart + middlePart + lastPart
66
67 if __name__ == "__main__":
68   signal.signal(signal.SIGINT, handler)
69   signal.signal(signal.SIGTERM, handler)
70   cmd = generateCmdForInternal()
71   proc = sp.Popen( cmd )
72   proc.communicate()
73   del ior_file
74   code = proc.returncode
75   if code != 0:
76     raise RuntimeError(f"Subprocess finished with a non zero status ({code}). Command returning non zero status was : {cmd}")