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
25 from . import architecture
26 from . import printcolors
29 from . import ElementTree
32 from . import environment
33 from . import fileEnviron
34 from . import compilation
35 from . import test_module
40 KNOWNFAILURE_STATUS = "KF"
41 TIMEOUT_STATUS = "TIMEOUT"
43 class SatException(Exception):
44 '''rename Exception Class
48 def ensure_path_exists(p):
49 '''Create a path if not existing
51 :param p str: The path.
53 if not os.path.exists(p):
56 def check_config_has_application( config, details = None ):
57 '''check that the config has the key APPLICATION. Else raise an exception.
59 :param config class 'common.pyconf.Config': The config.
61 if 'APPLICATION' not in config:
62 message = _("An APPLICATION is required. Use 'config --list' to get"
63 " the list of available applications.\n")
65 details.append(message)
66 raise SatException( message )
68 def check_config_has_profile( config, details = None ):
69 '''check that the config has the key APPLICATION.profile.
70 Else, raise an exception.
72 :param config class 'common.pyconf.Config': The config.
74 check_config_has_application(config)
75 if 'profile' not in config.APPLICATION:
76 message = _("A profile section is required in your application.\n")
78 details.append(message)
79 raise SatException( message )
81 def config_has_application( config ):
82 return 'APPLICATION' in config
84 def get_cfg_param(config, param_name, default):
85 '''Search for param_name value in config.
86 If param_name is not in config, then return default,
87 else, return the found value
89 :param config class 'common.pyconf.Config': The config.
90 :param param_name str: the name of the parameter to get the value
91 :param default str: The value to return if param_name is not in config
92 :return: see initial description of the function
95 if param_name in config:
96 return config[param_name]
99 def print_info(logger, info):
100 '''Prints the tuples that are in info variable in a formatted way.
102 :param logger Logger: The logging instance to use for the prints.
103 :param info list: The list of tuples to display
105 # find the maximum length of the first value of the tuples in info
106 smax = max(map(lambda l: len(l[0]), info))
107 # Print each item of info with good indentation
109 sp = " " * (smax - len(i[0]))
110 printcolors.print_value(logger, sp + i[0], i[1], 2)
111 logger.write("\n", 2)
113 def get_base_path(config):
114 '''Returns the path of the product base.
116 :param config Config: The global Config instance.
117 :return: The path of the product base.
120 if "base" in config.SITE:
121 base_path = config.SITE.base
124 base_path = config.USER.base
127 def get_salome_version(config):
128 if hasattr(config.APPLICATION, 'version_salome'):
129 Version = config.APPLICATION.version_salome
131 KERNEL_info = product.get_product_config(config, "KERNEL")
132 VERSION = os.path.join(
133 KERNEL_info.install_dir,
137 if not os.path.isfile(VERSION):
140 fVERSION = open(VERSION)
141 Version = fVERSION.readline()
144 VersionSalome = int(only_numbers(Version))
147 def only_numbers(str_num):
148 return ''.join([nb for nb in str_num if nb in '0123456789'] or '0')
150 def read_config_from_a_file(filePath):
152 cfg_file = pyconf.Config(filePath)
153 except pyconf.ConfigError as e:
154 raise SatException(_("Error in configuration file: %(file)s\n %(error)s") % \
155 { 'file': filePath, 'error': str(e) })
158 def get_tmp_filename(cfg, name):
159 if not os.path.exists(cfg.VARS.tmp_root):
160 os.makedirs(cfg.VARS.tmp_root)
162 return os.path.join(cfg.VARS.tmp_root, name)
165 # Utils class to simplify path manipulations.
167 def __init__(self, path):
168 self.path = str(path)
170 def __add__(self, other):
171 return Path(os.path.join(self.path, str(other)))
174 return Path(os.path.abspath(self.path))
179 def __eq__(self, other):
180 return self.path == other.path
183 return self.islink() or os.path.exists(self.path)
186 return os.path.islink(self.path)
189 return os.path.isdir(self.path)
192 return [Path(p) for p in os.listdir(self.path)]
195 return Path(os.path.dirname(self.path))
198 return Path(os.path.basename(self.path))
200 def make(self, mode=None):
201 os.makedirs(self.path)
203 os.chmod(self.path, mode)
205 def chmod(self, mode):
206 os.chmod(self.path, mode)
212 shutil.rmtree( self.path, onerror = handleRemoveReadonly )
214 def copy(self, path, smart=False):
215 if not isinstance(path, Path):
218 if os.path.islink(self.path):
219 return self.copylink(path)
220 elif os.path.isdir(self.path):
221 return self.copydir(path, smart)
223 return self.copyfile(path)
225 def smartcopy(self, path):
226 return self.copy(path, True)
230 return os.readlink(self.path)
234 def symlink(self, path):
236 os.symlink(str(path), self.path)
241 def copylink(self, path):
243 os.symlink(os.readlink(self.path), str(path))
248 def copydir(self, dst, smart=False):
258 if smart and (str(name) in [".git", "CVS", ".svn"]):
260 srcname = self + name
262 srcname.copy(dstname, smart)
267 def copyfile(self, path):
269 shutil.copy2(self.path, str(path))
274 def find_file_in_lpath(file_name, lpath, additional_dir = ""):
275 """Find in all the directories in lpath list the file that has the same name
276 as file_name. If it is found, return the full path of the file, else,
278 The additional_dir (optional) is the name of the directory to add to all
281 :param file_name str: The file name to search
282 :param lpath List: The list of directories where to search
283 :param additional_dir str: The name of the additional directory
284 :return: the full path of the file or False if not found
287 for directory in lpath:
288 dir_complete = os.path.join(directory, additional_dir)
289 if not os.path.isdir(directory) or not os.path.isdir(dir_complete):
291 l_files = os.listdir(dir_complete)
292 for file_n in l_files:
293 if file_n == file_name:
294 return os.path.join(dir_complete, file_name)
297 def handleRemoveReadonly(func, path, exc):
299 if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
300 os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
305 def deepcopy_list(input_list):
306 """ Do a deep copy of a list
308 :param input_list List: The list to copy
309 :return: The copy of the list
313 for elem in input_list:
317 def parse_date(date):
318 """Transform YYYYMMDD_hhmmss into YYYY-MM-DD hh:mm:ss.
320 :param date str: The date to transform
321 :return: The date in the new format
326 res = "%s-%s-%s %s:%s:%s" % (date[0:4],