Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | import oe.path |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 8 | import oe.types |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 9 | import subprocess |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | |
| 11 | class NotFoundError(bb.BBHandledException): |
| 12 | def __init__(self, path): |
| 13 | self.path = path |
| 14 | |
| 15 | def __str__(self): |
| 16 | return "Error: %s not found." % self.path |
| 17 | |
| 18 | class CmdError(bb.BBHandledException): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 19 | def __init__(self, command, exitstatus, output): |
| 20 | self.command = command |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | self.status = exitstatus |
| 22 | self.output = output |
| 23 | |
| 24 | def __str__(self): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 25 | return "Command Error: '%s' exited with %d Output:\n%s" % \ |
| 26 | (self.command, self.status, self.output) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def runcmd(args, dir = None): |
| 30 | import pipes |
| 31 | |
| 32 | if dir: |
| 33 | olddir = os.path.abspath(os.curdir) |
| 34 | if not os.path.exists(dir): |
| 35 | raise NotFoundError(dir) |
| 36 | os.chdir(dir) |
| 37 | # print("cwd: %s -> %s" % (olddir, dir)) |
| 38 | |
| 39 | try: |
| 40 | args = [ pipes.quote(str(arg)) for arg in args ] |
| 41 | cmd = " ".join(args) |
| 42 | # print("cmd: %s" % cmd) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 43 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 44 | stdout, stderr = proc.communicate() |
| 45 | stdout = stdout.decode('utf-8') |
| 46 | stderr = stderr.decode('utf-8') |
| 47 | exitstatus = proc.returncode |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | if exitstatus != 0: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 49 | raise CmdError(cmd, exitstatus >> 8, "stdout: %s\nstderr: %s" % (stdout, stderr)) |
| 50 | if " fuzz " in stdout and "Hunk " in stdout: |
Brad Bishop | d89cb5f | 2019-04-10 09:02:41 -0400 | [diff] [blame] | 51 | # Drop patch fuzz info with header and footer to log file so |
| 52 | # insane.bbclass can handle to throw error/warning |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 53 | bb.note("--- Patch fuzz start ---\n%s\n--- Patch fuzz end ---" % format(stdout)) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 54 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 55 | return stdout |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
| 57 | finally: |
| 58 | if dir: |
| 59 | os.chdir(olddir) |
| 60 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 61 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | class PatchError(Exception): |
| 63 | def __init__(self, msg): |
| 64 | self.msg = msg |
| 65 | |
| 66 | def __str__(self): |
| 67 | return "Patch Error: %s" % self.msg |
| 68 | |
| 69 | class PatchSet(object): |
| 70 | defaults = { |
| 71 | "strippath": 1 |
| 72 | } |
| 73 | |
| 74 | def __init__(self, dir, d): |
| 75 | self.dir = dir |
| 76 | self.d = d |
| 77 | self.patches = [] |
| 78 | self._current = None |
| 79 | |
| 80 | def current(self): |
| 81 | return self._current |
| 82 | |
| 83 | def Clean(self): |
| 84 | """ |
| 85 | Clean out the patch set. Generally includes unapplying all |
| 86 | patches and wiping out all associated metadata. |
| 87 | """ |
| 88 | raise NotImplementedError() |
| 89 | |
| 90 | def Import(self, patch, force): |
| 91 | if not patch.get("file"): |
| 92 | if not patch.get("remote"): |
| 93 | raise PatchError("Patch file must be specified in patch import.") |
| 94 | else: |
| 95 | patch["file"] = bb.fetch2.localpath(patch["remote"], self.d) |
| 96 | |
| 97 | for param in PatchSet.defaults: |
| 98 | if not patch.get(param): |
| 99 | patch[param] = PatchSet.defaults[param] |
| 100 | |
| 101 | if patch.get("remote"): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 102 | patch["file"] = self.d.expand(bb.fetch2.localpath(patch["remote"], self.d)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
| 104 | patch["filemd5"] = bb.utils.md5_file(patch["file"]) |
| 105 | |
| 106 | def Push(self, force): |
| 107 | raise NotImplementedError() |
| 108 | |
| 109 | def Pop(self, force): |
| 110 | raise NotImplementedError() |
| 111 | |
| 112 | def Refresh(self, remote = None, all = None): |
| 113 | raise NotImplementedError() |
| 114 | |
| 115 | @staticmethod |
| 116 | def getPatchedFiles(patchfile, striplevel, srcdir=None): |
| 117 | """ |
| 118 | Read a patch file and determine which files it will modify. |
| 119 | Params: |
| 120 | patchfile: the patch file to read |
| 121 | striplevel: the strip level at which the patch is going to be applied |
| 122 | srcdir: optional path to join onto the patched file paths |
| 123 | Returns: |
| 124 | A list of tuples of file path and change mode ('A' for add, |
| 125 | 'D' for delete or 'M' for modify) |
| 126 | """ |
| 127 | |
| 128 | def patchedpath(patchline): |
| 129 | filepth = patchline.split()[1] |
| 130 | if filepth.endswith('/dev/null'): |
| 131 | return '/dev/null' |
| 132 | filesplit = filepth.split(os.sep) |
| 133 | if striplevel > len(filesplit): |
| 134 | bb.error('Patch %s has invalid strip level %d' % (patchfile, striplevel)) |
| 135 | return None |
| 136 | return os.sep.join(filesplit[striplevel:]) |
| 137 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 138 | for encoding in ['utf-8', 'latin-1']: |
| 139 | try: |
| 140 | copiedmode = False |
| 141 | filelist = [] |
| 142 | with open(patchfile) as f: |
| 143 | for line in f: |
| 144 | if line.startswith('--- '): |
| 145 | patchpth = patchedpath(line) |
| 146 | if not patchpth: |
| 147 | break |
| 148 | if copiedmode: |
| 149 | addedfile = patchpth |
| 150 | else: |
| 151 | removedfile = patchpth |
| 152 | elif line.startswith('+++ '): |
| 153 | addedfile = patchedpath(line) |
| 154 | if not addedfile: |
| 155 | break |
| 156 | elif line.startswith('*** '): |
| 157 | copiedmode = True |
| 158 | removedfile = patchedpath(line) |
| 159 | if not removedfile: |
| 160 | break |
| 161 | else: |
| 162 | removedfile = None |
| 163 | addedfile = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 165 | if addedfile and removedfile: |
| 166 | if removedfile == '/dev/null': |
| 167 | mode = 'A' |
| 168 | elif addedfile == '/dev/null': |
| 169 | mode = 'D' |
| 170 | else: |
| 171 | mode = 'M' |
| 172 | if srcdir: |
| 173 | fullpath = os.path.abspath(os.path.join(srcdir, addedfile)) |
| 174 | else: |
| 175 | fullpath = addedfile |
| 176 | filelist.append((fullpath, mode)) |
| 177 | except UnicodeDecodeError: |
| 178 | continue |
| 179 | break |
| 180 | else: |
| 181 | raise PatchError('Unable to decode %s' % patchfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | |
| 183 | return filelist |
| 184 | |
| 185 | |
| 186 | class PatchTree(PatchSet): |
| 187 | def __init__(self, dir, d): |
| 188 | PatchSet.__init__(self, dir, d) |
| 189 | self.patchdir = os.path.join(self.dir, 'patches') |
| 190 | self.seriespath = os.path.join(self.dir, 'patches', 'series') |
| 191 | bb.utils.mkdirhier(self.patchdir) |
| 192 | |
| 193 | def _appendPatchFile(self, patch, strippath): |
| 194 | with open(self.seriespath, 'a') as f: |
| 195 | f.write(os.path.basename(patch) + "," + strippath + "\n") |
| 196 | shellcmd = ["cat", patch, ">" , self.patchdir + "/" + os.path.basename(patch)] |
| 197 | runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 198 | |
| 199 | def _removePatch(self, p): |
| 200 | patch = {} |
| 201 | patch['file'] = p.split(",")[0] |
| 202 | patch['strippath'] = p.split(",")[1] |
| 203 | self._applypatch(patch, False, True) |
| 204 | |
| 205 | def _removePatchFile(self, all = False): |
| 206 | if not os.path.exists(self.seriespath): |
| 207 | return |
| 208 | with open(self.seriespath, 'r+') as f: |
| 209 | patches = f.readlines() |
| 210 | if all: |
| 211 | for p in reversed(patches): |
| 212 | self._removePatch(os.path.join(self.patchdir, p.strip())) |
| 213 | patches = [] |
| 214 | else: |
| 215 | self._removePatch(os.path.join(self.patchdir, patches[-1].strip())) |
| 216 | patches.pop() |
| 217 | with open(self.seriespath, 'w') as f: |
| 218 | for p in patches: |
| 219 | f.write(p) |
| 220 | |
| 221 | def Import(self, patch, force = None): |
| 222 | """""" |
| 223 | PatchSet.Import(self, patch, force) |
| 224 | |
| 225 | if self._current is not None: |
| 226 | i = self._current + 1 |
| 227 | else: |
| 228 | i = 0 |
| 229 | self.patches.insert(i, patch) |
| 230 | |
| 231 | def _applypatch(self, patch, force = False, reverse = False, run = True): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 232 | shellcmd = ["cat", patch['file'], "|", "patch", "--no-backup-if-mismatch", "-p", patch['strippath']] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 233 | if reverse: |
| 234 | shellcmd.append('-R') |
| 235 | |
| 236 | if not run: |
| 237 | return "sh" + "-c" + " ".join(shellcmd) |
| 238 | |
| 239 | if not force: |
| 240 | shellcmd.append('--dry-run') |
| 241 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 242 | try: |
| 243 | output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 244 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 245 | if force: |
| 246 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 248 | shellcmd.pop(len(shellcmd) - 1) |
| 249 | output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 250 | except CmdError as err: |
| 251 | raise bb.BBHandledException("Applying '%s' failed:\n%s" % |
| 252 | (os.path.basename(patch['file']), err.output)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 253 | |
| 254 | if not reverse: |
| 255 | self._appendPatchFile(patch['file'], patch['strippath']) |
| 256 | |
| 257 | return output |
| 258 | |
| 259 | def Push(self, force = False, all = False, run = True): |
| 260 | bb.note("self._current is %s" % self._current) |
| 261 | bb.note("patches is %s" % self.patches) |
| 262 | if all: |
| 263 | for i in self.patches: |
| 264 | bb.note("applying patch %s" % i) |
| 265 | self._applypatch(i, force) |
| 266 | self._current = i |
| 267 | else: |
| 268 | if self._current is not None: |
| 269 | next = self._current + 1 |
| 270 | else: |
| 271 | next = 0 |
| 272 | |
| 273 | bb.note("applying patch %s" % self.patches[next]) |
| 274 | ret = self._applypatch(self.patches[next], force) |
| 275 | |
| 276 | self._current = next |
| 277 | return ret |
| 278 | |
| 279 | def Pop(self, force = None, all = None): |
| 280 | if all: |
| 281 | self._removePatchFile(True) |
| 282 | self._current = None |
| 283 | else: |
| 284 | self._removePatchFile(False) |
| 285 | |
| 286 | if self._current == 0: |
| 287 | self._current = None |
| 288 | |
| 289 | if self._current is not None: |
| 290 | self._current = self._current - 1 |
| 291 | |
| 292 | def Clean(self): |
| 293 | """""" |
| 294 | self.Pop(all=True) |
| 295 | |
| 296 | class GitApplyTree(PatchTree): |
| 297 | patch_line_prefix = '%% original patch' |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 298 | ignore_commit_prefix = '%% ignore' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 299 | |
| 300 | def __init__(self, dir, d): |
| 301 | PatchTree.__init__(self, dir, d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 302 | self.commituser = d.getVar('PATCH_GIT_USER_NAME') |
| 303 | self.commitemail = d.getVar('PATCH_GIT_USER_EMAIL') |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 304 | if not self._isInitialized(d): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 305 | self._initRepo() |
| 306 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 307 | def _isInitialized(self, d): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 308 | cmd = "git rev-parse --show-toplevel" |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 309 | try: |
| 310 | output = runcmd(cmd.split(), self.dir).strip() |
| 311 | except CmdError as err: |
| 312 | ## runcmd returned non-zero which most likely means 128 |
| 313 | ## Not a git directory |
| 314 | return False |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 315 | ## Make sure repo is in builddir to not break top-level git repos, or under workdir |
| 316 | return os.path.samefile(output, self.dir) or oe.path.is_path_parent(d.getVar('WORKDIR'), output) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 317 | |
| 318 | def _initRepo(self): |
| 319 | runcmd("git init".split(), self.dir) |
| 320 | runcmd("git add .".split(), self.dir) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 321 | runcmd("git commit -a --allow-empty -m bitbake_patching_started".split(), self.dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 322 | |
| 323 | @staticmethod |
| 324 | def extractPatchHeader(patchfile): |
| 325 | """ |
| 326 | Extract just the header lines from the top of a patch file |
| 327 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 328 | for encoding in ['utf-8', 'latin-1']: |
| 329 | lines = [] |
| 330 | try: |
| 331 | with open(patchfile, 'r', encoding=encoding) as f: |
| 332 | for line in f: |
| 333 | if line.startswith('Index: ') or line.startswith('diff -') or line.startswith('---'): |
| 334 | break |
| 335 | lines.append(line) |
| 336 | except UnicodeDecodeError: |
| 337 | continue |
| 338 | break |
| 339 | else: |
| 340 | raise PatchError('Unable to find a character encoding to decode %s' % patchfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 341 | return lines |
| 342 | |
| 343 | @staticmethod |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 344 | def decodeAuthor(line): |
| 345 | from email.header import decode_header |
| 346 | authorval = line.split(':', 1)[1].strip().replace('"', '') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 347 | result = decode_header(authorval)[0][0] |
| 348 | if hasattr(result, 'decode'): |
| 349 | result = result.decode('utf-8') |
| 350 | return result |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 351 | |
| 352 | @staticmethod |
| 353 | def interpretPatchHeader(headerlines): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 354 | import re |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 355 | author_re = re.compile(r'[\S ]+ <\S+@\S+\.\S+>') |
| 356 | from_commit_re = re.compile(r'^From [a-z0-9]{40} .*') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 357 | outlines = [] |
| 358 | author = None |
| 359 | date = None |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 360 | subject = None |
| 361 | for line in headerlines: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 362 | if line.startswith('Subject: '): |
| 363 | subject = line.split(':', 1)[1] |
| 364 | # Remove any [PATCH][oe-core] etc. |
| 365 | subject = re.sub(r'\[.+?\]\s*', '', subject) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 366 | continue |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 367 | elif line.startswith('From: ') or line.startswith('Author: '): |
| 368 | authorval = GitApplyTree.decodeAuthor(line) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 369 | # git is fussy about author formatting i.e. it must be Name <email@domain> |
| 370 | if author_re.match(authorval): |
| 371 | author = authorval |
| 372 | continue |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 373 | elif line.startswith('Date: '): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 374 | if date is None: |
| 375 | dateval = line.split(':', 1)[1].strip() |
| 376 | # Very crude check for date format, since git will blow up if it's not in the right |
| 377 | # format. Without e.g. a python-dateutils dependency we can't do a whole lot more |
| 378 | if len(dateval) > 12: |
| 379 | date = dateval |
| 380 | continue |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 381 | elif not author and line.lower().startswith('signed-off-by: '): |
| 382 | authorval = GitApplyTree.decodeAuthor(line) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | # git is fussy about author formatting i.e. it must be Name <email@domain> |
| 384 | if author_re.match(authorval): |
| 385 | author = authorval |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 386 | elif from_commit_re.match(line): |
| 387 | # We don't want the From <commit> line - if it's present it will break rebasing |
| 388 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 389 | outlines.append(line) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 390 | |
| 391 | if not subject: |
| 392 | firstline = None |
| 393 | for line in headerlines: |
| 394 | line = line.strip() |
| 395 | if firstline: |
| 396 | if line: |
| 397 | # Second line is not blank, the first line probably isn't usable |
| 398 | firstline = None |
| 399 | break |
| 400 | elif line: |
| 401 | firstline = line |
| 402 | if firstline and not firstline.startswith(('#', 'Index:', 'Upstream-Status:')) and len(firstline) < 100: |
| 403 | subject = firstline |
| 404 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 405 | return outlines, author, date, subject |
| 406 | |
| 407 | @staticmethod |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 408 | def gitCommandUserOptions(cmd, commituser=None, commitemail=None, d=None): |
| 409 | if d: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 410 | commituser = d.getVar('PATCH_GIT_USER_NAME') |
| 411 | commitemail = d.getVar('PATCH_GIT_USER_EMAIL') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 412 | if commituser: |
| 413 | cmd += ['-c', 'user.name="%s"' % commituser] |
| 414 | if commitemail: |
| 415 | cmd += ['-c', 'user.email="%s"' % commitemail] |
| 416 | |
| 417 | @staticmethod |
| 418 | def prepareCommit(patchfile, commituser=None, commitemail=None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 419 | """ |
| 420 | Prepare a git commit command line based on the header from a patch file |
| 421 | (typically this is useful for patches that cannot be applied with "git am" due to formatting) |
| 422 | """ |
| 423 | import tempfile |
| 424 | # Process patch header and extract useful information |
| 425 | lines = GitApplyTree.extractPatchHeader(patchfile) |
| 426 | outlines, author, date, subject = GitApplyTree.interpretPatchHeader(lines) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 427 | if not author or not subject or not date: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 428 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 429 | shellcmd = ["git", "log", "--format=email", "--follow", "--diff-filter=A", "--", patchfile] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 430 | out = runcmd(["sh", "-c", " ".join(shellcmd)], os.path.dirname(patchfile)) |
| 431 | except CmdError: |
| 432 | out = None |
| 433 | if out: |
| 434 | _, newauthor, newdate, newsubject = GitApplyTree.interpretPatchHeader(out.splitlines()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 435 | if not author: |
| 436 | # If we're setting the author then the date should be set as well |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 437 | author = newauthor |
| 438 | date = newdate |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 439 | elif not date: |
| 440 | # If we don't do this we'll get the current date, at least this will be closer |
| 441 | date = newdate |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 442 | if not subject: |
| 443 | subject = newsubject |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 444 | if subject and not (outlines and outlines[0].strip() == subject): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 445 | outlines.insert(0, '%s\n\n' % subject.strip()) |
| 446 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 447 | # Write out commit message to a file |
| 448 | with tempfile.NamedTemporaryFile('w', delete=False) as tf: |
| 449 | tmpfile = tf.name |
| 450 | for line in outlines: |
| 451 | tf.write(line) |
| 452 | # Prepare git command |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 453 | cmd = ["git"] |
| 454 | GitApplyTree.gitCommandUserOptions(cmd, commituser, commitemail) |
| 455 | cmd += ["commit", "-F", tmpfile] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 456 | # git doesn't like plain email addresses as authors |
| 457 | if author and '<' in author: |
| 458 | cmd.append('--author="%s"' % author) |
| 459 | if date: |
| 460 | cmd.append('--date="%s"' % date) |
| 461 | return (tmpfile, cmd) |
| 462 | |
| 463 | @staticmethod |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 464 | def extractPatches(tree, startcommit, outdir, paths=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 465 | import tempfile |
| 466 | import shutil |
| 467 | tempdir = tempfile.mkdtemp(prefix='oepatch') |
| 468 | try: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 469 | shellcmd = ["git", "format-patch", "--no-signature", "--no-numbered", startcommit, "-o", tempdir] |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 470 | if paths: |
| 471 | shellcmd.append('--') |
| 472 | shellcmd.extend(paths) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 473 | out = runcmd(["sh", "-c", " ".join(shellcmd)], tree) |
| 474 | if out: |
| 475 | for srcfile in out.split(): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 476 | for encoding in ['utf-8', 'latin-1']: |
| 477 | patchlines = [] |
| 478 | outfile = None |
| 479 | try: |
| 480 | with open(srcfile, 'r', encoding=encoding) as f: |
| 481 | for line in f: |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 482 | if line.startswith(GitApplyTree.patch_line_prefix): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 483 | outfile = line.split()[-1].strip() |
| 484 | continue |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 485 | if line.startswith(GitApplyTree.ignore_commit_prefix): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 486 | continue |
| 487 | patchlines.append(line) |
| 488 | except UnicodeDecodeError: |
| 489 | continue |
| 490 | break |
| 491 | else: |
| 492 | raise PatchError('Unable to find a character encoding to decode %s' % srcfile) |
| 493 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 494 | if not outfile: |
| 495 | outfile = os.path.basename(srcfile) |
| 496 | with open(os.path.join(outdir, outfile), 'w') as of: |
| 497 | for line in patchlines: |
| 498 | of.write(line) |
| 499 | finally: |
| 500 | shutil.rmtree(tempdir) |
| 501 | |
| 502 | def _applypatch(self, patch, force = False, reverse = False, run = True): |
| 503 | import shutil |
| 504 | |
| 505 | def _applypatchhelper(shellcmd, patch, force = False, reverse = False, run = True): |
| 506 | if reverse: |
| 507 | shellcmd.append('-R') |
| 508 | |
| 509 | shellcmd.append(patch['file']) |
| 510 | |
| 511 | if not run: |
| 512 | return "sh" + "-c" + " ".join(shellcmd) |
| 513 | |
| 514 | return runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 515 | |
| 516 | # Add hooks which add a pointer to the original patch file name in the commit message |
| 517 | reporoot = (runcmd("git rev-parse --show-toplevel".split(), self.dir) or '').strip() |
| 518 | if not reporoot: |
| 519 | raise Exception("Cannot get repository root for directory %s" % self.dir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 520 | hooks_dir = os.path.join(reporoot, '.git', 'hooks') |
| 521 | hooks_dir_backup = hooks_dir + '.devtool-orig' |
| 522 | if os.path.lexists(hooks_dir_backup): |
| 523 | raise Exception("Git hooks backup directory already exists: %s" % hooks_dir_backup) |
| 524 | if os.path.lexists(hooks_dir): |
| 525 | shutil.move(hooks_dir, hooks_dir_backup) |
| 526 | os.mkdir(hooks_dir) |
| 527 | commithook = os.path.join(hooks_dir, 'commit-msg') |
| 528 | applyhook = os.path.join(hooks_dir, 'applypatch-msg') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 529 | with open(commithook, 'w') as f: |
| 530 | # NOTE: the formatting here is significant; if you change it you'll also need to |
| 531 | # change other places which read it back |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 532 | f.write('echo "\n%s: $PATCHFILE" >> $1' % GitApplyTree.patch_line_prefix) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 533 | os.chmod(commithook, 0o755) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 534 | shutil.copy2(commithook, applyhook) |
| 535 | try: |
| 536 | patchfilevar = 'PATCHFILE="%s"' % os.path.basename(patch['file']) |
| 537 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 538 | shellcmd = [patchfilevar, "git", "--work-tree=%s" % reporoot] |
| 539 | self.gitCommandUserOptions(shellcmd, self.commituser, self.commitemail) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 540 | shellcmd += ["am", "-3", "--keep-cr", "--no-scissors", "-p%s" % patch['strippath']] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 541 | return _applypatchhelper(shellcmd, patch, force, reverse, run) |
| 542 | except CmdError: |
| 543 | # Need to abort the git am, or we'll still be within it at the end |
| 544 | try: |
| 545 | shellcmd = ["git", "--work-tree=%s" % reporoot, "am", "--abort"] |
| 546 | runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 547 | except CmdError: |
| 548 | pass |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 549 | # git am won't always clean up after itself, sadly, so... |
| 550 | shellcmd = ["git", "--work-tree=%s" % reporoot, "reset", "--hard", "HEAD"] |
| 551 | runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 552 | # Also need to take care of any stray untracked files |
| 553 | shellcmd = ["git", "--work-tree=%s" % reporoot, "clean", "-f"] |
| 554 | runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 555 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 556 | # Fall back to git apply |
| 557 | shellcmd = ["git", "--git-dir=%s" % reporoot, "apply", "-p%s" % patch['strippath']] |
| 558 | try: |
| 559 | output = _applypatchhelper(shellcmd, patch, force, reverse, run) |
| 560 | except CmdError: |
| 561 | # Fall back to patch |
| 562 | output = PatchTree._applypatch(self, patch, force, reverse, run) |
| 563 | # Add all files |
| 564 | shellcmd = ["git", "add", "-f", "-A", "."] |
| 565 | output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 566 | # Exclude the patches directory |
| 567 | shellcmd = ["git", "reset", "HEAD", self.patchdir] |
| 568 | output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 569 | # Commit the result |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 570 | (tmpfile, shellcmd) = self.prepareCommit(patch['file'], self.commituser, self.commitemail) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 571 | try: |
| 572 | shellcmd.insert(0, patchfilevar) |
| 573 | output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir) |
| 574 | finally: |
| 575 | os.remove(tmpfile) |
| 576 | return output |
| 577 | finally: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 578 | shutil.rmtree(hooks_dir) |
| 579 | if os.path.lexists(hooks_dir_backup): |
| 580 | shutil.move(hooks_dir_backup, hooks_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 581 | |
| 582 | |
| 583 | class QuiltTree(PatchSet): |
| 584 | def _runcmd(self, args, run = True): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 585 | quiltrc = self.d.getVar('QUILTRCFILE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 586 | if not run: |
| 587 | return ["quilt"] + ["--quiltrc"] + [quiltrc] + args |
| 588 | runcmd(["quilt"] + ["--quiltrc"] + [quiltrc] + args, self.dir) |
| 589 | |
| 590 | def _quiltpatchpath(self, file): |
| 591 | return os.path.join(self.dir, "patches", os.path.basename(file)) |
| 592 | |
| 593 | |
| 594 | def __init__(self, dir, d): |
| 595 | PatchSet.__init__(self, dir, d) |
| 596 | self.initialized = False |
| 597 | p = os.path.join(self.dir, 'patches') |
| 598 | if not os.path.exists(p): |
| 599 | os.makedirs(p) |
| 600 | |
| 601 | def Clean(self): |
| 602 | try: |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 603 | # make sure that patches/series file exists before quilt pop to keep quilt-0.67 happy |
| 604 | open(os.path.join(self.dir, "patches","series"), 'a').close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 605 | self._runcmd(["pop", "-a", "-f"]) |
| 606 | oe.path.remove(os.path.join(self.dir, "patches","series")) |
| 607 | except Exception: |
| 608 | pass |
| 609 | self.initialized = True |
| 610 | |
| 611 | def InitFromDir(self): |
| 612 | # read series -> self.patches |
| 613 | seriespath = os.path.join(self.dir, 'patches', 'series') |
| 614 | if not os.path.exists(self.dir): |
| 615 | raise NotFoundError(self.dir) |
| 616 | if os.path.exists(seriespath): |
| 617 | with open(seriespath, 'r') as f: |
| 618 | for line in f.readlines(): |
| 619 | patch = {} |
| 620 | parts = line.strip().split() |
| 621 | patch["quiltfile"] = self._quiltpatchpath(parts[0]) |
| 622 | patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"]) |
| 623 | if len(parts) > 1: |
| 624 | patch["strippath"] = parts[1][2:] |
| 625 | self.patches.append(patch) |
| 626 | |
| 627 | # determine which patches are applied -> self._current |
| 628 | try: |
| 629 | output = runcmd(["quilt", "applied"], self.dir) |
| 630 | except CmdError: |
| 631 | import sys |
| 632 | if sys.exc_value.output.strip() == "No patches applied": |
| 633 | return |
| 634 | else: |
| 635 | raise |
| 636 | output = [val for val in output.split('\n') if not val.startswith('#')] |
| 637 | for patch in self.patches: |
| 638 | if os.path.basename(patch["quiltfile"]) == output[-1]: |
| 639 | self._current = self.patches.index(patch) |
| 640 | self.initialized = True |
| 641 | |
| 642 | def Import(self, patch, force = None): |
| 643 | if not self.initialized: |
| 644 | self.InitFromDir() |
| 645 | PatchSet.Import(self, patch, force) |
| 646 | oe.path.symlink(patch["file"], self._quiltpatchpath(patch["file"]), force=True) |
| 647 | with open(os.path.join(self.dir, "patches", "series"), "a") as f: |
| 648 | f.write(os.path.basename(patch["file"]) + " -p" + patch["strippath"] + "\n") |
| 649 | patch["quiltfile"] = self._quiltpatchpath(patch["file"]) |
| 650 | patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"]) |
| 651 | |
| 652 | # TODO: determine if the file being imported: |
| 653 | # 1) is already imported, and is the same |
| 654 | # 2) is already imported, but differs |
| 655 | |
| 656 | self.patches.insert(self._current or 0, patch) |
| 657 | |
| 658 | |
| 659 | def Push(self, force = False, all = False, run = True): |
| 660 | # quilt push [-f] |
| 661 | |
| 662 | args = ["push"] |
| 663 | if force: |
| 664 | args.append("-f") |
| 665 | if all: |
| 666 | args.append("-a") |
| 667 | if not run: |
| 668 | return self._runcmd(args, run) |
| 669 | |
| 670 | self._runcmd(args) |
| 671 | |
| 672 | if self._current is not None: |
| 673 | self._current = self._current + 1 |
| 674 | else: |
| 675 | self._current = 0 |
| 676 | |
| 677 | def Pop(self, force = None, all = None): |
| 678 | # quilt pop [-f] |
| 679 | args = ["pop"] |
| 680 | if force: |
| 681 | args.append("-f") |
| 682 | if all: |
| 683 | args.append("-a") |
| 684 | |
| 685 | self._runcmd(args) |
| 686 | |
| 687 | if self._current == 0: |
| 688 | self._current = None |
| 689 | |
| 690 | if self._current is not None: |
| 691 | self._current = self._current - 1 |
| 692 | |
| 693 | def Refresh(self, **kwargs): |
| 694 | if kwargs.get("remote"): |
| 695 | patch = self.patches[kwargs["patch"]] |
| 696 | if not patch: |
| 697 | raise PatchError("No patch found at index %s in patchset." % kwargs["patch"]) |
| 698 | (type, host, path, user, pswd, parm) = bb.fetch.decodeurl(patch["remote"]) |
| 699 | if type == "file": |
| 700 | import shutil |
| 701 | if not patch.get("file") and patch.get("remote"): |
| 702 | patch["file"] = bb.fetch2.localpath(patch["remote"], self.d) |
| 703 | |
| 704 | shutil.copyfile(patch["quiltfile"], patch["file"]) |
| 705 | else: |
| 706 | raise PatchError("Unable to do a remote refresh of %s, unsupported remote url scheme %s." % (os.path.basename(patch["quiltfile"]), type)) |
| 707 | else: |
| 708 | # quilt refresh |
| 709 | args = ["refresh"] |
| 710 | if kwargs.get("quiltfile"): |
| 711 | args.append(os.path.basename(kwargs["quiltfile"])) |
| 712 | elif kwargs.get("patch"): |
| 713 | args.append(os.path.basename(self.patches[kwargs["patch"]]["quiltfile"])) |
| 714 | self._runcmd(args) |
| 715 | |
| 716 | class Resolver(object): |
| 717 | def __init__(self, patchset, terminal): |
| 718 | raise NotImplementedError() |
| 719 | |
| 720 | def Resolve(self): |
| 721 | raise NotImplementedError() |
| 722 | |
| 723 | def Revert(self): |
| 724 | raise NotImplementedError() |
| 725 | |
| 726 | def Finalize(self): |
| 727 | raise NotImplementedError() |
| 728 | |
| 729 | class NOOPResolver(Resolver): |
| 730 | def __init__(self, patchset, terminal): |
| 731 | self.patchset = patchset |
| 732 | self.terminal = terminal |
| 733 | |
| 734 | def Resolve(self): |
| 735 | olddir = os.path.abspath(os.curdir) |
| 736 | os.chdir(self.patchset.dir) |
| 737 | try: |
| 738 | self.patchset.Push() |
| 739 | except Exception: |
| 740 | import sys |
| 741 | os.chdir(olddir) |
| 742 | raise |
| 743 | |
| 744 | # Patch resolver which relies on the user doing all the work involved in the |
| 745 | # resolution, with the exception of refreshing the remote copy of the patch |
| 746 | # files (the urls). |
| 747 | class UserResolver(Resolver): |
| 748 | def __init__(self, patchset, terminal): |
| 749 | self.patchset = patchset |
| 750 | self.terminal = terminal |
| 751 | |
| 752 | # Force a push in the patchset, then drop to a shell for the user to |
| 753 | # resolve any rejected hunks |
| 754 | def Resolve(self): |
| 755 | olddir = os.path.abspath(os.curdir) |
| 756 | os.chdir(self.patchset.dir) |
| 757 | try: |
| 758 | self.patchset.Push(False) |
| 759 | except CmdError as v: |
| 760 | # Patch application failed |
| 761 | patchcmd = self.patchset.Push(True, False, False) |
| 762 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 763 | t = self.patchset.d.getVar('T') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 764 | if not t: |
| 765 | bb.msg.fatal("Build", "T not set") |
| 766 | bb.utils.mkdirhier(t) |
| 767 | import random |
| 768 | rcfile = "%s/bashrc.%s.%s" % (t, str(os.getpid()), random.random()) |
| 769 | with open(rcfile, "w") as f: |
| 770 | f.write("echo '*** Manual patch resolution mode ***'\n") |
| 771 | f.write("echo 'Dropping to a shell, so patch rejects can be fixed manually.'\n") |
| 772 | f.write("echo 'Run \"quilt refresh\" when patch is corrected, press CTRL+D to exit.'\n") |
| 773 | f.write("echo ''\n") |
| 774 | f.write(" ".join(patchcmd) + "\n") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 775 | os.chmod(rcfile, 0o775) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 776 | |
| 777 | self.terminal("bash --rcfile " + rcfile, 'Patch Rejects: Please fix patch rejects manually', self.patchset.d) |
| 778 | |
| 779 | # Construct a new PatchSet after the user's changes, compare the |
| 780 | # sets, checking patches for modifications, and doing a remote |
| 781 | # refresh on each. |
| 782 | oldpatchset = self.patchset |
| 783 | self.patchset = oldpatchset.__class__(self.patchset.dir, self.patchset.d) |
| 784 | |
| 785 | for patch in self.patchset.patches: |
| 786 | oldpatch = None |
| 787 | for opatch in oldpatchset.patches: |
| 788 | if opatch["quiltfile"] == patch["quiltfile"]: |
| 789 | oldpatch = opatch |
| 790 | |
| 791 | if oldpatch: |
| 792 | patch["remote"] = oldpatch["remote"] |
| 793 | if patch["quiltfile"] == oldpatch["quiltfile"]: |
| 794 | if patch["quiltfilemd5"] != oldpatch["quiltfilemd5"]: |
| 795 | bb.note("Patch %s has changed, updating remote url %s" % (os.path.basename(patch["quiltfile"]), patch["remote"])) |
| 796 | # user change? remote refresh |
| 797 | self.patchset.Refresh(remote=True, patch=self.patchset.patches.index(patch)) |
| 798 | else: |
| 799 | # User did not fix the problem. Abort. |
| 800 | raise PatchError("Patch application failed, and user did not fix and refresh the patch.") |
| 801 | except Exception: |
| 802 | os.chdir(olddir) |
| 803 | raise |
| 804 | os.chdir(olddir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 805 | |
| 806 | |
| 807 | def patch_path(url, fetch, workdir, expand=True): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 808 | """Return the local path of a patch, or return nothing if this isn't a patch""" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 809 | |
| 810 | local = fetch.localpath(url) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 811 | if os.path.isdir(local): |
| 812 | return |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 813 | base, ext = os.path.splitext(os.path.basename(local)) |
| 814 | if ext in ('.gz', '.bz2', '.xz', '.Z'): |
| 815 | if expand: |
| 816 | local = os.path.join(workdir, base) |
| 817 | ext = os.path.splitext(base)[1] |
| 818 | |
| 819 | urldata = fetch.ud[url] |
| 820 | if "apply" in urldata.parm: |
| 821 | apply = oe.types.boolean(urldata.parm["apply"]) |
| 822 | if not apply: |
| 823 | return |
| 824 | elif ext not in (".diff", ".patch"): |
| 825 | return |
| 826 | |
| 827 | return local |
| 828 | |
| 829 | def src_patches(d, all=False, expand=True): |
| 830 | workdir = d.getVar('WORKDIR') |
| 831 | fetch = bb.fetch2.Fetch([], d) |
| 832 | patches = [] |
| 833 | sources = [] |
| 834 | for url in fetch.urls: |
| 835 | local = patch_path(url, fetch, workdir, expand) |
| 836 | if not local: |
| 837 | if all: |
| 838 | local = fetch.localpath(url) |
| 839 | sources.append(local) |
| 840 | continue |
| 841 | |
| 842 | urldata = fetch.ud[url] |
| 843 | parm = urldata.parm |
| 844 | patchname = parm.get('pname') or os.path.basename(local) |
| 845 | |
| 846 | apply, reason = should_apply(parm, d) |
| 847 | if not apply: |
| 848 | if reason: |
| 849 | bb.note("Patch %s %s" % (patchname, reason)) |
| 850 | continue |
| 851 | |
| 852 | patchparm = {'patchname': patchname} |
| 853 | if "striplevel" in parm: |
| 854 | striplevel = parm["striplevel"] |
| 855 | elif "pnum" in parm: |
| 856 | #bb.msg.warn(None, "Deprecated usage of 'pnum' url parameter in '%s', please use 'striplevel'" % url) |
| 857 | striplevel = parm["pnum"] |
| 858 | else: |
| 859 | striplevel = '1' |
| 860 | patchparm['striplevel'] = striplevel |
| 861 | |
| 862 | patchdir = parm.get('patchdir') |
| 863 | if patchdir: |
| 864 | patchparm['patchdir'] = patchdir |
| 865 | |
| 866 | localurl = bb.fetch.encodeurl(('file', '', local, '', '', patchparm)) |
| 867 | patches.append(localurl) |
| 868 | |
| 869 | if all: |
| 870 | return sources |
| 871 | |
| 872 | return patches |
| 873 | |
| 874 | |
| 875 | def should_apply(parm, d): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 876 | import bb.utils |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 877 | if "mindate" in parm or "maxdate" in parm: |
| 878 | pn = d.getVar('PN') |
| 879 | srcdate = d.getVar('SRCDATE_%s' % pn) |
| 880 | if not srcdate: |
| 881 | srcdate = d.getVar('SRCDATE') |
| 882 | |
| 883 | if srcdate == "now": |
| 884 | srcdate = d.getVar('DATE') |
| 885 | |
| 886 | if "maxdate" in parm and parm["maxdate"] < srcdate: |
| 887 | return False, 'is outdated' |
| 888 | |
| 889 | if "mindate" in parm and parm["mindate"] > srcdate: |
| 890 | return False, 'is predated' |
| 891 | |
| 892 | |
| 893 | if "minrev" in parm: |
| 894 | srcrev = d.getVar('SRCREV') |
| 895 | if srcrev and srcrev < parm["minrev"]: |
| 896 | return False, 'applies to later revisions' |
| 897 | |
| 898 | if "maxrev" in parm: |
| 899 | srcrev = d.getVar('SRCREV') |
| 900 | if srcrev and srcrev > parm["maxrev"]: |
| 901 | return False, 'applies to earlier revisions' |
| 902 | |
| 903 | if "rev" in parm: |
| 904 | srcrev = d.getVar('SRCREV') |
| 905 | if srcrev and parm["rev"] not in srcrev: |
| 906 | return False, "doesn't apply to revision" |
| 907 | |
| 908 | if "notrev" in parm: |
| 909 | srcrev = d.getVar('SRCREV') |
| 910 | if srcrev and parm["notrev"] in srcrev: |
| 911 | return False, "doesn't apply to revision" |
| 912 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 913 | if "maxver" in parm: |
| 914 | pv = d.getVar('PV') |
| 915 | if bb.utils.vercmp_string_op(pv, parm["maxver"], ">"): |
| 916 | return False, "applies to earlier version" |
| 917 | |
| 918 | if "minver" in parm: |
| 919 | pv = d.getVar('PV') |
| 920 | if bb.utils.vercmp_string_op(pv, parm["minver"], "<"): |
| 921 | return False, "applies to later version" |
| 922 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 923 | return True, None |
| 924 | |