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)
28 Set of utility functions used by SALOME python scripts.
51 Check if specified parameter represents boolean value and returns its value.
52 String values like 'True', 'TRUE', 'YES', 'Yes', 'y', 'NO', 'false', 'n', etc
54 If <arg> does not represent a boolean, an exception is raised.
57 if type( arg ) == types.BooleanType :
59 elif type( arg ) == types.StringType :
60 v = str( arg ).lower()
61 if v in [ "yes", "y", "true" ]: return True
62 elif v in [ "no", "n", "false" ]: return False
64 raise Exception("Not boolean value")
70 Get omniORB current configuration.
71 Returns a list of three values: [ orb_version, host_name, port_number ].
73 The information is retrieved from the omniORB configuration file defined
74 by the OMNIORB_CONFIG environment variable.
75 If omniORB configuration file can not be accessed, a list of three empty
81 f = open( os.getenv( "OMNIORB_CONFIG" ) )
84 regvar = re.compile( "(ORB)?InitRef.*corbaname::(.*):(\d+)\s*$" )
89 if m.group(1) is None:
108 def getHostFromORBcfg():
110 Get current omniORB host.
112 return getORBcfgInfo()[1]
115 def getPortFromORBcfg():
117 Get current omniORB port.
119 return getORBcfgInfo()[2]
126 1. try USER environment variable
127 2. if fails, return 'unknown' as default user name
130 return os.getenv( "USER", "unknown" ) # 'unknown' is default user name
137 1. try socket python module gethostname() function
138 2. if fails, try HOSTNAME environment variable
139 3. if fails, try HOST environment variable
140 4. if fails, return 'unknown' as default host name
145 host = socket.gethostname()
149 if not host: host = os.getenv("HOSTNAME")
150 if not host: host = os.getenv("HOST")
151 if not host: host = "unknown" # 'unknown' is default host name
156 def getShortHostName():
159 1. try socket python module gethostname() function
160 2. if fails, try HOSTNAME environment variable
161 3. if fails, try HOST environment variable
162 4. if fails, return 'unknown' as default host name
165 return getHostName().split('.')[0]
168 return "unknown" # 'unknown' is default host name
174 Get application name:
175 1. try APPNAME environment variable
176 2. if fails, return 'SALOME' as default application name
179 return os.getenv( "APPNAME", "SALOME" ) # 'SALOME' is default user name
185 Get current naming server port number:
186 1. try NSPORT environment variable
187 1. if fails, try to parse config file defined by OMNIORB_CONFIG environment variable
188 2. if fails, return 2809 as default port number
192 return int( os.getenv( "NSPORT" ) )
195 port = getPortFromORBcfg()
196 if port is not None: return port
197 return 2809 # '2809' is default port number
203 Get directory to be used for the temporary files.
206 if sys.platform == "win32":
207 # for Windows: temporarily using home directory for tmp files;
208 # to be replaced with TEMP environment variable later...
209 dir = os.getenv("HOME")
211 # for Linux: use /tmp/logs/{user} folder
212 dir = os.path.join( '/tmp', 'logs', getUserName() )
218 def generateFileName( dir, prefix = None, suffix = None, extension = None,
219 unique = False, separator = "_", hidden = False, **kwargs ):
221 Generate file name by sepecified parameters. If necessary, file name
222 can be generated to be unique.
225 - dir : directory path
226 - prefix : file prefix (not added by default)
227 - suffix : file suffix (not added by default)
228 - extension : file extension (not added by default)
229 - unique : if this parameter is True, the unique file name is generated:
230 in this case, if the file with the generated name already exists
231 in the <dir> directory, an integer suffix is added to the end of the
232 file name. This parameter is False by default.
233 - separator : separator of the words ('_' by default)
234 - hidden : if this parameter is True, the file name is prepended by . (dot)
235 symbol. This parameter is False by default.
237 Other keyword parameters are:
238 - with_username : 'add user name' flag/option:
239 * boolean value can be passed to determine user name automatically
240 * string value to be used as user name
241 - with_hostname : 'add host name' flag/option:
242 * boolean value can be passed to determine host name automatically
243 * string value to be used as host name
244 - with_port : 'add port number' flag/option:
245 * boolean value can be passed to determine port number automatically
246 * string value to be used as port number
247 - with_app : 'add application name' flag/option:
248 * boolean value can be passed to determine application name automatically
249 * string value to be used as application name
250 All <with_...> parameters are optional.
252 supported = [ 'with_username', 'with_hostname', 'with_port', 'with_app' ]
253 from launchConfigureParser import verbose
256 if separator is None:
260 separator = str( separator )
262 # prefix (if specified)
263 if prefix is not None:
264 filename.append( str( prefix ) )
266 # additional keywords
267 ### check unsupported parameters
269 if kw not in supported and verbose():
270 print 'Warning! salome_utilitie.py: generateFileName(): parameter %s is not supported' % kw
273 ### process supported keywords
275 if kw not in kwargs: continue
277 if kw == 'with_username':
280 if _try_bool( kwargs[kw] ): filename.append( getUserName() )
283 # user name given as parameter
284 filename.append( kwargs[kw] )
288 elif kw == 'with_hostname':
291 if _try_bool( kwargs[kw] ): filename.append( getShortHostName() )
294 # host name given as parameter
295 filename.append( kwargs[kw] )
299 elif kw == 'with_port':
302 if _try_bool( kwargs[kw] ): filename.append( str( getPortNumber() ) )
305 # port number given as parameter
306 filename.append( str( kwargs[kw] ) )
310 elif kw == 'with_app':
312 # auto application name ?
313 if _try_bool( kwargs[kw] ): filename.append( getAppName() )
316 # application name given as parameter
317 filename.append( kwargs[kw] )
321 # suffix (if specified)
322 if suffix is not None:
323 filename.append( str( suffix ) )
325 # raise an exception if file name is empty
327 raise Exception("Empty file name")
329 if extension is not None and extension.startswith("."): extension = extension[1:]
332 name = separator.join( filename )
333 if hidden: name = "." + name # add dot for hidden files
334 if extension: name = name + "." + str( extension ) # add extension if defined
335 name = os.path.join( dir, name )
337 # create unique file name
339 while os.path.exists( name ):
341 name = separator.join( filename ) + separator + str( index )
342 if hidden: name = "." + name # add dot for hidden files
343 if extension: name = name + "." + str( extension ) # add extension if defined
344 name = os.path.join( dir, name )
347 return os.path.normpath(name)