Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Utility functions for reading and modifying recipes |
| 2 | # |
| 3 | # Some code borrowed from the OE layer index |
| 4 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 5 | # Copyright (C) 2013-2017 Intel Corporation |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0-only |
| 8 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | |
| 10 | import sys |
| 11 | import os |
| 12 | import os.path |
| 13 | import tempfile |
| 14 | import textwrap |
| 15 | import difflib |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 16 | from . import utils |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | import shutil |
| 18 | import re |
| 19 | import fnmatch |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 20 | import glob |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 21 | import bb.tinfoil |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 23 | from collections import OrderedDict, defaultdict |
| 24 | from bb.utils import vercmp_string |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
| 26 | # Help us to find places to insert values |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 27 | recipe_progression = ['SUMMARY', 'DESCRIPTION', 'AUTHOR', 'HOMEPAGE', 'BUGTRACKER', 'SECTION', 'LICENSE', 'LICENSE_FLAGS', 'LIC_FILES_CHKSUM', 'PROVIDES', 'DEPENDS', 'PR', 'PV', 'SRCREV', 'SRCPV', 'SRC_URI', 'S', 'do_fetch()', 'do_unpack()', 'do_patch()', 'EXTRA_OECONF', 'EXTRA_OECMAKE', 'EXTRA_OESCONS', 'do_configure()', 'EXTRA_OEMAKE', 'do_compile()', 'do_install()', 'do_populate_sysroot()', 'INITSCRIPT', 'USERADD', 'GROUPADD', 'PACKAGES', 'FILES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RPROVIDES', 'RREPLACES', 'RCONFLICTS', 'ALLOW_EMPTY', 'populate_packages()', 'do_package()', 'do_deploy()', 'BBCLASSEXTEND'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | # Variables that sometimes are a bit long but shouldn't be wrapped |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 29 | nowrap_vars = ['SUMMARY', 'HOMEPAGE', 'BUGTRACKER', r'SRC_URI\[(.+\.)?md5sum\]', r'SRC_URI\[(.+\.)?sha256sum\]'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | list_vars = ['SRC_URI', 'LIC_FILES_CHKSUM'] |
| 31 | meta_vars = ['SUMMARY', 'DESCRIPTION', 'HOMEPAGE', 'BUGTRACKER', 'SECTION'] |
| 32 | |
| 33 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 34 | def simplify_history(history, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 36 | Eliminate any irrelevant events from a variable history |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 38 | ret_history = [] |
| 39 | has_set = False |
| 40 | # Go backwards through the history and remove any immediate operations |
| 41 | # before the most recent set |
| 42 | for event in reversed(history): |
| 43 | if 'flag' in event or not 'file' in event: |
| 44 | continue |
| 45 | if event['op'] == 'set': |
| 46 | if has_set: |
| 47 | continue |
| 48 | has_set = True |
| 49 | elif event['op'] in ('append', 'prepend', 'postdot', 'predot'): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 50 | # Reminder: "append" and "prepend" mean += and =+ respectively, NOT :append / :prepend |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 51 | if has_set: |
| 52 | continue |
| 53 | ret_history.insert(0, event) |
| 54 | return ret_history |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | |
| 56 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | def get_var_files(fn, varlist, d): |
| 58 | """Find the file in which each of a list of variables is set. |
| 59 | Note: requires variable history to be enabled when parsing. |
| 60 | """ |
| 61 | varfiles = {} |
| 62 | for v in varlist: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | files = [] |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 64 | if '[' in v: |
| 65 | varsplit = v.split('[') |
| 66 | varflag = varsplit[1].split(']')[0] |
| 67 | history = d.varhistory.variable(varsplit[0]) |
| 68 | for event in history: |
| 69 | if 'file' in event and event.get('flag', '') == varflag: |
| 70 | files.append(event['file']) |
| 71 | else: |
| 72 | history = d.varhistory.variable(v) |
| 73 | for event in history: |
| 74 | if 'file' in event and not 'flag' in event: |
| 75 | files.append(event['file']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | if files: |
| 77 | actualfile = files[-1] |
| 78 | else: |
| 79 | actualfile = None |
| 80 | varfiles[v] = actualfile |
| 81 | |
| 82 | return varfiles |
| 83 | |
| 84 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 85 | def split_var_value(value, assignment=True): |
| 86 | """ |
| 87 | Split a space-separated variable's value into a list of items, |
| 88 | taking into account that some of the items might be made up of |
| 89 | expressions containing spaces that should not be split. |
| 90 | Parameters: |
| 91 | value: |
| 92 | The string value to split |
| 93 | assignment: |
| 94 | True to assume that the value represents an assignment |
| 95 | statement, False otherwise. If True, and an assignment |
| 96 | statement is passed in the first item in |
| 97 | the returned list will be the part of the assignment |
| 98 | statement up to and including the opening quote character, |
| 99 | and the last item will be the closing quote. |
| 100 | """ |
| 101 | inexpr = 0 |
| 102 | lastchar = None |
| 103 | out = [] |
| 104 | buf = '' |
| 105 | for char in value: |
| 106 | if char == '{': |
| 107 | if lastchar == '$': |
| 108 | inexpr += 1 |
| 109 | elif char == '}': |
| 110 | inexpr -= 1 |
| 111 | elif assignment and char in '"\'' and inexpr == 0: |
| 112 | if buf: |
| 113 | out.append(buf) |
| 114 | out.append(char) |
| 115 | char = '' |
| 116 | buf = '' |
| 117 | elif char.isspace() and inexpr == 0: |
| 118 | char = '' |
| 119 | if buf: |
| 120 | out.append(buf) |
| 121 | buf = '' |
| 122 | buf += char |
| 123 | lastchar = char |
| 124 | if buf: |
| 125 | out.append(buf) |
| 126 | |
| 127 | # Join together assignment statement and opening quote |
| 128 | outlist = out |
| 129 | if assignment: |
| 130 | assigfound = False |
| 131 | for idx, item in enumerate(out): |
| 132 | if '=' in item: |
| 133 | assigfound = True |
| 134 | if assigfound: |
| 135 | if '"' in item or "'" in item: |
| 136 | outlist = [' '.join(out[:idx+1])] |
| 137 | outlist.extend(out[idx+1:]) |
| 138 | break |
| 139 | return outlist |
| 140 | |
| 141 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 142 | def patch_recipe_lines(fromlines, values, trailing_newline=True): |
| 143 | """Update or insert variable values into lines from a recipe. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 144 | Note that some manual inspection/intervention may be required |
| 145 | since this cannot handle all situations. |
| 146 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 147 | |
| 148 | import bb.utils |
| 149 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | if trailing_newline: |
| 151 | newline = '\n' |
| 152 | else: |
| 153 | newline = '' |
| 154 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 155 | nowrap_vars_res = [] |
| 156 | for item in nowrap_vars: |
| 157 | nowrap_vars_res.append(re.compile('^%s$' % item)) |
| 158 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 159 | recipe_progression_res = [] |
| 160 | recipe_progression_restrs = [] |
| 161 | for item in recipe_progression: |
| 162 | if item.endswith('()'): |
| 163 | key = item[:-2] |
| 164 | else: |
| 165 | key = item |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 166 | restr = r'%s(_[a-zA-Z0-9-_$(){}]+|\[[^\]]*\])?' % key |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 167 | if item.endswith('()'): |
| 168 | recipe_progression_restrs.append(restr + '()') |
| 169 | else: |
| 170 | recipe_progression_restrs.append(restr) |
| 171 | recipe_progression_res.append(re.compile('^%s$' % restr)) |
| 172 | |
| 173 | def get_recipe_pos(variable): |
| 174 | for i, p in enumerate(recipe_progression_res): |
| 175 | if p.match(variable): |
| 176 | return i |
| 177 | return -1 |
| 178 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | remainingnames = {} |
| 180 | for k in values.keys(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 181 | remainingnames[k] = get_recipe_pos(k) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 182 | remainingnames = OrderedDict(sorted(remainingnames.items(), key=lambda x: x[1])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 183 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 184 | modifying = False |
| 185 | |
| 186 | def outputvalue(name, lines, rewindcomments=False): |
| 187 | if values[name] is None: |
| 188 | return |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 189 | if isinstance(values[name], tuple): |
| 190 | op, value = values[name] |
| 191 | if op == '+=' and value.strip() == '': |
| 192 | return |
| 193 | else: |
| 194 | value = values[name] |
| 195 | op = '=' |
| 196 | rawtext = '%s %s "%s"%s' % (name, op, value, newline) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 197 | addlines = [] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 198 | nowrap = False |
| 199 | for nowrap_re in nowrap_vars_res: |
| 200 | if nowrap_re.match(name): |
| 201 | nowrap = True |
| 202 | break |
| 203 | if nowrap: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 204 | addlines.append(rawtext) |
| 205 | elif name in list_vars: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 206 | splitvalue = split_var_value(value, assignment=False) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 207 | if len(splitvalue) > 1: |
| 208 | linesplit = ' \\\n' + (' ' * (len(name) + 4)) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 209 | addlines.append('%s %s "%s%s"%s' % (name, op, linesplit.join(splitvalue), linesplit, newline)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 210 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 211 | addlines.append(rawtext) |
| 212 | else: |
| 213 | wrapped = textwrap.wrap(rawtext) |
| 214 | for wrapline in wrapped[:-1]: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 215 | addlines.append('%s \\%s' % (wrapline, newline)) |
| 216 | addlines.append('%s%s' % (wrapped[-1], newline)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 217 | |
| 218 | # Split on newlines - this isn't strictly necessary if you are only |
| 219 | # going to write the output to disk, but if you want to compare it |
| 220 | # (as patch_recipe_file() will do if patch=True) then it's important. |
| 221 | addlines = [line for l in addlines for line in l.splitlines(True)] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 222 | if rewindcomments: |
| 223 | # Ensure we insert the lines before any leading comments |
| 224 | # (that we'd want to ensure remain leading the next value) |
| 225 | for i, ln in reversed(list(enumerate(lines))): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 226 | if not ln.startswith('#'): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 227 | lines[i+1:i+1] = addlines |
| 228 | break |
| 229 | else: |
| 230 | lines.extend(addlines) |
| 231 | else: |
| 232 | lines.extend(addlines) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 233 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 234 | existingnames = [] |
| 235 | def patch_recipe_varfunc(varname, origvalue, op, newlines): |
| 236 | if modifying: |
| 237 | # Insert anything that should come before this variable |
| 238 | pos = get_recipe_pos(varname) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 239 | for k in list(remainingnames): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 240 | if remainingnames[k] > -1 and pos >= remainingnames[k] and not k in existingnames: |
| 241 | outputvalue(k, newlines, rewindcomments=True) |
| 242 | del remainingnames[k] |
| 243 | # Now change this variable, if it needs to be changed |
| 244 | if varname in existingnames and op in ['+=', '=', '=+']: |
| 245 | if varname in remainingnames: |
| 246 | outputvalue(varname, newlines) |
| 247 | del remainingnames[varname] |
| 248 | return None, None, 0, True |
| 249 | else: |
| 250 | if varname in values: |
| 251 | existingnames.append(varname) |
| 252 | return origvalue, None, 0, True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 253 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 254 | # First run - establish which values we want to set are already in the file |
| 255 | varlist = [re.escape(item) for item in values.keys()] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 256 | bb.utils.edit_metadata(fromlines, varlist, patch_recipe_varfunc) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 257 | # Second run - actually set everything |
| 258 | modifying = True |
| 259 | varlist.extend(recipe_progression_restrs) |
| 260 | changed, tolines = bb.utils.edit_metadata(fromlines, varlist, patch_recipe_varfunc, match_overrides=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 261 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 262 | if remainingnames: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 263 | if tolines and tolines[-1].strip() != '': |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 264 | tolines.append('\n') |
| 265 | for k in remainingnames.keys(): |
| 266 | outputvalue(k, tolines) |
| 267 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 268 | return changed, tolines |
| 269 | |
| 270 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 271 | def patch_recipe_file(fn, values, patch=False, relpath='', redirect_output=None): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 272 | """Update or insert variable values into a recipe file (assuming you |
| 273 | have already identified the exact file you want to update.) |
| 274 | Note that some manual inspection/intervention may be required |
| 275 | since this cannot handle all situations. |
| 276 | """ |
| 277 | |
| 278 | with open(fn, 'r') as f: |
| 279 | fromlines = f.readlines() |
| 280 | |
| 281 | _, tolines = patch_recipe_lines(fromlines, values) |
| 282 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 283 | if redirect_output: |
| 284 | with open(os.path.join(redirect_output, os.path.basename(fn)), 'w') as f: |
| 285 | f.writelines(tolines) |
| 286 | return None |
| 287 | elif patch: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | relfn = os.path.relpath(fn, relpath) |
| 289 | diff = difflib.unified_diff(fromlines, tolines, 'a/%s' % relfn, 'b/%s' % relfn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 290 | return diff |
| 291 | else: |
| 292 | with open(fn, 'w') as f: |
| 293 | f.writelines(tolines) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 294 | return None |
| 295 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 296 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 297 | def localise_file_vars(fn, varfiles, varlist): |
| 298 | """Given a list of variables and variable history (fetched with get_var_files()) |
| 299 | find where each variable should be set/changed. This handles for example where a |
| 300 | recipe includes an inc file where variables might be changed - in most cases |
| 301 | we want to update the inc file when changing the variable value rather than adding |
| 302 | it to the recipe itself. |
| 303 | """ |
| 304 | fndir = os.path.dirname(fn) + os.sep |
| 305 | |
| 306 | first_meta_file = None |
| 307 | for v in meta_vars: |
| 308 | f = varfiles.get(v, None) |
| 309 | if f: |
| 310 | actualdir = os.path.dirname(f) + os.sep |
| 311 | if actualdir.startswith(fndir): |
| 312 | first_meta_file = f |
| 313 | break |
| 314 | |
| 315 | filevars = defaultdict(list) |
| 316 | for v in varlist: |
| 317 | f = varfiles[v] |
| 318 | # Only return files that are in the same directory as the recipe or in some directory below there |
| 319 | # (this excludes bbclass files and common inc files that wouldn't be appropriate to set the variable |
| 320 | # in if we were going to set a value specific to this recipe) |
| 321 | if f: |
| 322 | actualfile = f |
| 323 | else: |
| 324 | # Variable isn't in a file, if it's one of the "meta" vars, use the first file with a meta var in it |
| 325 | if first_meta_file: |
| 326 | actualfile = first_meta_file |
| 327 | else: |
| 328 | actualfile = fn |
| 329 | |
| 330 | actualdir = os.path.dirname(actualfile) + os.sep |
| 331 | if not actualdir.startswith(fndir): |
| 332 | actualfile = fn |
| 333 | filevars[actualfile].append(v) |
| 334 | |
| 335 | return filevars |
| 336 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 337 | def patch_recipe(d, fn, varvalues, patch=False, relpath='', redirect_output=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 338 | """Modify a list of variable values in the specified recipe. Handles inc files if |
| 339 | used by the recipe. |
| 340 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 341 | overrides = d.getVar('OVERRIDES').split(':') |
| 342 | def override_applicable(hevent): |
| 343 | op = hevent['op'] |
| 344 | if '[' in op: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 345 | opoverrides = op.split('[')[1].split(']')[0].split(':') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 346 | for opoverride in opoverrides: |
| 347 | if not opoverride in overrides: |
| 348 | return False |
| 349 | return True |
| 350 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 351 | varlist = varvalues.keys() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 352 | fn = os.path.abspath(fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 353 | varfiles = get_var_files(fn, varlist, d) |
| 354 | locs = localise_file_vars(fn, varfiles, varlist) |
| 355 | patches = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 356 | for f,v in locs.items(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 357 | vals = {k: varvalues[k] for k in v} |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 358 | f = os.path.abspath(f) |
| 359 | if f == fn: |
| 360 | extravals = {} |
| 361 | for var, value in vals.items(): |
| 362 | if var in list_vars: |
| 363 | history = simplify_history(d.varhistory.variable(var), d) |
| 364 | recipe_set = False |
| 365 | for event in history: |
| 366 | if os.path.abspath(event['file']) == fn: |
| 367 | if event['op'] == 'set': |
| 368 | recipe_set = True |
| 369 | if not recipe_set: |
| 370 | for event in history: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 371 | if event['op'].startswith(':remove'): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 372 | continue |
| 373 | if not override_applicable(event): |
| 374 | continue |
| 375 | newvalue = value.replace(event['detail'], '') |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 376 | if newvalue == value and os.path.abspath(event['file']) == fn and event['op'].startswith(':'): |
| 377 | op = event['op'].replace('[', ':').replace(']', '') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 378 | extravals[var + op] = None |
| 379 | value = newvalue |
| 380 | vals[var] = ('+=', value) |
| 381 | vals.update(extravals) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 382 | patchdata = patch_recipe_file(f, vals, patch, relpath, redirect_output) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | if patch: |
| 384 | patches.append(patchdata) |
| 385 | |
| 386 | if patch: |
| 387 | return patches |
| 388 | else: |
| 389 | return None |
| 390 | |
| 391 | |
| 392 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 393 | def copy_recipe_files(d, tgt_dir, whole_dir=False, download=True, all_variants=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 394 | """Copy (local) recipe files, including both files included via include/require, |
| 395 | and files referred to in the SRC_URI variable.""" |
| 396 | import bb.fetch2 |
| 397 | import oe.path |
| 398 | |
| 399 | # FIXME need a warning if the unexpanded SRC_URI value contains variable references |
| 400 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 401 | uri_values = [] |
| 402 | localpaths = [] |
| 403 | def fetch_urls(rdata): |
| 404 | # Collect the local paths from SRC_URI |
| 405 | srcuri = rdata.getVar('SRC_URI') or "" |
| 406 | if srcuri not in uri_values: |
| 407 | fetch = bb.fetch2.Fetch(srcuri.split(), rdata) |
| 408 | if download: |
| 409 | fetch.download() |
| 410 | for pth in fetch.localpaths(): |
| 411 | if pth not in localpaths: |
Andrew Geissler | 9b4d8b0 | 2021-02-19 12:26:16 -0600 | [diff] [blame] | 412 | localpaths.append(os.path.abspath(pth)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 413 | uri_values.append(srcuri) |
| 414 | |
| 415 | fetch_urls(d) |
| 416 | if all_variants: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 417 | # Get files for other variants e.g. in the case of a SRC_URI:append |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 418 | localdata = bb.data.createCopy(d) |
| 419 | variants = (localdata.getVar('BBCLASSEXTEND') or '').split() |
| 420 | if variants: |
| 421 | # Ensure we handle class-target if we're dealing with one of the variants |
| 422 | variants.append('target') |
| 423 | for variant in variants: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 424 | if variant.startswith("devupstream"): |
| 425 | localdata.setVar('SRCPV', 'git') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 426 | localdata.setVar('CLASSOVERRIDE', 'class-%s' % variant) |
| 427 | fetch_urls(localdata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 428 | |
| 429 | # Copy local files to target directory and gather any remote files |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 430 | bb_dir = os.path.abspath(os.path.dirname(d.getVar('FILE'))) + os.sep |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 431 | remotes = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 432 | copied = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 433 | # Need to do this in two steps since we want to check against the absolute path |
| 434 | includes = [os.path.abspath(path) for path in d.getVar('BBINCLUDED').split() if os.path.exists(path)] |
| 435 | # We also check this below, but we don't want any items in this list being considered remotes |
| 436 | includes = [path for path in includes if path.startswith(bb_dir)] |
| 437 | for path in localpaths + includes: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 438 | # Only import files that are under the meta directory |
| 439 | if path.startswith(bb_dir): |
| 440 | if not whole_dir: |
| 441 | relpath = os.path.relpath(path, bb_dir) |
| 442 | subdir = os.path.join(tgt_dir, os.path.dirname(relpath)) |
| 443 | if not os.path.exists(subdir): |
| 444 | os.makedirs(subdir) |
| 445 | shutil.copy2(path, os.path.join(tgt_dir, relpath)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 446 | copied.append(relpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 447 | else: |
| 448 | remotes.append(path) |
| 449 | # Simply copy whole meta dir, if requested |
| 450 | if whole_dir: |
| 451 | shutil.copytree(bb_dir, tgt_dir) |
| 452 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 453 | return copied, remotes |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 454 | |
| 455 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 456 | def get_recipe_local_files(d, patches=False, archives=False): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 457 | """Get a list of local files in SRC_URI within a recipe.""" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 458 | import oe.patch |
| 459 | uris = (d.getVar('SRC_URI') or "").split() |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 460 | fetch = bb.fetch2.Fetch(uris, d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 461 | # FIXME this list should be factored out somewhere else (such as the |
| 462 | # fetcher) though note that this only encompasses actual container formats |
| 463 | # i.e. that can contain multiple files as opposed to those that only |
| 464 | # contain a compressed stream (i.e. .tar.gz as opposed to just .gz) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 465 | archive_exts = ['.tar', '.tgz', '.tar.gz', '.tar.Z', '.tbz', '.tbz2', '.tar.bz2', '.txz', '.tar.xz', '.tar.lz', '.zip', '.jar', '.rpm', '.srpm', '.deb', '.ipk', '.tar.7z', '.7z'] |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 466 | ret = {} |
| 467 | for uri in uris: |
| 468 | if fetch.ud[uri].type == 'file': |
| 469 | if (not patches and |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 470 | oe.patch.patch_path(uri, fetch, '', expand=False)): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 471 | continue |
| 472 | # Skip files that are referenced by absolute path |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 473 | fname = fetch.ud[uri].basepath |
| 474 | if os.path.isabs(fname): |
| 475 | continue |
| 476 | # Handle subdir= |
| 477 | subdir = fetch.ud[uri].parm.get('subdir', '') |
| 478 | if subdir: |
| 479 | if os.path.isabs(subdir): |
| 480 | continue |
| 481 | fname = os.path.join(subdir, fname) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 482 | localpath = fetch.localpath(uri) |
| 483 | if not archives: |
| 484 | # Ignore archives that will be unpacked |
| 485 | if localpath.endswith(tuple(archive_exts)): |
| 486 | unpack = fetch.ud[uri].parm.get('unpack', True) |
| 487 | if unpack: |
| 488 | continue |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 489 | if os.path.isdir(localpath): |
| 490 | for root, dirs, files in os.walk(localpath): |
| 491 | for fname in files: |
| 492 | fileabspath = os.path.join(root,fname) |
| 493 | srcdir = os.path.dirname(localpath) |
| 494 | ret[os.path.relpath(fileabspath,srcdir)] = fileabspath |
| 495 | else: |
| 496 | ret[fname] = localpath |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 497 | return ret |
| 498 | |
| 499 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 500 | def get_recipe_patches(d): |
| 501 | """Get a list of the patches included in SRC_URI within a recipe.""" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 502 | import oe.patch |
| 503 | patches = oe.patch.src_patches(d, expand=False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 504 | patchfiles = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 505 | for patch in patches: |
| 506 | _, _, local, _, _, parm = bb.fetch.decodeurl(patch) |
| 507 | patchfiles.append(local) |
| 508 | return patchfiles |
| 509 | |
| 510 | |
| 511 | def get_recipe_patched_files(d): |
| 512 | """ |
| 513 | Get the list of patches for a recipe along with the files each patch modifies. |
| 514 | Params: |
| 515 | d: the datastore for the recipe |
| 516 | Returns: |
| 517 | a dict mapping patch file path to a list of tuples of changed files and |
| 518 | change mode ('A' for add, 'D' for delete or 'M' for modify) |
| 519 | """ |
| 520 | import oe.patch |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 521 | patches = oe.patch.src_patches(d, expand=False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 522 | patchedfiles = {} |
| 523 | for patch in patches: |
| 524 | _, _, patchfile, _, _, parm = bb.fetch.decodeurl(patch) |
| 525 | striplevel = int(parm['striplevel']) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 526 | patchedfiles[patchfile] = oe.patch.PatchSet.getPatchedFiles(patchfile, striplevel, os.path.join(d.getVar('S'), parm.get('patchdir', ''))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 527 | return patchedfiles |
| 528 | |
| 529 | |
| 530 | def validate_pn(pn): |
| 531 | """Perform validation on a recipe name (PN) for a new recipe.""" |
| 532 | reserved_names = ['forcevariable', 'append', 'prepend', 'remove'] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 533 | if not re.match('^[0-9a-z-.+]+$', pn): |
| 534 | return 'Recipe name "%s" is invalid: only characters 0-9, a-z, -, + and . are allowed' % pn |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 535 | elif pn in reserved_names: |
| 536 | return 'Recipe name "%s" is invalid: is a reserved keyword' % pn |
| 537 | elif pn.startswith('pn-'): |
| 538 | return 'Recipe name "%s" is invalid: names starting with "pn-" are reserved' % pn |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 539 | elif pn.endswith(('.bb', '.bbappend', '.bbclass', '.inc', '.conf')): |
| 540 | return 'Recipe name "%s" is invalid: should be just a name, not a file name' % pn |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 541 | return '' |
| 542 | |
| 543 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 544 | def get_bbfile_path(d, destdir, extrapathhint=None): |
| 545 | """ |
| 546 | Determine the correct path for a recipe within a layer |
| 547 | Parameters: |
| 548 | d: Recipe-specific datastore |
| 549 | destdir: destination directory. Can be the path to the base of the layer or a |
| 550 | partial path somewhere within the layer. |
| 551 | extrapathhint: a path relative to the base of the layer to try |
| 552 | """ |
| 553 | import bb.cookerdata |
| 554 | |
| 555 | destdir = os.path.abspath(destdir) |
| 556 | destlayerdir = find_layerdir(destdir) |
| 557 | |
| 558 | # Parse the specified layer's layer.conf file directly, in case the layer isn't in bblayers.conf |
| 559 | confdata = d.createCopy() |
| 560 | confdata.setVar('BBFILES', '') |
| 561 | confdata.setVar('LAYERDIR', destlayerdir) |
| 562 | destlayerconf = os.path.join(destlayerdir, "conf", "layer.conf") |
| 563 | confdata = bb.cookerdata.parse_config_file(destlayerconf, confdata) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 564 | pn = d.getVar('PN') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 565 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 566 | # Parse BBFILES_DYNAMIC and append to BBFILES |
| 567 | bbfiles_dynamic = (confdata.getVar('BBFILES_DYNAMIC') or "").split() |
| 568 | collections = (confdata.getVar('BBFILE_COLLECTIONS') or "").split() |
| 569 | invalid = [] |
| 570 | for entry in bbfiles_dynamic: |
| 571 | parts = entry.split(":", 1) |
| 572 | if len(parts) != 2: |
| 573 | invalid.append(entry) |
| 574 | continue |
| 575 | l, f = parts |
| 576 | invert = l[0] == "!" |
| 577 | if invert: |
| 578 | l = l[1:] |
| 579 | if (l in collections and not invert) or (l not in collections and invert): |
| 580 | confdata.appendVar("BBFILES", " " + f) |
| 581 | if invalid: |
| 582 | return None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 583 | bbfilespecs = (confdata.getVar('BBFILES') or '').split() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 584 | if destdir == destlayerdir: |
| 585 | for bbfilespec in bbfilespecs: |
| 586 | if not bbfilespec.endswith('.bbappend'): |
| 587 | for match in glob.glob(bbfilespec): |
| 588 | splitext = os.path.splitext(os.path.basename(match)) |
| 589 | if splitext[1] == '.bb': |
| 590 | mpn = splitext[0].split('_')[0] |
| 591 | if mpn == pn: |
| 592 | return os.path.dirname(match) |
| 593 | |
| 594 | # Try to make up a path that matches BBFILES |
| 595 | # this is a little crude, but better than nothing |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 596 | bpn = d.getVar('BPN') |
| 597 | recipefn = os.path.basename(d.getVar('FILE')) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 598 | pathoptions = [destdir] |
| 599 | if extrapathhint: |
| 600 | pathoptions.append(os.path.join(destdir, extrapathhint)) |
| 601 | if destdir == destlayerdir: |
| 602 | pathoptions.append(os.path.join(destdir, 'recipes-%s' % bpn, bpn)) |
| 603 | pathoptions.append(os.path.join(destdir, 'recipes', bpn)) |
| 604 | pathoptions.append(os.path.join(destdir, bpn)) |
| 605 | elif not destdir.endswith(('/' + pn, '/' + bpn)): |
| 606 | pathoptions.append(os.path.join(destdir, bpn)) |
| 607 | closepath = '' |
| 608 | for pathoption in pathoptions: |
| 609 | bbfilepath = os.path.join(pathoption, 'test.bb') |
| 610 | for bbfilespec in bbfilespecs: |
| 611 | if fnmatch.fnmatchcase(bbfilepath, bbfilespec): |
| 612 | return pathoption |
| 613 | return None |
| 614 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 615 | def get_bbappend_path(d, destlayerdir, wildcardver=False): |
| 616 | """Determine how a bbappend for a recipe should be named and located within another layer""" |
| 617 | |
| 618 | import bb.cookerdata |
| 619 | |
| 620 | destlayerdir = os.path.abspath(destlayerdir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 621 | recipefile = d.getVar('FILE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 622 | recipefn = os.path.splitext(os.path.basename(recipefile))[0] |
| 623 | if wildcardver and '_' in recipefn: |
| 624 | recipefn = recipefn.split('_', 1)[0] + '_%' |
| 625 | appendfn = recipefn + '.bbappend' |
| 626 | |
| 627 | # Parse the specified layer's layer.conf file directly, in case the layer isn't in bblayers.conf |
| 628 | confdata = d.createCopy() |
| 629 | confdata.setVar('BBFILES', '') |
| 630 | confdata.setVar('LAYERDIR', destlayerdir) |
| 631 | destlayerconf = os.path.join(destlayerdir, "conf", "layer.conf") |
| 632 | confdata = bb.cookerdata.parse_config_file(destlayerconf, confdata) |
| 633 | |
| 634 | origlayerdir = find_layerdir(recipefile) |
| 635 | if not origlayerdir: |
| 636 | return (None, False) |
| 637 | # Now join this to the path where the bbappend is going and check if it is covered by BBFILES |
| 638 | appendpath = os.path.join(destlayerdir, os.path.relpath(os.path.dirname(recipefile), origlayerdir), appendfn) |
| 639 | closepath = '' |
| 640 | pathok = True |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 641 | for bbfilespec in confdata.getVar('BBFILES').split(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 642 | if fnmatch.fnmatchcase(appendpath, bbfilespec): |
| 643 | # Our append path works, we're done |
| 644 | break |
| 645 | elif bbfilespec.startswith(destlayerdir) and fnmatch.fnmatchcase('test.bbappend', os.path.basename(bbfilespec)): |
| 646 | # Try to find the longest matching path |
| 647 | if len(bbfilespec) > len(closepath): |
| 648 | closepath = bbfilespec |
| 649 | else: |
| 650 | # Unfortunately the bbappend layer and the original recipe's layer don't have the same structure |
| 651 | if closepath: |
| 652 | # bbappend layer's layer.conf at least has a spec that picks up .bbappend files |
| 653 | # Now we just need to substitute out any wildcards |
| 654 | appendsubdir = os.path.relpath(os.path.dirname(closepath), destlayerdir) |
| 655 | if 'recipes-*' in appendsubdir: |
| 656 | # Try to copy this part from the original recipe path |
| 657 | res = re.search('/recipes-[^/]+/', recipefile) |
| 658 | if res: |
| 659 | appendsubdir = appendsubdir.replace('/recipes-*/', res.group(0)) |
| 660 | # This is crude, but we have to do something |
| 661 | appendsubdir = appendsubdir.replace('*', recipefn.split('_')[0]) |
| 662 | appendsubdir = appendsubdir.replace('?', 'a') |
| 663 | appendpath = os.path.join(destlayerdir, appendsubdir, appendfn) |
| 664 | else: |
| 665 | pathok = False |
| 666 | return (appendpath, pathok) |
| 667 | |
| 668 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 669 | def bbappend_recipe(rd, destlayerdir, srcfiles, install=None, wildcardver=False, machine=None, extralines=None, removevalues=None, redirect_output=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 670 | """ |
| 671 | Writes a bbappend file for a recipe |
| 672 | Parameters: |
| 673 | rd: data dictionary for the recipe |
| 674 | destlayerdir: base directory of the layer to place the bbappend in |
| 675 | (subdirectory path from there will be determined automatically) |
| 676 | srcfiles: dict of source files to add to SRC_URI, where the value |
| 677 | is the full path to the file to be added, and the value is the |
| 678 | original filename as it would appear in SRC_URI or None if it |
| 679 | isn't already present. You may pass None for this parameter if |
| 680 | you simply want to specify your own content via the extralines |
| 681 | parameter. |
| 682 | install: dict mapping entries in srcfiles to a tuple of two elements: |
| 683 | install path (*without* ${D} prefix) and permission value (as a |
| 684 | string, e.g. '0644'). |
| 685 | wildcardver: True to use a % wildcard in the bbappend filename, or |
| 686 | False to make the bbappend specific to the recipe version. |
| 687 | machine: |
| 688 | If specified, make the changes in the bbappend specific to this |
| 689 | machine. This will also cause PACKAGE_ARCH = "${MACHINE_ARCH}" |
| 690 | to be added to the bbappend. |
| 691 | extralines: |
| 692 | Extra lines to add to the bbappend. This may be a dict of name |
| 693 | value pairs, or simply a list of the lines. |
| 694 | removevalues: |
| 695 | Variable values to remove - a dict of names/values. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 696 | redirect_output: |
| 697 | If specified, redirects writing the output file to the |
| 698 | specified directory (for dry-run purposes) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 699 | """ |
| 700 | |
| 701 | if not removevalues: |
| 702 | removevalues = {} |
| 703 | |
| 704 | # Determine how the bbappend should be named |
| 705 | appendpath, pathok = get_bbappend_path(rd, destlayerdir, wildcardver) |
| 706 | if not appendpath: |
| 707 | bb.error('Unable to determine layer directory containing %s' % recipefile) |
| 708 | return (None, None) |
| 709 | if not pathok: |
| 710 | bb.warn('Unable to determine correct subdirectory path for bbappend file - check that what %s adds to BBFILES also matches .bbappend files. Using %s for now, but until you fix this the bbappend will not be applied.' % (os.path.join(destlayerdir, 'conf', 'layer.conf'), os.path.dirname(appendpath))) |
| 711 | |
| 712 | appenddir = os.path.dirname(appendpath) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 713 | if not redirect_output: |
| 714 | bb.utils.mkdirhier(appenddir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 715 | |
| 716 | # FIXME check if the bbappend doesn't get overridden by a higher priority layer? |
| 717 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 718 | layerdirs = [os.path.abspath(layerdir) for layerdir in rd.getVar('BBLAYERS').split()] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 719 | if not os.path.abspath(destlayerdir) in layerdirs: |
| 720 | bb.warn('Specified layer is not currently enabled in bblayers.conf, you will need to add it before this bbappend will be active') |
| 721 | |
| 722 | bbappendlines = [] |
| 723 | if extralines: |
| 724 | if isinstance(extralines, dict): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 725 | for name, value in extralines.items(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 726 | bbappendlines.append((name, '=', value)) |
| 727 | else: |
| 728 | # Do our best to split it |
| 729 | for line in extralines: |
| 730 | if line[-1] == '\n': |
| 731 | line = line[:-1] |
| 732 | splitline = line.split(None, 2) |
| 733 | if len(splitline) == 3: |
| 734 | bbappendlines.append(tuple(splitline)) |
| 735 | else: |
| 736 | raise Exception('Invalid extralines value passed') |
| 737 | |
| 738 | def popline(varname): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 739 | for i in range(0, len(bbappendlines)): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 740 | if bbappendlines[i][0] == varname: |
| 741 | line = bbappendlines.pop(i) |
| 742 | return line |
| 743 | return None |
| 744 | |
| 745 | def appendline(varname, op, value): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 746 | for i in range(0, len(bbappendlines)): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 747 | item = bbappendlines[i] |
| 748 | if item[0] == varname: |
| 749 | bbappendlines[i] = (item[0], item[1], item[2] + ' ' + value) |
| 750 | break |
| 751 | else: |
| 752 | bbappendlines.append((varname, op, value)) |
| 753 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 754 | destsubdir = rd.getVar('PN') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 755 | if srcfiles: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 756 | bbappendlines.append(('FILESEXTRAPATHS:prepend', ':=', '${THISDIR}/${PN}:')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 757 | |
| 758 | appendoverride = '' |
| 759 | if machine: |
| 760 | bbappendlines.append(('PACKAGE_ARCH', '=', '${MACHINE_ARCH}')) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 761 | appendoverride = ':%s' % machine |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 762 | copyfiles = {} |
| 763 | if srcfiles: |
| 764 | instfunclines = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 765 | for newfile, origsrcfile in srcfiles.items(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 766 | srcfile = origsrcfile |
| 767 | srcurientry = None |
| 768 | if not srcfile: |
| 769 | srcfile = os.path.basename(newfile) |
| 770 | srcurientry = 'file://%s' % srcfile |
| 771 | # Double-check it's not there already |
| 772 | # FIXME do we care if the entry is added by another bbappend that might go away? |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 773 | if not srcurientry in rd.getVar('SRC_URI').split(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 774 | if machine: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 775 | appendline('SRC_URI:append%s' % appendoverride, '=', ' ' + srcurientry) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 776 | else: |
| 777 | appendline('SRC_URI', '+=', srcurientry) |
| 778 | copyfiles[newfile] = srcfile |
| 779 | if install: |
| 780 | institem = install.pop(newfile, None) |
| 781 | if institem: |
| 782 | (destpath, perms) = institem |
| 783 | instdestpath = replace_dir_vars(destpath, rd) |
| 784 | instdirline = 'install -d ${D}%s' % os.path.dirname(instdestpath) |
| 785 | if not instdirline in instfunclines: |
| 786 | instfunclines.append(instdirline) |
| 787 | instfunclines.append('install -m %s ${WORKDIR}/%s ${D}%s' % (perms, os.path.basename(srcfile), instdestpath)) |
| 788 | if instfunclines: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 789 | bbappendlines.append(('do_install:append%s()' % appendoverride, '', instfunclines)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 790 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 791 | if redirect_output: |
| 792 | bb.note('Writing append file %s (dry-run)' % appendpath) |
| 793 | outfile = os.path.join(redirect_output, os.path.basename(appendpath)) |
| 794 | # Only take a copy if the file isn't already there (this function may be called |
| 795 | # multiple times per operation when we're handling overrides) |
| 796 | if os.path.exists(appendpath) and not os.path.exists(outfile): |
| 797 | shutil.copy2(appendpath, outfile) |
| 798 | else: |
| 799 | bb.note('Writing append file %s' % appendpath) |
| 800 | outfile = appendpath |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 801 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 802 | if os.path.exists(outfile): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 803 | # Work around lack of nonlocal in python 2 |
| 804 | extvars = {'destsubdir': destsubdir} |
| 805 | |
| 806 | def appendfile_varfunc(varname, origvalue, op, newlines): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 807 | if varname == 'FILESEXTRAPATHS:prepend': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 808 | if origvalue.startswith('${THISDIR}/'): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 809 | popline('FILESEXTRAPATHS:prepend') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 810 | extvars['destsubdir'] = rd.expand(origvalue.split('${THISDIR}/', 1)[1].rstrip(':')) |
| 811 | elif varname == 'PACKAGE_ARCH': |
| 812 | if machine: |
| 813 | popline('PACKAGE_ARCH') |
| 814 | return (machine, None, 4, False) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 815 | elif varname.startswith('do_install:append'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 816 | func = popline(varname) |
| 817 | if func: |
| 818 | instfunclines = [line.strip() for line in origvalue.strip('\n').splitlines()] |
| 819 | for line in func[2]: |
| 820 | if not line in instfunclines: |
| 821 | instfunclines.append(line) |
| 822 | return (instfunclines, None, 4, False) |
| 823 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 824 | splitval = split_var_value(origvalue, assignment=False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 825 | changed = False |
| 826 | removevar = varname |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 827 | if varname in ['SRC_URI', 'SRC_URI:append%s' % appendoverride]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 828 | removevar = 'SRC_URI' |
| 829 | line = popline(varname) |
| 830 | if line: |
| 831 | if line[2] not in splitval: |
| 832 | splitval.append(line[2]) |
| 833 | changed = True |
| 834 | else: |
| 835 | line = popline(varname) |
| 836 | if line: |
| 837 | splitval = [line[2]] |
| 838 | changed = True |
| 839 | |
| 840 | if removevar in removevalues: |
| 841 | remove = removevalues[removevar] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 842 | if isinstance(remove, str): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 843 | if remove in splitval: |
| 844 | splitval.remove(remove) |
| 845 | changed = True |
| 846 | else: |
| 847 | for removeitem in remove: |
| 848 | if removeitem in splitval: |
| 849 | splitval.remove(removeitem) |
| 850 | changed = True |
| 851 | |
| 852 | if changed: |
| 853 | newvalue = splitval |
| 854 | if len(newvalue) == 1: |
| 855 | # Ensure it's written out as one line |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 856 | if ':append' in varname: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 857 | newvalue = ' ' + newvalue[0] |
| 858 | else: |
| 859 | newvalue = newvalue[0] |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 860 | if not newvalue and (op in ['+=', '.='] or ':append' in varname): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 861 | # There's no point appending nothing |
| 862 | newvalue = None |
| 863 | if varname.endswith('()'): |
| 864 | indent = 4 |
| 865 | else: |
| 866 | indent = -1 |
| 867 | return (newvalue, None, indent, True) |
| 868 | return (origvalue, None, 4, False) |
| 869 | |
| 870 | varnames = [item[0] for item in bbappendlines] |
| 871 | if removevalues: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 872 | varnames.extend(list(removevalues.keys())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 873 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 874 | with open(outfile, 'r') as f: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 875 | (updated, newlines) = bb.utils.edit_metadata(f, varnames, appendfile_varfunc) |
| 876 | |
| 877 | destsubdir = extvars['destsubdir'] |
| 878 | else: |
| 879 | updated = False |
| 880 | newlines = [] |
| 881 | |
| 882 | if bbappendlines: |
| 883 | for line in bbappendlines: |
| 884 | if line[0].endswith('()'): |
| 885 | newlines.append('%s {\n %s\n}\n' % (line[0], '\n '.join(line[2]))) |
| 886 | else: |
| 887 | newlines.append('%s %s "%s"\n\n' % line) |
| 888 | updated = True |
| 889 | |
| 890 | if updated: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 891 | with open(outfile, 'w') as f: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 892 | f.writelines(newlines) |
| 893 | |
| 894 | if copyfiles: |
| 895 | if machine: |
| 896 | destsubdir = os.path.join(destsubdir, machine) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 897 | if redirect_output: |
| 898 | outdir = redirect_output |
| 899 | else: |
| 900 | outdir = appenddir |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 901 | for newfile, srcfile in copyfiles.items(): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 902 | filedest = os.path.join(outdir, destsubdir, os.path.basename(srcfile)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 903 | if os.path.abspath(newfile) != os.path.abspath(filedest): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 904 | if newfile.startswith(tempfile.gettempdir()): |
| 905 | newfiledisp = os.path.basename(newfile) |
| 906 | else: |
| 907 | newfiledisp = newfile |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 908 | if redirect_output: |
| 909 | bb.note('Copying %s to %s (dry-run)' % (newfiledisp, os.path.join(appenddir, destsubdir, os.path.basename(srcfile)))) |
| 910 | else: |
| 911 | bb.note('Copying %s to %s' % (newfiledisp, filedest)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 912 | bb.utils.mkdirhier(os.path.dirname(filedest)) |
| 913 | shutil.copyfile(newfile, filedest) |
| 914 | |
| 915 | return (appendpath, os.path.join(appenddir, destsubdir)) |
| 916 | |
| 917 | |
| 918 | def find_layerdir(fn): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 919 | """ Figure out the path to the base of the layer containing a file (e.g. a recipe)""" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 920 | pth = os.path.abspath(fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 921 | layerdir = '' |
| 922 | while pth: |
| 923 | if os.path.exists(os.path.join(pth, 'conf', 'layer.conf')): |
| 924 | layerdir = pth |
| 925 | break |
| 926 | pth = os.path.dirname(pth) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 927 | if pth == '/': |
| 928 | return None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 929 | return layerdir |
| 930 | |
| 931 | |
| 932 | def replace_dir_vars(path, d): |
| 933 | """Replace common directory paths with appropriate variable references (e.g. /etc becomes ${sysconfdir})""" |
| 934 | dirvars = {} |
| 935 | # Sort by length so we get the variables we're interested in first |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 936 | for var in sorted(list(d.keys()), key=len): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 937 | if var.endswith('dir') and var.lower() == var: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 938 | value = d.getVar(var) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 939 | if value.startswith('/') and not '\n' in value and value not in dirvars: |
| 940 | dirvars[value] = var |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 941 | for dirpath in sorted(list(dirvars.keys()), reverse=True): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 942 | path = path.replace(dirpath, '${%s}' % dirvars[dirpath]) |
| 943 | return path |
| 944 | |
| 945 | def get_recipe_pv_without_srcpv(pv, uri_type): |
| 946 | """ |
| 947 | Get PV without SRCPV common in SCM's for now only |
| 948 | support git. |
| 949 | |
| 950 | Returns tuple with pv, prefix and suffix. |
| 951 | """ |
| 952 | pfx = '' |
| 953 | sfx = '' |
| 954 | |
| 955 | if uri_type == 'git': |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 956 | git_regex = re.compile(r"(?P<pfx>v?)(?P<ver>.*?)(?P<sfx>\+[^\+]*(git)?r?(AUTOINC\+))(?P<rev>.*)") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 957 | m = git_regex.match(pv) |
| 958 | |
| 959 | if m: |
| 960 | pv = m.group('ver') |
| 961 | pfx = m.group('pfx') |
| 962 | sfx = m.group('sfx') |
| 963 | else: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 964 | regex = re.compile(r"(?P<pfx>(v|r)?)(?P<ver>.*)") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 965 | m = regex.match(pv) |
| 966 | if m: |
| 967 | pv = m.group('ver') |
| 968 | pfx = m.group('pfx') |
| 969 | |
| 970 | return (pv, pfx, sfx) |
| 971 | |
| 972 | def get_recipe_upstream_version(rd): |
| 973 | """ |
| 974 | Get upstream version of recipe using bb.fetch2 methods with support for |
| 975 | http, https, ftp and git. |
| 976 | |
| 977 | bb.fetch2 exceptions can be raised, |
| 978 | FetchError when don't have network access or upstream site don't response. |
| 979 | NoMethodError when uri latest_versionstring method isn't implemented. |
| 980 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 981 | Returns a dictonary with version, repository revision, current_version, type and datetime. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 982 | Type can be A for Automatic, M for Manual and U for Unknown. |
| 983 | """ |
| 984 | from bb.fetch2 import decodeurl |
| 985 | from datetime import datetime |
| 986 | |
| 987 | ru = {} |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 988 | ru['current_version'] = rd.getVar('PV') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 989 | ru['version'] = '' |
| 990 | ru['type'] = 'U' |
| 991 | ru['datetime'] = '' |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 992 | ru['revision'] = '' |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 993 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 994 | # XXX: If don't have SRC_URI means that don't have upstream sources so |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 995 | # returns the current recipe version, so that upstream version check |
| 996 | # declares a match. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 997 | src_uris = rd.getVar('SRC_URI') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 998 | if not src_uris: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 999 | ru['version'] = ru['current_version'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1000 | ru['type'] = 'M' |
| 1001 | ru['datetime'] = datetime.now() |
| 1002 | return ru |
| 1003 | |
| 1004 | # XXX: we suppose that the first entry points to the upstream sources |
| 1005 | src_uri = src_uris.split()[0] |
| 1006 | uri_type, _, _, _, _, _ = decodeurl(src_uri) |
| 1007 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1008 | (pv, pfx, sfx) = get_recipe_pv_without_srcpv(rd.getVar('PV'), uri_type) |
| 1009 | ru['current_version'] = pv |
| 1010 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1011 | manual_upstream_version = rd.getVar("RECIPE_UPSTREAM_VERSION") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1012 | if manual_upstream_version: |
| 1013 | # manual tracking of upstream version. |
| 1014 | ru['version'] = manual_upstream_version |
| 1015 | ru['type'] = 'M' |
| 1016 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1017 | manual_upstream_date = rd.getVar("CHECK_DATE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1018 | if manual_upstream_date: |
| 1019 | date = datetime.strptime(manual_upstream_date, "%b %d, %Y") |
| 1020 | else: |
| 1021 | date = datetime.now() |
| 1022 | ru['datetime'] = date |
| 1023 | |
| 1024 | elif uri_type == "file": |
| 1025 | # files are always up-to-date |
| 1026 | ru['version'] = pv |
| 1027 | ru['type'] = 'A' |
| 1028 | ru['datetime'] = datetime.now() |
| 1029 | else: |
| 1030 | ud = bb.fetch2.FetchData(src_uri, rd) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1031 | if rd.getVar("UPSTREAM_CHECK_COMMITS") == "1": |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1032 | bb.fetch2.get_srcrev(rd) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1033 | revision = ud.method.latest_revision(ud, rd, 'default') |
| 1034 | upversion = pv |
| 1035 | if revision != rd.getVar("SRCREV"): |
| 1036 | upversion = upversion + "-new-commits-available" |
| 1037 | else: |
| 1038 | pupver = ud.method.latest_versionstring(ud, rd) |
| 1039 | (upversion, revision) = pupver |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1040 | |
| 1041 | if upversion: |
| 1042 | ru['version'] = upversion |
| 1043 | ru['type'] = 'A' |
| 1044 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1045 | if revision: |
| 1046 | ru['revision'] = revision |
| 1047 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1048 | ru['datetime'] = datetime.now() |
| 1049 | |
| 1050 | return ru |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1051 | |
| 1052 | def _get_recipe_upgrade_status(data): |
| 1053 | uv = get_recipe_upstream_version(data) |
| 1054 | |
| 1055 | pn = data.getVar('PN') |
| 1056 | cur_ver = uv['current_version'] |
| 1057 | |
| 1058 | upstream_version_unknown = data.getVar('UPSTREAM_VERSION_UNKNOWN') |
| 1059 | if not uv['version']: |
| 1060 | status = "UNKNOWN" if upstream_version_unknown else "UNKNOWN_BROKEN" |
| 1061 | else: |
| 1062 | cmp = vercmp_string(uv['current_version'], uv['version']) |
| 1063 | if cmp == -1: |
| 1064 | status = "UPDATE" if not upstream_version_unknown else "KNOWN_BROKEN" |
| 1065 | elif cmp == 0: |
| 1066 | status = "MATCH" if not upstream_version_unknown else "KNOWN_BROKEN" |
| 1067 | else: |
| 1068 | status = "UNKNOWN" if upstream_version_unknown else "UNKNOWN_BROKEN" |
| 1069 | |
| 1070 | next_ver = uv['version'] if uv['version'] else "N/A" |
| 1071 | revision = uv['revision'] if uv['revision'] else "N/A" |
| 1072 | maintainer = data.getVar('RECIPE_MAINTAINER') |
| 1073 | no_upgrade_reason = data.getVar('RECIPE_NO_UPDATE_REASON') |
| 1074 | |
| 1075 | return (pn, status, cur_ver, next_ver, maintainer, revision, no_upgrade_reason) |
| 1076 | |
| 1077 | def get_recipe_upgrade_status(recipes=None): |
| 1078 | pkgs_list = [] |
| 1079 | data_copy_list = [] |
| 1080 | copy_vars = ('SRC_URI', |
| 1081 | 'PV', |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1082 | 'DL_DIR', |
| 1083 | 'PN', |
| 1084 | 'CACHE', |
| 1085 | 'PERSISTENT_DIR', |
| 1086 | 'BB_URI_HEADREVS', |
| 1087 | 'UPSTREAM_CHECK_COMMITS', |
| 1088 | 'UPSTREAM_CHECK_GITTAGREGEX', |
| 1089 | 'UPSTREAM_CHECK_REGEX', |
| 1090 | 'UPSTREAM_CHECK_URI', |
| 1091 | 'UPSTREAM_VERSION_UNKNOWN', |
| 1092 | 'RECIPE_MAINTAINER', |
| 1093 | 'RECIPE_NO_UPDATE_REASON', |
| 1094 | 'RECIPE_UPSTREAM_VERSION', |
| 1095 | 'RECIPE_UPSTREAM_DATE', |
| 1096 | 'CHECK_DATE', |
Andrew Geissler | 1e34c2d | 2020-05-29 16:02:59 -0500 | [diff] [blame] | 1097 | 'FETCHCMD_bzr', |
| 1098 | 'FETCHCMD_ccrc', |
| 1099 | 'FETCHCMD_cvs', |
| 1100 | 'FETCHCMD_git', |
| 1101 | 'FETCHCMD_hg', |
| 1102 | 'FETCHCMD_npm', |
| 1103 | 'FETCHCMD_osc', |
| 1104 | 'FETCHCMD_p4', |
| 1105 | 'FETCHCMD_repo', |
| 1106 | 'FETCHCMD_s3', |
| 1107 | 'FETCHCMD_svn', |
| 1108 | 'FETCHCMD_wget', |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1109 | ) |
| 1110 | |
| 1111 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 1112 | tinfoil.prepare(config_only=False) |
| 1113 | |
| 1114 | if not recipes: |
| 1115 | recipes = tinfoil.all_recipe_files(variants=False) |
| 1116 | |
| 1117 | for fn in recipes: |
| 1118 | try: |
| 1119 | if fn.startswith("/"): |
| 1120 | data = tinfoil.parse_recipe_file(fn) |
| 1121 | else: |
| 1122 | data = tinfoil.parse_recipe(fn) |
| 1123 | except bb.providers.NoProvider: |
| 1124 | bb.note(" No provider for %s" % fn) |
| 1125 | continue |
| 1126 | |
| 1127 | unreliable = data.getVar('UPSTREAM_CHECK_UNRELIABLE') |
| 1128 | if unreliable == "1": |
| 1129 | bb.note(" Skip package %s as upstream check unreliable" % pn) |
| 1130 | continue |
| 1131 | |
| 1132 | data_copy = bb.data.init() |
| 1133 | for var in copy_vars: |
| 1134 | data_copy.setVar(var, data.getVar(var)) |
| 1135 | for k in data: |
| 1136 | if k.startswith('SRCREV'): |
| 1137 | data_copy.setVar(k, data.getVar(k)) |
| 1138 | |
| 1139 | data_copy_list.append(data_copy) |
| 1140 | |
| 1141 | from concurrent.futures import ProcessPoolExecutor |
| 1142 | with ProcessPoolExecutor(max_workers=utils.cpu_count()) as executor: |
| 1143 | pkgs_list = executor.map(_get_recipe_upgrade_status, data_copy_list) |
| 1144 | |
| 1145 | return pkgs_list |