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 ftplib import FTP
33 from . import architecture
34 from . import printcolors
37 from . import ElementTree
40 from . import environment
41 from . import fileEnviron
42 from . import compilation
43 from . import test_module
44 from . import template
47 if platform.system() == "Windows" :
54 KNOWNFAILURE_STATUS = "KF"
55 TIMEOUT_STATUS = "TIMEOUT"
57 class SatException(Exception):
58 """rename Exception Class"""
61 def ensure_path_exists(p):
62 """Create a path if not existing
64 :param p str: The path.
66 if not os.path.exists(p):
69 def check_config_has_application( config, details = None ):
70 """check that the config has the key APPLICATION. Else raise an exception.
72 :param config class 'common.pyconf.Config': The config.
74 if 'APPLICATION' not in config:
75 message = _("An APPLICATION is required. Use 'config --list' to get the list of available applications.\n")
77 details.append(message)
78 raise SatException( message )
80 def check_config_has_profile( config, details = None ):
82 check that the config has the key APPLICATION.profile.
83 else, raise an exception.
85 :param config class 'common.pyconf.Config': The config.
87 check_config_has_application(config)
88 if 'profile' not in config.APPLICATION:
89 message = _("A profile section is required in your application.\n")
91 details.append(message)
92 raise SatException( message )
94 def config_has_application( config ):
95 return 'APPLICATION' in config
97 def get_cfg_param(config, param_name, default):
99 eearch for param_name value in config.
100 if param_name is not in config
102 else return the found value
104 :param config class 'common.pyconf.Config': The config.
105 :param param_name str: the name of the parameter to get the value
106 :param default str: The value to return if param_name is not in config
107 :return: see initial description of the function
110 if param_name in config:
111 return config[param_name]
114 def strSplitN(aList, nb, skip="\n "):
117 aStr = 'this-is-a-string'
119 split it by every 2nd '-' rather than every '-'
124 strValue += "%15s, " % str(v)
130 strValue = skip + strValue
133 def getProductNames(cfg, wildcards, logger):
134 """get products names using * or ? as wildcards like shell Linux"""
136 if type(wildcards) is list:
141 products = cfg.APPLICATION.products.keys()
142 for wild in wildcards:
144 for prod in products:
145 filtered = fnmatch.filter([prod], wild)
146 # print("filtered", prod, wild, filtered)
147 if len(filtered) > 0:
152 notFound[wild] = None
154 logger.warning("Empty list of products, from %s" % wilds)
155 if len(notFound.keys()) > 0:
156 strProd = strSplitN( sorted(products), 5)
157 logger.warning("products not found: %s\n availables products are:\n%s" % \
158 (sorted(notFound.keys()), strProd) )
162 def print_info(logger, info):
164 Prints the tuples that are in info variable in a formatted way.
166 :param logger Logger: The logging instance to use for the prints.
167 :param info list: The list of tuples to display
169 # find the maximum length of the first value of the tuples in info
170 smax = max(map(lambda l: len(l[0]), info))
171 # Print each item of info with good indentation
173 sp = " " * (smax - len(i[0]))
174 printcolors.print_value(logger, sp + i[0], i[1], 2)
175 logger.write("\n", 2)
177 def get_base_path(config):
179 Returns the path of the products base.
181 :param config Config: The global Config instance.
182 :return: The path of the products base.
185 if "base" not in config.LOCAL:
186 local_file_path = os.path.join(config.VARS.salometoolsway,
189 msg = _("Please define a base path in the file %s" % local_file_path)
190 raise SatException(msg)
192 base_path = os.path.abspath(config.LOCAL.base)
196 def get_launcher_name(config):
198 Returns the name of salome launcher.
200 :param config Config: The global Config instance.
201 :return: The name of salome launcher.
204 check_config_has_application(config)
205 if 'profile' in config.APPLICATION and 'launcher_name' in config.APPLICATION.profile:
206 launcher_name = config.APPLICATION.profile.launcher_name
208 launcher_name = 'salome'
212 def get_log_path(config):
214 Returns the path of the logs.
216 :param config Config: The global Config instance.
217 :return: The path of the logs.
220 if "log_dir" not in config.LOCAL:
221 local_file_path = os.path.join(config.VARS.salometoolsway,
224 msg = _("Please define a log_dir in the file %s" % local_file_path)
225 raise SatException(msg)
227 log_dir_path = os.path.abspath(config.LOCAL.log_dir)
231 def get_salome_version(config):
232 import versionMinorMajorPatch as VMMP
234 if hasattr(config.APPLICATION, 'version_salome'):
235 version = VMMP.MinorMajorPatch(config.APPLICATION.version_salome)
237 kernel_info = product.get_product_config(config, "KERNEL")
238 aFile = os.path.join(
239 kernel_info.install_dir,
243 if not os.path.isfile(aFile):
245 with open(aFile) as f:
246 line = f.readline() # example: '[SALOME KERNEL] : 8.4.0'
247 version = VMMP.MinorMajorPatch(line.split(":")[1])
249 res = version.strCompact()
250 # print("get_salome_version %s -> %s" % (version, res))
251 return int(res) # TODO may be future avoid test(s) on integer, use MajorMinorPatch
253 def read_config_from_a_file(filePath):
255 cfg_file = pyconf.Config(filePath)
256 except pyconf.ConfigError as e:
257 raise SatException(_("Error in configuration file: %(file)s\n %(error)s") % \
258 { 'file': filePath, 'error': str(e) })
261 def get_tmp_filename(cfg, name):
262 if not os.path.exists(cfg.VARS.tmp_root):
263 os.makedirs(cfg.VARS.tmp_root)
265 return os.path.join(cfg.VARS.tmp_root, name)
268 # Utils class to simplify path manipulations.
270 def __init__(self, path):
271 self.path = str(path)
273 def __add__(self, other):
274 return Path(os.path.join(self.path, str(other)))
277 return Path(os.path.abspath(self.path))
282 def __eq__(self, other):
283 return self.path == other.path
286 return self.islink() or os.path.exists(self.path)
289 return os.path.islink(self.path)
292 return os.path.isdir(self.path)
295 return os.path.isfile(self.path)
298 return [Path(p) for p in os.listdir(self.path)]
301 return Path(os.path.dirname(self.path))
304 return Path(os.path.basename(self.path))
306 def make(self, mode=None):
307 os.makedirs(self.path)
309 os.chmod(self.path, mode)
311 def chmod(self, mode):
312 os.chmod(self.path, mode)
318 shutil.rmtree( self.path, onerror = handleRemoveReadonly )
320 def copy(self, path, smart=False):
321 if not isinstance(path, Path):
324 if os.path.islink(self.path):
325 return self.copylink(path)
326 elif os.path.isdir(self.path):
327 return self.copydir(path, smart)
329 return self.copyfile(path)
331 def smartcopy(self, path):
332 return self.copy(path, True)
336 return os.readlink(self.path)
340 def symlink(self, path):
342 os.symlink(str(path), self.path)
347 def copylink(self, path):
349 os.symlink(os.readlink(self.path), str(path))
354 def copydir(self, dst, smart=False):
364 if smart and (str(name) in [".git", "CVS", ".svn"]):
366 srcname = self + name
368 srcname.copy(dstname, smart)
373 def copyfile(self, path):
375 shutil.copy2(self.path, str(path))
380 def find_file_in_lpath(file_name, lpath, additional_dir = ""):
382 Find in all the directories in lpath list the file that has the same name
385 then return the full path of the file
388 The additional_dir (optional) is the name of the directory to add to all
391 :param file_name str: The file name to search
392 :param lpath List: The list of directories where to search
393 :param additional_dir str: The name of the additional directory
394 :return: the full path of the file or False if not found
397 for directory in lpath:
398 dir_complete = os.path.join(directory, additional_dir)
399 if not os.path.isdir(directory) or not os.path.isdir(dir_complete):
401 l_files = os.listdir(dir_complete)
402 for file_n in l_files:
403 if file_n == file_name:
404 return os.path.join(dir_complete, file_name)
407 def find_file_in_ftppath(file_name, ftppath, installation_dir, logger):
409 Find in all ftp servers in ftppath the file called file_name
410 If it is found then return the destination path of the file
411 (the place where the file was downloaded"
414 :param file_name str: The file name to search
415 :param ftppath, List: The list of ftp servers where to search
416 :param installation_dir str: The name of the installation directory
417 :return: the full path of the file or False if not found
418 :param logger Logger: The logging instance to use for the prints.
422 # make sure installation_dir exists
423 if not os.path.exists(installation_dir):
424 os.makedirs(installation_dir)
426 destination=os.path.join(installation_dir, file_name)
428 # paths in ftppath may contain several paths separated by ":"
429 # we plit them, and push all paths in bigftppath
431 for ipath in ftppath:
432 splpath=ipath.split(":")
435 for ftp_archive in bigftppath:
437 # ftp_archive has the form ftp.xxx.yyy/dir1/dir2/...
438 ftp_archive_split=ftp_archive.split("/")
439 ftp_server=ftp_archive_split[0]
440 ftp = FTP(ftp_server)
441 logger.write(" Connect to ftp server %s\n" % ftp_server, 3)
443 for directory in ftp_archive_split[1:]:
444 logger.write(" Change directory to %s\n" % directory, 3)
447 logger.error("while connecting to ftp server %s\n" % ftp_server)
450 if ftp.size(file_name) > 0:
451 # if file exists and is non empty
452 with open(destination,'wb') as dest_file:
453 ftp.retrbinary("RETR "+file_name, dest_file.write)
454 logger.write(" Archive %s was retrieved and stored in %s\n" % (file_name, destination), 3)
457 logger.error("File not found in ftp_archive %s\n" % ftp_server)
462 def handleRemoveReadonly(func, path, exc):
464 if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
465 os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
470 def deepcopy_list(input_list):
472 Do a deep copy of a list
474 :param input_list List: The list to copy
475 :return: The copy of the list
479 for elem in input_list:
483 def remove_item_from_list(input_list, item):
485 Remove all occurences of item from input_list
487 :param input_list List: The list to modify
488 :return: The without any item
492 for elem in input_list:
498 def parse_date(date):
500 Transform YYYYMMDD_hhmmss into YYYY-MM-DD hh:mm:ss.
502 :param date str: The date to transform
503 :return: The date in the new format
508 res = "%s-%s-%s %s:%s:%s" % (date[0:4],
516 def merge_dicts(*dict_args):
518 Given any number of dicts, shallow copy and merge into a new dict,
519 precedence goes to key value pairs in latter dicts.
522 for dictionary in dict_args:
523 result.update(dictionary)
526 def replace_in_file(filein, strin, strout):
527 """Replace <strin> by <strout> in file <filein>"""
528 shutil.move(filein, filein + "_old")
530 filein = filein + "_old"
531 fin = open(filein, "r")
532 fout = open(fileout, "w")
534 fout.write(line.replace(strin, strout))
536 def get_property_in_product_cfg(product_cfg, pprty):
537 if not "properties" in product_cfg:
539 if not pprty in product_cfg.properties:
541 return product_cfg.properties[pprty]
543 def activate_mesa_property(config):
544 """Add mesa property into application properties
546 :param config Config: The global configuration. It must have an application!
548 # Verify the existence of the file
549 if not 'properties' in config.APPLICATION:
550 config.APPLICATION.addMapping( 'properties', pyconf.Mapping(), None )
551 config.APPLICATION.properties.use_mesa="yes"