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
37 class SatException(Exception):
38 '''rename Exception Class
42 def ensure_path_exists(p):
43 '''Create a path if not existing
45 :param p str: The path.
47 if not os.path.exists(p):
50 def check_config_has_application( config, details = None ):
51 '''check that the config has the key APPLICATION. Else raise an exception.
53 :param config class 'common.pyconf.Config': The config.
55 if 'APPLICATION' not in config:
56 message = _("An APPLICATION is required. Use 'config --list' to get"
57 " the list of available applications.\n")
59 details.append(message)
60 raise SatException( message )
62 def config_has_application( config ):
63 return 'APPLICATION' in config
65 def print_info(logger, info):
66 '''Prints the tuples that are in info variable in a formatted way.
68 :param logger Logger: The logging instance to use for the prints.
69 :param info list: The list of tuples to display
71 # find the maximum length of the first value of the tuples in info
72 smax = max(map(lambda l: len(l[0]), info))
73 # Print each item of info with good indentation
75 sp = " " * (smax - len(i[0]))
76 printcolors.print_value(logger, sp + i[0], i[1], 2)
80 # Utils class to simplify path manipulations.
82 def __init__(self, path):
85 def __add__(self, other):
86 return Path(os.path.join(self.path, str(other)))
89 return Path(os.path.abspath(self.path))
94 def __eq__(self, other):
95 return self.path == other.path
98 return self.islink() or os.path.exists(self.path)
101 return os.path.islink(self.path)
104 return os.path.isdir(self.path)
107 return [Path(p) for p in os.listdir(self.path)]
110 return Path(os.path.dirname(self.path))
113 return Path(os.path.basename(self.path))
115 def make(self, mode=None):
116 os.makedirs(self.path)
118 os.chmod(self.path, mode)
120 def chmod(self, mode):
121 os.chmod(self.path, mode)
127 shutil.rmtree( self.path, onerror = handleRemoveReadonly )
129 def copy(self, path, smart=False):
130 if not isinstance(path, Path):
133 if os.path.islink(self.path):
134 return self.copylink(path)
135 elif os.path.isdir(self.path):
136 return self.copydir(path, smart)
138 return self.copyfile(path)
140 def smartcopy(self, path):
141 return self.copy(path, True)
145 return os.readlink(self.path)
149 def symlink(self, path):
151 os.symlink(str(path), self.path)
156 def copylink(self, path):
158 os.symlink(os.readlink(self.path), str(path))
163 def copydir(self, dst, smart=False):
173 if smart and (str(name) in [".git", "CVS", ".svn"]):
175 srcname = self + name
177 srcname.copy(dstname, smart)
182 def copyfile(self, path):
184 shutil.copy2(self.path, str(path))
189 def handleRemoveReadonly(func, path, exc):
191 if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
192 os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777