1 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 # File : salome_utils.py
24 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
27 ## @package salome_utils
28 # \brief Set of utility functions used by SALOME python scripts.
52 Check if specified parameter represents boolean value and returns its value.
53 String values like 'True', 'TRUE', 'YES', 'Yes', 'y', 'NO', 'false', 'n', etc
55 If <arg> does not represent a boolean, an exception is raised.
58 if type( arg ) == types.BooleanType :
60 elif type( arg ) == types.StringType :
61 v = str( arg ).lower()
62 if v in [ "yes", "y", "true" ]: return True
63 elif v in [ "no", "n", "false" ]: return False
65 raise Exception("Not boolean value")
71 Get omniORB current configuration.
72 Returns a list of three values: [ orb_version, host_name, port_number ].
74 The information is retrieved from the omniORB configuration file defined
75 by the OMNIORB_CONFIG environment variable.
76 If omniORB configuration file can not be accessed, a list of three empty
82 f = open( os.getenv( "OMNIORB_CONFIG" ) )
85 regvar = re.compile( "(ORB)?InitRef.*corbaname::(.*):(\d+)\s*$" )
90 if m.group(1) is None:
109 def getHostFromORBcfg():
111 Get current omniORB host.
113 return getORBcfgInfo()[1]
116 def getPortFromORBcfg():
118 Get current omniORB port.
120 return getORBcfgInfo()[2]
127 1. try USER environment variable
128 2. if fails, return 'unknown' as default user name
131 return os.getenv( "USER", "unknown" ) # 'unknown' is default user name
138 1. try socket python module gethostname() function
139 2. if fails, try HOSTNAME environment variable
140 3. if fails, try HOST environment variable
141 4. if fails, return 'unknown' as default host name
146 host = socket.gethostname()
150 if not host: host = os.getenv("HOSTNAME")
151 if not host: host = os.getenv("HOST")
152 if not host: host = "unknown" # 'unknown' is default host name
157 def getShortHostName():
160 1. try socket python module gethostname() function
161 2. if fails, try HOSTNAME environment variable
162 3. if fails, try HOST environment variable
163 4. if fails, return 'unknown' as default host name
166 return getHostName().split('.')[0]
169 return "unknown" # 'unknown' is default host name
175 Get application name:
176 1. try APPNAME environment variable
177 2. if fails, return 'SALOME' as default application name
180 return os.getenv( "APPNAME", "SALOME" ) # 'SALOME' is default user name
186 Get current naming server port number:
187 1. try NSPORT environment variable
188 1. if fails, try to parse config file defined by OMNIORB_CONFIG environment variable
189 2. if fails, return 2809 as default port number
193 return int( os.getenv( "NSPORT" ) )
196 port = getPortFromORBcfg()
197 if port is not None: return port
198 return 2809 # '2809' is default port number
204 Get directory to be used for the temporary files.
207 if sys.platform == "win32":
208 # for Windows: temporarily using home directory for tmp files;
209 # to be replaced with TEMP environment variable later...
210 dir = os.getenv("HOME")
212 # for Linux: use /tmp/logs/{user} folder
213 dir = os.path.join( '/tmp', 'logs', getUserName() )
219 def generateFileName( dir, prefix = None, suffix = None, extension = None,
220 unique = False, separator = "_", hidden = False, **kwargs ):
222 Generate file name by sepecified parameters. If necessary, file name
223 can be generated to be unique.
226 - dir : directory path
227 - prefix : file prefix (not added by default)
228 - suffix : file suffix (not added by default)
229 - extension : file extension (not added by default)
230 - unique : if this parameter is True, the unique file name is generated:
231 in this case, if the file with the generated name already exists
232 in the <dir> directory, an integer suffix is added to the end of the
233 file name. This parameter is False by default.
234 - separator : separator of the words ('_' by default)
235 - hidden : if this parameter is True, the file name is prepended by . (dot)
236 symbol. This parameter is False by default.
238 Other keyword parameters are:
239 - with_username : 'add user name' flag/option:
240 * boolean value can be passed to determine user name automatically
241 * string value to be used as user name
242 - with_hostname : 'add host name' flag/option:
243 * boolean value can be passed to determine host name automatically
244 * string value to be used as host name
245 - with_port : 'add port number' flag/option:
246 * boolean value can be passed to determine port number automatically
247 * string value to be used as port number
248 - with_app : 'add application name' flag/option:
249 * boolean value can be passed to determine application name automatically
250 * string value to be used as application name
251 All <with_...> parameters are optional.
253 supported = [ 'with_username', 'with_hostname', 'with_port', 'with_app' ]
254 from launchConfigureParser import verbose
257 if separator is None:
261 separator = str( separator )
263 # prefix (if specified)
264 if prefix is not None:
265 filename.append( str( prefix ) )
267 # additional keywords
268 ### check unsupported parameters
270 if kw not in supported and verbose():
271 print 'Warning! salome_utilitie.py: generateFileName(): parameter %s is not supported' % kw
274 ### process supported keywords
276 if kw not in kwargs: continue
278 if kw == 'with_username':
281 if _try_bool( kwargs[kw] ): filename.append( getUserName() )
284 # user name given as parameter
285 filename.append( kwargs[kw] )
289 elif kw == 'with_hostname':
292 if _try_bool( kwargs[kw] ): filename.append( getShortHostName() )
295 # host name given as parameter
296 filename.append( kwargs[kw] )
300 elif kw == 'with_port':
303 if _try_bool( kwargs[kw] ): filename.append( str( getPortNumber() ) )
306 # port number given as parameter
307 filename.append( str( kwargs[kw] ) )
311 elif kw == 'with_app':
313 # auto application name ?
314 if _try_bool( kwargs[kw] ): filename.append( getAppName() )
317 # application name given as parameter
318 filename.append( kwargs[kw] )
322 # suffix (if specified)
323 if suffix is not None:
324 filename.append( str( suffix ) )
326 # raise an exception if file name is empty
328 raise Exception("Empty file name")
330 if extension is not None and extension.startswith("."): extension = extension[1:]
333 name = separator.join( filename )
334 if hidden: name = "." + name # add dot for hidden files
335 if extension: name = name + "." + str( extension ) # add extension if defined
336 name = os.path.join( dir, name )
338 # create unique file name
340 while os.path.exists( name ):
342 name = separator.join( filename ) + separator + str( index )
343 if hidden: name = "." + name # add dot for hidden files
344 if extension: name = name + "." + str( extension ) # add extension if defined
345 name = os.path.join( dir, name )
348 return os.path.normpath(name)