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 batch_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}
73 export prereq_install_Path=${out_dir_Path}/PREREQUISITES/INSTALL
74 export prereq_build_Path=${out_dir_Path}/PREREQUISITES/BUILD
76 ###########################################################################
80 cfgForPy_header='''# a generated SALOME Configuration file in python syntax from "sat application %s"
84 # Instantiate correct FileEnvironment sub-class.
85 def get_file_environ(output, shell, environ=None, config=None):
87 return BashFileEnviron(output, environ)
89 return BatchFileEnviron(output, environ)
90 if shell == "cfgForPy":
91 return CfgForPyFileEnviron(output, environ, config)
92 raise Exception("FileEnviron: Unknown shell = %s" % shell)
95 # Base class for shell environment.
97 def __init__(self, output, environ=None):
98 self._do_init(output, environ)
100 def _do_init(self, output, environ=None):
103 if environ is not None:
104 self.environ = environ
106 self.environ = os.environ
108 def add_line(self, number):
109 self.output.write("\n" * number)
111 def add_comment(self, comment):
112 self.output.write("# %s\n" % comment)
114 def add_echo(self, text):
115 self.output.write('echo %s"\n' % text)
117 def add_warning(self, warning):
118 self.output.write('echo "WARNING %s"\n' % warning)
120 def append_value(self, key, value, sep=os.pathsep):
121 self.set(key, self.get(key) + sep + value)
122 if (key, sep) not in self.toclean:
123 self.toclean.append((key, sep))
125 def append(self, key, value, sep=os.pathsep):
126 if isinstance(value, list):
127 self.append_value(key, sep.join(value), sep)
129 self.append_value(key, value, sep)
131 def prepend_value(self, key, value, sep=os.pathsep):
132 self.set(key, value + sep + self.get(key))
133 if (key, sep) not in self.toclean:
134 self.toclean.append((key, sep))
136 def prepend(self, key, value, sep=os.pathsep):
137 if isinstance(value, list):
138 self.prepend_value(key, sep.join(value), sep)
140 self.prepend_value(key, value, sep)
142 def is_defined(self, key):
143 return (key in self.environ)
146 def set(self, key, value):
147 raise NotImplementedError("set is not implement for this shell!")
152 def command_value(self, key, command):
153 raise NotImplementedError("command_value is not implement for this shell!")
155 def finish(self, required=True):
156 for (key, sep) in self.toclean:
158 self.output.write('clean %s "%s"\n' % (key, sep))
161 # Class for bash shell.
162 class BashFileEnviron(FileEnviron):
163 def __init__(self, output, environ=None):
164 self._do_init(output, environ)
165 self.output.write(bash_header)
167 def set(self, key, value):
168 self.output.write('export %s="%s"\n' % (key, value))
169 self.environ[key] = value
171 def command_value(self, key, command):
172 self.output.write('export %s=$(%s)\n' % (key, command))
174 def finish(self, required=True):
177 FileEnviron.finish(self, required)
180 # Class for Windows batch shell.
181 class BatchFileEnviron(FileEnviron):
182 def __init__(self, output, environ=None):
183 self._do_init(output, environ)
184 self.output.write(batch_header)
186 def add_comment(self, comment):
187 self.output.write("rem %s\n" % comment)
190 return '%%%s%%' % key
192 def set(self, key, value):
193 self.output.write('set %s=%s\n' % (key, value))
194 self.environ[key] = value
196 def command_value(self, key, command):
197 self.output.write('%s > tmp.txt\n' % (command))
198 self.output.write('set /p %s =< tmp.txt\n' % (key))
200 def finish(self, required=True):
204 # Base class for cfg environment file.
206 def __init__(self, output, environ=None):
207 self._do_init(output, environ)
209 def _do_init(self, output, environ=None):
212 if environ is not None:
213 self.environ = environ
215 self.environ = os.environ
216 if not self.environ.has_key("PATH"):
217 self.environ["PATH"]=""
218 if not self.environ.has_key("LD_LIBRARY_PATH"):
219 self.environ["LD_LIBRARY_PATH"]=""
220 if not self.environ.has_key("PYTHONPATH"):
221 self.environ["PYTHONPATH"]=""
222 if not self.environ.has_key("TCLLIBPATH"):
223 self.environ["TCLLIBPATH"]=""
224 if not self.environ.has_key("TKLIBPATH"):
225 self.environ["TKLIBPATH"]=""
228 def add_line(self, number):
229 self.output.write("\n" * number)
231 def add_comment(self, comment):
232 self.output.write("# %s\n" % comment)
234 def add_echo(self, text):
235 self.output.write('# %s"\n' % text)
237 def add_warning(self, warning):
238 self.output.write('# "WARNING %s"\n' % warning)
240 def append_value(self, key, value, sep=":"):
241 if self.is_defined(key) :
246 def append(self, key, value, sep=":"):
247 if isinstance(value, list):
248 self.append_value(key, sep.join(value), sep)
250 self.append_value(key, value, sep)
252 def prepend_value(self, key, value, sep=":"):
253 if self.is_defined(key) :
258 def prepend(self, key, value, sep=":"):
259 if isinstance(value, list):
260 self.prepend_value(key, sep.join(value), sep)
262 self.prepend_value(key, value, sep)
264 def is_defined(self, key):
265 return self.environ.has_key(key)
267 def set(self, key, value):
268 raise NotImplementedError("set is not implement for file .cfg!")
273 def command_value(self, key, command):
274 raise NotImplementedError("command_value is not implement for file .cfg!")
276 def finish(self, required=True):
277 #for (key, sep) in self.toclean:
279 # self.output.write('clean %s "%s"\n' % (key, sep))
280 #self.output.write("# finish\n")
284 def specialPathSeparator(name):
285 """TCLLIBPATH, TKLIBPATH, PV_PLUGIN_PATH environments variables needs some exotic path separator"""
286 specialBlanksKeys=["TCLLIBPATH", "TKLIBPATH"]
287 specialSemicolonKeys=["PV_PLUGIN_PATH"]
289 if name in specialBlanksKeys: res=" "
290 if name in specialSemicolonKeys: res=";"
296 # Class for generate a cfg file script (in python syntax) SalomeContext API.
297 class CfgForPyFileEnviron(FileCfg):
298 def __init__(self, output, environ=None, config=None):
300 self._do_init(output, environ)
302 self.indent=" " # four whitespaces for first indentation in a python script
303 self.prefix="context."
304 self.setVarEnv="setVariable"
306 self.begin=self.indent+self.prefix
307 self.output.write((cfgForPy_header) % config['VARS']['product'])
308 self.specialKeys={"PATH": "Path", "LD_LIBRARY_PATH": "LdLibraryPath", "PYTHONPATH": "PythonPath"}
310 def changeToCfg(self, value):
314 def set(self, key, value):
315 self.output.write(self.begin+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % (key, self.changeToCfg(value)))
316 self.environ[key] = value
318 def add(self, key, value):
319 if key in self.specialKeys.keys():
320 self.output.write(self.begin+'addTo%s(r"%s")\n' % (self.specialKeys[key], self.changeToCfg(value)))
321 self.environ[key]+=":"+value
323 sep=specialPathSeparator(key)
324 self.output.write(self.indent+'#temporary solution!!! have to be defined in API a ?dangerous? addToSpecial(r"%s", r"%s")\n' % (key, value))
325 #pathsep not precised because do not know future os launch?
326 self.output.write(self.begin+'addToSpecial(r"%s", r"%s")\n' % (key, self.changeToCfg(value)))
327 self.environ[key]+=sep+value #here yes we know os for current execution
329 def command_value(self, key, command):
330 #self.output.write('%s=`%s`\n' % (key, command))
331 self.output.write(self.indent+'#`%s`\n' % command)
333 import shlex, subprocess
334 args = shlex.split(command)
335 res=subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
336 out, __ = res.communicate()
337 self.output.write(self.begin+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % (key, out))
340 def add_comment(self, comment):
341 if comment=="DISTENE license":
342 self.output.write(self.indent+"#"+self.prefix+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % ('DISTENE_LICENSE_FILE', self.changeToCfg('Use global envvar: DLIM8VAR')))
343 self.output.write(self.indent+"#"+self.prefix+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % ('DISTENE_LICENCE_FILE_FOR_MGCLEANER', self.changeToCfg('<path to your license>')))
344 self.output.write(self.indent+"#"+self.prefix+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % ('DISTENE_LICENCE_FILE_FOR_YAMS', self.changeToCfg('<path to your license>')))
346 if "setting environ for" in comment:
347 self.output.write(self.indent+"#[%s]\n" % comment.split("setting environ for ")[1])
349 if "PRODUCT environment" in comment:
350 self.output.write(self.indent+"#[SALOME Configuration]\n\n")
351 self.output.write(self.indent+"# PRODUCT environment\n")
353 for i in self.config['PRODUCT']['modules']: tmp+=i+','
354 self.output.write(self.indent+"#only for information:\n")
355 self.output.write(self.indent+'#'+self.setVarEnv+'("SALOME_MODULES", r"%s", overwrite=True)\n\n' % tmp[1:-1])
357 #tmp1=self.config['APPLI']['module_appli']
358 #tmp2=self.config.TOOLS.common.module_info[tmp1].install_dir
359 tmp3=self.config.APPLI.module_appli_install_dir
360 relpath=os.path.relpath("/",os.getenv("HOME"))
361 tmp=relpath[0:-1]+tmp3
362 self.output.write(self.indent+"#only for information: ${APPLI} is preset and have to be relative to $HOME\n")
363 self.output.write(self.indent+'#'+self.setVarEnv+'("APPLI", r"%s", overwrite=True)\n\n' % tmp)
365 self.output.write(self.indent+"#only for information: ${APPLI} is by default\n")
367 self.output.write(self.indent+"# %s\n" % comment)