Salome HOME
Integration of [CEA 13233] support new xdr.h location
[modules/kernel.git] / bin / salome_utils.py
index 1933c1b568189b1d22ac539224085b94e4ec8525..80c2adac9fe700dd0ac54f11f299f4892cf62daf 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -56,10 +56,9 @@ def _try_bool( arg ):
     are supported.
     If <arg> does not represent a boolean, an exception is raised.
     """
-    import types
-    if type( arg ) == types.BooleanType  :
+    if isinstance(arg, bool)  :
         return arg
-    elif type( arg ) == types.StringType  :
+    elif isinstance(arg, (str, bytes)):
         v = str( arg ).lower()
         if   v in [ "yes", "y", "true"  ]: return True
         elif v in [ "no",  "n", "false" ]: return False
@@ -148,7 +147,6 @@ def getHostName():
     3. if fails, try HOST environment variable
     4. if fails, return 'unknown' as default host name
     """
-    import os
     try:
         import socket
         host = socket.gethostname()
@@ -158,6 +156,11 @@ def getHostName():
     if not host: host = os.getenv("HOSTNAME")
     if not host: host = os.getenv("HOST")
     if not host: host = "unknown"           # 'unknown' is default host name
+    try:
+        socket.gethostbyname(host)
+    except:
+        host = "localhost"
+    pass
     return host
 
 # ---
@@ -241,7 +244,7 @@ def getTmpDir():
 def generateFileName( dir, prefix = None, suffix = None, extension = None,
                       unique = False, separator = "_", hidden = False, **kwargs ):
     """
-    Generate file name by sepecified parameters. If necessary, file name
+    Generate file name by specified parameters. If necessary, file name
     can be generated to be unique.
 
     Parameters:
@@ -290,7 +293,7 @@ def generateFileName( dir, prefix = None, suffix = None, extension = None,
     ### check unsupported parameters
     for kw in kwargs:
         if kw not in supported and verbose():
-            print 'Warning! salome_utilitie.py: generateFileName(): parameter %s is not supported' % kw
+            print('Warning! salome_utilitie.py: generateFileName(): parameter %s is not supported' % kw)
             pass
         pass
     ### process supported keywords
@@ -371,7 +374,7 @@ def generateFileName( dir, prefix = None, suffix = None, extension = None,
 
 # ---
 
-def makeTmpDir( path, mode=0777 ):
+def makeTmpDir( path, mode=0o777 ):
     """
     Make temporary directory with the specified path.
     If the directory exists then clear its contents.
@@ -506,12 +509,12 @@ def killpid(pid, sig = 9):
     if not pid: return
     import os, sys
     if sig != 0:
-        if verbose(): print "######## killpid pid = ", pid
+        if verbose(): print("######## killpid pid = ", pid)
     try:
         if sys.platform == "win32":
             import ctypes
             if sig == 0:
-                # PROCESS_QUERY_INFORMATION (0x0400)   Required to retrieve certain information about a process
+                # PROCESS_QUERY_INFORMATION (0x0400)    Required to retrieve certain information about a process
                 handle = ctypes.windll.kernel32.OpenProcess(0x0400, False, int(pid))
                 if handle: 
                     ret = 1
@@ -519,7 +522,7 @@ def killpid(pid, sig = 9):
                 else:
                     ret = 0
             if sig == 9:
-                # PROCESS_TERMINATE (0x0001)   Required to terminate a process using TerminateProcess.
+                # PROCESS_TERMINATE (0x0001)    Required to terminate a process using TerminateProcess.
                 handle = ctypes.windll.kernel32.OpenProcess(0x0001, False, int(pid))
                 ret = ctypes.windll.kernel32.TerminateProcess(handle, -1)
                 ctypes.windll.kernel32.CloseHandle(handle)
@@ -531,7 +534,7 @@ def killpid(pid, sig = 9):
             ret = 1
             pass
         pass
-    except OSError, e:
+    except OSError as e:
         # errno.ESRCH == 3 is 'No such process'
         if e.errno == 3:
             ret = 0