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
31 from . import architecture
32 from . import printcolors
35 from . import ElementTree
38 from . import environment
39 from . import fileEnviron
40 from . import compilation
41 from . import test_module
42 from . import template
45 if platform.system() == "Windows" :
52 KNOWNFAILURE_STATUS = "KF"
53 TIMEOUT_STATUS = "TIMEOUT"
55 class SatException(Exception):
56 """rename Exception Class"""
59 def ensure_path_exists(p):
60 """Create a path if not existing
62 :param p str: The path.
64 if not os.path.exists(p):
67 def check_config_has_application( config, details = None ):
68 """check that the config has the key APPLICATION. Else raise an exception.
70 :param config class 'common.pyconf.Config': The config.
72 if 'APPLICATION' not in config:
73 message = _("An APPLICATION is required. Use 'config --list' to get the list of available applications.\n")
75 details.append(message)
76 raise SatException( message )
78 def check_config_has_profile( config, details = None ):
80 check that the config has the key APPLICATION.profile.
81 else, raise an exception.
83 :param config class 'common.pyconf.Config': The config.
85 check_config_has_application(config)
86 if 'profile' not in config.APPLICATION:
87 message = _("A profile section is required in your application.\n")
89 details.append(message)
90 raise SatException( message )
92 def config_has_application( config ):
93 return 'APPLICATION' in config
95 def get_cfg_param(config, param_name, default):
97 eearch for param_name value in config.
98 if param_name is not in config
100 else return the found value
102 :param config class 'common.pyconf.Config': The config.
103 :param param_name str: the name of the parameter to get the value
104 :param default str: The value to return if param_name is not in config
105 :return: see initial description of the function
108 if param_name in config:
109 return config[param_name]
113 def getProductNames(cfg, wildcards, logger):
114 """get products names using * or ? as wildcards like shell Linux"""
116 if type(wildcards) is list:
120 products = cfg.APPLICATION.products.keys()
121 for prod in products:
122 for wild in wildcards:
123 filtered = fnmatch.filter([prod], wild)
124 # print("filtered", prod, wild, filtered)
125 if len(filtered) > 0:
129 logger.warning("Empty list of products, from %s" % wilds)
133 def print_info(logger, info):
135 Prints the tuples that are in info variable in a formatted way.
137 :param logger Logger: The logging instance to use for the prints.
138 :param info list: The list of tuples to display
140 # find the maximum length of the first value of the tuples in info
141 smax = max(map(lambda l: len(l[0]), info))
142 # Print each item of info with good indentation
144 sp = " " * (smax - len(i[0]))
145 printcolors.print_value(logger, sp + i[0], i[1], 2)
146 logger.write("\n", 2)
148 def get_base_path(config):
150 Returns the path of the products base.
152 :param config Config: The global Config instance.
153 :return: The path of the products base.
156 if "base" not in config.LOCAL:
157 local_file_path = os.path.join(config.VARS.salometoolsway,
160 msg = _("Please define a base path in the file %s" % local_file_path)
161 raise SatException(msg)
163 base_path = os.path.abspath(config.LOCAL.base)
167 def get_launcher_name(config):
169 Returns the name of salome launcher.
171 :param config Config: The global Config instance.
172 :return: The name of salome launcher.
175 check_config_has_application(config)
176 if 'profile' in config.APPLICATION and 'launcher_name' in config.APPLICATION.profile:
177 launcher_name = config.APPLICATION.profile.launcher_name
179 launcher_name = 'salome'
183 def get_log_path(config):
185 Returns the path of the logs.
187 :param config Config: The global Config instance.
188 :return: The path of the logs.
191 if "log_dir" not in config.LOCAL:
192 local_file_path = os.path.join(config.VARS.salometoolsway,
195 msg = _("Please define a log_dir in the file %s" % local_file_path)
196 raise SatException(msg)
198 log_dir_path = os.path.abspath(config.LOCAL.log_dir)
202 def get_salome_version(config):
203 import versionMinorMajorPatch as VMMP
205 if hasattr(config.APPLICATION, 'version_salome'):
206 version = VMMP.MinorMajorPatch(config.APPLICATION.version_salome)
208 kernel_info = product.get_product_config(config, "KERNEL")
209 aFile = os.path.join(
210 kernel_info.install_dir,
214 if not os.path.isfile(aFile):
216 with open(aFile) as f:
217 line = f.readline() # example: '[SALOME KERNEL] : 8.4.0'
218 version = VMMP.MinorMajorPatch(line.split(":")[1])
220 res = version.strCompact()
221 # print("get_salome_version %s -> %s" % (version, res))
224 def read_config_from_a_file(filePath):
226 cfg_file = pyconf.Config(filePath)
227 except pyconf.ConfigError as e:
228 raise SatException(_("Error in configuration file: %(file)s\n %(error)s") % \
229 { 'file': filePath, 'error': str(e) })
232 def get_tmp_filename(cfg, name):
233 if not os.path.exists(cfg.VARS.tmp_root):
234 os.makedirs(cfg.VARS.tmp_root)
236 return os.path.join(cfg.VARS.tmp_root, name)
239 # Utils class to simplify path manipulations.
241 def __init__(self, path):
242 self.path = str(path)
244 def __add__(self, other):
245 return Path(os.path.join(self.path, str(other)))
248 return Path(os.path.abspath(self.path))
253 def __eq__(self, other):
254 return self.path == other.path
257 return self.islink() or os.path.exists(self.path)
260 return os.path.islink(self.path)
263 return os.path.isdir(self.path)
266 return os.path.isfile(self.path)
269 return [Path(p) for p in os.listdir(self.path)]
272 return Path(os.path.dirname(self.path))
275 return Path(os.path.basename(self.path))
277 def make(self, mode=None):
278 os.makedirs(self.path)
280 os.chmod(self.path, mode)
282 def chmod(self, mode):
283 os.chmod(self.path, mode)
289 shutil.rmtree( self.path, onerror = handleRemoveReadonly )
291 def copy(self, path, smart=False):
292 if not isinstance(path, Path):
295 if os.path.islink(self.path):
296 return self.copylink(path)
297 elif os.path.isdir(self.path):
298 return self.copydir(path, smart)
300 return self.copyfile(path)
302 def smartcopy(self, path):
303 return self.copy(path, True)
307 return os.readlink(self.path)
311 def symlink(self, path):
313 os.symlink(str(path), self.path)
318 def copylink(self, path):
320 os.symlink(os.readlink(self.path), str(path))
325 def copydir(self, dst, smart=False):
335 if smart and (str(name) in [".git", "CVS", ".svn"]):
337 srcname = self + name
339 srcname.copy(dstname, smart)
344 def copyfile(self, path):
346 shutil.copy2(self.path, str(path))
351 def find_file_in_lpath(file_name, lpath, additional_dir = ""):
353 Find in all the directories in lpath list the file that has the same name
356 then return the full path of the file
359 The additional_dir (optional) is the name of the directory to add to all
362 :param file_name str: The file name to search
363 :param lpath List: The list of directories where to search
364 :param additional_dir str: The name of the additional directory
365 :return: the full path of the file or False if not found
368 for directory in lpath:
369 dir_complete = os.path.join(directory, additional_dir)
370 if not os.path.isdir(directory) or not os.path.isdir(dir_complete):
372 l_files = os.listdir(dir_complete)
373 for file_n in l_files:
374 if file_n == file_name:
375 return os.path.join(dir_complete, file_name)
378 def handleRemoveReadonly(func, path, exc):
380 if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
381 os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
386 def deepcopy_list(input_list):
388 Do a deep copy of a list
390 :param input_list List: The list to copy
391 :return: The copy of the list
395 for elem in input_list:
399 def remove_item_from_list(input_list, item):
401 Remove all occurences of item from input_list
403 :param input_list List: The list to modify
404 :return: The without any item
408 for elem in input_list:
414 def parse_date(date):
416 Transform YYYYMMDD_hhmmss into YYYY-MM-DD hh:mm:ss.
418 :param date str: The date to transform
419 :return: The date in the new format
424 res = "%s-%s-%s %s:%s:%s" % (date[0:4],
432 def merge_dicts(*dict_args):
434 Given any number of dicts, shallow copy and merge into a new dict,
435 precedence goes to key value pairs in latter dicts.
438 for dictionary in dict_args:
439 result.update(dictionary)
442 def replace_in_file(filein, strin, strout):
443 """Replace <strin> by <strout> in file <filein>"""
444 shutil.move(filein, filein + "_old")
446 filein = filein + "_old"
447 fin = open(filein, "r")
448 fout = open(fileout, "w")
450 fout.write(line.replace(strin, strout))
452 def get_property_in_product_cfg(product_cfg, pprty):
453 if not "properties" in product_cfg:
455 if not pprty in product_cfg.properties:
457 return product_cfg.properties[pprty]
459 def activate_mesa_property(config):
460 """Add mesa property into application properties
462 :param config Config: The global configuration. It must have an application!
464 # Verify the existence of the file
465 if not 'properties' in config.APPLICATION:
466 config.APPLICATION.addMapping( 'properties', pyconf.Mapping(), None )
467 config.APPLICATION.properties.use_mesa="yes"