1 # ex:ts=4:sw=4:sts=4:et
2 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 BitBake Utility Functions
7 # Copyright (C) 2004 Michael Lauer
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License version 2 as
11 # published by the Free Software Foundation.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 ascii_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
27 def explode_version(s):
29 alpha_regexp = re.compile('^([a-zA-Z]+)(.*)$')
30 numeric_regexp = re.compile('^(\d+)(.*)$')
33 m = numeric_regexp.match(s)
34 r.append(int(m.group(1)))
37 if s[0] in ascii_letters:
38 m = alpha_regexp.match(s)
45 def vercmp_part(a, b):
46 va = explode_version(a)
47 vb = explode_version(b)
57 if ca == None and cb == None:
70 r = vercmp_part(va, vb)
72 r = vercmp_part(ra, rb)
77 Take an RDEPENDS style string of format:
78 "DEPEND1 (optional version) DEPEND2 (optional version) ..."
79 and return a list of dependencies.
80 Version information is ignored.
93 if flag and i.endswith(')'):
96 #r[-1] += ' ' + ' '.join(j)
101 def _print_trace(body, line):
103 Print the Environment of a Text Body
107 # print the environment of the method
108 bb.msg.error(bb.msg.domain.Util, "Printing the environment of the function")
109 min_line = max(1,line-4)
110 max_line = min(line+4,len(body)-1)
111 for i in range(min_line,max_line+1):
112 bb.msg.error(bb.msg.domain.Util, "\t%.4d:%s" % (i, body[i-1]) )
115 def better_compile(text, file, realfile):
117 A better compile method. This method
118 will print the offending lines.
121 return compile(text, file, "exec")
125 # split the text into lines again
126 body = text.split('\n')
127 bb.msg.error(bb.msg.domain.Util, "Error in compiling: ", realfile)
128 bb.msg.error(bb.msg.domain.Util, "The lines resulting into this error were:")
129 bb.msg.error(bb.msg.domain.Util, "\t%d:%s:'%s'" % (e.lineno, e.__class__.__name__, body[e.lineno-1]))
131 _print_trace(body, e.lineno)
136 def better_exec(code, context, text, realfile):
138 Similiar to better_compile, better_exec will
139 print the lines that are responsible for the
146 (t,value,tb) = sys.exc_info()
148 if t in [bb.parse.SkipPackage, bb.build.FuncFailed]:
151 # print the Header of the Error Message
152 bb.msg.error(bb.msg.domain.Util, "Error in executing: ", realfile)
153 bb.msg.error(bb.msg.domain.Util, "Exception:%s Message:%s" % (t,value) )
155 # let us find the line number now
160 line = traceback.tb_lineno(tb)
162 _print_trace( text.split('\n'), line )
168 A simple class to give Enum support
171 assert names, "Empty enums are not supported"
173 class EnumClass(object):
175 def __iter__(self): return iter(constants)
176 def __len__(self): return len(constants)
177 def __getitem__(self, i): return constants[i]
178 def __repr__(self): return 'Enum' + str(names)
179 def __str__(self): return 'enum ' + str(constants)
181 class EnumValue(object):
182 __slots__ = ('__value')
183 def __init__(self, value): self.__value = value
184 Value = property(lambda self: self.__value)
185 EnumType = property(lambda self: EnumType)
186 def __hash__(self): return hash(self.__value)
187 def __cmp__(self, other):
188 # C fans might want to remove the following assertion
189 # to make all enums comparable by ordinal value {;))
190 assert self.EnumType is other.EnumType, "Only values from the same enum are comparable"
191 return cmp(self.__value, other.__value)
192 def __invert__(self): return constants[maximum - self.__value]
193 def __nonzero__(self): return bool(self.__value)
194 def __repr__(self): return str(names[self.__value])
196 maximum = len(names) - 1
197 constants = [None] * len(names)
198 for i, each in enumerate(names):
200 setattr(EnumClass, each, val)
202 constants = tuple(constants)
203 EnumType = EnumClass()
208 Use the file fn as a lock file, return when the lock has been aquired.
209 Returns a variable to pass to unlockfile().
212 # If we leave the lockfiles lying around there is no problem
213 # but we should clean up after ourselves. This gives potential
214 # for races though. To work around this, when we aquire the lock
215 # we check the file we locked was still the lock file on disk.
216 # by comparing inode numbers. If they don't match or the lockfile
217 # no longer exists, we start again.
219 # This implementation is unfair since the last person to request the
220 # lock is the most likely to win it.
222 lf = open(name, "a+")
223 fcntl.flock(lf.fileno(), fcntl.LOCK_EX)
224 statinfo = os.fstat(lf.fileno())
225 if os.path.exists(lf.name):
226 statinfo2 = os.stat(lf.name)
227 if statinfo.st_ino == statinfo2.st_ino:
229 # File no longer exists or changed, retry
234 Unlock a file locked using lockfile()
237 fcntl.flock(lf.fileno(), fcntl.LOCK_UN)