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