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