From: abd Date: Fri, 3 Oct 2008 08:50:09 +0000 (+0000) Subject: Win32 Porting X-Git-Tag: RELIQUAT_4x_25102008~23 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2b4d5520dd97f0dc2ec196efff6edea71404e346;p=modules%2Fkernel.git Win32 Porting Renaming of salome_utilities.py to salome_utils.py because it conflicts in name with SALOME_utilities.py from Utils package (Win32 python is not case sensitive for file system) --- diff --git a/bin/Makefile.am b/bin/Makefile.am index 077743e33..328b52a91 100644 --- a/bin/Makefile.am +++ b/bin/Makefile.am @@ -61,7 +61,7 @@ dist_salomescript_SCRIPTS = \ runSalome.py \ salomeConsole.py \ salome_session.py \ - salome_utilities.py \ + salome_utils.py \ server.py \ setenv.py \ showNS.py \ diff --git a/bin/NSparam.py b/bin/NSparam.py index 2f93c9e54..e209063ff 100755 --- a/bin/NSparam.py +++ b/bin/NSparam.py @@ -31,7 +31,7 @@ def getNSparams(info=""): elif info==port print host else print 2 strings on stdout on one line: host port """ - from salome_utilities import getORBcfgInfo + from salome_utils import getORBcfgInfo my_version, my_host, my_port = getORBcfgInfo() if info=='host': # keep print, stdout used in shell diff --git a/bin/addToKillList.py b/bin/addToKillList.py index 548a8958d..e13aebc5d 100755 --- a/bin/addToKillList.py +++ b/bin/addToKillList.py @@ -29,7 +29,7 @@ def findFileDict(): Detect current SALOME session's port number. Returns port number. """ - from salome_utilities import getPortNumber + from salome_utils import getPortNumber port = getPortNumber() if verbose(): print "myport = ", port return port diff --git a/bin/killSalomeWithPort.py b/bin/killSalomeWithPort.py index 2308505a0..748d8e9d9 100755 --- a/bin/killSalomeWithPort.py +++ b/bin/killSalomeWithPort.py @@ -39,7 +39,7 @@ def getPiDict(port,appname='salome',full=True,hidden=True): - hidden : if True, file name is prefixed with . (dot) symbol; this internal parameter is used to support compatibility with older versions of SALOME """ - from salome_utilities import generateFileName, getTmpDir + from salome_utils import generateFileName, getTmpDir dir = "" if full: # full path to the pidict file is requested @@ -68,7 +68,7 @@ def appliCleanOmniOrbConfig(port): - ${HOME}/${APPLI}/.omniORB_last.cfg the last is removed only if the link points to the first file. """ - from salome_utilities import generateFileName + from salome_utils import generateFileName home = os.getenv("HOME") appli = os.getenv("APPLI") if appli is None: @@ -115,7 +115,7 @@ def killMyPort(port): try: fpid = open(filedict, 'r') # - from salome_utilities import generateFileName + from salome_utils import generateFileName fpidomniNames = generateFileName(os.path.join('/tmp/logs', os.getenv('USER')), prefix="", suffix="Pid_omniNames", diff --git a/bin/runSalome.py b/bin/runSalome.py index aae7d0dfa..334f5bafb 100755 --- a/bin/runSalome.py +++ b/bin/runSalome.py @@ -204,7 +204,7 @@ class LoggerServer(Server): def __init__(self,args): self.args=args self.initArgs() - from salome_utilities import generateFileName + from salome_utils import generateFileName if sys.platform == "win32": dirpath = os.environ["HOME"] else: dirpath = "/tmp" logfile = generateFileName( dirpath, @@ -749,7 +749,7 @@ def searchFreePort(args, save_config=1): if not portIsUsed(NSPORT, ports): print "%s - OK"%(NSPORT) # - from salome_utilities import generateFileName, getHostName + from salome_utils import generateFileName, getHostName hostname = getHostName() # home = os.getenv("HOME") @@ -831,7 +831,7 @@ def no_main(): def main(): """Salome launch as a main application""" import sys - from salome_utilities import getHostName + from salome_utils import getHostName print "runSalome running on %s" % getHostName() args, modules_list, modules_root_dir = setenv.get_config() kill_salome(args) diff --git a/bin/salome_session.py b/bin/salome_session.py index 64e13c951..cbf5a136f 100644 --- a/bin/salome_session.py +++ b/bin/salome_session.py @@ -39,7 +39,7 @@ def getShortHostName(): socket.gethostname() gives short or complete Hostname, depending on defined aliases. """ - from salome_utilities import getShortHostName + from salome_utils import getShortHostName return getShortHostName() def searchFreePort(): @@ -48,7 +48,7 @@ def searchFreePort(): Returns first found free port number. """ print "Searching a free port for naming service:", - from salome_utilities import generateFileName, getHostName + from salome_utils import generateFileName, getHostName hostname = getHostName() NSPORT = 2810 limit = NSPORT+100 diff --git a/bin/salome_utilities.py b/bin/salome_utilities.py deleted file mode 100644 index b4349cd20..000000000 --- a/bin/salome_utilities.py +++ /dev/null @@ -1,346 +0,0 @@ -# Copyright (C) 2005 OPEN CASCADE, CEA, EDF R&D, LEG -# PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License. -# -# This library is distributed in the hope that it will be useful -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -# -# --- -# -# File : salome_utilities.py -# Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com) -# -# --- - -""" -Set of utility functions used by SALOME python scripts. -""" - -# -# Exported functions -# -__all__ = [ - 'getORBcfgInfo', - 'getHostFromORBcfg', - 'getPortFromORBcfg', - 'getUserName', - 'getHostName', - 'getShortHostName', - 'getAppName', - 'getPortNumber', - 'getTmpDir', - 'generateFileName', - ] - -# --- - -def _try_bool( arg ): - """ - Check if specified parameter represents boolean value and returns its value. - String values like 'True', 'TRUE', 'YES', 'Yes', 'y', 'NO', 'false', 'n', etc - are supported. - If does not represent a boolean, an exception is raised. - """ - import types - if type( arg ) == types.BooleanType : - return arg - elif type( arg ) == types.StringType : - v = str( arg ).lower() - if v in [ "yes", "y", "true" ]: return True - elif v in [ "no", "n", "false" ]: return False - pass - raise Exception("Not boolean value") - -# --- - -def getORBcfgInfo(): - """ - Get omniORB current configuration. - Returns a list of three values: [ orb_version, host_name, port_number ]. - - The information is retrieved from the omniORB configuration file defined - by the OMNIORB_CONFIG environment variable. - If omniORB configuration file can not be accessed, a list of three empty - strings is returned. - """ - import os, re - ret = [ "", "", "" ] - try: - f = open( os.getenv( "OMNIORB_CONFIG" ) ) - lines = f.readlines() - f.close() - regvar = re.compile( "(ORB)?InitRef.*corbaname::(.*):(\d+)\s*$" ) - for l in lines: - try: - m = regvar.match( l ) - if m: - if m.group(1) is None: - ret[0] = "4" - else: - ret[0] = "3" - pass - ret[1] = m.group(2) - ret[2] = m.group(3) - break - pass - except: - pass - pass - pass - except: - pass - return ret - -# --- - -def getHostFromORBcfg(): - """ - Get current omniORB host. - """ - return getORBcfgInfo()[1] -# --- - -def getPortFromORBcfg(): - """ - Get current omniORB port. - """ - return getORBcfgInfo()[2] - -# --- - -def getUserName(): - """ - Get user name: - 1. try USER environment variable - 2. if fails, return 'unknown' as default user name - """ - import os - return os.getenv( "USER", "unknown" ) # 'unknown' is default user name - -# --- - -def getHostName(): - """ - Get host name: - 1. try socket python module gethostname() function - 2. if fails, try HOSTNAME environment variable - 3. if fails, try HOST environment variable - 4. if fails, return 'unknown' as default host name - """ - import os - try: - import socket - host = socket.gethostname() - except: - host = None - pass - if not host: host = os.getenv("HOSTNAME") - if not host: host = os.getenv("HOST") - if not host: host = "unknown" # 'unknown' is default host name - return host - -# --- - -def getShortHostName(): - """ - Get short host name: - 1. try socket python module gethostname() function - 2. if fails, try HOSTNAME environment variable - 3. if fails, try HOST environment variable - 4. if fails, return 'unknown' as default host name - """ - try: - return getHostName().split('.')[0] - except: - pass - return "unknown" # 'unknown' is default host name - -# --- - -def getAppName(): - """ - Get application name: - 1. try APPNAME environment variable - 2. if fails, return 'SALOME' as default application name - """ - import os - return os.getenv( "APPNAME", "SALOME" ) # 'SALOME' is default user name - -# --- - -def getPortNumber(): - """ - Get current naming server port number: - 1. try NSPORT environment variable - 1. if fails, try to parse config file defined by OMNIORB_CONFIG environment variable - 2. if fails, return 2809 as default port number - """ - import os - try: - return int( os.getenv( "NSPORT" ) ) - except: - pass - port = getPortFromORBcfg() - if port is not None: return port - return 2809 # '2809' is default port number - -# --- - -def getTmpDir(): - """ - Get directory to be used for the temporary files. - """ - import os, sys - if sys.platform == "win32": - # for Windows: temporarily using home directory for tmp files; - # to be replaced with TEMP environment variable later... - dir = os.getenv("HOME") - else: - # for Linux: use /tmp/logs/{user} folder - dir = os.path.join( '/tmp', 'logs', getUserName() ) - pass - return dir - -# --- - -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 - can be generated to be unique. - - Parameters: - - dir : directory path - - prefix : file prefix (not added by default) - - suffix : file suffix (not added by default) - - extension : file extension (not added by default) - - unique : if this parameter is True, the unique file name is generated: - in this case, if the file with the generated name already exists - in the directory, an integer suffix is added to the end of the - file name. This parameter is False by default. - - separator : separator of the words ('_' by default) - - hidden : if this parameter is True, the file name is prepended by . (dot) - symbol. This parameter is False by default. - - Other keyword parameters are: - - with_username : 'add user name' flag/option: - * boolean value can be passed to determine user name automatically - * string value to be used as user name - - with_hostname : 'add host name' flag/option: - * boolean value can be passed to determine host name automatically - * string value to be used as host name - - with_port : 'add port number' flag/option: - * boolean value can be passed to determine port number automatically - * string value to be used as port number - - with_app : 'add application name' flag/option: - * boolean value can be passed to determine application name automatically - * string value to be used as application name - All parameters are optional. - """ - supported = [ 'with_username', 'with_hostname', 'with_port', 'with_app' ] - from launchConfigureParser import verbose - filename = [] - # separator - if separator is None: - separator = "" - pass - else: - separator = str( separator ) - pass - # prefix (if specified) - if prefix is not None: - filename.append( str( prefix ) ) - pass - # additional keywords - ### 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 - pass - pass - ### process supported keywords - for kw in supported: - if kw not in kwargs: continue - ### user name - if kw == 'with_username': - try: - # auto user name ? - if _try_bool( kwargs[kw] ): filename.append( getUserName() ) - pass - except: - # user name given as parameter - filename.append( kwargs[kw] ) - pass - pass - ### host name - elif kw == 'with_hostname': - try: - # auto host name ? - if _try_bool( kwargs[kw] ): filename.append( getShortHostName() ) - pass - except: - # host name given as parameter - filename.append( kwargs[kw] ) - pass - pass - ### port number - elif kw == 'with_port': - try: - # auto port number ? - if _try_bool( kwargs[kw] ): filename.append( str( getPortNumber() ) ) - pass - except: - # port number given as parameter - filename.append( str( kwargs[kw] ) ) - pass - pass - ### application name - elif kw == 'with_app': - try: - # auto application name ? - if _try_bool( kwargs[kw] ): filename.append( getAppName() ) - pass - except: - # application name given as parameter - filename.append( kwargs[kw] ) - pass - pass - pass - # suffix (if specified) - if suffix is not None: - filename.append( str( suffix ) ) - pass - # raise an exception if file name is empty - if not filename: - raise Exception("Empty file name") - # - if extension is not None and extension.startswith("."): extension = extension[1:] - # - import os - name = separator.join( filename ) - if hidden: name = "." + name # add dot for hidden files - if extension: name = name + "." + str( extension ) # add extension if defined - name = os.path.join( dir, name ) - if unique: - # create unique file name - index = 0 - while os.path.exists( name ): - index = index + 1 - name = separator.join( filename ) + separator + str( index ) - if hidden: name = "." + name # add dot for hidden files - if extension: name = name + "." + str( extension ) # add extension if defined - name = os.path.join( dir, name ) - pass - pass - return name diff --git a/bin/salome_utils.py b/bin/salome_utils.py new file mode 100644 index 000000000..7b3aadeac --- /dev/null +++ b/bin/salome_utils.py @@ -0,0 +1,346 @@ +# Copyright (C) 2005 OPEN CASCADE, CEA, EDF R&D, LEG +# PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# --- +# +# File : salome_utils.py +# Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com) +# +# --- + +""" +Set of utility functions used by SALOME python scripts. +""" + +# +# Exported functions +# +__all__ = [ + 'getORBcfgInfo', + 'getHostFromORBcfg', + 'getPortFromORBcfg', + 'getUserName', + 'getHostName', + 'getShortHostName', + 'getAppName', + 'getPortNumber', + 'getTmpDir', + 'generateFileName', + ] + +# --- + +def _try_bool( arg ): + """ + Check if specified parameter represents boolean value and returns its value. + String values like 'True', 'TRUE', 'YES', 'Yes', 'y', 'NO', 'false', 'n', etc + are supported. + If does not represent a boolean, an exception is raised. + """ + import types + if type( arg ) == types.BooleanType : + return arg + elif type( arg ) == types.StringType : + v = str( arg ).lower() + if v in [ "yes", "y", "true" ]: return True + elif v in [ "no", "n", "false" ]: return False + pass + raise Exception("Not boolean value") + +# --- + +def getORBcfgInfo(): + """ + Get omniORB current configuration. + Returns a list of three values: [ orb_version, host_name, port_number ]. + + The information is retrieved from the omniORB configuration file defined + by the OMNIORB_CONFIG environment variable. + If omniORB configuration file can not be accessed, a list of three empty + strings is returned. + """ + import os, re + ret = [ "", "", "" ] + try: + f = open( os.getenv( "OMNIORB_CONFIG" ) ) + lines = f.readlines() + f.close() + regvar = re.compile( "(ORB)?InitRef.*corbaname::(.*):(\d+)\s*$" ) + for l in lines: + try: + m = regvar.match( l ) + if m: + if m.group(1) is None: + ret[0] = "4" + else: + ret[0] = "3" + pass + ret[1] = m.group(2) + ret[2] = m.group(3) + break + pass + except: + pass + pass + pass + except: + pass + return ret + +# --- + +def getHostFromORBcfg(): + """ + Get current omniORB host. + """ + return getORBcfgInfo()[1] +# --- + +def getPortFromORBcfg(): + """ + Get current omniORB port. + """ + return getORBcfgInfo()[2] + +# --- + +def getUserName(): + """ + Get user name: + 1. try USER environment variable + 2. if fails, return 'unknown' as default user name + """ + import os + return os.getenv( "USER", "unknown" ) # 'unknown' is default user name + +# --- + +def getHostName(): + """ + Get host name: + 1. try socket python module gethostname() function + 2. if fails, try HOSTNAME environment variable + 3. if fails, try HOST environment variable + 4. if fails, return 'unknown' as default host name + """ + import os + try: + import socket + host = socket.gethostname() + except: + host = None + pass + if not host: host = os.getenv("HOSTNAME") + if not host: host = os.getenv("HOST") + if not host: host = "unknown" # 'unknown' is default host name + return host + +# --- + +def getShortHostName(): + """ + Get short host name: + 1. try socket python module gethostname() function + 2. if fails, try HOSTNAME environment variable + 3. if fails, try HOST environment variable + 4. if fails, return 'unknown' as default host name + """ + try: + return getHostName().split('.')[0] + except: + pass + return "unknown" # 'unknown' is default host name + +# --- + +def getAppName(): + """ + Get application name: + 1. try APPNAME environment variable + 2. if fails, return 'SALOME' as default application name + """ + import os + return os.getenv( "APPNAME", "SALOME" ) # 'SALOME' is default user name + +# --- + +def getPortNumber(): + """ + Get current naming server port number: + 1. try NSPORT environment variable + 1. if fails, try to parse config file defined by OMNIORB_CONFIG environment variable + 2. if fails, return 2809 as default port number + """ + import os + try: + return int( os.getenv( "NSPORT" ) ) + except: + pass + port = getPortFromORBcfg() + if port is not None: return port + return 2809 # '2809' is default port number + +# --- + +def getTmpDir(): + """ + Get directory to be used for the temporary files. + """ + import os, sys + if sys.platform == "win32": + # for Windows: temporarily using home directory for tmp files; + # to be replaced with TEMP environment variable later... + dir = os.getenv("HOME") + else: + # for Linux: use /tmp/logs/{user} folder + dir = os.path.join( '/tmp', 'logs', getUserName() ) + pass + return dir + +# --- + +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 + can be generated to be unique. + + Parameters: + - dir : directory path + - prefix : file prefix (not added by default) + - suffix : file suffix (not added by default) + - extension : file extension (not added by default) + - unique : if this parameter is True, the unique file name is generated: + in this case, if the file with the generated name already exists + in the directory, an integer suffix is added to the end of the + file name. This parameter is False by default. + - separator : separator of the words ('_' by default) + - hidden : if this parameter is True, the file name is prepended by . (dot) + symbol. This parameter is False by default. + + Other keyword parameters are: + - with_username : 'add user name' flag/option: + * boolean value can be passed to determine user name automatically + * string value to be used as user name + - with_hostname : 'add host name' flag/option: + * boolean value can be passed to determine host name automatically + * string value to be used as host name + - with_port : 'add port number' flag/option: + * boolean value can be passed to determine port number automatically + * string value to be used as port number + - with_app : 'add application name' flag/option: + * boolean value can be passed to determine application name automatically + * string value to be used as application name + All parameters are optional. + """ + supported = [ 'with_username', 'with_hostname', 'with_port', 'with_app' ] + from launchConfigureParser import verbose + filename = [] + # separator + if separator is None: + separator = "" + pass + else: + separator = str( separator ) + pass + # prefix (if specified) + if prefix is not None: + filename.append( str( prefix ) ) + pass + # additional keywords + ### 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 + pass + pass + ### process supported keywords + for kw in supported: + if kw not in kwargs: continue + ### user name + if kw == 'with_username': + try: + # auto user name ? + if _try_bool( kwargs[kw] ): filename.append( getUserName() ) + pass + except: + # user name given as parameter + filename.append( kwargs[kw] ) + pass + pass + ### host name + elif kw == 'with_hostname': + try: + # auto host name ? + if _try_bool( kwargs[kw] ): filename.append( getShortHostName() ) + pass + except: + # host name given as parameter + filename.append( kwargs[kw] ) + pass + pass + ### port number + elif kw == 'with_port': + try: + # auto port number ? + if _try_bool( kwargs[kw] ): filename.append( str( getPortNumber() ) ) + pass + except: + # port number given as parameter + filename.append( str( kwargs[kw] ) ) + pass + pass + ### application name + elif kw == 'with_app': + try: + # auto application name ? + if _try_bool( kwargs[kw] ): filename.append( getAppName() ) + pass + except: + # application name given as parameter + filename.append( kwargs[kw] ) + pass + pass + pass + # suffix (if specified) + if suffix is not None: + filename.append( str( suffix ) ) + pass + # raise an exception if file name is empty + if not filename: + raise Exception("Empty file name") + # + if extension is not None and extension.startswith("."): extension = extension[1:] + # + import os + name = separator.join( filename ) + if hidden: name = "." + name # add dot for hidden files + if extension: name = name + "." + str( extension ) # add extension if defined + name = os.path.join( dir, name ) + if unique: + # create unique file name + index = 0 + while os.path.exists( name ): + index = index + 1 + name = separator.join( filename ) + separator + str( index ) + if hidden: name = "." + name # add dot for hidden files + if extension: name = name + "." + str( extension ) # add extension if defined + name = os.path.join( dir, name ) + pass + pass + return name