blob: 630ae967af4c19ed6de5103c77773cf362e8409e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Utility functions for reading and modifying recipes
2#
3# Some code borrowed from the OE layer index
4#
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005# Copyright (C) 2013-2017 Intel Corporation
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
Brad Bishopc342db32019-05-15 21:57:59 -04007# SPDX-License-Identifier: GPL-2.0-only
8#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
10import sys
11import os
12import os.path
13import tempfile
14import textwrap
15import difflib
Patrick Williamsc0f7c042017-02-23 20:41:17 -060016from . import utils
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017import shutil
18import re
19import fnmatch
Patrick Williamsc0f7c042017-02-23 20:41:17 -060020import glob
Brad Bishop19323692019-04-05 15:28:33 -040021import bb.tinfoil
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022
Brad Bishop19323692019-04-05 15:28:33 -040023from collections import OrderedDict, defaultdict
24from bb.utils import vercmp_string
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
26# Help us to find places to insert values
Patrick Williamsc0f7c042017-02-23 20:41:17 -060027recipe_progression = ['SUMMARY', 'DESCRIPTION', '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()']
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028# Variables that sometimes are a bit long but shouldn't be wrapped
Brad Bishop19323692019-04-05 15:28:33 -040029nowrap_vars = ['SUMMARY', 'HOMEPAGE', 'BUGTRACKER', r'SRC_URI\[(.+\.)?md5sum\]', r'SRC_URI\[(.+\.)?sha256sum\]']
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030list_vars = ['SRC_URI', 'LIC_FILES_CHKSUM']
31meta_vars = ['SUMMARY', 'DESCRIPTION', 'HOMEPAGE', 'BUGTRACKER', 'SECTION']
32
33
Brad Bishop19323692019-04-05 15:28:33 -040034def simplify_history(history, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 """
Brad Bishop19323692019-04-05 15:28:33 -040036 Eliminate any irrelevant events from a variable history
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 """
Brad Bishop19323692019-04-05 15:28:33 -040038 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'):
50 # Reminder: "append" and "prepend" mean += and =+ respectively, NOT _append / _prepend
51 if has_set:
52 continue
53 ret_history.insert(0, event)
54 return ret_history
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055
56
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057def 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 Williamsc124f4f2015-09-15 14:41:29 -050063 files = []
Brad Bishop19323692019-04-05 15:28:33 -040064 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 Williamsc124f4f2015-09-15 14:41:29 -050076 if files:
77 actualfile = files[-1]
78 else:
79 actualfile = None
80 varfiles[v] = actualfile
81
82 return varfiles
83
84
Patrick Williamsf1e5d692016-03-30 15:21:19 -050085def 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 Williamsc0f7c042017-02-23 20:41:17 -0600142def patch_recipe_lines(fromlines, values, trailing_newline=True):
143 """Update or insert variable values into lines from a recipe.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 Note that some manual inspection/intervention may be required
145 since this cannot handle all situations.
146 """
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500147
148 import bb.utils
149
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150 if trailing_newline:
151 newline = '\n'
152 else:
153 newline = ''
154
Brad Bishop316dfdd2018-06-25 12:45:53 -0400155 nowrap_vars_res = []
156 for item in nowrap_vars:
157 nowrap_vars_res.append(re.compile('^%s$' % item))
158
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500159 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 Bishop19323692019-04-05 15:28:33 -0400166 restr = r'%s(_[a-zA-Z0-9-_$(){}]+|\[[^\]]*\])?' % key
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167 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 Williamsc124f4f2015-09-15 14:41:29 -0500179 remainingnames = {}
180 for k in values.keys():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500181 remainingnames[k] = get_recipe_pos(k)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182 remainingnames = OrderedDict(sorted(remainingnames.items(), key=lambda x: x[1]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500184 modifying = False
185
186 def outputvalue(name, lines, rewindcomments=False):
187 if values[name] is None:
188 return
Brad Bishop19323692019-04-05 15:28:33 -0400189 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 Williamsd8c66bc2016-06-20 12:57:21 -0500197 addlines = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400198 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 Williamsd8c66bc2016-06-20 12:57:21 -0500204 addlines.append(rawtext)
205 elif name in list_vars:
Brad Bishop19323692019-04-05 15:28:33 -0400206 splitvalue = split_var_value(value, assignment=False)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500207 if len(splitvalue) > 1:
208 linesplit = ' \\\n' + (' ' * (len(name) + 4))
Brad Bishop19323692019-04-05 15:28:33 -0400209 addlines.append('%s %s "%s%s"%s' % (name, op, linesplit.join(splitvalue), linesplit, newline))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500211 addlines.append(rawtext)
212 else:
213 wrapped = textwrap.wrap(rawtext)
214 for wrapline in wrapped[:-1]:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600215 addlines.append('%s \\%s' % (wrapline, newline))
216 addlines.append('%s%s' % (wrapped[-1], newline))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500217
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 Williamsd8c66bc2016-06-20 12:57:21 -0500222 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 Williamsc0f7c042017-02-23 20:41:17 -0600226 if not ln.startswith('#'):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500227 lines[i+1:i+1] = addlines
228 break
229 else:
230 lines.extend(addlines)
231 else:
232 lines.extend(addlines)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500234 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 Williamsc0f7c042017-02-23 20:41:17 -0600239 for k in list(remainingnames):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240 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 Williamsc124f4f2015-09-15 14:41:29 -0500253
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500254 # 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 Williamsc0f7c042017-02-23 20:41:17 -0600256 bb.utils.edit_metadata(fromlines, varlist, patch_recipe_varfunc)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500257 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500261
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500262 if remainingnames:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600263 if tolines and tolines[-1].strip() != '':
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500264 tolines.append('\n')
265 for k in remainingnames.keys():
266 outputvalue(k, tolines)
267
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600268 return changed, tolines
269
270
Brad Bishop316dfdd2018-06-25 12:45:53 -0400271def patch_recipe_file(fn, values, patch=False, relpath='', redirect_output=None):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600272 """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 Bishop316dfdd2018-06-25 12:45:53 -0400283 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 Williamsc124f4f2015-09-15 14:41:29 -0500288 relfn = os.path.relpath(fn, relpath)
289 diff = difflib.unified_diff(fromlines, tolines, 'a/%s' % relfn, 'b/%s' % relfn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290 return diff
291 else:
292 with open(fn, 'w') as f:
293 f.writelines(tolines)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500294 return None
295
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500296
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297def 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 Bishop316dfdd2018-06-25 12:45:53 -0400337def patch_recipe(d, fn, varvalues, patch=False, relpath='', redirect_output=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500338 """Modify a list of variable values in the specified recipe. Handles inc files if
339 used by the recipe.
340 """
Brad Bishop19323692019-04-05 15:28:33 -0400341 overrides = d.getVar('OVERRIDES').split(':')
342 def override_applicable(hevent):
343 op = hevent['op']
344 if '[' in op:
345 opoverrides = op.split('[')[1].split(']')[0].split('_')
346 for opoverride in opoverrides:
347 if not opoverride in overrides:
348 return False
349 return True
350
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351 varlist = varvalues.keys()
Brad Bishop19323692019-04-05 15:28:33 -0400352 fn = os.path.abspath(fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500353 varfiles = get_var_files(fn, varlist, d)
354 locs = localise_file_vars(fn, varfiles, varlist)
355 patches = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600356 for f,v in locs.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500357 vals = {k: varvalues[k] for k in v}
Brad Bishop19323692019-04-05 15:28:33 -0400358 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:
371 if event['op'].startswith('_remove'):
372 continue
373 if not override_applicable(event):
374 continue
375 newvalue = value.replace(event['detail'], '')
376 if newvalue == value and os.path.abspath(event['file']) == fn and event['op'].startswith('_'):
377 op = event['op'].replace('[', '_').replace(']', '')
378 extravals[var + op] = None
379 value = newvalue
380 vals[var] = ('+=', value)
381 vals.update(extravals)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400382 patchdata = patch_recipe_file(f, vals, patch, relpath, redirect_output)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383 if patch:
384 patches.append(patchdata)
385
386 if patch:
387 return patches
388 else:
389 return None
390
391
392
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500393def copy_recipe_files(d, tgt_dir, whole_dir=False, download=True, all_variants=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500394 """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 Bishopd7bf8c12018-02-25 22:55:05 -0500401 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:
412 localpaths.append(pth)
413 uri_values.append(srcuri)
414
415 fetch_urls(d)
416 if all_variants:
417 # Get files for other variants e.g. in the case of a SRC_URI_append
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:
424 localdata.setVar('CLASSOVERRIDE', 'class-%s' % variant)
425 fetch_urls(localdata)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500426
427 # Copy local files to target directory and gather any remote files
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500428 bb_dir = os.path.abspath(os.path.dirname(d.getVar('FILE'))) + os.sep
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500429 remotes = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600430 copied = []
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500431 # Need to do this in two steps since we want to check against the absolute path
432 includes = [os.path.abspath(path) for path in d.getVar('BBINCLUDED').split() if os.path.exists(path)]
433 # We also check this below, but we don't want any items in this list being considered remotes
434 includes = [path for path in includes if path.startswith(bb_dir)]
435 for path in localpaths + includes:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500436 # Only import files that are under the meta directory
437 if path.startswith(bb_dir):
438 if not whole_dir:
439 relpath = os.path.relpath(path, bb_dir)
440 subdir = os.path.join(tgt_dir, os.path.dirname(relpath))
441 if not os.path.exists(subdir):
442 os.makedirs(subdir)
443 shutil.copy2(path, os.path.join(tgt_dir, relpath))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600444 copied.append(relpath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500445 else:
446 remotes.append(path)
447 # Simply copy whole meta dir, if requested
448 if whole_dir:
449 shutil.copytree(bb_dir, tgt_dir)
450
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600451 return copied, remotes
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500452
453
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500454def get_recipe_local_files(d, patches=False, archives=False):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500455 """Get a list of local files in SRC_URI within a recipe."""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500456 import oe.patch
457 uris = (d.getVar('SRC_URI') or "").split()
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500458 fetch = bb.fetch2.Fetch(uris, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500459 # FIXME this list should be factored out somewhere else (such as the
460 # fetcher) though note that this only encompasses actual container formats
461 # i.e. that can contain multiple files as opposed to those that only
462 # contain a compressed stream (i.e. .tar.gz as opposed to just .gz)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400463 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 Williamsf1e5d692016-03-30 15:21:19 -0500464 ret = {}
465 for uri in uris:
466 if fetch.ud[uri].type == 'file':
467 if (not patches and
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500468 oe.patch.patch_path(uri, fetch, '', expand=False)):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500469 continue
470 # Skip files that are referenced by absolute path
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600471 fname = fetch.ud[uri].basepath
472 if os.path.isabs(fname):
473 continue
474 # Handle subdir=
475 subdir = fetch.ud[uri].parm.get('subdir', '')
476 if subdir:
477 if os.path.isabs(subdir):
478 continue
479 fname = os.path.join(subdir, fname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500480 localpath = fetch.localpath(uri)
481 if not archives:
482 # Ignore archives that will be unpacked
483 if localpath.endswith(tuple(archive_exts)):
484 unpack = fetch.ud[uri].parm.get('unpack', True)
485 if unpack:
486 continue
Brad Bishop19323692019-04-05 15:28:33 -0400487 if os.path.isdir(localpath):
488 for root, dirs, files in os.walk(localpath):
489 for fname in files:
490 fileabspath = os.path.join(root,fname)
491 srcdir = os.path.dirname(localpath)
492 ret[os.path.relpath(fileabspath,srcdir)] = fileabspath
493 else:
494 ret[fname] = localpath
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500495 return ret
496
497
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500498def get_recipe_patches(d):
499 """Get a list of the patches included in SRC_URI within a recipe."""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500500 import oe.patch
501 patches = oe.patch.src_patches(d, expand=False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500502 patchfiles = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500503 for patch in patches:
504 _, _, local, _, _, parm = bb.fetch.decodeurl(patch)
505 patchfiles.append(local)
506 return patchfiles
507
508
509def get_recipe_patched_files(d):
510 """
511 Get the list of patches for a recipe along with the files each patch modifies.
512 Params:
513 d: the datastore for the recipe
514 Returns:
515 a dict mapping patch file path to a list of tuples of changed files and
516 change mode ('A' for add, 'D' for delete or 'M' for modify)
517 """
518 import oe.patch
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500519 patches = oe.patch.src_patches(d, expand=False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500520 patchedfiles = {}
521 for patch in patches:
522 _, _, patchfile, _, _, parm = bb.fetch.decodeurl(patch)
523 striplevel = int(parm['striplevel'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500524 patchedfiles[patchfile] = oe.patch.PatchSet.getPatchedFiles(patchfile, striplevel, os.path.join(d.getVar('S'), parm.get('patchdir', '')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500525 return patchedfiles
526
527
528def validate_pn(pn):
529 """Perform validation on a recipe name (PN) for a new recipe."""
530 reserved_names = ['forcevariable', 'append', 'prepend', 'remove']
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600531 if not re.match('^[0-9a-z-.+]+$', pn):
532 return 'Recipe name "%s" is invalid: only characters 0-9, a-z, -, + and . are allowed' % pn
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500533 elif pn in reserved_names:
534 return 'Recipe name "%s" is invalid: is a reserved keyword' % pn
535 elif pn.startswith('pn-'):
536 return 'Recipe name "%s" is invalid: names starting with "pn-" are reserved' % pn
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500537 elif pn.endswith(('.bb', '.bbappend', '.bbclass', '.inc', '.conf')):
538 return 'Recipe name "%s" is invalid: should be just a name, not a file name' % pn
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500539 return ''
540
541
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600542def get_bbfile_path(d, destdir, extrapathhint=None):
543 """
544 Determine the correct path for a recipe within a layer
545 Parameters:
546 d: Recipe-specific datastore
547 destdir: destination directory. Can be the path to the base of the layer or a
548 partial path somewhere within the layer.
549 extrapathhint: a path relative to the base of the layer to try
550 """
551 import bb.cookerdata
552
553 destdir = os.path.abspath(destdir)
554 destlayerdir = find_layerdir(destdir)
555
556 # Parse the specified layer's layer.conf file directly, in case the layer isn't in bblayers.conf
557 confdata = d.createCopy()
558 confdata.setVar('BBFILES', '')
559 confdata.setVar('LAYERDIR', destlayerdir)
560 destlayerconf = os.path.join(destlayerdir, "conf", "layer.conf")
561 confdata = bb.cookerdata.parse_config_file(destlayerconf, confdata)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500562 pn = d.getVar('PN')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600563
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500564 bbfilespecs = (confdata.getVar('BBFILES') or '').split()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600565 if destdir == destlayerdir:
566 for bbfilespec in bbfilespecs:
567 if not bbfilespec.endswith('.bbappend'):
568 for match in glob.glob(bbfilespec):
569 splitext = os.path.splitext(os.path.basename(match))
570 if splitext[1] == '.bb':
571 mpn = splitext[0].split('_')[0]
572 if mpn == pn:
573 return os.path.dirname(match)
574
575 # Try to make up a path that matches BBFILES
576 # this is a little crude, but better than nothing
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500577 bpn = d.getVar('BPN')
578 recipefn = os.path.basename(d.getVar('FILE'))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600579 pathoptions = [destdir]
580 if extrapathhint:
581 pathoptions.append(os.path.join(destdir, extrapathhint))
582 if destdir == destlayerdir:
583 pathoptions.append(os.path.join(destdir, 'recipes-%s' % bpn, bpn))
584 pathoptions.append(os.path.join(destdir, 'recipes', bpn))
585 pathoptions.append(os.path.join(destdir, bpn))
586 elif not destdir.endswith(('/' + pn, '/' + bpn)):
587 pathoptions.append(os.path.join(destdir, bpn))
588 closepath = ''
589 for pathoption in pathoptions:
590 bbfilepath = os.path.join(pathoption, 'test.bb')
591 for bbfilespec in bbfilespecs:
592 if fnmatch.fnmatchcase(bbfilepath, bbfilespec):
593 return pathoption
594 return None
595
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500596def get_bbappend_path(d, destlayerdir, wildcardver=False):
597 """Determine how a bbappend for a recipe should be named and located within another layer"""
598
599 import bb.cookerdata
600
601 destlayerdir = os.path.abspath(destlayerdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500602 recipefile = d.getVar('FILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500603 recipefn = os.path.splitext(os.path.basename(recipefile))[0]
604 if wildcardver and '_' in recipefn:
605 recipefn = recipefn.split('_', 1)[0] + '_%'
606 appendfn = recipefn + '.bbappend'
607
608 # Parse the specified layer's layer.conf file directly, in case the layer isn't in bblayers.conf
609 confdata = d.createCopy()
610 confdata.setVar('BBFILES', '')
611 confdata.setVar('LAYERDIR', destlayerdir)
612 destlayerconf = os.path.join(destlayerdir, "conf", "layer.conf")
613 confdata = bb.cookerdata.parse_config_file(destlayerconf, confdata)
614
615 origlayerdir = find_layerdir(recipefile)
616 if not origlayerdir:
617 return (None, False)
618 # Now join this to the path where the bbappend is going and check if it is covered by BBFILES
619 appendpath = os.path.join(destlayerdir, os.path.relpath(os.path.dirname(recipefile), origlayerdir), appendfn)
620 closepath = ''
621 pathok = True
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500622 for bbfilespec in confdata.getVar('BBFILES').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500623 if fnmatch.fnmatchcase(appendpath, bbfilespec):
624 # Our append path works, we're done
625 break
626 elif bbfilespec.startswith(destlayerdir) and fnmatch.fnmatchcase('test.bbappend', os.path.basename(bbfilespec)):
627 # Try to find the longest matching path
628 if len(bbfilespec) > len(closepath):
629 closepath = bbfilespec
630 else:
631 # Unfortunately the bbappend layer and the original recipe's layer don't have the same structure
632 if closepath:
633 # bbappend layer's layer.conf at least has a spec that picks up .bbappend files
634 # Now we just need to substitute out any wildcards
635 appendsubdir = os.path.relpath(os.path.dirname(closepath), destlayerdir)
636 if 'recipes-*' in appendsubdir:
637 # Try to copy this part from the original recipe path
638 res = re.search('/recipes-[^/]+/', recipefile)
639 if res:
640 appendsubdir = appendsubdir.replace('/recipes-*/', res.group(0))
641 # This is crude, but we have to do something
642 appendsubdir = appendsubdir.replace('*', recipefn.split('_')[0])
643 appendsubdir = appendsubdir.replace('?', 'a')
644 appendpath = os.path.join(destlayerdir, appendsubdir, appendfn)
645 else:
646 pathok = False
647 return (appendpath, pathok)
648
649
Brad Bishop316dfdd2018-06-25 12:45:53 -0400650def bbappend_recipe(rd, destlayerdir, srcfiles, install=None, wildcardver=False, machine=None, extralines=None, removevalues=None, redirect_output=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500651 """
652 Writes a bbappend file for a recipe
653 Parameters:
654 rd: data dictionary for the recipe
655 destlayerdir: base directory of the layer to place the bbappend in
656 (subdirectory path from there will be determined automatically)
657 srcfiles: dict of source files to add to SRC_URI, where the value
658 is the full path to the file to be added, and the value is the
659 original filename as it would appear in SRC_URI or None if it
660 isn't already present. You may pass None for this parameter if
661 you simply want to specify your own content via the extralines
662 parameter.
663 install: dict mapping entries in srcfiles to a tuple of two elements:
664 install path (*without* ${D} prefix) and permission value (as a
665 string, e.g. '0644').
666 wildcardver: True to use a % wildcard in the bbappend filename, or
667 False to make the bbappend specific to the recipe version.
668 machine:
669 If specified, make the changes in the bbappend specific to this
670 machine. This will also cause PACKAGE_ARCH = "${MACHINE_ARCH}"
671 to be added to the bbappend.
672 extralines:
673 Extra lines to add to the bbappend. This may be a dict of name
674 value pairs, or simply a list of the lines.
675 removevalues:
676 Variable values to remove - a dict of names/values.
Brad Bishop316dfdd2018-06-25 12:45:53 -0400677 redirect_output:
678 If specified, redirects writing the output file to the
679 specified directory (for dry-run purposes)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500680 """
681
682 if not removevalues:
683 removevalues = {}
684
685 # Determine how the bbappend should be named
686 appendpath, pathok = get_bbappend_path(rd, destlayerdir, wildcardver)
687 if not appendpath:
688 bb.error('Unable to determine layer directory containing %s' % recipefile)
689 return (None, None)
690 if not pathok:
691 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)))
692
693 appenddir = os.path.dirname(appendpath)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400694 if not redirect_output:
695 bb.utils.mkdirhier(appenddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500696
697 # FIXME check if the bbappend doesn't get overridden by a higher priority layer?
698
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500699 layerdirs = [os.path.abspath(layerdir) for layerdir in rd.getVar('BBLAYERS').split()]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500700 if not os.path.abspath(destlayerdir) in layerdirs:
701 bb.warn('Specified layer is not currently enabled in bblayers.conf, you will need to add it before this bbappend will be active')
702
703 bbappendlines = []
704 if extralines:
705 if isinstance(extralines, dict):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600706 for name, value in extralines.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500707 bbappendlines.append((name, '=', value))
708 else:
709 # Do our best to split it
710 for line in extralines:
711 if line[-1] == '\n':
712 line = line[:-1]
713 splitline = line.split(None, 2)
714 if len(splitline) == 3:
715 bbappendlines.append(tuple(splitline))
716 else:
717 raise Exception('Invalid extralines value passed')
718
719 def popline(varname):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600720 for i in range(0, len(bbappendlines)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500721 if bbappendlines[i][0] == varname:
722 line = bbappendlines.pop(i)
723 return line
724 return None
725
726 def appendline(varname, op, value):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600727 for i in range(0, len(bbappendlines)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728 item = bbappendlines[i]
729 if item[0] == varname:
730 bbappendlines[i] = (item[0], item[1], item[2] + ' ' + value)
731 break
732 else:
733 bbappendlines.append((varname, op, value))
734
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500735 destsubdir = rd.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500736 if srcfiles:
737 bbappendlines.append(('FILESEXTRAPATHS_prepend', ':=', '${THISDIR}/${PN}:'))
738
739 appendoverride = ''
740 if machine:
741 bbappendlines.append(('PACKAGE_ARCH', '=', '${MACHINE_ARCH}'))
742 appendoverride = '_%s' % machine
743 copyfiles = {}
744 if srcfiles:
745 instfunclines = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600746 for newfile, origsrcfile in srcfiles.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500747 srcfile = origsrcfile
748 srcurientry = None
749 if not srcfile:
750 srcfile = os.path.basename(newfile)
751 srcurientry = 'file://%s' % srcfile
752 # Double-check it's not there already
753 # FIXME do we care if the entry is added by another bbappend that might go away?
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500754 if not srcurientry in rd.getVar('SRC_URI').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500755 if machine:
756 appendline('SRC_URI_append%s' % appendoverride, '=', ' ' + srcurientry)
757 else:
758 appendline('SRC_URI', '+=', srcurientry)
759 copyfiles[newfile] = srcfile
760 if install:
761 institem = install.pop(newfile, None)
762 if institem:
763 (destpath, perms) = institem
764 instdestpath = replace_dir_vars(destpath, rd)
765 instdirline = 'install -d ${D}%s' % os.path.dirname(instdestpath)
766 if not instdirline in instfunclines:
767 instfunclines.append(instdirline)
768 instfunclines.append('install -m %s ${WORKDIR}/%s ${D}%s' % (perms, os.path.basename(srcfile), instdestpath))
769 if instfunclines:
770 bbappendlines.append(('do_install_append%s()' % appendoverride, '', instfunclines))
771
Brad Bishop316dfdd2018-06-25 12:45:53 -0400772 if redirect_output:
773 bb.note('Writing append file %s (dry-run)' % appendpath)
774 outfile = os.path.join(redirect_output, os.path.basename(appendpath))
775 # Only take a copy if the file isn't already there (this function may be called
776 # multiple times per operation when we're handling overrides)
777 if os.path.exists(appendpath) and not os.path.exists(outfile):
778 shutil.copy2(appendpath, outfile)
779 else:
780 bb.note('Writing append file %s' % appendpath)
781 outfile = appendpath
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500782
Brad Bishop316dfdd2018-06-25 12:45:53 -0400783 if os.path.exists(outfile):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500784 # Work around lack of nonlocal in python 2
785 extvars = {'destsubdir': destsubdir}
786
787 def appendfile_varfunc(varname, origvalue, op, newlines):
788 if varname == 'FILESEXTRAPATHS_prepend':
789 if origvalue.startswith('${THISDIR}/'):
790 popline('FILESEXTRAPATHS_prepend')
791 extvars['destsubdir'] = rd.expand(origvalue.split('${THISDIR}/', 1)[1].rstrip(':'))
792 elif varname == 'PACKAGE_ARCH':
793 if machine:
794 popline('PACKAGE_ARCH')
795 return (machine, None, 4, False)
796 elif varname.startswith('do_install_append'):
797 func = popline(varname)
798 if func:
799 instfunclines = [line.strip() for line in origvalue.strip('\n').splitlines()]
800 for line in func[2]:
801 if not line in instfunclines:
802 instfunclines.append(line)
803 return (instfunclines, None, 4, False)
804 else:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500805 splitval = split_var_value(origvalue, assignment=False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500806 changed = False
807 removevar = varname
808 if varname in ['SRC_URI', 'SRC_URI_append%s' % appendoverride]:
809 removevar = 'SRC_URI'
810 line = popline(varname)
811 if line:
812 if line[2] not in splitval:
813 splitval.append(line[2])
814 changed = True
815 else:
816 line = popline(varname)
817 if line:
818 splitval = [line[2]]
819 changed = True
820
821 if removevar in removevalues:
822 remove = removevalues[removevar]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600823 if isinstance(remove, str):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500824 if remove in splitval:
825 splitval.remove(remove)
826 changed = True
827 else:
828 for removeitem in remove:
829 if removeitem in splitval:
830 splitval.remove(removeitem)
831 changed = True
832
833 if changed:
834 newvalue = splitval
835 if len(newvalue) == 1:
836 # Ensure it's written out as one line
837 if '_append' in varname:
838 newvalue = ' ' + newvalue[0]
839 else:
840 newvalue = newvalue[0]
841 if not newvalue and (op in ['+=', '.='] or '_append' in varname):
842 # There's no point appending nothing
843 newvalue = None
844 if varname.endswith('()'):
845 indent = 4
846 else:
847 indent = -1
848 return (newvalue, None, indent, True)
849 return (origvalue, None, 4, False)
850
851 varnames = [item[0] for item in bbappendlines]
852 if removevalues:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600853 varnames.extend(list(removevalues.keys()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500854
Brad Bishop316dfdd2018-06-25 12:45:53 -0400855 with open(outfile, 'r') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500856 (updated, newlines) = bb.utils.edit_metadata(f, varnames, appendfile_varfunc)
857
858 destsubdir = extvars['destsubdir']
859 else:
860 updated = False
861 newlines = []
862
863 if bbappendlines:
864 for line in bbappendlines:
865 if line[0].endswith('()'):
866 newlines.append('%s {\n %s\n}\n' % (line[0], '\n '.join(line[2])))
867 else:
868 newlines.append('%s %s "%s"\n\n' % line)
869 updated = True
870
871 if updated:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400872 with open(outfile, 'w') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500873 f.writelines(newlines)
874
875 if copyfiles:
876 if machine:
877 destsubdir = os.path.join(destsubdir, machine)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400878 if redirect_output:
879 outdir = redirect_output
880 else:
881 outdir = appenddir
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600882 for newfile, srcfile in copyfiles.items():
Brad Bishop316dfdd2018-06-25 12:45:53 -0400883 filedest = os.path.join(outdir, destsubdir, os.path.basename(srcfile))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500884 if os.path.abspath(newfile) != os.path.abspath(filedest):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500885 if newfile.startswith(tempfile.gettempdir()):
886 newfiledisp = os.path.basename(newfile)
887 else:
888 newfiledisp = newfile
Brad Bishop316dfdd2018-06-25 12:45:53 -0400889 if redirect_output:
890 bb.note('Copying %s to %s (dry-run)' % (newfiledisp, os.path.join(appenddir, destsubdir, os.path.basename(srcfile))))
891 else:
892 bb.note('Copying %s to %s' % (newfiledisp, filedest))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500893 bb.utils.mkdirhier(os.path.dirname(filedest))
894 shutil.copyfile(newfile, filedest)
895
896 return (appendpath, os.path.join(appenddir, destsubdir))
897
898
899def find_layerdir(fn):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600900 """ Figure out the path to the base of the layer containing a file (e.g. a recipe)"""
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500901 pth = os.path.abspath(fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500902 layerdir = ''
903 while pth:
904 if os.path.exists(os.path.join(pth, 'conf', 'layer.conf')):
905 layerdir = pth
906 break
907 pth = os.path.dirname(pth)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600908 if pth == '/':
909 return None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500910 return layerdir
911
912
913def replace_dir_vars(path, d):
914 """Replace common directory paths with appropriate variable references (e.g. /etc becomes ${sysconfdir})"""
915 dirvars = {}
916 # Sort by length so we get the variables we're interested in first
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600917 for var in sorted(list(d.keys()), key=len):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500918 if var.endswith('dir') and var.lower() == var:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500919 value = d.getVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500920 if value.startswith('/') and not '\n' in value and value not in dirvars:
921 dirvars[value] = var
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600922 for dirpath in sorted(list(dirvars.keys()), reverse=True):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500923 path = path.replace(dirpath, '${%s}' % dirvars[dirpath])
924 return path
925
926def get_recipe_pv_without_srcpv(pv, uri_type):
927 """
928 Get PV without SRCPV common in SCM's for now only
929 support git.
930
931 Returns tuple with pv, prefix and suffix.
932 """
933 pfx = ''
934 sfx = ''
935
936 if uri_type == 'git':
Brad Bishop15ae2502019-06-18 21:44:24 -0400937 git_regex = re.compile(r"(?P<pfx>v?)(?P<ver>.*?)(?P<sfx>\+[^\+]*(git)?r?(AUTOINC\+))(?P<rev>.*)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500938 m = git_regex.match(pv)
939
940 if m:
941 pv = m.group('ver')
942 pfx = m.group('pfx')
943 sfx = m.group('sfx')
944 else:
Brad Bishop19323692019-04-05 15:28:33 -0400945 regex = re.compile(r"(?P<pfx>(v|r)?)(?P<ver>.*)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500946 m = regex.match(pv)
947 if m:
948 pv = m.group('ver')
949 pfx = m.group('pfx')
950
951 return (pv, pfx, sfx)
952
953def get_recipe_upstream_version(rd):
954 """
955 Get upstream version of recipe using bb.fetch2 methods with support for
956 http, https, ftp and git.
957
958 bb.fetch2 exceptions can be raised,
959 FetchError when don't have network access or upstream site don't response.
960 NoMethodError when uri latest_versionstring method isn't implemented.
961
Brad Bishop316dfdd2018-06-25 12:45:53 -0400962 Returns a dictonary with version, repository revision, current_version, type and datetime.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500963 Type can be A for Automatic, M for Manual and U for Unknown.
964 """
965 from bb.fetch2 import decodeurl
966 from datetime import datetime
967
968 ru = {}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400969 ru['current_version'] = rd.getVar('PV')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500970 ru['version'] = ''
971 ru['type'] = 'U'
972 ru['datetime'] = ''
Brad Bishop316dfdd2018-06-25 12:45:53 -0400973 ru['revision'] = ''
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500974
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500975 # XXX: If don't have SRC_URI means that don't have upstream sources so
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500976 # returns the current recipe version, so that upstream version check
977 # declares a match.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500978 src_uris = rd.getVar('SRC_URI')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500979 if not src_uris:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400980 ru['version'] = ru['current_version']
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500981 ru['type'] = 'M'
982 ru['datetime'] = datetime.now()
983 return ru
984
985 # XXX: we suppose that the first entry points to the upstream sources
986 src_uri = src_uris.split()[0]
987 uri_type, _, _, _, _, _ = decodeurl(src_uri)
988
Brad Bishop316dfdd2018-06-25 12:45:53 -0400989 (pv, pfx, sfx) = get_recipe_pv_without_srcpv(rd.getVar('PV'), uri_type)
990 ru['current_version'] = pv
991
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500992 manual_upstream_version = rd.getVar("RECIPE_UPSTREAM_VERSION")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500993 if manual_upstream_version:
994 # manual tracking of upstream version.
995 ru['version'] = manual_upstream_version
996 ru['type'] = 'M'
997
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500998 manual_upstream_date = rd.getVar("CHECK_DATE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500999 if manual_upstream_date:
1000 date = datetime.strptime(manual_upstream_date, "%b %d, %Y")
1001 else:
1002 date = datetime.now()
1003 ru['datetime'] = date
1004
1005 elif uri_type == "file":
1006 # files are always up-to-date
1007 ru['version'] = pv
1008 ru['type'] = 'A'
1009 ru['datetime'] = datetime.now()
1010 else:
1011 ud = bb.fetch2.FetchData(src_uri, rd)
Brad Bishop316dfdd2018-06-25 12:45:53 -04001012 if rd.getVar("UPSTREAM_CHECK_COMMITS") == "1":
1013 revision = ud.method.latest_revision(ud, rd, 'default')
1014 upversion = pv
1015 if revision != rd.getVar("SRCREV"):
1016 upversion = upversion + "-new-commits-available"
1017 else:
1018 pupver = ud.method.latest_versionstring(ud, rd)
1019 (upversion, revision) = pupver
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001020
1021 if upversion:
1022 ru['version'] = upversion
1023 ru['type'] = 'A'
1024
Brad Bishop316dfdd2018-06-25 12:45:53 -04001025 if revision:
1026 ru['revision'] = revision
1027
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001028 ru['datetime'] = datetime.now()
1029
1030 return ru
Brad Bishop19323692019-04-05 15:28:33 -04001031
1032def _get_recipe_upgrade_status(data):
1033 uv = get_recipe_upstream_version(data)
1034
1035 pn = data.getVar('PN')
1036 cur_ver = uv['current_version']
1037
1038 upstream_version_unknown = data.getVar('UPSTREAM_VERSION_UNKNOWN')
1039 if not uv['version']:
1040 status = "UNKNOWN" if upstream_version_unknown else "UNKNOWN_BROKEN"
1041 else:
1042 cmp = vercmp_string(uv['current_version'], uv['version'])
1043 if cmp == -1:
1044 status = "UPDATE" if not upstream_version_unknown else "KNOWN_BROKEN"
1045 elif cmp == 0:
1046 status = "MATCH" if not upstream_version_unknown else "KNOWN_BROKEN"
1047 else:
1048 status = "UNKNOWN" if upstream_version_unknown else "UNKNOWN_BROKEN"
1049
1050 next_ver = uv['version'] if uv['version'] else "N/A"
1051 revision = uv['revision'] if uv['revision'] else "N/A"
1052 maintainer = data.getVar('RECIPE_MAINTAINER')
1053 no_upgrade_reason = data.getVar('RECIPE_NO_UPDATE_REASON')
1054
1055 return (pn, status, cur_ver, next_ver, maintainer, revision, no_upgrade_reason)
1056
1057def get_recipe_upgrade_status(recipes=None):
1058 pkgs_list = []
1059 data_copy_list = []
1060 copy_vars = ('SRC_URI',
1061 'PV',
1062 'GITDIR',
1063 'DL_DIR',
1064 'PN',
1065 'CACHE',
1066 'PERSISTENT_DIR',
1067 'BB_URI_HEADREVS',
1068 'UPSTREAM_CHECK_COMMITS',
1069 'UPSTREAM_CHECK_GITTAGREGEX',
1070 'UPSTREAM_CHECK_REGEX',
1071 'UPSTREAM_CHECK_URI',
1072 'UPSTREAM_VERSION_UNKNOWN',
1073 'RECIPE_MAINTAINER',
1074 'RECIPE_NO_UPDATE_REASON',
1075 'RECIPE_UPSTREAM_VERSION',
1076 'RECIPE_UPSTREAM_DATE',
1077 'CHECK_DATE',
1078 )
1079
1080 with bb.tinfoil.Tinfoil() as tinfoil:
1081 tinfoil.prepare(config_only=False)
1082
1083 if not recipes:
1084 recipes = tinfoil.all_recipe_files(variants=False)
1085
1086 for fn in recipes:
1087 try:
1088 if fn.startswith("/"):
1089 data = tinfoil.parse_recipe_file(fn)
1090 else:
1091 data = tinfoil.parse_recipe(fn)
1092 except bb.providers.NoProvider:
1093 bb.note(" No provider for %s" % fn)
1094 continue
1095
1096 unreliable = data.getVar('UPSTREAM_CHECK_UNRELIABLE')
1097 if unreliable == "1":
1098 bb.note(" Skip package %s as upstream check unreliable" % pn)
1099 continue
1100
1101 data_copy = bb.data.init()
1102 for var in copy_vars:
1103 data_copy.setVar(var, data.getVar(var))
1104 for k in data:
1105 if k.startswith('SRCREV'):
1106 data_copy.setVar(k, data.getVar(k))
1107
1108 data_copy_list.append(data_copy)
1109
1110 from concurrent.futures import ProcessPoolExecutor
1111 with ProcessPoolExecutor(max_workers=utils.cpu_count()) as executor:
1112 pkgs_list = executor.map(_get_recipe_upgrade_status, data_copy_list)
1113
1114 return pkgs_list