Back to index

salome-kernel  6.5.0
addToKillList.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 import os, sys, pickle, string, signal
00026 from launchConfigureParser import verbose
00027 
00028 ########## adds to the kill list of SALOME one more process ##########
00029 
00030 def findFileDict():
00031     """
00032     Detect current SALOME session's port number.
00033     Returns port number.
00034     """
00035     from salome_utils import getPortNumber
00036     port = getPortNumber()
00037     if verbose(): print "myport = ", port
00038     return port
00039     
00040 def addToKillList(command_pid, command, port=None):
00041     """
00042     Add the process to the SALOME processes dictionary file.
00043     Parameters:
00044     - command_pid : command PID
00045     - command     : command (string or list of strings)
00046     - [port]      : SALOME port number; if this parameter is None (default),
00047     it is detected automatically
00048     """
00049     # retrieve current processes dictionary
00050     from killSalomeWithPort import getPiDict
00051     if port is None: port=findFileDict()
00052     filedict=getPiDict(port)
00053     try:
00054         fpid=open(filedict, 'r')
00055         process_ids=pickle.load(fpid)
00056         fpid.close()
00057     except:
00058         process_ids=[]
00059         pass
00060     # check if PID is already in dictionary
00061     already_in=False
00062     for process_id in process_ids:
00063         for pid, cmd in process_id.items():
00064             if int(pid) == int(command_pid):
00065                 already_in=True
00066                 break
00067             pass
00068         if already_in: break
00069         pass
00070     # add process to the dictionary
00071     if not already_in:
00072         import types
00073         if type(command) == types.ListType: command=" ".join([str(c) for c in command])
00074         command=command.split()[0]
00075         try:
00076             if verbose(): print "addToKillList: %s : %s" % ( str(command_pid), command )
00077             process_ids.append({int(command_pid): [command]})
00078             dir = os.path.dirname(filedict)
00079             if not os.path.exists(dir): os.makedirs(dir, 0777)
00080             fpid = open(filedict,'w')
00081             pickle.dump(process_ids, fpid)
00082             fpid.close()
00083         except:
00084             if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
00085             pass
00086         pass
00087     pass
00088 
00089 def killList(port=None):
00090     """
00091     Kill all the processes listed in the SALOME processes dictionary file.
00092     - [port]      : SALOME port number; if this parameter is None (default),
00093     it is detected automatically
00094     """
00095     # retrieve processes dictionary
00096     from killSalomeWithPort import getPiDict
00097     if port is None: port=findFileDict()
00098     # new-style dot-prefixed pidict file
00099     filedict=getPiDict(port, hidden=True)
00100     # provide compatibility with old-style pidict file (not dot-prefixed)
00101     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
00102     try:
00103         fpid=open(filedict, 'r')
00104         process_ids=pickle.load(fpid)
00105         fpid.close()
00106     except:
00107         process_ids=[]
00108         pass
00109     # kill processes
00110     for process_id in process_ids:
00111         #print process_id
00112         for pid, cmd in process_id.items():
00113             print "stop process %s : %s"% (pid, cmd[0])
00114             try:
00115                 os.kill(int(pid),signal.SIGKILL)
00116             except:
00117                 print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
00118                 pass
00119             pass
00120         pass
00121     # remove processes dictionary file
00122     os.remove(filedict)
00123     pass
00124 
00125 if __name__ == "__main__":
00126     if verbose(): print sys.argv
00127     addToKillList(sys.argv[1], sys.argv[2])
00128     pass