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