diff options
Diffstat (limited to 'meta/lib/oe/patch.py')
| -rw-r--r-- | meta/lib/oe/patch.py | 407 |
1 files changed, 407 insertions, 0 deletions
diff --git a/meta/lib/oe/patch.py b/meta/lib/oe/patch.py new file mode 100644 index 0000000000..54c12bac2a --- /dev/null +++ b/meta/lib/oe/patch.py | |||
| @@ -0,0 +1,407 @@ | |||
| 1 | class NotFoundError(Exception): | ||
| 2 | def __init__(self, path): | ||
| 3 | self.path = path | ||
| 4 | |||
| 5 | def __str__(self): | ||
| 6 | return "Error: %s not found." % self.path | ||
| 7 | |||
| 8 | class CmdError(Exception): | ||
| 9 | def __init__(self, exitstatus, output): | ||
| 10 | self.status = exitstatus | ||
| 11 | self.output = output | ||
| 12 | |||
| 13 | def __str__(self): | ||
| 14 | return "Command Error: exit status: %d Output:\n%s" % (self.status, self.output) | ||
| 15 | |||
| 16 | |||
| 17 | def runcmd(args, dir = None): | ||
| 18 | import commands | ||
| 19 | |||
| 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)) | ||
| 26 | |||
| 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 | ||
| 35 | |||
| 36 | finally: | ||
| 37 | if dir: | ||
| 38 | os.chdir(olddir) | ||
| 39 | |||
| 40 | class PatchError(Exception): | ||
| 41 | def __init__(self, msg): | ||
| 42 | self.msg = msg | ||
| 43 | |||
| 44 | def __str__(self): | ||
| 45 | return "Patch Error: %s" % self.msg | ||
| 46 | |||
| 47 | class PatchSet(object): | ||
| 48 | defaults = { | ||
| 49 | "strippath": 1 | ||
| 50 | } | ||
| 51 | |||
| 52 | def __init__(self, dir, d): | ||
| 53 | self.dir = dir | ||
| 54 | self.d = d | ||
| 55 | self.patches = [] | ||
| 56 | self._current = None | ||
| 57 | |||
| 58 | def current(self): | ||
| 59 | return self._current | ||
| 60 | |||
| 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() | ||
| 67 | |||
| 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) | ||
| 74 | |||
| 75 | for param in PatchSet.defaults: | ||
| 76 | if not patch.get(param): | ||
| 77 | patch[param] = PatchSet.defaults[param] | ||
| 78 | |||
| 79 | if patch.get("remote"): | ||
| 80 | patch["file"] = bb.data.expand(bb.fetch.localpath(patch["remote"], self.d), self.d) | ||
| 81 | |||
| 82 | patch["filemd5"] = bb.utils.md5_file(patch["file"]) | ||
| 83 | |||
| 84 | def Push(self, force): | ||
| 85 | raise NotImplementedError() | ||
| 86 | |||
| 87 | def Pop(self, force): | ||
| 88 | raise NotImplementedError() | ||
| 89 | |||
| 90 | def Refresh(self, remote = None, all = None): | ||
| 91 | raise NotImplementedError() | ||
| 92 | |||
| 93 | |||
| 94 | class PatchTree(PatchSet): | ||
| 95 | def __init__(self, dir, d): | ||
| 96 | PatchSet.__init__(self, dir, d) | ||
| 97 | |||
| 98 | def Import(self, patch, force = None): | ||
| 99 | """""" | ||
| 100 | PatchSet.Import(self, patch, force) | ||
| 101 | |||
| 102 | if self._current is not None: | ||
| 103 | i = self._current + 1 | ||
| 104 | else: | ||
| 105 | i = 0 | ||
| 106 | self.patches.insert(i, patch) | ||
| 107 | |||
| 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') | ||
| 112 | |||
| 113 | if not run: | ||
| 114 | return "sh" + "-c" + " ".join(shellcmd) | ||
| 115 | |||
| 116 | if not force: | ||
| 117 | shellcmd.append('--dry-run') | ||
| 118 | |||
| 119 | output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) | ||
| 120 | |||
| 121 | if force: | ||
| 122 | return | ||
| 123 | |||
| 124 | shellcmd.pop(len(shellcmd) - 1) | ||
| 125 | output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) | ||
| 126 | return output | ||
| 127 | |||
| 128 | def Push(self, force = False, all = False, run = True): | ||
| 129 | bb.note("self._current is %s" % self._current) | ||
| 130 | bb.note("patches is %s" % self.patches) | ||
| 131 | if all: | ||
| 132 | for i in self.patches: | ||
| 133 | if self._current is not None: | ||
| 134 | self._current = self._current + 1 | ||
| 135 | else: | ||
| 136 | self._current = 0 | ||
| 137 | bb.note("applying patch %s" % i) | ||
| 138 | self._applypatch(i, force) | ||
| 139 | else: | ||
| 140 | if self._current is not None: | ||
| 141 | self._current = self._current + 1 | ||
| 142 | else: | ||
| 143 | self._current = 0 | ||
| 144 | bb.note("applying patch %s" % self.patches[self._current]) | ||
| 145 | return self._applypatch(self.patches[self._current], force) | ||
| 146 | |||
| 147 | |||
| 148 | def Pop(self, force = None, all = None): | ||
| 149 | if all: | ||
| 150 | for i in self.patches: | ||
| 151 | self._applypatch(i, force, True) | ||
| 152 | else: | ||
| 153 | self._applypatch(self.patches[self._current], force, True) | ||
| 154 | |||
| 155 | def Clean(self): | ||
| 156 | """""" | ||
| 157 | |||
| 158 | class GitApplyTree(PatchTree): | ||
| 159 | def __init__(self, dir, d): | ||
| 160 | PatchTree.__init__(self, dir, d) | ||
| 161 | |||
| 162 | def _applypatch(self, patch, force = False, reverse = False, run = True): | ||
| 163 | shellcmd = ["git", "--git-dir=.", "apply", "-p%s" % patch['strippath']] | ||
| 164 | |||
| 165 | if reverse: | ||
| 166 | shellcmd.append('-R') | ||
| 167 | |||
| 168 | shellcmd.append(patch['file']) | ||
| 169 | |||
| 170 | if not run: | ||
| 171 | return "sh" + "-c" + " ".join(shellcmd) | ||
| 172 | |||
| 173 | return runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) | ||
| 174 | |||
| 175 | |||
| 176 | class QuiltTree(PatchSet): | ||
| 177 | def _runcmd(self, args, run = True): | ||
| 178 | quiltrc = bb.data.getVar('QUILTRCFILE', self.d, 1) | ||
| 179 | if not run: | ||
| 180 | return ["quilt"] + ["--quiltrc"] + [quiltrc] + args | ||
| 181 | runcmd(["quilt"] + ["--quiltrc"] + [quiltrc] + args, self.dir) | ||
| 182 | |||
| 183 | def _quiltpatchpath(self, file): | ||
| 184 | return os.path.join(self.dir, "patches", os.path.basename(file)) | ||
| 185 | |||
| 186 | |||
| 187 | def __init__(self, dir, d): | ||
| 188 | PatchSet.__init__(self, dir, d) | ||
| 189 | self.initialized = False | ||
| 190 | p = os.path.join(self.dir, 'patches') | ||
| 191 | if not os.path.exists(p): | ||
| 192 | os.makedirs(p) | ||
| 193 | |||
| 194 | def Clean(self): | ||
| 195 | try: | ||
| 196 | self._runcmd(["pop", "-a", "-f"]) | ||
| 197 | except Exception: | ||
| 198 | pass | ||
| 199 | self.initialized = True | ||
| 200 | |||
| 201 | def InitFromDir(self): | ||
| 202 | # read series -> self.patches | ||
| 203 | seriespath = os.path.join(self.dir, 'patches', 'series') | ||
| 204 | if not os.path.exists(self.dir): | ||
| 205 | raise Exception("Error: %s does not exist." % self.dir) | ||
| 206 | if os.path.exists(seriespath): | ||
| 207 | series = file(seriespath, 'r') | ||
| 208 | for line in series.readlines(): | ||
| 209 | patch = {} | ||
| 210 | parts = line.strip().split() | ||
| 211 | patch["quiltfile"] = self._quiltpatchpath(parts[0]) | ||
| 212 | patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"]) | ||
| 213 | if len(parts) > 1: | ||
| 214 | patch["strippath"] = parts[1][2:] | ||
| 215 | self.patches.append(patch) | ||
| 216 | series.close() | ||
| 217 | |||
| 218 | # determine which patches are applied -> self._current | ||
| 219 | try: | ||
| 220 | output = runcmd(["quilt", "applied"], self.dir) | ||
| 221 | except CmdError: | ||
| 222 | import sys | ||
| 223 | if sys.exc_value.output.strip() == "No patches applied": | ||
| 224 | return | ||
| 225 | else: | ||
| 226 | raise sys.exc_value | ||
| 227 | output = [val for val in output.split('\n') if not val.startswith('#')] | ||
| 228 | for patch in self.patches: | ||
| 229 | if os.path.basename(patch["quiltfile"]) == output[-1]: | ||
| 230 | self._current = self.patches.index(patch) | ||
| 231 | self.initialized = True | ||
| 232 | |||
| 233 | def Import(self, patch, force = None): | ||
| 234 | if not self.initialized: | ||
| 235 | self.InitFromDir() | ||
| 236 | PatchSet.Import(self, patch, force) | ||
| 237 | |||
| 238 | args = ["import", "-p", patch["strippath"]] | ||
| 239 | if force: | ||
| 240 | args.append("-f") | ||
| 241 | args.append("-dn") | ||
| 242 | args.append(patch["file"]) | ||
| 243 | |||
| 244 | self._runcmd(args) | ||
| 245 | |||
| 246 | patch["quiltfile"] = self._quiltpatchpath(patch["file"]) | ||
| 247 | patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"]) | ||
| 248 | |||
| 249 | # TODO: determine if the file being imported: | ||
| 250 | # 1) is already imported, and is the same | ||
| 251 | # 2) is already imported, but differs | ||
| 252 | |||
| 253 | self.patches.insert(self._current or 0, patch) | ||
| 254 | |||
| 255 | |||
| 256 | def Push(self, force = False, all = False, run = True): | ||
| 257 | # quilt push [-f] | ||
| 258 | |||
| 259 | args = ["push"] | ||
| 260 | if force: | ||
| 261 | args.append("-f") | ||
| 262 | if all: | ||
| 263 | args.append("-a") | ||
| 264 | if not run: | ||
| 265 | return self._runcmd(args, run) | ||
| 266 | |||
| 267 | self._runcmd(args) | ||
| 268 | |||
| 269 | if self._current is not None: | ||
| 270 | self._current = self._current + 1 | ||
| 271 | else: | ||
| 272 | self._current = 0 | ||
| 273 | |||
| 274 | def Pop(self, force = None, all = None): | ||
| 275 | # quilt pop [-f] | ||
| 276 | args = ["pop"] | ||
| 277 | if force: | ||
| 278 | args.append("-f") | ||
| 279 | if all: | ||
| 280 | args.append("-a") | ||
| 281 | |||
| 282 | self._runcmd(args) | ||
| 283 | |||
| 284 | if self._current == 0: | ||
| 285 | self._current = None | ||
| 286 | |||
| 287 | if self._current is not None: | ||
| 288 | self._current = self._current - 1 | ||
| 289 | |||
| 290 | def Refresh(self, **kwargs): | ||
| 291 | if kwargs.get("remote"): | ||
| 292 | patch = self.patches[kwargs["patch"]] | ||
| 293 | if not patch: | ||
| 294 | raise PatchError("No patch found at index %s in patchset." % kwargs["patch"]) | ||
| 295 | (type, host, path, user, pswd, parm) = bb.decodeurl(patch["remote"]) | ||
| 296 | if type == "file": | ||
| 297 | import shutil | ||
| 298 | if not patch.get("file") and patch.get("remote"): | ||
| 299 | patch["file"] = bb.fetch.localpath(patch["remote"], self.d) | ||
| 300 | |||
| 301 | shutil.copyfile(patch["quiltfile"], patch["file"]) | ||
| 302 | else: | ||
| 303 | raise PatchError("Unable to do a remote refresh of %s, unsupported remote url scheme %s." % (os.path.basename(patch["quiltfile"]), type)) | ||
| 304 | else: | ||
| 305 | # quilt refresh | ||
| 306 | args = ["refresh"] | ||
| 307 | if kwargs.get("quiltfile"): | ||
| 308 | args.append(os.path.basename(kwargs["quiltfile"])) | ||
| 309 | elif kwargs.get("patch"): | ||
| 310 | args.append(os.path.basename(self.patches[kwargs["patch"]]["quiltfile"])) | ||
| 311 | self._runcmd(args) | ||
| 312 | |||
| 313 | class Resolver(object): | ||
| 314 | def __init__(self, patchset): | ||
| 315 | raise NotImplementedError() | ||
| 316 | |||
| 317 | def Resolve(self): | ||
| 318 | raise NotImplementedError() | ||
| 319 | |||
| 320 | def Revert(self): | ||
| 321 | raise NotImplementedError() | ||
| 322 | |||
| 323 | def Finalize(self): | ||
| 324 | raise NotImplementedError() | ||
| 325 | |||
| 326 | class NOOPResolver(Resolver): | ||
| 327 | def __init__(self, patchset): | ||
| 328 | self.patchset = patchset | ||
| 329 | |||
| 330 | def Resolve(self): | ||
| 331 | olddir = os.path.abspath(os.curdir) | ||
| 332 | os.chdir(self.patchset.dir) | ||
| 333 | try: | ||
| 334 | self.patchset.Push() | ||
| 335 | except Exception: | ||
| 336 | import sys | ||
| 337 | os.chdir(olddir) | ||
| 338 | raise sys.exc_value | ||
| 339 | |||
| 340 | # Patch resolver which relies on the user doing all the work involved in the | ||
| 341 | # resolution, with the exception of refreshing the remote copy of the patch | ||
| 342 | # files (the urls). | ||
| 343 | class UserResolver(Resolver): | ||
| 344 | def __init__(self, patchset): | ||
| 345 | self.patchset = patchset | ||
| 346 | |||
| 347 | # Force a push in the patchset, then drop to a shell for the user to | ||
| 348 | # resolve any rejected hunks | ||
| 349 | def Resolve(self): | ||
| 350 | |||
| 351 | olddir = os.path.abspath(os.curdir) | ||
| 352 | os.chdir(self.patchset.dir) | ||
| 353 | try: | ||
| 354 | self.patchset.Push(False) | ||
| 355 | except CmdError, v: | ||
| 356 | # Patch application failed | ||
| 357 | patchcmd = self.patchset.Push(True, False, False) | ||
| 358 | |||
| 359 | t = bb.data.getVar('T', d, 1) | ||
| 360 | if not t: | ||
| 361 | bb.msg.fatal(bb.msg.domain.Build, "T not set") | ||
| 362 | bb.mkdirhier(t) | ||
| 363 | import random | ||
| 364 | rcfile = "%s/bashrc.%s.%s" % (t, str(os.getpid()), random.random()) | ||
| 365 | f = open(rcfile, "w") | ||
| 366 | f.write("echo '*** Manual patch resolution mode ***'\n") | ||
| 367 | f.write("echo 'Dropping to a shell, so patch rejects can be fixed manually.'\n") | ||
| 368 | f.write("echo 'Run \"quilt refresh\" when patch is corrected, press CTRL+D to exit.'\n") | ||
| 369 | f.write("echo ''\n") | ||
| 370 | f.write(" ".join(patchcmd) + "\n") | ||
| 371 | f.write("#" + bb.data.getVar('TERMCMDRUN', d, 1)) | ||
| 372 | f.close() | ||
| 373 | os.chmod(rcfile, 0775) | ||
| 374 | |||
| 375 | os.environ['TERMWINDOWTITLE'] = "Bitbake: Please fix patch rejects manually" | ||
| 376 | os.environ['TERMRCFILE'] = rcfile | ||
| 377 | rc = os.system(bb.data.getVar('TERMCMDRUN', d, 1)) | ||
| 378 | if os.WIFEXITED(rc) and os.WEXITSTATUS(rc) != 0: | ||
| 379 | bb.msg.fatal(bb.msg.domain.Build, ("Cannot proceed with manual patch resolution - '%s' not found. " \ | ||
| 380 | + "Check TERMCMDRUN variable.") % bb.data.getVar('TERMCMDRUN', d, 1)) | ||
| 381 | |||
| 382 | # Construct a new PatchSet after the user's changes, compare the | ||
| 383 | # sets, checking patches for modifications, and doing a remote | ||
| 384 | # refresh on each. | ||
| 385 | oldpatchset = self.patchset | ||
| 386 | self.patchset = oldpatchset.__class__(self.patchset.dir, self.patchset.d) | ||
| 387 | |||
| 388 | for patch in self.patchset.patches: | ||
| 389 | oldpatch = None | ||
| 390 | for opatch in oldpatchset.patches: | ||
| 391 | if opatch["quiltfile"] == patch["quiltfile"]: | ||
| 392 | oldpatch = opatch | ||
| 393 | |||
| 394 | if oldpatch: | ||
| 395 | patch["remote"] = oldpatch["remote"] | ||
| 396 | if patch["quiltfile"] == oldpatch["quiltfile"]: | ||
| 397 | if patch["quiltfilemd5"] != oldpatch["quiltfilemd5"]: | ||
| 398 | bb.note("Patch %s has changed, updating remote url %s" % (os.path.basename(patch["quiltfile"]), patch["remote"])) | ||
| 399 | # user change? remote refresh | ||
| 400 | self.patchset.Refresh(remote=True, patch=self.patchset.patches.index(patch)) | ||
| 401 | else: | ||
| 402 | # User did not fix the problem. Abort. | ||
| 403 | raise PatchError("Patch application failed, and user did not fix and refresh the patch.") | ||
| 404 | except Exception: | ||
| 405 | os.chdir(olddir) | ||
| 406 | raise | ||
| 407 | os.chdir(olddir) | ||
