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 initial imports and utilities methods for salomeTools
30 from . import architecture
31 from . import printcolors
34 from . import ElementTree
37 from . import environment
38 from . import fileEnviron
39 from . import compilation
40 from . import test_module
41 from . import template
44 if platform.system() == "Windows" :
51 KNOWNFAILURE_STATUS = "KF"
52 TIMEOUT_STATUS = "TIMEOUT"
54 CONFIG_FILENAME = "sat-config.pyconf"
56 class SatException(Exception):
57 """rename Exception Class"""
60 def ensure_path_exists(p):
61 """Create a path if not existing
63 :param p str: The path.
65 if not os.path.exists(p):
68 def check_config_has_application( config, details = None ):
69 """check that the config has the key APPLICATION. Else raise an exception.
71 :param config class 'common.pyconf.Config': The config.
73 if 'APPLICATION' not in config:
74 message = _("An APPLICATION is required. Use 'config --list' to get the list of available applications.\n")
76 details.append(message)
77 raise SatException( message )
79 def check_config_has_profile( config, details = None ):
81 check that the config has the key APPLICATION.profile.
82 else, raise an exception.
84 :param config class 'common.pyconf.Config': The config.
86 check_config_has_application(config)
87 if 'profile' not in config.APPLICATION:
88 message = _("A profile section is required in your application.\n")
90 details.append(message)
91 raise SatException( message )
93 def config_has_application( config ):
94 return 'APPLICATION' in config
96 def get_cfg_param(config, param_name, default):
98 eearch for param_name value in config.
99 if param_name is not in config
101 else return the found value
103 :param config class 'common.pyconf.Config': The config.
104 :param param_name str: the name of the parameter to get the value
105 :param default str: The value to return if param_name is not in config
106 :return: see initial description of the function
109 if param_name in config:
110 return config[param_name]
113 def print_info(logger, info):
115 Prints the tuples that are in info variable in a formatted way.
117 :param logger Logger: The logging instance to use for the prints.
118 :param info list: The list of tuples to display
120 # find the maximum length of the first value of the tuples in info
121 smax = max(map(lambda l: len(l[0]), info))
122 # Print each item of info with good indentation
124 sp = " " * (smax - len(i[0]))
125 printcolors.print_value(logger, sp + i[0], i[1], 2)
126 logger.write("\n", 2)
128 def get_base_path(config):
130 Returns the path of the products base.
132 :param config Config: The global Config instance.
133 :return: The path of the products base.
136 if "base" not in config.LOCAL:
137 local_file_path = os.path.join(config.VARS.salometoolsway,
140 msg = _("Please define a base path in the file %s" % local_file_path)
141 raise SatException(msg)
143 base_path = os.path.abspath(config.LOCAL.base)
147 def get_launcher_name(config):
149 Returns the name of salome launcher.
151 :param config Config: The global Config instance.
152 :return: The name of salome launcher.
155 check_config_has_application(config)
156 if 'profile' in config.APPLICATION and 'launcher_name' in config.APPLICATION.profile:
157 launcher_name = config.APPLICATION.profile.launcher_name
159 launcher_name = 'salome'
163 def get_log_path(config):
165 Returns the path of the logs.
167 :param config Config: The global Config instance.
168 :return: The path of the logs.
171 if "log_dir" not in config.LOCAL:
172 local_file_path = os.path.join(config.VARS.salometoolsway,
175 msg = _("Please define a log_dir in the file %s" % local_file_path)
176 raise SatException(msg)
178 log_dir_path = os.path.abspath(config.LOCAL.log_dir)
182 def get_salome_version(config):
183 import versionMinorMajorPatch as VMMP
185 if hasattr(config.APPLICATION, 'version_salome'):
186 version = VMMP.MinorMajorPatch(config.APPLICATION.version_salome)
188 kernel_info = product.get_product_config(config, "KERNEL")
189 aFile = os.path.join(
190 kernel_info.install_dir,
194 if not os.path.isfile(aFile):
196 with open(aFile) as f:
197 line = f.readline() # example: '[SALOME KERNEL] : 8.4.0'
198 version = VMMP.MinorMajorPatch(line.split(":")[1])
200 res = version.strCompact()
201 # print("get_salome_version %s -> %s" % (version, res))
204 def read_config_from_a_file(filePath):
206 cfg_file = pyconf.Config(filePath)
207 except pyconf.ConfigError as e:
208 raise SatException(_("Error in configuration file: %(file)s\n %(error)s") % \
209 { 'file': filePath, 'error': str(e) })
212 def get_tmp_filename(cfg, name):
213 if not os.path.exists(cfg.VARS.tmp_root):
214 os.makedirs(cfg.VARS.tmp_root)
216 return os.path.join(cfg.VARS.tmp_root, name)
219 # Utils class to simplify path manipulations.
221 def __init__(self, path):
222 self.path = str(path)
224 def __add__(self, other):
225 return Path(os.path.join(self.path, str(other)))
228 return Path(os.path.abspath(self.path))
233 def __eq__(self, other):
234 return self.path == other.path
237 return self.islink() or os.path.exists(self.path)
240 return os.path.islink(self.path)
243 return os.path.isdir(self.path)
246 return os.path.isfile(self.path)
249 return [Path(p) for p in os.listdir(self.path)]
252 return Path(os.path.dirname(self.path))
255 return Path(os.path.basename(self.path))
257 def make(self, mode=None):
258 os.makedirs(self.path)
260 os.chmod(self.path, mode)
262 def chmod(self, mode):
263 os.chmod(self.path, mode)
269 shutil.rmtree( self.path, onerror = handleRemoveReadonly )
271 def copy(self, path, smart=False):
272 if not isinstance(path, Path):
275 if os.path.islink(self.path):
276 return self.copylink(path)
277 elif os.path.isdir(self.path):
278 return self.copydir(path, smart)
280 return self.copyfile(path)
282 def smartcopy(self, path):
283 return self.copy(path, True)
287 return os.readlink(self.path)
291 def symlink(self, path):
293 os.symlink(str(path), self.path)
298 def copylink(self, path):
300 os.symlink(os.readlink(self.path), str(path))
305 def copydir(self, dst, smart=False):
315 if smart and (str(name) in [".git", "CVS", ".svn"]):
317 srcname = self + name
319 srcname.copy(dstname, smart)
324 def copyfile(self, path):
326 shutil.copy2(self.path, str(path))
331 def find_file_in_lpath(file_name, lpath, additional_dir = ""):
333 Find in all the directories in lpath list the file that has the same name
336 then return the full path of the file
339 The additional_dir (optional) is the name of the directory to add to all
342 :param file_name str: The file name to search
343 :param lpath List: The list of directories where to search
344 :param additional_dir str: The name of the additional directory
345 :return: the full path of the file or False if not found
348 for directory in lpath:
349 dir_complete = os.path.join(directory, additional_dir)
350 if not os.path.isdir(directory) or not os.path.isdir(dir_complete):
352 l_files = os.listdir(dir_complete)
353 for file_n in l_files:
354 if file_n == file_name:
355 return os.path.join(dir_complete, file_name)
358 def handleRemoveReadonly(func, path, exc):
360 if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
361 os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
366 def deepcopy_list(input_list):
368 Do a deep copy of a list
370 :param input_list List: The list to copy
371 :return: The copy of the list
375 for elem in input_list:
379 def remove_item_from_list(input_list, item):
381 Remove all occurences of item from input_list
383 :param input_list List: The list to modify
384 :return: The without any item
388 for elem in input_list:
394 def parse_date(date):
396 Transform YYYYMMDD_hhmmss into YYYY-MM-DD hh:mm:ss.
398 :param date str: The date to transform
399 :return: The date in the new format
404 res = "%s-%s-%s %s:%s:%s" % (date[0:4],
412 def merge_dicts(*dict_args):
414 Given any number of dicts, shallow copy and merge into a new dict,
415 precedence goes to key value pairs in latter dicts.
418 for dictionary in dict_args:
419 result.update(dictionary)
422 def replace_in_file(filein, strin, strout):
423 """Replace <strin> by <strout> in file <filein>"""
424 shutil.move(filein, filein + "_old")
426 filein = filein + "_old"
427 fin = open(filein, "r")
428 fout = open(fileout, "w")
430 fout.write(line.replace(strin, strout))
432 def get_property_in_product_cfg(product_cfg, pprty):
433 if not "properties" in product_cfg:
435 if not pprty in product_cfg.properties:
437 return product_cfg.properties[pprty]
439 def activate_mesa_property(config):
440 """Add mesa property into application properties
442 :param config Config: The global configuration. It must have an application!
444 # Verify the existence of the file
445 if not 'properties' in config.APPLICATION:
446 config.APPLICATION.addMapping( 'properties', pyconf.Mapping(), None )
447 config.APPLICATION.properties.use_mesa="yes"