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
40 class SatException(Exception):
41 '''rename Exception Class
45 def ensure_path_exists(p):
46 '''Create a path if not existing
48 :param p str: The path.
50 if not os.path.exists(p):
53 def check_config_has_application( config, details = None ):
54 '''check that the config has the key APPLICATION. Else raise an exception.
56 :param config class 'common.pyconf.Config': The config.
58 if 'APPLICATION' not in config:
59 message = _("An APPLICATION is required. Use 'config --list' to get"
60 " the list of available applications.\n")
62 details.append(message)
63 raise SatException( message )
65 def check_config_has_profile( config, details = None ):
66 '''check that the config has the key APPLICATION.profile.
67 Else, raise an exception.
69 :param config class 'common.pyconf.Config': The config.
71 check_config_has_application(config)
72 if 'profile' not in config.APPLICATION:
73 message = _("A profile section is required in your application.\n")
75 details.append(message)
76 raise SatException( message )
78 def config_has_application( config ):
79 return 'APPLICATION' in config
81 def get_cfg_param(config, param_name, default):
82 '''Search for param_name value in config.
83 If param_name is not in config, then return default,
84 else, return the found value
86 :param config class 'common.pyconf.Config': The config.
87 :param param_name str: the name of the parameter to get the value
88 :param default str: The value to return if param_name is not in config
89 :return: see initial description of the function
92 if param_name in config:
93 return config[param_name]
96 def print_info(logger, info):
97 '''Prints the tuples that are in info variable in a formatted way.
99 :param logger Logger: The logging instance to use for the prints.
100 :param info list: The list of tuples to display
102 # find the maximum length of the first value of the tuples in info
103 smax = max(map(lambda l: len(l[0]), info))
104 # Print each item of info with good indentation
106 sp = " " * (smax - len(i[0]))
107 printcolors.print_value(logger, sp + i[0], i[1], 2)
108 logger.write("\n", 2)
110 def get_base_path(config):
111 '''Returns the path of the product base.
113 :param config Config: The global Config instance.
114 :return: The path of the product base.
117 if "base" in config.APPLICATION:
118 base_name = config.APPLICATION.base
119 base_path = config.USER.bases[base_name]
122 base_path = config.USER.bases.base
125 def only_numbers(str_num):
126 return ''.join([nb for nb in str_num if nb in '0123456789'] or '0')
128 def read_config_from_a_file(filePath):
130 cfg_file = pyconf.Config(filePath)
131 except pyconf.ConfigError as e:
132 raise SatException(_("Error in configuration file: %(file)s\n %(error)s") % \
133 { 'file': filePath, 'error': str(e) })
136 def get_tmp_filename(cfg, name):
137 if not os.path.exists(cfg.VARS.tmp_root):
138 os.makedirs(cfg.VARS.tmp_root)
140 return os.path.join(cfg.VARS.tmp_root, name)
143 # Utils class to simplify path manipulations.
145 def __init__(self, path):
146 self.path = str(path)
148 def __add__(self, other):
149 return Path(os.path.join(self.path, str(other)))
152 return Path(os.path.abspath(self.path))
157 def __eq__(self, other):
158 return self.path == other.path
161 return self.islink() or os.path.exists(self.path)
164 return os.path.islink(self.path)
167 return os.path.isdir(self.path)
170 return [Path(p) for p in os.listdir(self.path)]
173 return Path(os.path.dirname(self.path))
176 return Path(os.path.basename(self.path))
178 def make(self, mode=None):
179 os.makedirs(self.path)
181 os.chmod(self.path, mode)
183 def chmod(self, mode):
184 os.chmod(self.path, mode)
190 shutil.rmtree( self.path, onerror = handleRemoveReadonly )
192 def copy(self, path, smart=False):
193 if not isinstance(path, Path):
196 if os.path.islink(self.path):
197 return self.copylink(path)
198 elif os.path.isdir(self.path):
199 return self.copydir(path, smart)
201 return self.copyfile(path)
203 def smartcopy(self, path):
204 return self.copy(path, True)
208 return os.readlink(self.path)
212 def symlink(self, path):
214 os.symlink(str(path), self.path)
219 def copylink(self, path):
221 os.symlink(os.readlink(self.path), str(path))
226 def copydir(self, dst, smart=False):
236 if smart and (str(name) in [".git", "CVS", ".svn"]):
238 srcname = self + name
240 srcname.copy(dstname, smart)
245 def copyfile(self, path):
247 shutil.copy2(self.path, str(path))
252 def handleRemoveReadonly(func, path, exc):
254 if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
255 os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777