- fpidomniNamesFile = open(fpidomniNames)
- lines = fpidomniNamesFile.readlines()
- fpidomniNamesFile.close()
- os.remove(fpidomniNames)
- for l in lines:
- try:
- pidfield = l.split()[0] # pid should be at the first position
- if sys.platform == "win32":
- import win32pm
- if verbose(): print 'stop process '+pidfield+' : omniNames'
- win32pm.killpid(int(pidfield),0)
- else:
- if verbose(): print 'stop process '+pidfield+' : omniNames'
- os.kill(int(pidfield),signal.SIGKILL)
- pass
- pass
- except:
- pass
- pass
- pass
- except:
- pass
- #
+ processes.append(psutil.Process(pid))
+ except psutil.NoSuchProcess:
+ if verbose():
+ print(" ------------------ Process {} not found".format(pid))
+ __killProcesses(processes)
+
+def __killMyPort(port, filedict):
+ """
+ Kill processes for given port (internal).
+ :param port : port number
+ :param filedict : pidict file
+ """
+ # ensure port is an integer
+ with suppress(ValueError):
+ port = int(port)
+
+ # read pids from pidict file
+ with suppress(Exception), open(filedict, 'rb') as fpid:
+ pids_lists = pickle.load(fpid)
+ # note: pids_list is a list of tuples!
+ for pids in pids_lists:
+ __killPids(pids)
+
+ # finally remove pidict file
+ os.remove(filedict)
+
+def __guessPiDictFilename(port):
+ """
+ Guess and return pidict file for given `port` (internal).
+ :param port : port number
+ :return pidict file's path
+ """
+ # Check all possible versions of pidict file
+ # new-style - dot-prefixed pidict file: hidden is True, auto hostname
+ # old-style - not dot-prefixed pidict file: hidden is False, auto hostname
+ # old-style - dot-prefixed pidict file: hidden is True, short hostname
+ # old-style - not dot-prefixed pidict file: hidden is False, short hostname
+ # old-style - dot-prefixed pidict file: hidden is True, long hostname
+ # old-style - not dot-prefixed pidict file: hidden is False, long hostname
+ for hostname, hidden in itertools.product((None, getShortHostName(), getHostName()),
+ (True, False)):
+ filedict = getPiDict(port, hidden=hidden, hostname=hostname)
+ if not osp.exists(filedict):
+ if verbose():
+ print('Trying {}... not found'.format(filedict))
+ continue
+ if verbose():
+ print('Trying {}... OK'.format(filedict))
+ return filedict
+
+ return None
+
+def killMyPort(*ports):
+ """
+ Kill SALOME session running on the specified port.
+ :param ports : port numbers
+ """
+ for port in ports:
+ # ensure port is an integer
+ with suppress(ValueError):
+ port = int(port)
+
+ with suppress(Exception):
+ # DO NOT REMOVE NEXT LINE: it tests PortManager availability!
+ from PortManager import releasePort
+ # get pidict file
+ filedict = getPiDict(port)
+ if not osp.isfile(filedict): # removed by previous call, see (1) above
+ if verbose():
+ print("SALOME session on port {} is already stopped".format(port))
+ # remove port from PortManager config file
+ with suppress(ImportError):
+ if verbose():
+ print("Removing port from PortManager configuration file")
+ releasePort(port)
+ return
+
+ # try to shutdown session normally
+ Thread(target=shutdownMyPort, args=(port, True)).start()
+ # wait a little...
+ sleep(3)
+ # ... then kill processes (should be done if shutdown procedure hangs up)