bitbake.conf: use rootfs/${PN} for IMAGE_ROOTFS
[vuplus_openembedded] / classes / patch.bbclass
1 # Copyright (C) 2006  OpenedHand LTD
2
3 # Point to an empty file so any user's custom settings don't break things
4 QUILTRCFILE ?= "${STAGING_BINDIR_NATIVE}/quiltrc"
5
6 def patch_init(d):
7         import os, sys
8
9         class NotFoundError(Exception):
10                 def __init__(self, path):
11                         self.path = path
12                 def __str__(self):
13                         return "Error: %s not found." % self.path
14
15         def md5sum(fname):
16                 import md5, sys
17
18                 try:
19                         f = file(fname, 'rb')
20                 except IOError:
21                         raise NotFoundError(fname)
22
23                 m = md5.new()
24                 while True:
25                         d = f.read(8096)
26                         if not d:
27                                 break
28                         m.update(d)
29                 f.close()
30                 return m.hexdigest()
31
32         class CmdError(Exception):
33                 def __init__(self, exitstatus, output):
34                         self.status = exitstatus
35                         self.output = output
36
37                 def __str__(self):
38                         return "Command Error: exit status: %d  Output:\n%s" % (self.status, self.output)
39
40
41         def runcmd(args, dir = None):
42                 import commands
43
44                 if dir:
45                         olddir = os.path.abspath(os.curdir)
46                         if not os.path.exists(dir):
47                                 raise NotFoundError(dir)
48                         os.chdir(dir)
49                         # print("cwd: %s -> %s" % (olddir, dir))
50
51                 try:
52                         args = [ commands.mkarg(str(arg)) for arg in args ]
53                         cmd = " ".join(args)
54                         # print("cmd: %s" % cmd)
55                         (exitstatus, output) = commands.getstatusoutput(cmd)
56                         if exitstatus != 0:
57                                 raise CmdError(exitstatus >> 8, output)
58                         return output
59
60                 finally:
61                         if dir:
62                                 os.chdir(olddir)
63
64         class PatchError(Exception):
65                 def __init__(self, msg):
66                         self.msg = msg
67
68                 def __str__(self):
69                         return "Patch Error: %s" % self.msg
70
71         import bb, bb.data, bb.fetch
72
73         class PatchSet(object):
74                 defaults = {
75                         "strippath": 1
76                 }
77
78                 def __init__(self, dir, d):
79                         self.dir = dir
80                         self.d = d
81                         self.patches = []
82                         self._current = None
83
84                 def current(self):
85                         return self._current
86
87                 def Clean(self):
88                         """
89                         Clean out the patch set.  Generally includes unapplying all
90                         patches and wiping out all associated metadata.
91                         """
92                         raise NotImplementedError()
93
94                 def Import(self, patch, force):
95                         if not patch.get("file"):
96                                 if not patch.get("remote"):
97                                         raise PatchError("Patch file must be specified in patch import.")
98                                 else:
99                                         patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
100
101                         for param in PatchSet.defaults:
102                                 if not patch.get(param):
103                                         patch[param] = PatchSet.defaults[param]
104
105                         if patch.get("remote"):
106                                 patch["file"] = bb.data.expand(bb.fetch.localpath(patch["remote"], self.d), self.d)
107
108                         patch["filemd5"] = md5sum(patch["file"])
109
110                 def Push(self, force):
111                         raise NotImplementedError()
112
113                 def Pop(self, force):
114                         raise NotImplementedError()
115
116                 def Refresh(self, remote = None, all = None):
117                         raise NotImplementedError()
118
119
120         class PatchTree(PatchSet):
121                 def __init__(self, dir, d):
122                         PatchSet.__init__(self, dir, d)
123
124                 def Import(self, patch, force = None):
125                         """"""
126                         PatchSet.Import(self, patch, force)
127
128                         if self._current is not None:
129                                 i = self._current + 1
130                         else:
131                                 i = 0
132                         self.patches.insert(i, patch)
133
134                 def _applypatch(self, patch, force = False, reverse = False, run = True):
135                         shellcmd = ["cat", patch['file'], "|", "patch", "-p", patch['strippath']]
136                         if reverse:
137                                 shellcmd.append('-R')
138
139                         if not run:
140                                 return "sh" + "-c" + " ".join(shellcmd)
141
142                         if not force:
143                                 shellcmd.append('--dry-run')
144
145                         output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
146
147                         if force:
148                                 return
149
150                         shellcmd.pop(len(shellcmd) - 1)
151                         output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
152                         return output
153
154                 def Push(self, force = False, all = False, run = True):
155                         bb.note("self._current is %s" % self._current)
156                         bb.note("patches is %s" % self.patches)
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 QuiltTree(PatchSet):
185                 def _runcmd(self, args, run = True):
186                         quiltrc = bb.data.getVar('QUILTRCFILE', self.d, 1)
187                         if not run:
188                                 return ["quilt"] + ["--quiltrc"] + [quiltrc] + args
189                         runcmd(["quilt"] + ["--quiltrc"] + [quiltrc] + args, self.dir)
190
191                 def _quiltpatchpath(self, file):
192                         return os.path.join(self.dir, "patches", os.path.basename(file))
193
194
195                 def __init__(self, dir, d):
196                         PatchSet.__init__(self, dir, d)
197                         self.initialized = False
198                         p = os.path.join(self.dir, 'patches')
199                         if not os.path.exists(p):
200                                 os.makedirs(p)
201
202                 def Clean(self):
203                         try:
204                                 self._runcmd(["pop", "-a", "-f"])
205                         except Exception:
206                                 pass
207                         self.initialized = True
208
209                 def InitFromDir(self):
210                         # read series -> self.patches
211                         seriespath = os.path.join(self.dir, 'patches', 'series')
212                         if not os.path.exists(self.dir):
213                                 raise Exception("Error: %s does not exist." % self.dir)
214                         if os.path.exists(seriespath):
215                                 series = file(seriespath, 'r')
216                                 for line in series.readlines():
217                                         patch = {}
218                                         parts = line.strip().split()
219                                         patch["quiltfile"] = self._quiltpatchpath(parts[0])
220                                         patch["quiltfilemd5"] = md5sum(patch["quiltfile"])
221                                         if len(parts) > 1:
222                                                 patch["strippath"] = parts[1][2:]
223                                         self.patches.append(patch)
224                                 series.close()
225
226                                 # determine which patches are applied -> self._current
227                                 try:
228                                         output = runcmd(["quilt", "applied"], self.dir)
229                                 except CmdError:
230                                         if sys.exc_value.output.strip() == "No patches applied":
231                                                 return
232                                         else:
233                                                 raise sys.exc_value
234                                 output = [val for val in output.split('\n') if not val.startswith('#')]
235                                 for patch in self.patches:
236                                         if os.path.basename(patch["quiltfile"]) == output[-1]:
237                                                 self._current = self.patches.index(patch)
238                         self.initialized = True
239
240                 def Import(self, patch, force = None):
241                         if not self.initialized:
242                                 self.InitFromDir()
243                         PatchSet.Import(self, patch, force)
244
245                         args = ["import", "-p", patch["strippath"]]
246                         if force:
247                                 args.append("-f")
248                                 args.append("-dn")
249                         args.append(patch["file"])
250
251                         self._runcmd(args)
252
253                         patch["quiltfile"] = self._quiltpatchpath(patch["file"])
254                         patch["quiltfilemd5"] = md5sum(patch["quiltfile"])
255
256                         # TODO: determine if the file being imported:
257                         #          1) is already imported, and is the same
258                         #          2) is already imported, but differs
259
260                         self.patches.insert(self._current or 0, patch)
261
262
263                 def Push(self, force = False, all = False, run = True):
264                         # quilt push [-f]
265
266                         args = ["push"]
267                         if force:
268                                 args.append("-f")
269                         if all:
270                                 args.append("-a")
271                         if not run:
272                                 return self._runcmd(args, run)
273
274                         self._runcmd(args)
275
276                         if self._current is not None:
277                                 self._current = self._current + 1
278                         else:
279                                 self._current = 0
280
281                 def Pop(self, force = None, all = None):
282                         # quilt pop [-f]
283                         args = ["pop"]
284                         if force:
285                                 args.append("-f")
286                         if all:
287                                 args.append("-a")
288
289                         self._runcmd(args)
290
291                         if self._current == 0:
292                                 self._current = None
293
294                         if self._current is not None:
295                                 self._current = self._current - 1
296
297                 def Refresh(self, **kwargs):
298                         if kwargs.get("remote"):
299                                 patch = self.patches[kwargs["patch"]]
300                                 if not patch:
301                                         raise PatchError("No patch found at index %s in patchset." % kwargs["patch"])
302                                 (type, host, path, user, pswd, parm) = bb.decodeurl(patch["remote"])
303                                 if type == "file":
304                                         import shutil
305                                         if not patch.get("file") and patch.get("remote"):
306                                                 patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
307
308                                         shutil.copyfile(patch["quiltfile"], patch["file"])
309                                 else:
310                                         raise PatchError("Unable to do a remote refresh of %s, unsupported remote url scheme %s." % (os.path.basename(patch["quiltfile"]), type))
311                         else:
312                                 # quilt refresh
313                                 args = ["refresh"]
314                                 if kwargs.get("quiltfile"):
315                                         args.append(os.path.basename(kwargs["quiltfile"]))
316                                 elif kwargs.get("patch"):
317                                         args.append(os.path.basename(self.patches[kwargs["patch"]]["quiltfile"]))
318                                 self._runcmd(args)
319
320         class Resolver(object):
321                 def __init__(self, patchset):
322                         raise NotImplementedError()
323
324                 def Resolve(self):
325                         raise NotImplementedError()
326
327                 def Revert(self):
328                         raise NotImplementedError()
329
330                 def Finalize(self):
331                         raise NotImplementedError()
332
333         class NOOPResolver(Resolver):
334                 def __init__(self, patchset):
335                         self.patchset = patchset
336
337                 def Resolve(self):
338                         olddir = os.path.abspath(os.curdir)
339                         os.chdir(self.patchset.dir)
340                         try:
341                                 self.patchset.Push()
342                         except Exception:
343                                 os.chdir(olddir)
344                                 raise sys.exc_value
345
346         # Patch resolver which relies on the user doing all the work involved in the
347         # resolution, with the exception of refreshing the remote copy of the patch
348         # files (the urls).
349         class UserResolver(Resolver):
350                 def __init__(self, patchset):
351                         self.patchset = patchset
352
353                 # Force a push in the patchset, then drop to a shell for the user to
354                 # resolve any rejected hunks
355                 def Resolve(self):
356
357                         olddir = os.path.abspath(os.curdir)
358                         os.chdir(self.patchset.dir)
359                         try:
360                                 self.patchset.Push(False)
361                         except CmdError, v:
362                                 # Patch application failed
363                                 patchcmd = self.patchset.Push(True, False, False)
364  
365                                 t = bb.data.getVar('T', d, 1)
366                                 if not t:
367                                         bb.msg.fatal(bb.msg.domain.Build, "T not set")
368                                 bb.mkdirhier(t)
369                                 import random
370                                 rcfile = "%s/bashrc.%s.%s" % (t, str(os.getpid()), random.random())
371                                 f = open(rcfile, "w")
372                                 f.write("echo '*** Manual patch resolution mode ***'\n")
373                                 f.write("echo 'Dropping to a shell, so patch rejects can be fixed manually.'\n")
374                                 f.write("echo 'Run \"quilt refresh\" when patch is corrected, press CTRL+D to exit.'\n")
375                                 f.write("echo ''\n")
376                                 f.write(" ".join(patchcmd) + "\n")
377                                 f.write("#" + bb.data.getVar('TERMCMDRUN', d, 1))
378                                 f.close()
379                                 os.chmod(rcfile, 0775)
380  
381                                 os.environ['TERMWINDOWTITLE'] = "Bitbake: Please fix patch rejects manually"
382                                 os.environ['TERMRCFILE'] = rcfile
383                                 rc = os.system(bb.data.getVar('TERMCMDRUN', d, 1))
384                                 if os.WIFEXITED(rc) and os.WEXITSTATUS(rc) != 0:
385                                         bb.msg.fatal(bb.msg.domain.Build, ("Cannot proceed with manual patch resolution - '%s' not found. " \
386                                             + "Check TERMCMDRUN variable.") % bb.data.getVar('TERMCMDRUN', d, 1))
387
388                                 # Construct a new PatchSet after the user's changes, compare the
389                                 # sets, checking patches for modifications, and doing a remote
390                                 # refresh on each.
391                                 oldpatchset = self.patchset
392                                 self.patchset = oldpatchset.__class__(self.patchset.dir, self.patchset.d)
393
394                                 for patch in self.patchset.patches:
395                                         oldpatch = None
396                                         for opatch in oldpatchset.patches:
397                                                 if opatch["quiltfile"] == patch["quiltfile"]:
398                                                         oldpatch = opatch
399
400                                         if oldpatch:
401                                                 patch["remote"] = oldpatch["remote"]
402                                                 if patch["quiltfile"] == oldpatch["quiltfile"]:
403                                                         if patch["quiltfilemd5"] != oldpatch["quiltfilemd5"]:
404                                                                 bb.note("Patch %s has changed, updating remote url %s" % (os.path.basename(patch["quiltfile"]), patch["remote"]))
405                                                                 # user change?  remote refresh
406                                                                 self.patchset.Refresh(remote=True, patch=self.patchset.patches.index(patch))
407                                                         else:
408                                                                 # User did not fix the problem.  Abort.
409                                                                 raise PatchError("Patch application failed, and user did not fix and refresh the patch.")
410                         except Exception:
411                                 os.chdir(olddir)
412                                 raise
413                         os.chdir(olddir)
414
415         g = globals()
416         g["PatchSet"] = PatchSet
417         g["PatchTree"] = PatchTree
418         g["QuiltTree"] = QuiltTree
419         g["Resolver"] = Resolver
420         g["UserResolver"] = UserResolver
421         g["NOOPResolver"] = NOOPResolver
422         g["NotFoundError"] = NotFoundError
423         g["CmdError"] = CmdError
424
425 addtask patch after do_unpack
426 do_patch[dirs] = "${WORKDIR}"
427
428 PATCHDEPENDENCY = "${PATCHTOOL}-native:do_populate_staging"
429 do_patch[depends] = "${PATCHDEPENDENCY}"
430
431 python patch_do_patch() {
432         import re
433         import bb.fetch
434
435         patch_init(d)
436
437         src_uri = (bb.data.getVar('SRC_URI', d, 1) or '').split()
438         if not src_uri:
439                 return
440
441         patchsetmap = {
442                 "patch": PatchTree,
443                 "quilt": QuiltTree,
444         }
445
446         cls = patchsetmap[bb.data.getVar('PATCHTOOL', d, 1) or 'quilt']
447
448         resolvermap = {
449                 "noop": NOOPResolver,
450                 "user": UserResolver,
451         }
452
453         rcls = resolvermap[bb.data.getVar('PATCHRESOLVE', d, 1) or 'user']
454
455         s = bb.data.getVar('S', d, 1)
456
457         path = os.getenv('PATH')
458         os.putenv('PATH', bb.data.getVar('PATH', d, 1))
459         patchset = cls(s, d)
460         patchset.Clean()
461
462         resolver = rcls(patchset)
463
464         workdir = bb.data.getVar('WORKDIR', d, 1)
465         for url in src_uri:
466                 (type, host, path, user, pswd, parm) = bb.decodeurl(url)
467                 if not "patch" in parm:
468                         continue
469
470                 bb.fetch.init([url],d)
471                 url = bb.encodeurl((type, host, path, user, pswd, []))
472                 local = os.path.join('/', bb.fetch.localpath(url, d))
473
474                 # did it need to be unpacked?
475                 dots = os.path.basename(local).split(".")
476                 if dots[-1] in ['gz', 'bz2', 'Z']:
477                         unpacked = os.path.join(bb.data.getVar('WORKDIR', d),'.'.join(dots[0:-1]))
478                 else:
479                         unpacked = local
480                 unpacked = bb.data.expand(unpacked, d)
481
482                 if "pnum" in parm:
483                         pnum = parm["pnum"]
484                 else:
485                         pnum = "1"
486
487                 if "pname" in parm:
488                         pname = parm["pname"]
489                 else:
490                         pname = os.path.basename(unpacked)
491
492                 if "mindate" in parm or "maxdate" in parm:
493                         pn = bb.data.getVar('PN', d, 1)
494                         srcdate = bb.data.getVar('SRCDATE_%s' % pn, d, 1)
495                         if not srcdate:
496                                 srcdate = bb.data.getVar('SRCDATE', d, 1)
497
498                         if srcdate == "now":
499                                 srcdate = bb.data.getVar('DATE', d, 1)
500
501                         if "maxdate" in parm and parm["maxdate"] < srcdate:
502                                 bb.note("Patch '%s' is outdated" % pname)
503                                 continue
504
505                         if "mindate" in parm and parm["mindate"] > srcdate:
506                                 bb.note("Patch '%s' is predated" % pname)
507                                 continue
508
509
510                 if "minrev" in parm:
511                         srcrev = bb.data.getVar('SRCREV', d, 1)
512                         if srcrev and srcrev < parm["minrev"]:
513                                 bb.note("Patch '%s' applies to later revisions" % pname)
514                                 continue
515
516                 if "maxrev" in parm:
517                         srcrev = bb.data.getVar('SRCREV', d, 1)         
518                         if srcrev and srcrev > parm["maxrev"]:
519                                 bb.note("Patch '%s' applies to earlier revisions" % pname)
520                                 continue
521
522                 bb.note("Applying patch '%s' (%s)" % (pname, unpacked))
523                 try:
524                         patchset.Import({"file":unpacked, "remote":url, "strippath": pnum}, True)
525                 except:
526                         import sys
527                         raise bb.build.FuncFailed(str(sys.exc_value))
528                 resolver.Resolve()
529 }
530
531 EXPORT_FUNCTIONS do_patch