gdbm: switch to BBCLASSEXTENDED and bump PR
[openembedded.git] / lib / oe / patch.py
blob607ad4432123338c30d0ffba553748f73ed25202
1 class NotFoundError(Exception):
2 def __init__(self, path):
3 self.path = path
5 def __str__(self):
6 return "Error: %s not found." % self.path
8 class CmdError(Exception):
9 def __init__(self, exitstatus, output):
10 self.status = exitstatus
11 self.output = output
13 def __str__(self):
14 return "Command Error: exit status: %d Output:\n%s" % (self.status, self.output)
17 def runcmd(args, dir = None):
18 import commands
20 if dir:
21 olddir = os.path.abspath(os.curdir)
22 if not os.path.exists(dir):
23 raise NotFoundError(dir)
24 os.chdir(dir)
25 # print("cwd: %s -> %s" % (olddir, dir))
27 try:
28 args = [ commands.mkarg(str(arg)) for arg in args ]
29 cmd = " ".join(args)
30 # print("cmd: %s" % cmd)
31 (exitstatus, output) = commands.getstatusoutput(cmd)
32 if exitstatus != 0:
33 raise CmdError(exitstatus >> 8, output)
34 return output
36 finally:
37 if dir:
38 os.chdir(olddir)
40 class PatchError(Exception):
41 def __init__(self, msg):
42 self.msg = msg
44 def __str__(self):
45 return "Patch Error: %s" % self.msg
47 class PatchSet(object):
48 defaults = {
49 "strippath": 1
52 def __init__(self, dir, d):
53 self.dir = dir
54 self.d = d
55 self.patches = []
56 self._current = None
58 def current(self):
59 return self._current
61 def Clean(self):
62 """
63 Clean out the patch set. Generally includes unapplying all
64 patches and wiping out all associated metadata.
65 """
66 raise NotImplementedError()
68 def Import(self, patch, force):
69 if not patch.get("file"):
70 if not patch.get("remote"):
71 raise PatchError("Patch file must be specified in patch import.")
72 else:
73 patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
75 for param in PatchSet.defaults:
76 if not patch.get(param):
77 patch[param] = PatchSet.defaults[param]
79 if patch.get("remote"):
80 patch["file"] = bb.data.expand(bb.fetch.localpath(patch["remote"], self.d), self.d)
82 patch["filemd5"] = bb.utils.md5_file(patch["file"])
84 def Push(self, force):
85 raise NotImplementedError()
87 def Pop(self, force):
88 raise NotImplementedError()
90 def Refresh(self, remote = None, all = None):
91 raise NotImplementedError()
94 class PatchTree(PatchSet):
95 def __init__(self, dir, d):
96 PatchSet.__init__(self, dir, d)
98 def Import(self, patch, force = None):
99 """"""
100 PatchSet.Import(self, patch, force)
102 if self._current is not None:
103 i = self._current + 1
104 else:
105 i = 0
106 self.patches.insert(i, patch)
108 def _applypatch(self, patch, force = False, reverse = False, run = True):
109 shellcmd = ["cat", patch['file'], "|", "patch", "-p", patch['strippath']]
110 if reverse:
111 shellcmd.append('-R')
113 if not run:
114 return "sh" + "-c" + " ".join(shellcmd)
116 if not force:
117 shellcmd.append('--dry-run')
119 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
121 if force:
122 return
124 shellcmd.pop(len(shellcmd) - 1)
125 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
126 return output
128 def Push(self, force = False, all = False, run = True):
129 if all:
130 for i in self.patches:
131 if self._current is not None:
132 self._current = self._current + 1
133 else:
134 self._current = 0
135 self._applypatch(i, force)
136 else:
137 if self._current is not None:
138 self._current = self._current + 1
139 else:
140 self._current = 0
141 return self._applypatch(self.patches[self._current], force)
144 def Pop(self, force = None, all = None):
145 if all:
146 for i in self.patches:
147 self._applypatch(i, force, True)
148 else:
149 self._applypatch(self.patches[self._current], force, True)
151 def Clean(self):
152 """"""
154 class GitApplyTree(PatchTree):
155 def __init__(self, dir, d):
156 PatchTree.__init__(self, dir, d)
158 def _applypatch(self, patch, force = False, reverse = False, run = True):
159 shellcmd = ["git", "--git-dir=.", "apply", "-p%s" % patch['strippath']]
161 if reverse:
162 shellcmd.append('-R')
164 shellcmd.append(patch['file'])
166 if not run:
167 return "sh" + "-c" + " ".join(shellcmd)
169 return runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
172 class QuiltTree(PatchSet):
173 def _runcmd(self, args, run = True):
174 quiltrc = bb.data.getVar('QUILTRCFILE', self.d, 1)
175 if not run:
176 return ["quilt"] + ["--quiltrc"] + [quiltrc] + args
177 runcmd(["quilt"] + ["--quiltrc"] + [quiltrc] + args, self.dir)
179 def _quiltpatchpath(self, file):
180 return os.path.join(self.dir, "patches", os.path.basename(file))
183 def __init__(self, dir, d):
184 PatchSet.__init__(self, dir, d)
185 self.initialized = False
186 p = os.path.join(self.dir, 'patches')
187 if not os.path.exists(p):
188 os.makedirs(p)
190 def Clean(self):
191 try:
192 self._runcmd(["pop", "-a", "-f"])
193 except Exception:
194 pass
195 self.initialized = True
197 def InitFromDir(self):
198 # read series -> self.patches
199 seriespath = os.path.join(self.dir, 'patches', 'series')
200 if not os.path.exists(self.dir):
201 raise Exception("Error: %s does not exist." % self.dir)
202 if os.path.exists(seriespath):
203 series = file(seriespath, 'r')
204 for line in series.readlines():
205 patch = {}
206 parts = line.strip().split()
207 patch["quiltfile"] = self._quiltpatchpath(parts[0])
208 patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"])
209 if len(parts) > 1:
210 patch["strippath"] = parts[1][2:]
211 self.patches.append(patch)
212 series.close()
214 # determine which patches are applied -> self._current
215 try:
216 output = runcmd(["quilt", "applied"], self.dir)
217 except CmdError:
218 import sys
219 if sys.exc_value.output.strip() == "No patches applied":
220 return
221 else:
222 raise sys.exc_value
223 output = [val for val in output.split('\n') if not val.startswith('#')]
224 for patch in self.patches:
225 if os.path.basename(patch["quiltfile"]) == output[-1]:
226 self._current = self.patches.index(patch)
227 self.initialized = True
229 def Import(self, patch, force = None):
230 if not self.initialized:
231 self.InitFromDir()
232 PatchSet.Import(self, patch, force)
233 os.symlink(patch["file"], self._quiltpatchpath(patch["file"]))
234 f = open(os.path.join(self.dir, "patches","series"), "a");
235 f.write(os.path.basename(patch["file"]) + " -p" + patch["strippath"]+"\n")
236 f.close()
237 patch["quiltfile"] = self._quiltpatchpath(patch["file"])
238 patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"])
240 # TODO: determine if the file being imported:
241 # 1) is already imported, and is the same
242 # 2) is already imported, but differs
244 self.patches.insert(self._current or 0, patch)
247 def Push(self, force = False, all = False, run = True):
248 # quilt push [-f]
250 args = ["push"]
251 if force:
252 args.append("-f")
253 if all:
254 args.append("-a")
255 if not run:
256 return self._runcmd(args, run)
258 self._runcmd(args)
260 if self._current is not None:
261 self._current = self._current + 1
262 else:
263 self._current = 0
265 def Pop(self, force = None, all = None):
266 # quilt pop [-f]
267 args = ["pop"]
268 if force:
269 args.append("-f")
270 if all:
271 args.append("-a")
273 self._runcmd(args)
275 if self._current == 0:
276 self._current = None
278 if self._current is not None:
279 self._current = self._current - 1
281 def Refresh(self, **kwargs):
282 if kwargs.get("remote"):
283 patch = self.patches[kwargs["patch"]]
284 if not patch:
285 raise PatchError("No patch found at index %s in patchset." % kwargs["patch"])
286 (type, host, path, user, pswd, parm) = bb.decodeurl(patch["remote"])
287 if type == "file":
288 import shutil
289 if not patch.get("file") and patch.get("remote"):
290 patch["file"] = bb.fetch.localpath(patch["remote"], self.d)
292 shutil.copyfile(patch["quiltfile"], patch["file"])
293 else:
294 raise PatchError("Unable to do a remote refresh of %s, unsupported remote url scheme %s." % (os.path.basename(patch["quiltfile"]), type))
295 else:
296 # quilt refresh
297 args = ["refresh"]
298 if kwargs.get("quiltfile"):
299 args.append(os.path.basename(kwargs["quiltfile"]))
300 elif kwargs.get("patch"):
301 args.append(os.path.basename(self.patches[kwargs["patch"]]["quiltfile"]))
302 self._runcmd(args)
304 class Resolver(object):
305 def __init__(self, patchset):
306 raise NotImplementedError()
308 def Resolve(self):
309 raise NotImplementedError()
311 def Revert(self):
312 raise NotImplementedError()
314 def Finalize(self):
315 raise NotImplementedError()
317 class NOOPResolver(Resolver):
318 def __init__(self, patchset):
319 self.patchset = patchset
321 def Resolve(self):
322 olddir = os.path.abspath(os.curdir)
323 os.chdir(self.patchset.dir)
324 try:
325 self.patchset.Push()
326 except Exception:
327 import sys
328 os.chdir(olddir)
329 raise sys.exc_value
331 # Patch resolver which relies on the user doing all the work involved in the
332 # resolution, with the exception of refreshing the remote copy of the patch
333 # files (the urls).
334 class UserResolver(Resolver):
335 def __init__(self, patchset):
336 self.patchset = patchset
338 # Force a push in the patchset, then drop to a shell for the user to
339 # resolve any rejected hunks
340 def Resolve(self):
342 olddir = os.path.abspath(os.curdir)
343 os.chdir(self.patchset.dir)
344 try:
345 self.patchset.Push(False)
346 except CmdError, v:
347 # Patch application failed
348 patchcmd = self.patchset.Push(True, False, False)
350 t = bb.data.getVar('T', d, 1)
351 if not t:
352 bb.msg.fatal(bb.msg.domain.Build, "T not set")
353 bb.mkdirhier(t)
354 import random
355 rcfile = "%s/bashrc.%s.%s" % (t, str(os.getpid()), random.random())
356 f = open(rcfile, "w")
357 f.write("echo '*** Manual patch resolution mode ***'\n")
358 f.write("echo 'Dropping to a shell, so patch rejects can be fixed manually.'\n")
359 f.write("echo 'Run \"quilt refresh\" when patch is corrected, press CTRL+D to exit.'\n")
360 f.write("echo ''\n")
361 f.write(" ".join(patchcmd) + "\n")
362 f.write("#" + bb.data.getVar('TERMCMDRUN', d, 1))
363 f.close()
364 os.chmod(rcfile, 0775)
366 os.environ['TERMWINDOWTITLE'] = "Bitbake: Please fix patch rejects manually"
367 os.environ['TERMRCFILE'] = rcfile
368 rc = os.system(bb.data.getVar('TERMCMDRUN', d, 1))
369 if os.WIFEXITED(rc) and os.WEXITSTATUS(rc) != 0:
370 bb.msg.fatal(bb.msg.domain.Build, ("Cannot proceed with manual patch resolution - '%s' not found. " \
371 + "Check TERMCMDRUN variable.") % bb.data.getVar('TERMCMDRUN', d, 1))
373 # Construct a new PatchSet after the user's changes, compare the
374 # sets, checking patches for modifications, and doing a remote
375 # refresh on each.
376 oldpatchset = self.patchset
377 self.patchset = oldpatchset.__class__(self.patchset.dir, self.patchset.d)
379 for patch in self.patchset.patches:
380 oldpatch = None
381 for opatch in oldpatchset.patches:
382 if opatch["quiltfile"] == patch["quiltfile"]:
383 oldpatch = opatch
385 if oldpatch:
386 patch["remote"] = oldpatch["remote"]
387 if patch["quiltfile"] == oldpatch["quiltfile"]:
388 if patch["quiltfilemd5"] != oldpatch["quiltfilemd5"]:
389 bb.note("Patch %s has changed, updating remote url %s" % (os.path.basename(patch["quiltfile"]), patch["remote"]))
390 # user change? remote refresh
391 self.patchset.Refresh(remote=True, patch=self.patchset.patches.index(patch))
392 else:
393 # User did not fix the problem. Abort.
394 raise PatchError("Patch application failed, and user did not fix and refresh the patch.")
395 except Exception:
396 os.chdir(olddir)
397 raise
398 os.chdir(olddir)