summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe
diff options
context:
space:
mode:
authorJoshua Lock <josh@linux.intel.com>2010-04-30 16:35:50 +0100
committerJoshua Lock <josh@linux.intel.com>2010-05-06 12:48:05 +0100
commitac023d775b651c9b1e28a7a725e72949fe54ad47 (patch)
treeda69e91c7d2ce4785ddf6af6b756758d1789d5a2 /meta/lib/oe
parent14196cb03190d9dac93be309763e3076385eb831 (diff)
downloadpoky-ac023d775b651c9b1e28a7a725e72949fe54ad47.tar.gz
lib/oe: Import oe lib from OE.dev
This library moves the common Python methods into modules of an 'oe' Python package. Signed-off-by: Joshua Lock <josh@linux.intel.com>
Diffstat (limited to 'meta/lib/oe')
-rw-r--r--meta/lib/oe/__init__.py0
-rw-r--r--meta/lib/oe/patch.py407
-rw-r--r--meta/lib/oe/path.py44
-rw-r--r--meta/lib/oe/qa.py76
-rw-r--r--meta/lib/oe/utils.py69
5 files changed, 596 insertions, 0 deletions
diff --git a/meta/lib/oe/__init__.py b/meta/lib/oe/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/meta/lib/oe/__init__.py
diff --git a/meta/lib/oe/patch.py b/meta/lib/oe/patch.py
new file mode 100644
index 0000000000..54c12bac2a
--- /dev/null
+++ b/meta/lib/oe/patch.py
@@ -0,0 +1,407 @@
1class NotFoundError(Exception):
2 def __init__(self, path):
3 self.path = path
4
5 def __str__(self):
6 return "Error: %s not found." % self.path
7
8class CmdError(Exception):
9 def __init__(self, exitstatus, output):
10 self.status = exitstatus
11 self.output = output
12
13 def __str__(self):
14 return "Command Error: exit status: %d Output:\n%s" % (self.status, self.output)
15
16
17def runcmd(args, dir = None):
18 import commands
19
20 if dir:
21 olddir = os.path.abspath(os.curdir)
22 if not os.path.exists(dir):
23 raise NotFoundError(dir)
24 os.chdir(dir)
25 # print("cwd: %s -> %s" % (olddir, dir))
26
27 try:
28 args = [ commands.mkarg(str(arg)) for arg in args ]
29 cmd = " ".join(args)
30 # print("cmd: %s" % cmd)
31 (exitstatus, output) = commands.getstatusoutput(cmd)
32 if exitstatus != 0:
33 raise CmdError(exitstatus >> 8, output)
34 return output
35
36 finally:
37 if dir:
38 os.chdir(olddir)
39
40class PatchError(Exception):
41 def __init__(self, msg):
42 self.msg = msg
43
44 def __str__(self):
45 return "Patch Error: %s" % self.msg
46
47class PatchSet(object):
48 defaults = {
49 "strippath": 1
50 }
51
52 def __init__(self, dir, d):
53 self.dir = dir
54 self.d = d
55 self.patches = []
56 self._current = None
57
58 def current(self):
59 return self._current
60
61 def Clean(self):
62 """
63 Clean out the patch set. Generally includes unapplying all
64 patches and wiping out all associated metadata.
65 """
66 raise NotImplementedError()
67
68 def Import(self, patch, force):
69 if not patch.get("file"):
70 if not patch.get("remote"):
71 raise PatchError("Patch file must be specified in patch import.")
72 else:
73 patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
74
75 for param in PatchSet.defaults:
76 if not patch.get(param):
77 patch[param] = PatchSet.defaults[param]
78
79 if patch.get("remote"):
80 patch["file"] = bb.data.expand(bb.fetch.localpath(patch["remote"], self.d), self.d)
81
82 patch["filemd5"] = bb.utils.md5_file(patch["file"])
83
84 def Push(self, force):
85 raise NotImplementedError()
86
87 def Pop(self, force):
88 raise NotImplementedError()
89
90 def Refresh(self, remote = None, all = None):
91 raise NotImplementedError()
92
93
94class PatchTree(PatchSet):
95 def __init__(self, dir, d):
96 PatchSet.__init__(self, dir, d)
97
98 def Import(self, patch, force = None):
99 """"""
100 PatchSet.Import(self, patch, force)
101
102 if self._current is not None:
103 i = self._current + 1
104 else:
105 i = 0
106 self.patches.insert(i, patch)
107
108 def _applypatch(self, patch, force = False, reverse = False, run = True):
109 shellcmd = ["cat", patch['file'], "|", "patch", "-p", patch['strippath']]
110 if reverse:
111 shellcmd.append('-R')
112
113 if not run:
114 return "sh" + "-c" + " ".join(shellcmd)
115
116 if not force:
117 shellcmd.append('--dry-run')
118
119 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
120
121 if force:
122 return
123
124 shellcmd.pop(len(shellcmd) - 1)
125 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
126 return output
127
128 def Push(self, force = False, all = False, run = True):
129 bb.note("self._current is %s" % self._current)
130 bb.note("patches is %s" % self.patches)
131 if all:
132 for i in self.patches:
133 if self._current is not None:
134 self._current = self._current + 1
135 else:
136 self._current = 0
137 bb.note("applying patch %s" % i)
138 self._applypatch(i, force)
139 else:
140 if self._current is not None:
141 self._current = self._current + 1
142 else:
143 self._current = 0
144 bb.note("applying patch %s" % self.patches[self._current])
145 return self._applypatch(self.patches[self._current], force)
146
147
148 def Pop(self, force = None, all = None):
149 if all:
150 for i in self.patches:
151 self._applypatch(i, force, True)
152 else:
153 self._applypatch(self.patches[self._current], force, True)
154
155 def Clean(self):
156 """"""
157
158class GitApplyTree(PatchTree):
159 def __init__(self, dir, d):
160 PatchTree.__init__(self, dir, d)
161
162 def _applypatch(self, patch, force = False, reverse = False, run = True):
163 shellcmd = ["git", "--git-dir=.", "apply", "-p%s" % patch['strippath']]
164
165 if reverse:
166 shellcmd.append('-R')
167
168 shellcmd.append(patch['file'])
169
170 if not run:
171 return "sh" + "-c" + " ".join(shellcmd)
172
173 return runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
174
175
176class QuiltTree(PatchSet):
177 def _runcmd(self, args, run = True):
178 quiltrc = bb.data.getVar('QUILTRCFILE', self.d, 1)
179 if not run:
180 return ["quilt"] + ["--quiltrc"] + [quiltrc] + args
181 runcmd(["quilt"] + ["--quiltrc"] + [quiltrc] + args, self.dir)
182
183 def _quiltpatchpath(self, file):
184 return os.path.join(self.dir, "patches", os.path.basename(file))
185
186
187 def __init__(self, dir, d):
188 PatchSet.__init__(self, dir, d)
189 self.initialized = False
190 p = os.path.join(self.dir, 'patches')
191 if not os.path.exists(p):
192 os.makedirs(p)
193
194 def Clean(self):
195 try:
196 self._runcmd(["pop", "-a", "-f"])
197 except Exception:
198 pass
199 self.initialized = True
200
201 def InitFromDir(self):
202 # read series -> self.patches
203 seriespath = os.path.join(self.dir, 'patches', 'series')
204 if not os.path.exists(self.dir):
205 raise Exception("Error: %s does not exist." % self.dir)
206 if os.path.exists(seriespath):
207 series = file(seriespath, 'r')
208 for line in series.readlines():
209 patch = {}
210 parts = line.strip().split()
211 patch["quiltfile"] = self._quiltpatchpath(parts[0])
212 patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"])
213 if len(parts) > 1:
214 patch["strippath"] = parts[1][2:]
215 self.patches.append(patch)
216 series.close()
217
218 # determine which patches are applied -> self._current
219 try:
220 output = runcmd(["quilt", "applied"], self.dir)
221 except CmdError:
222 import sys
223 if sys.exc_value.output.strip() == "No patches applied":
224 return
225 else:
226 raise sys.exc_value
227 output = [val for val in output.split('\n') if not val.startswith('#')]
228 for patch in self.patches:
229 if os.path.basename(patch["quiltfile"]) == output[-1]:
230 self._current = self.patches.index(patch)
231 self.initialized = True
232
233 def Import(self, patch, force = None):
234 if not self.initialized:
235 self.InitFromDir()
236 PatchSet.Import(self, patch, force)
237
238 args = ["import", "-p", patch["strippath"]]
239 if force:
240 args.append("-f")
241 args.append("-dn")
242 args.append(patch["file"])
243
244 self._runcmd(args)
245
246 patch["quiltfile"] = self._quiltpatchpath(patch["file"])
247 patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"])
248
249 # TODO: determine if the file being imported:
250 # 1) is already imported, and is the same
251 # 2) is already imported, but differs
252
253 self.patches.insert(self._current or 0, patch)
254
255
256 def Push(self, force = False, all = False, run = True):
257 # quilt push [-f]
258
259 args = ["push"]
260 if force:
261 args.append("-f")
262 if all:
263 args.append("-a")
264 if not run:
265 return self._runcmd(args, run)
266
267 self._runcmd(args)
268
269 if self._current is not None:
270 self._current = self._current + 1
271 else:
272 self._current = 0
273
274 def Pop(self, force = None, all = None):
275 # quilt pop [-f]
276 args = ["pop"]
277 if force:
278 args.append("-f")
279 if all:
280 args.append("-a")
281
282 self._runcmd(args)
283
284 if self._current == 0:
285 self._current = None
286
287 if self._current is not None:
288 self._current = self._current - 1
289
290 def Refresh(self, **kwargs):
291 if kwargs.get("remote"):
292 patch = self.patches[kwargs["patch"]]
293 if not patch:
294 raise PatchError("No patch found at index %s in patchset." % kwargs["patch"])
295 (type, host, path, user, pswd, parm) = bb.decodeurl(patch["remote"])
296 if type == "file":
297 import shutil
298 if not patch.get("file") and patch.get("remote"):
299 patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
300
301 shutil.copyfile(patch["quiltfile"], patch["file"])
302 else:
303 raise PatchError("Unable to do a remote refresh of %s, unsupported remote url scheme %s." % (os.path.basename(patch["quiltfile"]), type))
304 else:
305 # quilt refresh
306 args = ["refresh"]
307 if kwargs.get("quiltfile"):
308 args.append(os.path.basename(kwargs["quiltfile"]))
309 elif kwargs.get("patch"):
310 args.append(os.path.basename(self.patches[kwargs["patch"]]["quiltfile"]))
311 self._runcmd(args)
312
313class Resolver(object):
314 def __init__(self, patchset):
315 raise NotImplementedError()
316
317 def Resolve(self):
318 raise NotImplementedError()
319
320 def Revert(self):
321 raise NotImplementedError()
322
323 def Finalize(self):
324 raise NotImplementedError()
325
326class NOOPResolver(Resolver):
327 def __init__(self, patchset):
328 self.patchset = patchset
329
330 def Resolve(self):
331 olddir = os.path.abspath(os.curdir)
332 os.chdir(self.patchset.dir)
333 try:
334 self.patchset.Push()
335 except Exception:
336 import sys
337 os.chdir(olddir)
338 raise sys.exc_value
339
340# Patch resolver which relies on the user doing all the work involved in the
341# resolution, with the exception of refreshing the remote copy of the patch
342# files (the urls).
343class UserResolver(Resolver):
344 def __init__(self, patchset):
345 self.patchset = patchset
346
347 # Force a push in the patchset, then drop to a shell for the user to
348 # resolve any rejected hunks
349 def Resolve(self):
350
351 olddir = os.path.abspath(os.curdir)
352 os.chdir(self.patchset.dir)
353 try:
354 self.patchset.Push(False)
355 except CmdError, v:
356 # Patch application failed
357 patchcmd = self.patchset.Push(True, False, False)
358
359 t = bb.data.getVar('T', d, 1)
360 if not t:
361 bb.msg.fatal(bb.msg.domain.Build, "T not set")
362 bb.mkdirhier(t)
363 import random
364 rcfile = "%s/bashrc.%s.%s" % (t, str(os.getpid()), random.random())
365 f = open(rcfile, "w")
366 f.write("echo '*** Manual patch resolution mode ***'\n")
367 f.write("echo 'Dropping to a shell, so patch rejects can be fixed manually.'\n")
368 f.write("echo 'Run \"quilt refresh\" when patch is corrected, press CTRL+D to exit.'\n")
369 f.write("echo ''\n")
370 f.write(" ".join(patchcmd) + "\n")
371 f.write("#" + bb.data.getVar('TERMCMDRUN', d, 1))
372 f.close()
373 os.chmod(rcfile, 0775)
374
375 os.environ['TERMWINDOWTITLE'] = "Bitbake: Please fix patch rejects manually"
376 os.environ['TERMRCFILE'] = rcfile
377 rc = os.system(bb.data.getVar('TERMCMDRUN', d, 1))
378 if os.WIFEXITED(rc) and os.WEXITSTATUS(rc) != 0:
379 bb.msg.fatal(bb.msg.domain.Build, ("Cannot proceed with manual patch resolution - '%s' not found. " \
380 + "Check TERMCMDRUN variable.") % bb.data.getVar('TERMCMDRUN', d, 1))
381
382 # Construct a new PatchSet after the user's changes, compare the
383 # sets, checking patches for modifications, and doing a remote
384 # refresh on each.
385 oldpatchset = self.patchset
386 self.patchset = oldpatchset.__class__(self.patchset.dir, self.patchset.d)
387
388 for patch in self.patchset.patches:
389 oldpatch = None
390 for opatch in oldpatchset.patches:
391 if opatch["quiltfile"] == patch["quiltfile"]:
392 oldpatch = opatch
393
394 if oldpatch:
395 patch["remote"] = oldpatch["remote"]
396 if patch["quiltfile"] == oldpatch["quiltfile"]:
397 if patch["quiltfilemd5"] != oldpatch["quiltfilemd5"]:
398 bb.note("Patch %s has changed, updating remote url %s" % (os.path.basename(patch["quiltfile"]), patch["remote"]))
399 # user change? remote refresh
400 self.patchset.Refresh(remote=True, patch=self.patchset.patches.index(patch))
401 else:
402 # User did not fix the problem. Abort.
403 raise PatchError("Patch application failed, and user did not fix and refresh the patch.")
404 except Exception:
405 os.chdir(olddir)
406 raise
407 os.chdir(olddir)
diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
new file mode 100644
index 0000000000..8902951581
--- /dev/null
+++ b/meta/lib/oe/path.py
@@ -0,0 +1,44 @@
1def join(*paths):
2 """Like os.path.join but doesn't treat absolute RHS specially"""
3 import os.path
4 return os.path.normpath("/".join(paths))
5
6def relative(src, dest):
7 """ Return a relative path from src to dest.
8
9 >>> relative("/usr/bin", "/tmp/foo/bar")
10 ../../tmp/foo/bar
11
12 >>> relative("/usr/bin", "/usr/lib")
13 ../lib
14
15 >>> relative("/tmp", "/tmp/foo/bar")
16 foo/bar
17 """
18 import os.path
19
20 if hasattr(os.path, "relpath"):
21 return os.path.relpath(dest, src)
22 else:
23 destlist = os.path.normpath(dest).split(os.path.sep)
24 srclist = os.path.normpath(src).split(os.path.sep)
25
26 # Find common section of the path
27 common = os.path.commonprefix([destlist, srclist])
28 commonlen = len(common)
29
30 # Climb back to the point where they differentiate
31 relpath = [ os.path.pardir ] * (len(srclist) - commonlen)
32 if commonlen < len(destlist):
33 # Add remaining portion
34 relpath += destlist[commonlen:]
35
36 return os.path.sep.join(relpath)
37
38def format_display(path, metadata):
39 """ Prepare a path for display to the user. """
40 rel = relative(metadata.getVar("TOPDIR", 1), path)
41 if len(rel) > len(path):
42 return path
43 else:
44 return rel
diff --git a/meta/lib/oe/qa.py b/meta/lib/oe/qa.py
new file mode 100644
index 0000000000..01813931bb
--- /dev/null
+++ b/meta/lib/oe/qa.py
@@ -0,0 +1,76 @@
1class ELFFile:
2 EI_NIDENT = 16
3
4 EI_CLASS = 4
5 EI_DATA = 5
6 EI_VERSION = 6
7 EI_OSABI = 7
8 EI_ABIVERSION = 8
9
10 # possible values for EI_CLASS
11 ELFCLASSNONE = 0
12 ELFCLASS32 = 1
13 ELFCLASS64 = 2
14
15 # possible value for EI_VERSION
16 EV_CURRENT = 1
17
18 # possible values for EI_DATA
19 ELFDATANONE = 0
20 ELFDATA2LSB = 1
21 ELFDATA2MSB = 2
22
23 def my_assert(self, expectation, result):
24 if not expectation == result:
25 #print "'%x','%x' %s" % (ord(expectation), ord(result), self.name)
26 raise Exception("This does not work as expected")
27
28 def __init__(self, name, bits32):
29 self.name = name
30 self.bits32 = bits32
31
32 def open(self):
33 self.file = file(self.name, "r")
34 self.data = self.file.read(ELFFile.EI_NIDENT+4)
35
36 self.my_assert(len(self.data), ELFFile.EI_NIDENT+4)
37 self.my_assert(self.data[0], chr(0x7f) )
38 self.my_assert(self.data[1], 'E')
39 self.my_assert(self.data[2], 'L')
40 self.my_assert(self.data[3], 'F')
41 if self.bits32 :
42 self.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS32))
43 else:
44 self.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS64))
45 self.my_assert(self.data[ELFFile.EI_VERSION], chr(ELFFile.EV_CURRENT) )
46
47 self.sex = self.data[ELFFile.EI_DATA]
48 if self.sex == chr(ELFFile.ELFDATANONE):
49 raise Exception("self.sex == ELFDATANONE")
50 elif self.sex == chr(ELFFile.ELFDATA2LSB):
51 self.sex = "<"
52 elif self.sex == chr(ELFFile.ELFDATA2MSB):
53 self.sex = ">"
54 else:
55 raise Exception("Unknown self.sex")
56
57 def osAbi(self):
58 return ord(self.data[ELFFile.EI_OSABI])
59
60 def abiVersion(self):
61 return ord(self.data[ELFFile.EI_ABIVERSION])
62
63 def isLittleEndian(self):
64 return self.sex == "<"
65
66 def isBigEngian(self):
67 return self.sex == ">"
68
69 def machine(self):
70 """
71 We know the sex stored in self.sex and we
72 know the position
73 """
74 import struct
75 (a,) = struct.unpack(self.sex+"H", self.data[18:20])
76 return a
diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
new file mode 100644
index 0000000000..e61d663a50
--- /dev/null
+++ b/meta/lib/oe/utils.py
@@ -0,0 +1,69 @@
1def read_file(filename):
2 try:
3 f = file( filename, "r" )
4 except IOError, reason:
5 return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M:
6 else:
7 return f.read().strip()
8 return None
9
10def ifelse(condition, iftrue = True, iffalse = False):
11 if condition:
12 return iftrue
13 else:
14 return iffalse
15
16def conditional(variable, checkvalue, truevalue, falsevalue, d):
17 if bb.data.getVar(variable,d,1) == checkvalue:
18 return truevalue
19 else:
20 return falsevalue
21
22def less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
23 if float(bb.data.getVar(variable,d,1)) <= float(checkvalue):
24 return truevalue
25 else:
26 return falsevalue
27
28def version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
29 result = bb.vercmp(bb.data.getVar(variable,d,True), checkvalue)
30 if result <= 0:
31 return truevalue
32 else:
33 return falsevalue
34
35def contains(variable, checkvalues, truevalue, falsevalue, d):
36 val = bb.data.getVar(variable,d,1)
37 if not val:
38 return falsevalue
39 matches = 0
40 if type(checkvalues).__name__ == "str":
41 checkvalues = [checkvalues]
42 for value in checkvalues:
43 if val.find(value) != -1:
44 matches = matches + 1
45 if matches == len(checkvalues):
46 return truevalue
47 return falsevalue
48
49def both_contain(variable1, variable2, checkvalue, d):
50 if bb.data.getVar(variable1,d,1).find(checkvalue) != -1 and bb.data.getVar(variable2,d,1).find(checkvalue) != -1:
51 return checkvalue
52 else:
53 return ""
54
55def prune_suffix(var, suffixes, d):
56 # See if var ends with any of the suffixes listed and
57 # remove it if found
58 for suffix in suffixes:
59 if var.endswith(suffix):
60 return var.replace(suffix, "")
61 return var
62
63def str_filter(f, str, d):
64 from re import match
65 return " ".join(filter(lambda x: match(f, x, 0), str.split()))
66
67def str_filter_out(f, str, d):
68 from re import match
69 return " ".join(filter(lambda x: not match(f, x, 0), str.split()))