summaryrefslogtreecommitdiffstats
path: root/meta/classes/patch.bbclass
diff options
context:
space:
mode:
authorJoshua Lock <josh@linux.intel.com>2010-05-05 16:17:07 +0100
committerJoshua Lock <josh@linux.intel.com>2010-05-06 12:53:16 +0100
commite7fa3529f32f4037fe122699e0d7f3789a871834 (patch)
treed5a8d407a5b0ac84a6a69f5aaff76e114d563da6 /meta/classes/patch.bbclass
parentac023d775b651c9b1e28a7a725e72949fe54ad47 (diff)
downloadpoky-e7fa3529f32f4037fe122699e0d7f3789a871834.tar.gz
Update classes to use lib/oe
Adapt modifications from upstream to make Poky classes use lib/oe for the common Python functionality. Signed-off-by: Joshua Lock <josh@linux.intel.com>
Diffstat (limited to 'meta/classes/patch.bbclass')
-rw-r--r--meta/classes/patch.bbclass465
1 files changed, 10 insertions, 455 deletions
diff --git a/meta/classes/patch.bbclass b/meta/classes/patch.bbclass
index 098bb92b89..b8207680ba 100644
--- a/meta/classes/patch.bbclass
+++ b/meta/classes/patch.bbclass
@@ -3,474 +3,26 @@
3# Point to an empty file so any user's custom settings don't break things 3# Point to an empty file so any user's custom settings don't break things
4QUILTRCFILE ?= "${STAGING_BINDIR_NATIVE}/quiltrc" 4QUILTRCFILE ?= "${STAGING_BINDIR_NATIVE}/quiltrc"
5 5
6def patch_init(d):
7 class NotFoundError(Exception):
8 def __init__(self, path):
9 self.path = path
10 def __str__(self):
11 return "Error: %s not found." % self.path
12
13 def md5sum(fname):
14 # when we move to Python 2.5 as minimal supported
15 # we can kill that try/except as hashlib is 2.5+
16 try:
17 import hashlib
18 m = hashlib.md5()
19 except ImportError:
20 import md5
21 m = md5.new()
22
23 try:
24 f = file(fname, 'rb')
25 except IOError:
26 raise NotFoundError(fname)
27
28 while True:
29 d = f.read(8096)
30 if not d:
31 break
32 m.update(d)
33 f.close()
34 return m.hexdigest()
35
36 class CmdError(Exception):
37 def __init__(self, exitstatus, output):
38 self.status = exitstatus
39 self.output = output
40
41 def __str__(self):
42 return "Command Error: exit status: %d Output:\n%s" % (self.status, self.output)
43
44
45 def runcmd(args, dir = None):
46 import commands
47
48 if dir:
49 olddir = os.path.abspath(os.curdir)
50 if not os.path.exists(dir):
51 raise NotFoundError(dir)
52 os.chdir(dir)
53 # print("cwd: %s -> %s" % (olddir, dir))
54
55 try:
56 args = [ commands.mkarg(str(arg)) for arg in args ]
57 cmd = " ".join(args)
58 # print("cmd: %s" % cmd)
59 (exitstatus, output) = commands.getstatusoutput(cmd)
60 if exitstatus != 0:
61 raise CmdError(exitstatus >> 8, output)
62 return output
63
64 finally:
65 if dir:
66 os.chdir(olddir)
67
68 class PatchError(Exception):
69 def __init__(self, msg):
70 self.msg = msg
71
72 def __str__(self):
73 return "Patch Error: %s" % self.msg
74
75 class PatchSet(object):
76 defaults = {
77 "strippath": 1
78 }
79
80 def __init__(self, dir, d):
81 self.dir = dir
82 self.d = d
83 self.patches = []
84 self._current = None
85
86 def current(self):
87 return self._current
88
89 def Clean(self):
90 """
91 Clean out the patch set. Generally includes unapplying all
92 patches and wiping out all associated metadata.
93 """
94 raise NotImplementedError()
95
96 def Import(self, patch, force):
97 if not patch.get("file"):
98 if not patch.get("remote"):
99 raise PatchError("Patch file must be specified in patch import.")
100 else:
101 patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
102
103 for param in PatchSet.defaults:
104 if not patch.get(param):
105 patch[param] = PatchSet.defaults[param]
106
107 if patch.get("remote"):
108 patch["file"] = bb.data.expand(bb.fetch.localpath(patch["remote"], self.d), self.d)
109
110 patch["filemd5"] = md5sum(patch["file"])
111
112 def Push(self, force):
113 raise NotImplementedError()
114
115 def Pop(self, force):
116 raise NotImplementedError()
117
118 def Refresh(self, remote = None, all = None):
119 raise NotImplementedError()
120
121
122 class PatchTree(PatchSet):
123 def __init__(self, dir, d):
124 PatchSet.__init__(self, dir, d)
125
126 def Import(self, patch, force = None):
127 """"""
128 PatchSet.Import(self, patch, force)
129
130 if self._current is not None:
131 i = self._current + 1
132 else:
133 i = 0
134 self.patches.insert(i, patch)
135
136 def _applypatch(self, patch, force = False, reverse = False, run = True):
137 shellcmd = ["cat", patch['file'], "|", "patch", "-p", patch['strippath']]
138 if reverse:
139 shellcmd.append('-R')
140
141 if not run:
142 return "sh" + "-c" + " ".join(shellcmd)
143
144 if not force:
145 shellcmd.append('--dry-run')
146
147 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
148
149 if force:
150 return
151
152 shellcmd.pop(len(shellcmd) - 1)
153 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
154 return output
155
156 def Push(self, force = False, all = False, run = True):
157 if all:
158 for i in self.patches:
159 if self._current is not None:
160 self._current = self._current + 1
161 else:
162 self._current = 0
163 bb.note("applying patch %s" % i)
164 self._applypatch(i, force)
165 else:
166 if self._current is not None:
167 self._current = self._current + 1
168 else:
169 self._current = 0
170 bb.note("applying patch %s" % self.patches[self._current])
171 return self._applypatch(self.patches[self._current], force)
172
173
174 def Pop(self, force = None, all = None):
175 if all:
176 for i in self.patches:
177 self._applypatch(i, force, True)
178 else:
179 self._applypatch(self.patches[self._current], force, True)
180
181 def Clean(self):
182 """"""
183
184 class GitApplyTree(PatchTree):
185 def __init__(self, dir, d):
186 PatchTree.__init__(self, dir, d)
187
188 def _applypatch(self, patch, force = False, reverse = False, run = True):
189 shellcmd = ["git", "--git-dir=.", "apply", "-p%s" % patch['strippath']]
190
191 if reverse:
192 shellcmd.append('-R')
193
194 shellcmd.append(patch['file'])
195
196 if not run:
197 return "sh" + "-c" + " ".join(shellcmd)
198
199 return runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
200
201
202 class QuiltTree(PatchSet):
203 def _runcmd(self, args, run = True):
204 quiltrc = bb.data.getVar('QUILTRCFILE', self.d, 1)
205 if not run:
206 return ["quilt"] + ["--quiltrc"] + [quiltrc] + args
207 runcmd(["quilt"] + ["--quiltrc"] + [quiltrc] + args, self.dir)
208
209 def _quiltpatchpath(self, file):
210 return os.path.join(self.dir, "patches", os.path.basename(file))
211
212
213 def __init__(self, dir, d):
214 PatchSet.__init__(self, dir, d)
215 self.initialized = False
216 p = os.path.join(self.dir, 'patches')
217 if not os.path.exists(p):
218 os.makedirs(p)
219
220 def Clean(self):
221 try:
222 self._runcmd(["pop", "-a", "-f"])
223 except Exception:
224 pass
225 self.initialized = True
226
227 def InitFromDir(self):
228 # read series -> self.patches
229 seriespath = os.path.join(self.dir, 'patches', 'series')
230 if not os.path.exists(self.dir):
231 raise Exception("Error: %s does not exist." % self.dir)
232 if os.path.exists(seriespath):
233 series = file(seriespath, 'r')
234 for line in series.readlines():
235 patch = {}
236 parts = line.strip().split()
237 patch["quiltfile"] = self._quiltpatchpath(parts[0])
238 patch["quiltfilemd5"] = md5sum(patch["quiltfile"])
239 if len(parts) > 1:
240 patch["strippath"] = parts[1][2:]
241 self.patches.append(patch)
242 series.close()
243
244 # determine which patches are applied -> self._current
245 try:
246 output = runcmd(["quilt", "applied"], self.dir)
247 except CmdError:
248 import sys
249 if sys.exc_value.output.strip() == "No patches applied":
250 return
251 else:
252 raise sys.exc_value
253 output = [val for val in output.split('\n') if not val.startswith('#')]
254 for patch in self.patches:
255 if os.path.basename(patch["quiltfile"]) == output[-1]:
256 self._current = self.patches.index(patch)
257 self.initialized = True
258
259 def Import(self, patch, force = None):
260 if not self.initialized:
261 self.InitFromDir()
262 PatchSet.Import(self, patch, force)
263
264 args = ["import", "-p", patch["strippath"]]
265 if force:
266 args.append("-f")
267 args.append("-dn")
268 args.append(patch["file"])
269
270 self._runcmd(args)
271
272 patch["quiltfile"] = self._quiltpatchpath(patch["file"])
273 patch["quiltfilemd5"] = md5sum(patch["quiltfile"])
274
275 # TODO: determine if the file being imported:
276 # 1) is already imported, and is the same
277 # 2) is already imported, but differs
278
279 self.patches.insert(self._current or 0, patch)
280
281
282 def Push(self, force = False, all = False, run = True):
283 # quilt push [-f]
284
285 args = ["push"]
286 if force:
287 args.append("-f")
288 if all:
289 args.append("-a")
290 if not run:
291 return self._runcmd(args, run)
292
293 self._runcmd(args)
294
295 if self._current is not None:
296 self._current = self._current + 1
297 else:
298 self._current = 0
299
300 def Pop(self, force = None, all = None):
301 # quilt pop [-f]
302 args = ["pop"]
303 if force:
304 args.append("-f")
305 if all:
306 args.append("-a")
307
308 self._runcmd(args)
309
310 if self._current == 0:
311 self._current = None
312
313 if self._current is not None:
314 self._current = self._current - 1
315
316 def Refresh(self, **kwargs):
317 if kwargs.get("remote"):
318 patch = self.patches[kwargs["patch"]]
319 if not patch:
320 raise PatchError("No patch found at index %s in patchset." % kwargs["patch"])
321 (type, host, path, user, pswd, parm) = bb.decodeurl(patch["remote"])
322 if type == "file":
323 import shutil
324 if not patch.get("file") and patch.get("remote"):
325 patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
326
327 shutil.copyfile(patch["quiltfile"], patch["file"])
328 else:
329 raise PatchError("Unable to do a remote refresh of %s, unsupported remote url scheme %s." % (os.path.basename(patch["quiltfile"]), type))
330 else:
331 # quilt refresh
332 args = ["refresh"]
333 if kwargs.get("quiltfile"):
334 args.append(os.path.basename(kwargs["quiltfile"]))
335 elif kwargs.get("patch"):
336 args.append(os.path.basename(self.patches[kwargs["patch"]]["quiltfile"]))
337 self._runcmd(args)
338
339 class Resolver(object):
340 def __init__(self, patchset):
341 raise NotImplementedError()
342
343 def Resolve(self):
344 raise NotImplementedError()
345
346 def Revert(self):
347 raise NotImplementedError()
348
349 def Finalize(self):
350 raise NotImplementedError()
351
352 class NOOPResolver(Resolver):
353 def __init__(self, patchset):
354 self.patchset = patchset
355
356 def Resolve(self):
357 olddir = os.path.abspath(os.curdir)
358 os.chdir(self.patchset.dir)
359 try:
360 self.patchset.Push()
361 except Exception:
362 import sys
363 os.chdir(olddir)
364 raise sys.exc_value
365
366 # Patch resolver which relies on the user doing all the work involved in the
367 # resolution, with the exception of refreshing the remote copy of the patch
368 # files (the urls).
369 class UserResolver(Resolver):
370 def __init__(self, patchset):
371 self.patchset = patchset
372
373 # Force a push in the patchset, then drop to a shell for the user to
374 # resolve any rejected hunks
375 def Resolve(self):
376
377 olddir = os.path.abspath(os.curdir)
378 os.chdir(self.patchset.dir)
379 try:
380 self.patchset.Push(False)
381 except CmdError, v:
382 # Patch application failed
383 patchcmd = self.patchset.Push(True, False, False)
384
385 t = bb.data.getVar('T', d, 1)
386 if not t:
387 bb.msg.fatal(bb.msg.domain.Build, "T not set")
388 bb.mkdirhier(t)
389 import random
390 rcfile = "%s/bashrc.%s.%s" % (t, str(os.getpid()), random.random())
391 f = open(rcfile, "w")
392 f.write("echo '*** Manual patch resolution mode ***'\n")
393 f.write("echo 'Dropping to a shell, so patch rejects can be fixed manually.'\n")
394 f.write("echo 'Run \"quilt refresh\" when patch is corrected, press CTRL+D to exit.'\n")
395 f.write("echo ''\n")
396 f.write(" ".join(patchcmd) + "\n")
397 f.write("#" + bb.data.getVar('TERMCMDRUN', d, 1))
398 f.close()
399 os.chmod(rcfile, 0775)
400
401 bb.utils.build_environment(d)
402 os.environ['TERMWINDOWTITLE'] = "Bitbake: Please fix patch rejects manually"
403 os.environ['TERMRCFILE'] = rcfile
404 bb.debug(bb.data.getVar('TERMCMDRUN', d, 1))
405 rc = os.system(bb.data.getVar('TERMCMDRUN', d, 1))
406 if os.WIFEXITED(rc) and os.WEXITSTATUS(rc) != 0:
407 bb.msg.fatal(bb.msg.domain.Build, ("Cannot proceed with manual patch resolution - '%s' not found. " \
408 + "Check TERMCMDRUN variable.") % bb.data.getVar('TERMCMDRUN', d, 1))
409
410 # Construct a new PatchSet after the user's changes, compare the
411 # sets, checking patches for modifications, and doing a remote
412 # refresh on each.
413 oldpatchset = self.patchset
414 self.patchset = oldpatchset.__class__(self.patchset.dir, self.patchset.d)
415 self.patchset.InitFromDir()
416
417 for patch in self.patchset.patches:
418 oldpatch = None
419 for opatch in oldpatchset.patches:
420 if opatch["quiltfile"] == patch["quiltfile"]:
421 oldpatch = opatch
422
423 if oldpatch:
424 patch["remote"] = oldpatch["remote"]
425 if patch["quiltfile"] == oldpatch["quiltfile"]:
426 if patch["quiltfilemd5"] != oldpatch["quiltfilemd5"]:
427 bb.note("Patch %s has changed, updating remote url %s" % (os.path.basename(patch["quiltfile"]), patch["remote"]))
428 # user change? remote refresh
429 self.patchset.Refresh(remote=True, patch=self.patchset.patches.index(patch))
430 else:
431 # User did not fix the problem. Abort.
432 raise PatchError("Patch application failed, and user did not fix and refresh the patch.")
433 except Exception:
434 os.chdir(olddir)
435 raise
436 os.chdir(olddir)
437
438 g = globals()
439 g["PatchSet"] = PatchSet
440 g["PatchTree"] = PatchTree
441 g["QuiltTree"] = QuiltTree
442 g["GitApplyTree"] = GitApplyTree
443 g["Resolver"] = Resolver
444 g["UserResolver"] = UserResolver
445 g["NOOPResolver"] = NOOPResolver
446 g["NotFoundError"] = NotFoundError
447 g["CmdError"] = CmdError
448 g["PatchError"] = PatchError
449
450addtask patch after do_unpack
451do_patch[dirs] = "${WORKDIR}"
452
453PATCHDEPENDENCY = "${PATCHTOOL}-native:do_populate_sysroot" 6PATCHDEPENDENCY = "${PATCHTOOL}-native:do_populate_sysroot"
454do_patch[depends] = "${PATCHDEPENDENCY}"
455 7
456python patch_do_patch() { 8python patch_do_patch() {
457 patch_init(d) 9 import oe.patch
458 10
459 src_uri = (bb.data.getVar('SRC_URI', d, 1) or '').split() 11 src_uri = (bb.data.getVar('SRC_URI', d, 1) or '').split()
460 if not src_uri: 12 if not src_uri:
461 return 13 return
462 14
463 patchsetmap = { 15 patchsetmap = {
464 "patch": PatchTree, 16 "patch": oe.patch.PatchTree,
465 "quilt": QuiltTree, 17 "quilt": oe.patch.QuiltTree,
466 "git": GitApplyTree, 18 "git": oe.patch.GitApplyTree,
467 } 19 }
468 20
469 cls = patchsetmap[bb.data.getVar('PATCHTOOL', d, 1) or 'quilt'] 21 cls = patchsetmap[bb.data.getVar('PATCHTOOL', d, 1) or 'quilt']
470 22
471 resolvermap = { 23 resolvermap = {
472 "noop": NOOPResolver, 24 "noop": oe.patch.NOOPResolver,
473 "user": UserResolver, 25 "user": oe.patch.UserResolver,
474 } 26 }
475 27
476 rcls = resolvermap[bb.data.getVar('PATCHRESOLVE', d, 1) or 'noop'] 28 rcls = resolvermap[bb.data.getVar('PATCHRESOLVE', d, 1) or 'noop']
@@ -558,9 +110,12 @@ python patch_do_patch() {
558 try: 110 try:
559 patchset.Import({"file":unpacked, "remote":url, "strippath": pnum}, True) 111 patchset.Import({"file":unpacked, "remote":url, "strippath": pnum}, True)
560 resolver.Resolve() 112 resolver.Resolve()
561 except: 113 except Exception:
562 import sys 114 import sys
563 raise bb.build.FuncFailed(str(sys.exc_value)) 115 raise bb.build.FuncFailed(str(sys.exc_value))
564} 116}
565 117
118addtask patch after do_unpack
119do_patch[dirs] = "${WORKDIR}"
120do_patch[depends] = "${PATCHDEPENDENCY}"
566EXPORT_FUNCTIONS do_patch 121EXPORT_FUNCTIONS do_patch