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