From 2db5a7399c759f43963abfe616507b629718cd3b Mon Sep 17 00:00:00 2001 From: Serge Rehbinder Date: Wed, 27 Apr 2016 15:08:59 +0200 Subject: [PATCH] commentaries --- src/environment.py | 6 +- src/fileEnviron.py | 399 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 302 insertions(+), 103 deletions(-) diff --git a/src/environment.py b/src/environment.py index 0f10463..7059350 100644 --- a/src/environment.py +++ b/src/environment.py @@ -615,8 +615,7 @@ class FileEnvWriter: env_file = open(os.path.join(self.out_dir, filename), "w") tmp = src.fileEnviron.get_file_environ(env_file, shell, - {}, - self.config ) + {}) env = SalomeEnviron(self.config, tmp, forBuild) env.silent = self.silent @@ -651,8 +650,7 @@ class FileEnvWriter: # create then env object tmp = src.fileEnviron.get_file_environ(filename, "cfgForPy", - {}, - self.config) + {}) # environment for launch env = SalomeEnviron(self.config, tmp, forBuild=False) env.silent = self.silent diff --git a/src/fileEnviron.py b/src/fileEnviron.py index e23d2a3..8463fc8 100644 --- a/src/fileEnviron.py +++ b/src/fileEnviron.py @@ -77,27 +77,41 @@ export prereq_build_Path=${out_dir_Path}/PREREQUISITES/BUILD """ -cfgForPy_header='''# a generated SALOME Configuration file in python syntax from "sat application %s" +Launcher_header='''# a generated SALOME Configuration file using python syntax ''' -## -# Instantiate correct FileEnvironment sub-class. -def get_file_environ(output, shell, environ=None, config=None): +def get_file_environ(output, shell, environ=None): + """Instantiate correct FileEnvironment sub-class. + + :param output file: the output file stream. + :param shell str: the type of shell syntax to use. + :param environ dict: a potential additional environment. + """ if shell == "bash": return BashFileEnviron(output, environ) if shell == "batch": return BatchFileEnviron(output, environ) if shell == "cfgForPy": - return CfgForPyFileEnviron(output, environ, config) + return LauncherFileEnviron(output, environ) raise Exception("FileEnviron: Unknown shell = %s" % shell) -## -# Base class for shell environment. class FileEnviron: + """Base class for shell environment + """ def __init__(self, output, environ=None): + """Initialization + + :param output file: the output file stream. + :param environ dict: a potential additional environment. + """ self._do_init(output, environ) def _do_init(self, output, environ=None): + """Initialization + + :param output file: the output file stream. + :param environ dict: a potential additional environment. + """ self.output = output self.toclean = [] if environ is not None: @@ -106,113 +120,250 @@ class FileEnviron: self.environ = os.environ def add_line(self, number): + """Add some empty lines in the shell file + + :param number int: the number of lines to add + """ self.output.write("\n" * number) def add_comment(self, comment): + """Add a comment in the shell file + + :param comment str: the comment to add + """ self.output.write("# %s\n" % comment) def add_echo(self, text): + """Add a "echo" in the shell file + + :param text str: the text to echo + """ self.output.write('echo %s"\n' % text) def add_warning(self, warning): + """Add a warning "echo" in the shell file + + :param warning str: the text to echo + """ self.output.write('echo "WARNING %s"\n' % warning) def append_value(self, key, value, sep=os.pathsep): + '''append value to key using sep + + :param key str: the environment variable to append + :param value str: the value to append to key + :param sep str: the separator string + ''' self.set(key, self.get(key) + sep + value) if (key, sep) not in self.toclean: self.toclean.append((key, sep)) def append(self, key, value, sep=os.pathsep): + '''Same as append_value but the value argument can be a list + + :param key str: the environment variable to append + :param value str or list: the value(s) to append to key + :param sep str: the separator string + ''' if isinstance(value, list): self.append_value(key, sep.join(value), sep) else: self.append_value(key, value, sep) def prepend_value(self, key, value, sep=os.pathsep): + '''prepend value to key using sep + + :param key str: the environment variable to prepend + :param value str: the value to prepend to key + :param sep str: the separator string + ''' self.set(key, value + sep + self.get(key)) if (key, sep) not in self.toclean: self.toclean.append((key, sep)) def prepend(self, key, value, sep=os.pathsep): + '''Same as prepend_value but the value argument can be a list + + :param key str: the environment variable to prepend + :param value str or list: the value(s) to prepend to key + :param sep str: the separator string + ''' if isinstance(value, list): self.prepend_value(key, sep.join(value), sep) else: self.prepend_value(key, value, sep) def is_defined(self, key): + '''Check if the key exists in the environment + + :param key str: the environment variable to check + ''' return (key in self.environ) - def set(self, key, value): + '''Set the environment variable "key" to value "value" + + :param key str: the environment variable to set + :param value str: the value + ''' raise NotImplementedError("set is not implement for this shell!") def get(self, key): + '''Get the value of the environment variable "key" + + :param key str: the environment variable + ''' return '${%s}' % key def command_value(self, key, command): - raise NotImplementedError("command_value is not implement for this shell!") + '''Get the value given by the system command "command" + and put it in the environment variable key. + Has to be overwritten in the derived classes + This can be seen as a virtual method + + :param key str: the environment variable + :param command str: the command to execute + ''' + raise NotImplementedError("command_value is not implement " + "for this shell!") def finish(self, required=True): + """Add a final instruction in the out file (in case of file generation) + + :param required bool: Do nothing if required is False + """ for (key, sep) in self.toclean: if sep != ' ': self.output.write('clean %s "%s"\n' % (key, sep)) -## -# Class for bash shell. class BashFileEnviron(FileEnviron): + """Class for bash shell. + """ def __init__(self, output, environ=None): + """Initialization + + :param output file: the output file stream. + :param environ dict: a potential additional environment. + """ self._do_init(output, environ) self.output.write(bash_header) def set(self, key, value): + '''Set the environment variable "key" to value "value" + + :param key str: the environment variable to set + :param value str: the value + ''' self.output.write('export %s="%s"\n' % (key, value)) self.environ[key] = value def command_value(self, key, command): + '''Get the value given by the system command "command" + and put it in the environment variable key. + Has to be overwritten in the derived classes + This can be seen as a virtual method + + :param key str: the environment variable + :param command str: the command to execute + ''' self.output.write('export %s=$(%s)\n' % (key, command)) def finish(self, required=True): + """Add a final instruction in the out file (in case of file generation) + + :param required bool: Do nothing if required is False + """ if not required: return FileEnviron.finish(self, required) -## -# Class for Windows batch shell. class BatchFileEnviron(FileEnviron): + """for Windows batch shell. + """ def __init__(self, output, environ=None): + """Initialization + + :param output file: the output file stream. + :param environ dict: a potential additional environment. + """ self._do_init(output, environ) self.output.write(batch_header) def add_comment(self, comment): + """Add a comment in the shell file + + :param comment str: the comment to add + """ self.output.write("rem %s\n" % comment) def get(self, key): + '''Get the value of the environment variable "key" + + :param key str: the environment variable + ''' return '%%%s%%' % key def set(self, key, value): + '''Set the environment variable "key" to value "value" + + :param key str: the environment variable to set + :param value str: the value + ''' self.output.write('set %s=%s\n' % (key, value)) self.environ[key] = value def command_value(self, key, command): + '''Get the value given by the system command "command" + and put it in the environment variable key. + Has to be overwritten in the derived classes + This can be seen as a virtual method + + :param key str: the environment variable + :param command str: the command to execute + ''' self.output.write('%s > tmp.txt\n' % (command)) self.output.write('set /p %s =< tmp.txt\n' % (key)) def finish(self, required=True): + """Add a final instruction in the out file (in case of file generation) + In the particular windows case, do nothing + + :param required bool: Do nothing if required is False + """ return -## -# Base class for cfg environment file. -class FileCfg: - def __init__(self, output, environ=None): - self._do_init(output, environ) +def special_path_separator(name): + """TCLLIBPATH, TKLIBPATH, PV_PLUGIN_PATH environments variables need + some exotic path separator. + This function gives the separator regarding the name of the variable + to append or prepend. + + :param name str: The name of the variable to find the separator + """ + special_blanks_keys=["TCLLIBPATH", "TKLIBPATH"] + special_semicolon_keys=["PV_PLUGIN_PATH"] + res=os.pathsep + if name in special_blanks_keys: res=" " + if name in special_semicolon_keys: res=";" + return res - def _do_init(self, output, environ=None): +class LauncherFileEnviron: + """Class to generate a launcher file script + (in python syntax) SalomeContext API + """ + def __init__(self, output, environ=None): + """Initialization + + :param output file: the output file stream. + :param environ dict: a potential additional environment. + """ self.output = output self.toclean = [] if environ is not None: self.environ = environ else: self.environ = os.environ + # Initialize some variables if not self.environ.has_key("PATH"): self.environ["PATH"]="" if not self.environ.has_key("LD_LIBRARY_PATH"): @@ -224,145 +375,195 @@ class FileCfg: if not self.environ.has_key("TKLIBPATH"): self.environ["TKLIBPATH"]="" + # four whitespaces for first indentation in a python script + self.indent=" " + self.prefix="context." + self.setVarEnv="setVariable" + + self.begin=self.indent+self.prefix + self.output.write(Launcher_header) + self.specialKeys={"PATH": "Path", + "LD_LIBRARY_PATH": "LdLibraryPath", + "PYTHONPATH": "PythonPath"} + + def change_to_launcher(self, value): + """ + """ + res=value + return res def add_line(self, number): + """Add some empty lines in the launcher file + + :param number int: the number of lines to add + """ self.output.write("\n" * number) - def add_comment(self, comment): - self.output.write("# %s\n" % comment) - def add_echo(self, text): + """Add a comment + + :param text str: the comment to add + """ self.output.write('# %s"\n' % text) def add_warning(self, warning): + """Add a warning + + :param text str: the warning to add + """ self.output.write('# "WARNING %s"\n' % warning) def append_value(self, key, value, sep=":"): + '''append value to key using sep + + :param key str: the environment variable to append + :param value str: the value to append to key + :param sep str: the separator string + ''' if self.is_defined(key) : self.add(key, value) else : self.set(key, value) def append(self, key, value, sep=":"): + '''Same as append_value but the value argument can be a list + + :param key str: the environment variable to append + :param value str or list: the value(s) to append to key + :param sep str: the separator string + ''' if isinstance(value, list): self.append_value(key, sep.join(value), sep) else: self.append_value(key, value, sep) def prepend_value(self, key, value, sep=":"): + '''prepend value to key using sep + + :param key str: the environment variable to prepend + :param value str: the value to prepend to key + :param sep str: the separator string + ''' if self.is_defined(key) : self.add(key, value) else : self.set(key, value) def prepend(self, key, value, sep=":"): + '''Same as prepend_value but the value argument can be a list + + :param key str: the environment variable to prepend + :param value str or list: the value(s) to prepend to key + :param sep str: the separator string + ''' if isinstance(value, list): self.prepend_value(key, sep.join(value), sep) else: self.prepend_value(key, value, sep) def is_defined(self, key): + '''Check if the key exists in the environment + + :param key str: the environment variable to check + ''' return self.environ.has_key(key) - def set(self, key, value): - raise NotImplementedError("set is not implement for file .cfg!") - def get(self, key): - return '${%s}' % key - - def command_value(self, key, command): - raise NotImplementedError("command_value is not implement for file .cfg!") - - def finish(self, required=True): - #for (key, sep) in self.toclean: - # if sep != ' ': - # self.output.write('clean %s "%s"\n' % (key, sep)) - #self.output.write("# finish\n") - pass - - -def specialPathSeparator(name): - """TCLLIBPATH, TKLIBPATH, PV_PLUGIN_PATH environments variables needs some exotic path separator""" - specialBlanksKeys=["TCLLIBPATH", "TKLIBPATH"] - specialSemicolonKeys=["PV_PLUGIN_PATH"] - res=os.pathsep - if name in specialBlanksKeys: res=" " - if name in specialSemicolonKeys: res=";" - return res - -## -# -## -# Class for generate a cfg file script (in python syntax) SalomeContext API. -class CfgForPyFileEnviron(FileCfg): - def __init__(self, output, environ=None, config=None): - self.config=config - self._do_init(output, environ) - - self.indent=" " # four whitespaces for first indentation in a python script - self.prefix="context." - self.setVarEnv="setVariable" + '''Get the value of the environment variable "key" - self.begin=self.indent+self.prefix - self.output.write((cfgForPy_header) % config['VARS']['product']) - self.specialKeys={"PATH": "Path", "LD_LIBRARY_PATH": "LdLibraryPath", "PYTHONPATH": "PythonPath"} - - def changeToCfg(self, value): - res=value - return res + :param key str: the environment variable + ''' + return '${%s}' % key def set(self, key, value): - self.output.write(self.begin+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % (key, self.changeToCfg(value))) + '''Set the environment variable "key" to value "value" + + :param key str: the environment variable to set + :param value str: the value + ''' + self.output.write(self.begin+self.setVarEnv+ + '(r"%s", r"%s", overwrite=True)\n' % + (key, self.change_to_launcher(value))) self.environ[key] = value - - def add(self, key, value): + + def add(self, key, value): + '''prepend value to key using sep + + :param key str: the environment variable to prepend + :param value str: the value to prepend to key + ''' if key in self.specialKeys.keys(): - self.output.write(self.begin+'addTo%s(r"%s")\n' % (self.specialKeys[key], self.changeToCfg(value))) + self.output.write(self.begin+'addTo%s(r"%s")\n' % + (self.specialKeys[key], + self.change_to_launcher(value))) self.environ[key]+=":"+value return - sep=specialPathSeparator(key) - self.output.write(self.indent+'#temporary solution!!! have to be defined in API a ?dangerous? addToSpecial(r"%s", r"%s")\n' % (key, value)) + sep=special_path_separator(key) + self.output.write(self.indent+ + '#temporary solution!!! have to be defined in API a ' + '?dangerous? addToSpecial(r"%s", r"%s")\n' % + (key, value)) #pathsep not precised because do not know future os launch? - self.output.write(self.begin+'addToSpecial(r"%s", r"%s")\n' % (key, self.changeToCfg(value))) + self.output.write(self.begin+'addToSpecial(r"%s", r"%s")\n' + % (key, self.change_to_launcher(value))) self.environ[key]+=sep+value #here yes we know os for current execution def command_value(self, key, command): - #self.output.write('%s=`%s`\n' % (key, command)) + '''Get the value given by the system command "command" + and put it in the environment variable key. + + :param key str: the environment variable + :param command str: the command to execute + ''' self.output.write(self.indent+'#`%s`\n' % command) import shlex, subprocess args = shlex.split(command) res=subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, __ = res.communicate() - self.output.write(self.begin+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % (key, out)) - pass + self.output.write(self.begin+ + self.setVarEnv+ + '(r"%s", r"%s", overwrite=True)\n' % (key, out)) def add_comment(self, comment): + # Special comment in case of the distène licence if comment=="DISTENE license": - 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'))) - self.output.write(self.indent+"#"+self.prefix+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % ('DISTENE_LICENCE_FILE_FOR_MGCLEANER', self.changeToCfg(''))) - self.output.write(self.indent+"#"+self.prefix+self.setVarEnv+'(r"%s", r"%s", overwrite=True)\n' % ('DISTENE_LICENCE_FILE_FOR_YAMS', self.changeToCfg(''))) + self.output.write(self.indent+ + "#"+ + self.prefix+ + self.setVarEnv+ + '(r"%s", r"%s", overwrite=True)\n' % + ('DISTENE_LICENSE_FILE', + self.change_to_launcher( + 'Use global envvar: DLIM8VAR'))) + self.output.write(self.indent+ + "#"+ + self.prefix+ + self.setVarEnv+ + '(r"%s", r"%s", overwrite=True)\n' % + ('DISTENE_LICENCE_FILE_FOR_MGCLEANER', + self.change_to_launcher( + ''))) + self.output.write(self.indent+ + "#"+ + self.prefix+ + self.setVarEnv+ + '(r"%s", r"%s", overwrite=True)\n' % + ('DISTENE_LICENCE_FILE_FOR_YAMS', + self.change_to_launcher( + ''))) return if "setting environ for" in comment: - self.output.write(self.indent+"#[%s]\n" % comment.split("setting environ for ")[1]) - return - if "PRODUCT environment" in comment: - self.output.write(self.indent+"#[SALOME Configuration]\n\n") - self.output.write(self.indent+"# PRODUCT environment\n") - tmp="," - for i in self.config['PRODUCT']['modules']: tmp+=i+',' - self.output.write(self.indent+"#only for information:\n") - self.output.write(self.indent+'#'+self.setVarEnv+'("SALOME_MODULES", r"%s", overwrite=True)\n\n' % tmp[1:-1]) - try: - #tmp1=self.config['APPLI']['module_appli'] - #tmp2=self.config.TOOLS.common.module_info[tmp1].install_dir - tmp3=self.config.APPLI.module_appli_install_dir - relpath=os.path.relpath("/",os.getenv("HOME")) - tmp=relpath[0:-1]+tmp3 - self.output.write(self.indent+"#only for information: ${APPLI} is preset and have to be relative to $HOME\n") - self.output.write(self.indent+'#'+self.setVarEnv+'("APPLI", r"%s", overwrite=True)\n\n' % tmp) - except: - self.output.write(self.indent+"#only for information: ${APPLI} is by default\n") + self.output.write(self.indent+"#[%s]\n" % + comment.split("setting environ for ")[1]) return + self.output.write(self.indent+"# %s\n" % comment) - \ No newline at end of file + + def finish(self, required=True): + """Add a final instruction in the out file (in case of file generation) + In the particular launcher case, do nothing + + :param required bool: Do nothing if required is False + """ + return \ No newline at end of file -- 2.39.2