Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | import codecs |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 8 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | |
| 10 | def packaged(pkg, d): |
| 11 | return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK) |
| 12 | |
| 13 | def read_pkgdatafile(fn): |
| 14 | pkgdata = {} |
| 15 | |
| 16 | def decode(str): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 17 | c = codecs.getdecoder("unicode_escape") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | return c(str)[0] |
| 19 | |
| 20 | if os.access(fn, os.R_OK): |
| 21 | import re |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 22 | with open(fn, 'r') as f: |
| 23 | lines = f.readlines() |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 24 | r = re.compile(r"(^.+?):\s+(.*)") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | for l in lines: |
| 26 | m = r.match(l) |
| 27 | if m: |
| 28 | pkgdata[m.group(1)] = decode(m.group(2)) |
| 29 | |
| 30 | return pkgdata |
| 31 | |
| 32 | def get_subpkgedata_fn(pkg, d): |
| 33 | return d.expand('${PKGDATA_DIR}/runtime/%s' % pkg) |
| 34 | |
| 35 | def has_subpkgdata(pkg, d): |
| 36 | return os.access(get_subpkgedata_fn(pkg, d), os.R_OK) |
| 37 | |
| 38 | def read_subpkgdata(pkg, d): |
| 39 | return read_pkgdatafile(get_subpkgedata_fn(pkg, d)) |
| 40 | |
| 41 | def has_pkgdata(pn, d): |
| 42 | fn = d.expand('${PKGDATA_DIR}/%s' % pn) |
| 43 | return os.access(fn, os.R_OK) |
| 44 | |
| 45 | def read_pkgdata(pn, d): |
| 46 | fn = d.expand('${PKGDATA_DIR}/%s' % pn) |
| 47 | return read_pkgdatafile(fn) |
| 48 | |
| 49 | # |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 50 | # Collapse FOO:pkg variables into FOO |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | # |
| 52 | def read_subpkgdata_dict(pkg, d): |
| 53 | ret = {} |
| 54 | subd = read_pkgdatafile(get_subpkgedata_fn(pkg, d)) |
| 55 | for var in subd: |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 56 | newvar = var.replace(":" + pkg, "") |
| 57 | if newvar == var and var + ":" + pkg in subd: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | continue |
| 59 | ret[newvar] = subd[var] |
| 60 | return ret |
| 61 | |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 62 | def read_subpkgdata_extended(pkg, d): |
| 63 | import json |
| 64 | import bb.compress.zstd |
| 65 | |
| 66 | fn = d.expand("${PKGDATA_DIR}/extended/%s.json.zstd" % pkg) |
| 67 | try: |
| 68 | num_threads = int(d.getVar("BB_NUMBER_THREADS")) |
| 69 | with bb.compress.zstd.open(fn, "rt", encoding="utf-8", num_threads=num_threads) as f: |
| 70 | return json.load(f) |
| 71 | except FileNotFoundError: |
| 72 | return None |
| 73 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | def _pkgmap(d): |
| 75 | """Return a dictionary mapping package to recipe name.""" |
| 76 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 77 | pkgdatadir = d.getVar("PKGDATA_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | |
| 79 | pkgmap = {} |
| 80 | try: |
| 81 | files = os.listdir(pkgdatadir) |
| 82 | except OSError: |
| 83 | bb.warn("No files in %s?" % pkgdatadir) |
| 84 | files = [] |
| 85 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 86 | for pn in [f for f in files if not os.path.isdir(os.path.join(pkgdatadir, f))]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | try: |
| 88 | pkgdata = read_pkgdatafile(os.path.join(pkgdatadir, pn)) |
| 89 | except OSError: |
| 90 | continue |
| 91 | |
| 92 | packages = pkgdata.get("PACKAGES") or "" |
| 93 | for pkg in packages.split(): |
| 94 | pkgmap[pkg] = pn |
| 95 | |
| 96 | return pkgmap |
| 97 | |
| 98 | def pkgmap(d): |
| 99 | """Return a dictionary mapping package to recipe name. |
| 100 | Cache the mapping in the metadata""" |
| 101 | |
| 102 | pkgmap_data = d.getVar("__pkgmap_data", False) |
| 103 | if pkgmap_data is None: |
| 104 | pkgmap_data = _pkgmap(d) |
| 105 | d.setVar("__pkgmap_data", pkgmap_data) |
| 106 | |
| 107 | return pkgmap_data |
| 108 | |
| 109 | def recipename(pkg, d): |
| 110 | """Return the recipe name for the given binary package name.""" |
| 111 | |
| 112 | return pkgmap(d).get(pkg) |