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.
54 Check if specified parameter represents boolean value and returns its value.
55 String values like 'True', 'TRUE', 'YES', 'Yes', 'y', 'NO', 'false', 'n', etc
57 If <arg> does not represent a boolean, an exception is raised.
60 if type( arg ) == types.BooleanType :
62 elif type( arg ) == types.StringType :
63 v = str( arg ).lower()
64 if v in [ "yes", "y", "true" ]: return True
65 elif v in [ "no", "n", "false" ]: return False
67 raise Exception("Not boolean value")
73 Get omniORB current configuration.
74 Returns a list of three values: [ orb_version, host_name, port_number ].
76 The information is retrieved from the omniORB configuration file defined
77 by the OMNIORB_CONFIG environment variable.
78 If omniORB configuration file can not be accessed, a list of three empty
84 f = open( os.getenv( "OMNIORB_CONFIG" ) )
87 regvar = re.compile( "(ORB)?InitRef.*corbaname::(.*):(\d+)\s*$" )
92 if m.group(1) is None:
111 def getHostFromORBcfg():
113 Get current omniORB host.
115 return getORBcfgInfo()[1]
118 def getPortFromORBcfg():
120 Get current omniORB port.
122 return getORBcfgInfo()[2]
129 1. try USER environment variable
130 2. if fails, return 'unknown' as default user name
133 return os.getenv( "USER", "unknown" ) # 'unknown' is default user name
140 1. try socket python module gethostname() function
141 2. if fails, try HOSTNAME environment variable
142 3. if fails, try HOST environment variable
143 4. if fails, return 'unknown' as default host name
148 host = socket.gethostname()
152 if not host: host = os.getenv("HOSTNAME")
153 if not host: host = os.getenv("HOST")
154 if not host: host = "unknown" # 'unknown' is default host name
159 def getShortHostName():
162 1. try socket python module gethostname() function
163 2. if fails, try HOSTNAME environment variable
164 3. if fails, try HOST environment variable
165 4. if fails, return 'unknown' as default host name
168 return getHostName().split('.')[0]
171 return "unknown" # 'unknown' is default host name
177 Get application name:
178 1. try APPNAME environment variable
179 2. if fails, return 'SALOME' as default application name
182 return os.getenv( "APPNAME", "SALOME" ) # 'SALOME' is default user name
188 Get current naming server port number:
189 1. try NSPORT environment variable
190 1. if fails, try to parse config file defined by OMNIORB_CONFIG environment variable
191 2. if fails, return 2809 as default port number
195 return int( os.getenv( "NSPORT" ) )
198 port = getPortFromORBcfg()
199 if port is not None: return port
200 return 2809 # '2809' is default port number
206 Get directory to be used for the temporary files.
209 if sys.platform == "win32":
210 # for Windows: temporarily using home directory for tmp files;
211 # to be replaced with TEMP environment variable later...
212 dir = os.getenv("HOME")
214 # for Linux: use /tmp/logs/{user} folder
215 dir = os.path.join( '/tmp', 'logs', getUserName() )
221 def generateFileName( dir, prefix = None, suffix = None, extension = None,
222 unique = False, separator = "_", hidden = False, **kwargs ):
224 Generate file name by sepecified parameters. If necessary, file name
225 can be generated to be unique.
228 - dir : directory path
229 - prefix : file prefix (not added by default)
230 - suffix : file suffix (not added by default)
231 - extension : file extension (not added by default)
232 - unique : if this parameter is True, the unique file name is generated:
233 in this case, if the file with the generated name already exists
234 in the <dir> directory, an integer suffix is added to the end of the
235 file name. This parameter is False by default.
236 - separator : separator of the words ('_' by default)
237 - hidden : if this parameter is True, the file name is prepended by . (dot)
238 symbol. This parameter is False by default.
240 Other keyword parameters are:
241 - with_username : 'add user name' flag/option:
242 * boolean value can be passed to determine user name automatically
243 * string value to be used as user name
244 - with_hostname : 'add host name' flag/option:
245 * boolean value can be passed to determine host name automatically
246 * string value to be used as host name
247 - with_port : 'add port number' flag/option:
248 * boolean value can be passed to determine port number automatically
249 * string value to be used as port number
250 - with_app : 'add application name' flag/option:
251 * boolean value can be passed to determine application name automatically
252 * string value to be used as application name
253 All <with_...> parameters are optional.
255 supported = [ 'with_username', 'with_hostname', 'with_port', 'with_app' ]
256 from launchConfigureParser import verbose
259 if separator is None:
263 separator = str( separator )
265 # prefix (if specified)
266 if prefix is not None:
267 filename.append( str( prefix ) )
269 # additional keywords
270 ### check unsupported parameters
272 if kw not in supported and verbose():
273 print 'Warning! salome_utilitie.py: generateFileName(): parameter %s is not supported' % kw
276 ### process supported keywords
278 if kw not in kwargs: continue
280 if kw == 'with_username':
283 if _try_bool( kwargs[kw] ): filename.append( getUserName() )
286 # user name given as parameter
287 filename.append( kwargs[kw] )
291 elif kw == 'with_hostname':
294 if _try_bool( kwargs[kw] ): filename.append( getShortHostName() )
297 # host name given as parameter
298 filename.append( kwargs[kw] )
302 elif kw == 'with_port':
305 if _try_bool( kwargs[kw] ): filename.append( str( getPortNumber() ) )
308 # port number given as parameter
309 filename.append( str( kwargs[kw] ) )
313 elif kw == 'with_app':
315 # auto application name ?
316 if _try_bool( kwargs[kw] ): filename.append( getAppName() )
319 # application name given as parameter
320 filename.append( kwargs[kw] )
324 # suffix (if specified)
325 if suffix is not None:
326 filename.append( str( suffix ) )
328 # raise an exception if file name is empty
330 raise Exception("Empty file name")
332 if extension is not None and extension.startswith("."): extension = extension[1:]
335 name = separator.join( filename )
336 if hidden: name = "." + name # add dot for hidden files
337 if extension: name = name + "." + str( extension ) # add extension if defined
338 name = os.path.join( dir, name )
340 # create unique file name
342 while os.path.exists( name ):
344 name = separator.join( filename ) + separator + str( index )
345 if hidden: name = "." + name # add dot for hidden files
346 if extension: name = name + "." + str( extension ) # add extension if defined
347 name = os.path.join( dir, name )
350 return os.path.normpath(name)
354 def makeTmpDir( path ):
356 Make temporary directory with the specified path.
357 If the directory exists then clear its contents.
360 - path : absolute path to the directory to be created.
364 if os.path.exists( path ):
365 os.system( "rm -rf " + path + "/*" )
368 os.makedirs( path, 0777 )
373 def uniteFiles( src_file, dest_file ):
375 Unite contents of the source file with contents of the destination file
376 and put result of the uniting to the destination file.
377 If the destination file does not exist then the source file is simply
381 - src_file : absolute path to the source file
382 - dest_file : absolute path to the destination file
386 if not os.path.exists( src_file ):
390 if os.path.exists( dest_file ):
391 # add a symbol of new line to contents of the destination file (just in case)
392 dest = open( dest_file, 'r' )
393 dest_lines = dest.readlines()
396 dest_lines.append( "\n" )
398 dest = open( dest_file, 'w' )
399 dest.writelines( dest_lines )
402 command = "cat " + src_file + " >> " + dest_file
405 command = "cp " + src_file + " " + dest_file