3 # Copyright (C) 2010-2013 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 bat_header="""@echo off
23 rem The following variables are used only in case of a sat package
24 set out_dir_Path=%~dp0
25 set PRODUCT_OUT_DIR=%out_dir_Path%
26 set prereq_install_Path=%out_dir_Path%\PREREQUISITES\INSTALL
27 set prereq_build_Path=%out_dir_Path%\PREREQUISITES\BUILD
31 bash_header="""#!/bin/bash
32 ##########################################################################
35 # cleanup a path (first parameter) from duplicated entries;
36 # second parameter is the separator
38 out_var=`echo $1 | awk -v sep=$2 '{ \\
39 na = split($1,a,sep); \\
41 for(i=0;i<=na;i++) { \\
58 }' | sed -e 's|\\(.*\\)$1|\\1|g' -e 's|^[:;]||' -e 's|[:;]$||'`
65 out_var=`cleandup $xenv $2`
69 # The 3 following variables are used only in case of a sat package
70 export out_dir_Path=`dirname "${BASH_SOURCE[0]}"`
71 export PRODUCT_OUT_DIR=${out_dir_Path}
72 export PRODUCT_ROOT_DIR=${PRODUCT_OUT_DIR}
74 ###########################################################################
78 Launcher_header='''# a generated SALOME Configuration file using python syntax
81 def get_file_environ(output, shell, environ=None):
82 """Instantiate correct FileEnvironment sub-class.
84 :param output file: the output file stream.
85 :param shell str: the type of shell syntax to use.
86 :param environ dict: a potential additional environment.
89 return BashFileEnviron(output, environ)
91 return BatFileEnviron(output, environ)
92 if shell == "cfgForPy":
93 return LauncherFileEnviron(output, environ)
94 raise Exception("FileEnviron: Unknown shell = %s" % shell)
97 """Base class for shell environment
99 def __init__(self, output, environ=None):
102 :param output file: the output file stream.
103 :param environ dict: a potential additional environment.
105 self._do_init(output, environ)
107 def _do_init(self, output, environ=None):
110 :param output file: the output file stream.
111 :param environ dict: a potential additional environment.
115 if environ is not None:
116 self.environ = environ
118 self.environ = os.environ
120 def add_line(self, number):
121 """Add some empty lines in the shell file
123 :param number int: the number of lines to add
125 self.output.write("\n" * number)
127 def add_comment(self, comment):
128 """Add a comment in the shell file
130 :param comment str: the comment to add
132 self.output.write("# %s\n" % comment)
134 def add_echo(self, text):
135 """Add a "echo" in the shell file
137 :param text str: the text to echo
139 self.output.write('echo %s"\n' % text)
141 def add_warning(self, warning):
142 """Add a warning "echo" in the shell file
144 :param warning str: the text to echo
146 self.output.write('echo "WARNING %s"\n' % warning)
148 def append_value(self, key, value, sep=os.pathsep):
149 '''append value to key using sep
151 :param key str: the environment variable to append
152 :param value str: the value to append to key
153 :param sep str: the separator string
155 self.set(key, self.get(key) + sep + value)
156 if (key, sep) not in self.toclean:
157 self.toclean.append((key, sep))
159 def append(self, key, value, sep=os.pathsep):
160 '''Same as append_value but the value argument can be a list
162 :param key str: the environment variable to append
163 :param value str or list: the value(s) to append to key
164 :param sep str: the separator string
166 if isinstance(value, list):
167 self.append_value(key, sep.join(value), sep)
169 self.append_value(key, value, sep)
171 def prepend_value(self, key, value, sep=os.pathsep):
172 '''prepend value to key using sep
174 :param key str: the environment variable to prepend
175 :param value str: the value to prepend to key
176 :param sep str: the separator string
178 self.set(key, value + sep + self.get(key))
179 if (key, sep) not in self.toclean:
180 self.toclean.append((key, sep))
182 def prepend(self, key, value, sep=os.pathsep):
183 '''Same as prepend_value but the value argument can be a list
185 :param key str: the environment variable to prepend
186 :param value str or list: the value(s) to prepend to key
187 :param sep str: the separator string
189 if isinstance(value, list):
190 self.prepend_value(key, sep.join(value), sep)
192 self.prepend_value(key, value, sep)
194 def is_defined(self, key):
195 '''Check if the key exists in the environment
197 :param key str: the environment variable to check
199 return (key in self.environ)
201 def set(self, key, value):
202 '''Set the environment variable "key" to value "value"
204 :param key str: the environment variable to set
205 :param value str: the value
207 raise NotImplementedError("set is not implement for this shell!")
210 '''Get the value of the environment variable "key"
212 :param key str: the environment variable
216 def command_value(self, key, command):
217 '''Get the value given by the system command "command"
218 and put it in the environment variable key.
219 Has to be overwritten in the derived classes
220 This can be seen as a virtual method
222 :param key str: the environment variable
223 :param command str: the command to execute
225 raise NotImplementedError("command_value is not implement "
228 def finish(self, required=True):
229 """Add a final instruction in the out file (in case of file generation)
231 :param required bool: Do nothing if required is False
233 for (key, sep) in self.toclean:
235 self.output.write('clean %s "%s"\n' % (key, sep))
237 class BashFileEnviron(FileEnviron):
238 """Class for bash shell.
240 def __init__(self, output, environ=None):
243 :param output file: the output file stream.
244 :param environ dict: a potential additional environment.
246 self._do_init(output, environ)
247 self.output.write(bash_header)
249 def set(self, key, value):
250 '''Set the environment variable "key" to value "value"
252 :param key str: the environment variable to set
253 :param value str: the value
255 self.output.write('export %s="%s"\n' % (key, value))
256 self.environ[key] = value
258 def command_value(self, key, command):
259 '''Get the value given by the system command "command"
260 and put it in the environment variable key.
261 Has to be overwritten in the derived classes
262 This can be seen as a virtual method
264 :param key str: the environment variable
265 :param command str: the command to execute
267 self.output.write('export %s=$(%s)\n' % (key, command))
269 def finish(self, required=True):
270 """Add a final instruction in the out file (in case of file generation)
272 :param required bool: Do nothing if required is False
276 FileEnviron.finish(self, required)
278 class BatFileEnviron(FileEnviron):
279 """for Windows batch shell.
281 def __init__(self, output, environ=None):
284 :param output file: the output file stream.
285 :param environ dict: a potential additional environment.
287 self._do_init(output, environ)
288 self.output.write(bat_header)
290 def add_comment(self, comment):
291 """Add a comment in the shell file
293 :param comment str: the comment to add
295 self.output.write("rem %s\n" % comment)
298 '''Get the value of the environment variable "key"
300 :param key str: the environment variable
302 return '%%%s%%' % key
304 def set(self, key, value):
305 '''Set the environment variable "key" to value "value"
307 :param key str: the environment variable to set
308 :param value str: the value
310 self.output.write('set %s=%s\n' % (key, value))
311 self.environ[key] = value
313 def command_value(self, key, command):
314 '''Get the value given by the system command "command"
315 and put it in the environment variable key.
316 Has to be overwritten in the derived classes
317 This can be seen as a virtual method
319 :param key str: the environment variable
320 :param command str: the command to execute
322 self.output.write('%s > tmp.txt\n' % (command))
323 self.output.write('set /p %s =< tmp.txt\n' % (key))
325 def finish(self, required=True):
326 """Add a final instruction in the out file (in case of file generation)
327 In the particular windows case, do nothing
329 :param required bool: Do nothing if required is False
333 def special_path_separator(name):
334 """TCLLIBPATH, TKLIBPATH, PV_PLUGIN_PATH environments variables need
335 some exotic path separator.
336 This function gives the separator regarding the name of the variable
337 to append or prepend.
339 :param name str: The name of the variable to find the separator
341 special_blanks_keys=["TCLLIBPATH", "TKLIBPATH"]
342 special_semicolon_keys=["PV_PLUGIN_PATH"]
344 if name in special_blanks_keys: res=" "
345 if name in special_semicolon_keys: res=";"
348 class LauncherFileEnviron:
349 """Class to generate a launcher file script
350 (in python syntax) SalomeContext API
352 def __init__(self, output, environ=None):
355 :param output file: the output file stream.
356 :param environ dict: a potential additional environment.
360 if environ is not None:
361 self.environ = environ
363 self.environ = os.environ
364 # Initialize some variables
365 if not "PATH" in self.environ.keys():
366 self.environ["PATH"]=""
367 if not "LD_LIBRARY_PATH" in self.environ.keys():
368 self.environ["LD_LIBRARY_PATH"]=""
369 if not "PYTHONPATH" in self.environ.keys():
370 self.environ["PYTHONPATH"]=""
371 if not "TCLLIBPATH" in self.environ.keys():
372 self.environ["TCLLIBPATH"]=""
373 if not "TKLIBPATH" in self.environ.keys():
374 self.environ["TKLIBPATH"]=""
376 # four whitespaces for first indentation in a python script
378 self.prefix="context."
379 self.setVarEnv="setVariable"
381 self.begin=self.indent+self.prefix
382 self.output.write(Launcher_header)
383 self.specialKeys={"PATH": "Path",
384 "LD_LIBRARY_PATH": "LdLibraryPath",
385 "PYTHONPATH": "PythonPath"}
387 def change_to_launcher(self, value):
393 def add_line(self, number):
394 """Add some empty lines in the launcher file
396 :param number int: the number of lines to add
398 self.output.write("\n" * number)
400 def add_echo(self, text):
403 :param text str: the comment to add
405 self.output.write('# %s"\n' % text)
407 def add_warning(self, warning):
410 :param text str: the warning to add
412 self.output.write('# "WARNING %s"\n' % warning)
414 def append_value(self, key, value, sep=":"):
415 '''append value to key using sep
417 :param key str: the environment variable to append
418 :param value str: the value to append to key
419 :param sep str: the separator string
421 if self.is_defined(key) :
426 def append(self, key, value, sep=":"):
427 '''Same as append_value but the value argument can be a list
429 :param key str: the environment variable to append
430 :param value str or list: the value(s) to append to key
431 :param sep str: the separator string
433 if isinstance(value, list):
434 self.append_value(key, sep.join(value), sep)
436 self.append_value(key, value, sep)
438 def prepend_value(self, key, value, sep=":"):
439 '''prepend value to key using sep
441 :param key str: the environment variable to prepend
442 :param value str: the value to prepend to key
443 :param sep str: the separator string
445 if self.is_defined(key) :
450 def prepend(self, key, value, sep=":"):
451 '''Same as prepend_value but the value argument can be a list
453 :param key str: the environment variable to prepend
454 :param value str or list: the value(s) to prepend to key
455 :param sep str: the separator string
457 if isinstance(value, list):
458 self.prepend_value(key, sep.join(value), sep)
460 self.prepend_value(key, value, sep)
462 def is_defined(self, key):
463 '''Check if the key exists in the environment
465 :param key str: the environment variable to check
467 return key in self.environ.keys()
470 '''Get the value of the environment variable "key"
472 :param key str: the environment variable
476 def set(self, key, value):
477 '''Set the environment variable "key" to value "value"
479 :param key str: the environment variable to set
480 :param value str: the value
482 self.output.write(self.begin+self.setVarEnv+
483 '(r"%s", r"%s", overwrite=True)\n' %
484 (key, self.change_to_launcher(value)))
485 self.environ[key] = value
487 def add(self, key, value):
488 '''prepend value to key using sep
490 :param key str: the environment variable to prepend
491 :param value str: the value to prepend to key
493 if key in self.specialKeys.keys():
494 self.output.write(self.begin+'addTo%s(r"%s")\n' %
495 (self.specialKeys[key],
496 self.change_to_launcher(value)))
497 self.environ[key]+=":"+value
499 sep=special_path_separator(key)
500 self.output.write(self.indent+
501 '#temporary solution!!! have to be defined in API a '
502 '?dangerous? addToSpecial(r"%s", r"%s")\n' %
504 #pathsep not precised because do not know future os launch?
505 self.output.write(self.begin+'addToSpecial(r"%s", r"%s")\n'
506 % (key, self.change_to_launcher(value)))
507 self.environ[key]+=sep+value #here yes we know os for current execution
509 def command_value(self, key, command):
510 '''Get the value given by the system command "command"
511 and put it in the environment variable key.
513 :param key str: the environment variable
514 :param command str: the command to execute
516 self.output.write(self.indent+'#`%s`\n' % command)
518 import shlex, subprocess
519 args = shlex.split(command)
520 res=subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
521 out, __ = res.communicate()
522 self.output.write(self.begin+
524 '(r"%s", r"%s", overwrite=True)\n' % (key, out))
526 def add_comment(self, comment):
527 # Special comment in case of the distène licence
528 if comment=="DISTENE license":
529 self.output.write(self.indent+
533 '(r"%s", r"%s", overwrite=True)\n' %
534 ('DISTENE_LICENSE_FILE',
535 self.change_to_launcher(
536 'Use global envvar: DLIM8VAR')))
537 self.output.write(self.indent+
541 '(r"%s", r"%s", overwrite=True)\n' %
542 ('DISTENE_LICENCE_FILE_FOR_MGCLEANER',
543 self.change_to_launcher(
544 '<path to your license>')))
545 self.output.write(self.indent+
549 '(r"%s", r"%s", overwrite=True)\n' %
550 ('DISTENE_LICENCE_FILE_FOR_YAMS',
551 self.change_to_launcher(
552 '<path to your license>')))
554 if "setting environ for" in comment:
555 self.output.write(self.indent+"#[%s]\n" %
556 comment.split("setting environ for ")[1])
559 self.output.write(self.indent+"# %s\n" % comment)
561 def finish(self, required=True):
562 """Add a final instruction in the out file (in case of file generation)
563 In the particular launcher case, do nothing
565 :param required bool: Do nothing if required is False
569 class ScreenEnviron(FileEnviron):
570 def __init__(self, output, environ=None):
571 self._do_init(output, environ)
574 def add_line(self, number):
577 def add_comment(self, comment):
580 def add_echo(self, text):
583 def add_warning(self, warning):
586 def write(self, command, name, value, sign="="):
588 self.output.write(" %s%s %s %s %s\n" % \
589 (src.printcolors.printcLabel(command),
590 " " * (12 - len(command)),
591 src.printcolors.printcInfo(name), sign, value))
593 def is_defined(self, name):
594 return self.defined.has_key(name)
597 return "${%s}" % name
599 def set(self, name, value):
600 self.write("set", name, value)
601 self.defined[name] = value
603 def prepend(self, name, value, sep=":"):
604 if isinstance(value, list):
605 value = sep.join(value)
606 value = value + sep + self.get(name)
607 self.write("prepend", name, value)
609 def append(self, name, value, sep=":"):
610 if isinstance(value, list):
611 value = sep.join(value)
612 value = self.get(name) + sep + value
613 self.write("append", name, value)
615 def command_value(self, key, command):
618 def run_env_script(self, module, script):
619 self.write("load", script, "", sign="")
621 # The SALOME launcher template
622 withProfile = """#! /usr/bin/env python
624 ################################################################
625 # WARNING: this file is automatically generated by SalomeTools #
626 # WARNING: and so could be overwritten at any time. #
627 ################################################################
633 # Add the pwdPath to able to run the launcher after unpacking a package
634 # Used only in case of a salomeTools package
635 out_dir_Path=os.path.abspath(os.path.dirname(__file__))
637 # Preliminary work to initialize path to SALOME Python modules
640 sys.path[:0] = [ os.path.join( 'PROFILE_INSTALL_DIR', 'bin', 'salome' ) ]
641 os.environ['ABSOLUTE_APPLI_PATH'] = 'PROFILE_INSTALL_DIR'
643 # define folder to store omniorb config (initially in virtual application folder)
645 from salomeContextUtils import setOmniOrbUserPath
650 # End of preliminary work
653 # Identify application path then locate configuration files
656 if args == ['--help']:
657 from salomeContext import usage
661 #from salomeContextUtils import getConfigFileNames
662 #configFileNames, args, unexisting = getConfigFileNames( args, checkExistence=True )
663 #if len(unexisting) > 0:
664 # print "ERROR: unexisting configuration file(s): " + ', '.join(unexisting)
667 # Create a SalomeContext which parses configFileNames to initialize environment
669 from salomeContext import SalomeContext, SalomeContextException
670 SalomeContext.addToSpecial=addToSpecial
671 context = SalomeContext(None)
673 # Here set specific variables, if needed
674 # context.addToPath('mypath')
675 # context.addToLdLibraryPath('myldlibrarypath')
676 # context.addToPythonPath('mypythonpath')
677 # context.setVariable('myvarname', 'value')
680 context.getLogger().setLevel(40)
682 context.setVariable(r"PRODUCT_ROOT_DIR", out_dir_Path, overwrite=True)
683 # here your local standalone environment
685 # Start SALOME, parsing command line arguments
686 context.runSalome(args)
687 #print 'Thank you for using SALOME!'
690 context.getLogger().setLevel(20)
692 except SalomeContextException, e:
694 logging.getLogger("salome").error(e)
697 def addToSpecial(self, name, value, pathSep=None):
698 "add special dangerous cases: TCLLIBPATH PV_PLUGIN_PATH etc..."
699 #http://computer-programming-forum.com/57-tcl/1dfddc136afccb94.htm
700 #TCLLIBPATH: Tcl treats the contents of that variable as a list. Be happy, for you can now use drive letters on windows.
704 specialBlanksKeys=["TCLLIBPATH", "TKLIBPATH"]
705 specialSemicolonKeys=["PV_PLUGIN_PATH"]
707 if name in specialBlanksKeys: res=" "
708 if name in specialSemicolonKeys: res=";"
714 value = os.path.expandvars(value) # expand environment variables
715 self.getLogger().debug("Add to %s: %s", name, value)
716 env = os.getenv(name, None)
718 os.environ[name] = value
720 os.environ[name] = value + sep + env #explicitely or not special path separator ?whitespace, semicolon?
722 if __name__ == "__main__":